OpenGL颜色

本文功能类似Nehe教程第二课,在OpenGL中使用颜色。着色可以使用两种方式,包括Flat模式(单调着色)和Smooth模式(平滑着色)。

本代码在第一个OpenGL程序代码基础上扩充。增加一个四边形的填充显示。

修改paintGL()函数代码如下:

void WGLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(-1.5,0.0,-6.0); // 绘制三角形,平滑着色 glBegin(GL_TRIANGLES); 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,0.0); glColor3f(0.0,0.0,1.0); glVertex3f(1.0,-1.0,0.0); glEnd(); // 绘制五角星 glTranslatef(3.0,-1.5,0.0); glColor3f(1.0,1.0,1.0); glBegin(GL_LINE_LOOP); glArrayElement(1); glArrayElement(4); glArrayElement(2); glArrayElement(0); glArrayElement(3); glEnd(); // 绘制四边形填充,单调着色 glTranslatef(0.0,2.5,0.0); glColor3f(0.0,1.0,0.0); glBegin(GL_POLYGON); glVertex3f(-1.0,1.0,0.0); glVertex3f(-1.0,-1.0,0.0); glVertex3f(1.0,-1.0,0.0); glVertex3f(1.0,1.0,0.0); glEnd(); }

OpenGL是一个状态机,颜色为状态中的一种,一旦设置了后续的物体都使用该颜色。

你可能感兴趣的:(buffer)