與多邊形同理。
我的這個代碼加入了旋轉的代碼,旋轉的幾種方法在下一節解釋。
代碼如下:
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);
}
}
}
運行結果: