本文主要是介绍Qt_OpengGL:平面图形的着色渲染小测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Qt_OpengGL:平面图形的着色渲染小测
//.h
#ifndef GLWIDGET_H
#define GLWIDGET_H#include <QtOpenGL>
#include <QWidget>class GLWidget : public QGLWidget
{Q_OBJECTpublic:explicit GLWidget(QGLWidget *parent = 0);~GLWidget();protected:void initializeGL();void paintGL();void resizeGL(int width, int height);void keyPressEvent(QKeyEvent *e);bool fullscreen;private:float triangle_rotate;float circle_rotate;float quads_rotate;};#endif // GLWIDGET_H
//.cpp
#include "glwidget.h"
#include <QtGui>
#include <QtCore>
#include <QtOpenGL>#define GL_PI 3.1415926
#define GL_RADIUX 0.2fGLWidget::GLWidget(QGLWidget *parent) :QGLWidget(parent)
{// setWindowTitle("The Opengl for Qt Framework");fullscreen = false;triangle_rotate = 0.0;quads_rotate = 0.0;circle_rotate = 0.0;
}//这是对虚函数,这里是重写该函数
void GLWidget::initializeGL()
{setGeometry(300, 150, 500, 500);//设置窗口初始位置和大小glShadeModel(GL_SMOOTH);//设置阴影平滑模式glClearColor(0.0, 0.0, 0.0, 0.0);//改变窗口的背景颜色,不过我这里貌似设置后并没有什么效果glClearDepth(1.0);//设置深度缓存glEnable(GL_DEPTH_TEST);//允许深度测试glDepthFunc(GL_LEQUAL);//设置深度测试类型glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);//进行透视校正
}void GLWidget::paintGL()
{glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //glClear()函数在这里就是对initializeGL()函数//中设置的颜色和缓存深度等起作用glLoadIdentity();//重置当前的模型观察矩阵,该句执行完后,将焦点移动到了屏幕的中心/*下面几句代码是用来画三角形的,以glBegin()开始,glEnd()结束;glVertex3f为给定一个3维的顶点,坐标值为浮点型*/glTranslatef(-0.3, 0.3, -0.6);glRotatef(triangle_rotate, 0.2, 0.2, 0.0);//设置旋转的角度,这里为了观察,同时沿x轴和y轴做了旋转glBegin(GL_TRIANGLES);//GL_TRIANGLES代表为画三角形glVertex3f(0.0f, 0.2f, 0.0f);//上顶点坐标glColor3f(1.0f, 0.0f, 0.0f);glVertex3f(-0.2f, -0.2f, 0.0f);//左下角坐标glColor3f(0.0f, 1.0f, 0.0f);glVertex3f(0.2f, -0.2f, 0.0f);//右下角坐标glColor3f(0.0f, 0.0f, 1.0f);glEnd();//结束画完triangle_rotate += 0.05; //为了看到动态效果,这里每次刷新后旋转的角度都加0.05glLoadIdentity();//重新焦点定位,同样是屏幕的中心glTranslatef(0.3f,0.3f,0.0f); // 向x轴正方向移动0.3个单位glRotatef(quads_rotate, 0.0, 0.2, 0.2);glColor3f(0.0f,1.0f,0.0f);//颜色设置放在这个地方,对下面的顶点设置都是有效的/*下面开始绘制四边形*/glBegin(GL_QUADS);glVertex3f(-0.2f, 0.2f, 0.0f); // 左上顶点glColor3f(1.0f, 0.0f, 0.0f);glVertex3f( 0.2f, 0.2f, 0.0f); // 右上顶点glColor3f(0.0f, 1.0f, 0.0f);glVertex3f( 0.2f, -0.2f, 0.0f); // 右下顶点glColor3f(0.0f, 0.0f, 1.0f);glVertex3f(-0.2f, -0.2f, 0.0f); // 左下顶点glColor3f(1.0f, 0.2f, 0.8f);glEnd(); // 四边形绘制结束quads_rotate += 0.05;glLoadIdentity();glTranslatef(0.0f, -0.3f, 0.0f);glColor3f(0.0f, 0.0f, 1.0f);glRotatef(circle_rotate, 0.2, 0.0, 0.2);/*这里用连续的三角形面积来逼近圆的面积实现圆周的画法*/GLint circle_points = 100, i = 0;glBegin(GL_TRIANGLE_FAN);for(int i = 0; i < circle_points; i++ ){double angle = 2*GL_PI*i/circle_points;//qrand()%10为产生0~10的整数,这里的颜色是随机产生的glColor3f((GLfloat)(qrand()%10)/10, (GLfloat)(qrand()%10)/10, (GLfloat)(qrand()%10)/10);glVertex3f(GL_RADIUX*cos(angle), GL_RADIUX*sin(angle), 0);}glEnd();circle_rotate += 0.05;}//该程序是设置opengl场景透视图,程序中至少被执行一次(程序启动时).
void GLWidget::resizeGL(int width, int height)
{if(0 == height)height = 1;//防止一条边为0glViewport(0, 0, (GLint)width, (GLint)height);//重置当前视口,本身不是重置窗口的,只不过是这里被Qt给封装好了glMatrixMode(GL_PROJECTION);//选择投影矩阵glLoadIdentity();//重置选择好的投影矩阵// gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0);//建立透视投影矩阵glMatrixMode(GL_MODELVIEW);glLoadIdentity();}
void GLWidget::keyPressEvent(QKeyEvent *e)
{switch(e->key()){//F1键为全屏和普通屏显示切换键case Qt::Key_F1:fullscreen = !fullscreen;if(fullscreen)showFullScreen();else{setGeometry(300, 150, 500, 500);showNormal();}updateGL();break;//Ese为退出程序键case Qt::Key_Escape:close();}
}GLWidget::~GLWidget()
{
}
//.main
#include "glwidget.h"
#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsItem>
int main(int argc, char *argv[])
{QApplication a(argc, argv);GLWidget w;w.show();return a.exec();
}
//运行结果:
这篇关于Qt_OpengGL:平面图形的着色渲染小测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!