与多边形同理。
我的这个代码加入了旋转的代码,旋转的几种方法在下一节解释。
代码如下:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <GL/gl.h>
#include <GL/glu.h>
#include <QGLWidget>//这个头文件版本较低。高版本<QOpenGLWidget>
namespace Ui {
class Widget;
}
class Widget : public QGLWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
void initializeGL();
void resizeGL(int w, int h);
void paintGL();
void keyPressEvent(QKeyEvent *event);
private:
Ui::Widget *ui;
bool fullscreen,light;
GLfloat m_rtri; //控制三角形的角度
GLfloat m_rquad; //控制四边形的角度
int winWidth,winHeight;
GLbyte* colorArr;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
#include <QTimer>
#include <iostream>
using namespace std;
Widget::Widget(QWidget *parent) :
QGLWidget(parent),
ui(new Ui::Widget)
{
fullscreen = false;
light = right;
m_rtri = 0.0f;
m_rquad = 0.0f;
winWidth=700;winHeight=500;
int arrLen = winWidth * winHeight *3;
colorArr = new GLbyte[arrLen];
QTimer *timer = new QTimer(this); //创建一个定时器
//将定时器的计时信号与updateGL()绑定
connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
timer->start(10); //以10ms为一个计时周期
ui->setupUi(this);
//this->setGeometry(50,50,700,500);//从屏幕上(700,400)位置开始,现实一个宽1200*高1000的界面
this->setGeometry(50,50,winWidth,winHeight);
}
Widget::~Widget()
{
delete ui;
}
void Widget::initializeGL() //此处开始对OpenGL进行所以设置
{
glClearColor(0.0, 0.0, 0.0, 0.0); //黑色背景
glShadeModel(GL_SMOOTH); //启用阴影平滑
glClearDepth(1.0); //设置深度缓存
glEnable(GL_DEPTH_TEST); //启用深度测试
glDepthFunc(GL_LEQUAL); //所作深度测试的类型
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //告诉系统对透视进行修正
//glLightfv(GL_LIGHT1,GL_AMBIENT,light);//设置环境光
//glLightfv(GL_LIGHT1,GL_DIFFUSE,position);//设置漫射光
GLfloat position[] = {0.0,0.0,-3.0,1.0};//
glLightfv(GL_LIGHT0,GL_POSITION,position);//设置光源位置
}
void Widget::resizeGL(int w, int h) //重置OpenGL窗口的大小
{
glViewport(0, 0, (GLint)w, (GLint)h); //重置当前的视口
glMatrixMode(GL_PROJECTION); //选择投影矩阵
glLoadIdentity(); //重置投影矩阵
//设置视口的大小
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW); //选择模型观察矩阵
glLoadIdentity(); //重置模型观察矩阵
}
void Widget::paintGL() //从这里开始进行所以的绘制
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清除屏幕和深度缓存
glLoadIdentity(); //重置当前的模型观察矩阵
glTranslatef(0.0f, 0.0f, -6.0f);
//glTranslatef(-1.5f, 0.0f, -6.0f); //左移1.5单位,并移入屏幕6.0单位
glRotatef(m_rtri, 0.0f, 1.0f, 0.0f); //绕y轴旋转三角形
glBegin(GL_TRIANGLES); //开始绘制三角形
//----------------------------------------1
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -1.0, -1.0, 1.0 );
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 1.0, -1.0, 1.0 );
//----------------------------------------2
//glColor3f( 1.0, 0.0, 0.0 ); //控制每一点的颜色
glVertex3f( 0.0, 1.0, 0.0 );
//glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 1.0, -1.0, 1.0 );
//glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 1.0, -1.0, -1.0 );
//----------------------------------------3
//glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
//glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 1.0, -1.0, -1.0 );
///glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( -1.0, -1.0, -1.0 );
//----------------------------------------4
//glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.0, 1.0, 0.0 );
//glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( -1.0, -1.0, -1.0 );
//glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -1.0, -1.0, 1.0 );
glEnd(); //三角形绘制结束
m_rtri += 0.5f; //增加三角形的旋转变量
m_rquad -= 0.5f; //减少四边形的旋转变量
}
void Widget::keyPressEvent(QKeyEvent *event)
{
GLint viewPort[4] = {0};
switch (event->key())
{
//F1为全屏和普通屏的切换键
case Qt::Key_F2:
fullscreen = !fullscreen;
if (fullscreen)
{
showFullScreen();
}
else
{
showNormal();
}
updateGL();
break;
//ESC为退出键
case Qt::Key_Escape:
close();
case Qt::Key_C:
//glGetIntegerv(GL_VIEWPORT,viewPort);
case Qt::Key_L:
light = !light;
if(!light){
glDisable(GL_LIGHTING);
}else{
glEnable(GL_LIGHTING);
}
}
}
运行结果: