QT与OpenGL之3D图形绘制
只给出绘制图形的代码,别的代码前面的文章有,可以参照。
绘制金字塔的函数:
void GLWidget::paintGL()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity();
//renderText(10,20,"Hello,My Dear");
// glColor3f(1.0, 0.0, 0.0);
/*
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 1.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glEnd();
*/
// glTranslatef(-1.5, 0.0, -6.0);
// glRotatef(rTri, 0.0, 0.0, 1.0);
/*************************三角形************************
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
*****************************************************/
glRotatef(rTri, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glLoadIdentity();
renderText(20, 100, "Hello Jcy, OpenGL welcome to you.");
}
在编写这种三维图形的时候需要注意的一个问题就是模式的选择,开始的时候我选择的是GL_FLAT模式,这种模式画出来的效果就是前面介绍三角形绘制时候的效果,全部是蓝色,但是我的三角形代码跟这个上面的代码是一样的,而我现在选择的是GL_SMOOTH模式,这种方式出来的效果就是下面的效果,显而易见的,GL_SMOOTH画出来的更能展现出三维,更立体。
效果如下:
旋转时候的效果: