Qt5使用openGL,进行3d绘图
https://blog.csdn.net/qq_35703848/article/details/79819854
1、
1 #ifndef WIDGET_H 2 #define WIDGET_H 3 4 #include <QWidget> 5 #include <QOpenGLWindow> 6 #include <QOpenGLFunctions> 7 8 class Widget : public QOpenGLWindow,protected QOpenGLFunctions 9 { 10 Q_OBJECT 11 12 public: 13 // Widget(QWidget *parent = 0); 14 //使用时,直接Widget w;即可。 15 ~Widget(){}; 16 17 void initializeGL() 18 { 19 initializeOpenGLFunctions(); 20 glClearColor(0.0f,0.0f,0.0f,0.0f); 21 } 22 void resizeGL(int width, int height){} //若无需对高、宽进行处理,此函数可省 23 void paintGL() 24 { 25 glClear(GL_COLOR_BUFFER_BIT); 26 } 27 }; 28 29 #endif // WIDGET_H
2、使用QGLWidget
1 #ifndef OPENGLWIDGET_H 2 #define OPENGLWIDGET_H 3 #include <QtOpenGL> 4 5 class OpenglWidget : public QGLWidget 6 { 7 public: 8 OpenglWidget(QWidget *parent=0); 9 protected: 10 void initializeGL(); 11 void initWidget(); 12 void paintGL(); 13 void resizeGL(int width, int height); 14 private: 15 GLfloat angle; 16 GLfloat angle1; 17 }; 18 #endif // OPENGLWIDGET_H
1 #include "widget.h" 2 3 OpenglWidget::OpenglWidget(QWidget *parent):QGLWidget(parent) 4 { 5 initWidget(); 6 initializeGL(); 7 } 8 void OpenglWidget::initializeGL() 9 { 10 //设置着色模式,平滑的 11 glShadeModel(GL_SMOOTH); 12 //清除掉之前的所有颜色 13 glClearColor(0.0,0.0,0.0,0.0); 14 //深度缓存,设置初始值为1.0,小于1.0的部分是可见的 15 glClearDepth(1.0); 16 //启动OPenGL的相关功能,由参数决定,这里指 17 //(启用了之后,OpenGL在绘制的时候就会检查,当前像素前面是否有别的像素,如果别的像素挡道了它,那它就不会绘制,也就是说,OpenGL就只绘制最前面的一层) 18 glEnable(GL_DEPTH_TEST); 19 //制定深度缓存比较值 20 //这里参数指的是如果输入的深度值小于或者等于参考值则通过 21 glDepthFunc(GL_LEQUAL); 22 23 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); 24 25 } 26 27 void OpenglWidget::initWidget() 28 { 29 //从屏幕上(400,400)为起始点,显示一个640*400的界面 30 setGeometry(400,200,640,480); 31 setWindowTitle("My OPenGL"); 32 } 33 34 void OpenglWidget::paintGL() 35 { 36 //清除颜色缓冲和深度缓冲 37 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 38 //将当前点移置屏幕中心,相当于复位的操作 39 glLoadIdentity(); 40 //平移函数,参数指的是分别从X轴,Y轴,Z轴平移 41 glTranslatef(-1.5,0.0,-6.0); 42 43 44 glRotated(angle,0.0,1.0,0.0); 45 //创建图元,是绘制什么图形的接口,参数是绘制多边形的意思 46 glBegin(GL_QUADS); 47 //设置顶点值(f代表浮点),三维空间坐标 48 glColor3f( 1.0, 0.0, 0.0 ); 49 glBegin( GL_QUADS ); 50 51 glColor3f(1, 0, 0); 52 glVertex3f(-1,1,1); 53 glVertex3f(-1,1,-1); 54 glVertex3f(-1,-1,-1); 55 glVertex3f(-1,-1,1); 56 57 glColor3f(0, 0, 1); 58 glVertex3f(-1,1,-1); 59 glVertex3f(-1,-1,-1); 60 glVertex3f(1,-1,-1); 61 glVertex3f(1,1,-1); 62 63 glColor3f(0, 1, 0); 64 glVertex3f(1,1,-1); 65 glVertex3f(1,-1,-1); 66 glVertex3f(1,-1,1); 67 glVertex3f(1,1,1); 68 69 glColor3f(0, 0, 1); 70 glVertex3f(1,-1,1); 71 glVertex3f(1,1,1); 72 glVertex3f(-1,1,1); 73 glVertex3f(-1,-1,1); 74 75 glColor3f(0, 1, 0); 76 glVertex3f(-1,1,1); 77 glVertex3f(1,1,1); 78 glVertex3f(1,1,-1); 79 glVertex3f(-1,1,-1); 80 81 glColor3f(0, 0, 1); 82 glVertex3f(-1,-1,1); 83 glVertex3f(-1,-1,-1); 84 glVertex3f(1,-1,-1); 85 glVertex3f(1,-1,1); 86 glEnd(); 87 88 glLoadIdentity(); 89 90 glTranslatef(1.5,0.0,-6.0); 91 glRotatef( angle1,0.0,1.0,0.0); 92 93 glBegin( GL_TRIANGLES ); 94 95 glColor3f( 1.0, 0.0, 0.0 ); 96 glVertex3f( 0.0, 1.0, 0.0 ); 97 glColor3f( 0.0, 0.0, 1.0 ); 98 glVertex3f( -1.0, -1.0, -1.0 ); 99 glColor3f( 0.0, 1.0, 0.0 ); 100 glVertex3f( -1.0, -1.0, 1.0 ); 101 102 glColor3f( 1.0, 0.0, 0.0 ); 103 glVertex3f( 0.0, 1.0, 0.0 ); 104 glColor3f( 0.0, 0.0, 1.0 ); 105 glVertex3f( 1.0, -1.0, 1.0 ); 106 glColor3f( 0.0, 1.0, 0.0 ); 107 glVertex3f( 1.0, -1.0, -1.0 ); 108 109 glColor3f( 1.0, 0.0, 0.0 ); 110 glVertex3f( 0.0, 1.0, 0.0 ); 111 glColor3f( 0.0, 1.0, 0.0 ); 112 glVertex3f( -1.0, -1.0, 1.0 ); 113 glColor3f( 0.0, 0.0, 1.0 ); 114 glVertex3f( 1.0, -1.0, 1.0 ); 115 116 117 glColor3f( 1.0, 0.0, 0.0 ); 118 glVertex3f( 0.0, 1.0, 0.0 ); 119 glColor3f( 0.0, 1.0, 0.0 ); 120 glVertex3f( 1.0, -1.0, -1.0 ); 121 glColor3f( 0.0, 0.0, 1.0 ); 122 glVertex3f( -1.0, -1.0, -1.0 ); 123 124 glEnd(); 125 126 angle+=70; 127 angle1+=10; 128 129 } 130 131 void OpenglWidget::resizeGL(int width, int height) 132 { 133 if(0==height) 134 height=1; 135 //告诉绘制到窗体的哪个位置 136 glViewport(0,0,width,height); 137 // 设置矩阵模式,参数是设置为投影矩阵 138 glMatrixMode(GL_PROJECTION); 139 //复位操作 140 glLoadIdentity(); 141 142 GLdouble aspectRatio=(GLfloat)width/(GLfloat)height; 143 GLdouble rFov=45.0*3.14159265/180.0; 144 GLdouble zNear=0.1; 145 GLdouble zFar=100.0; 146 //调用glFrustum,生成矩阵与当前矩阵相乘,生成透视效果 147 glFrustum(-zNear*tan(rFov/2.0)*aspectRatio, 148 zNear*tan(rFov/2.0)*aspectRatio, 149 -zNear*tan(rFov/2.0), 150 zNear*tan(rFov/2.0), 151 zNear,zFar); 152 //切回模型视图矩阵 153 glMatrixMode(GL_MODELVIEW); 154 //复位 155 glLoadIdentity(); 156 }