{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f); // Move Right 3 Units
glRotatef(rquad,1.0f,0.0f,0.0f);
glBegin(GL_QUADS); // Draw A Quad
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glColor3f(1.0f,1.0f,1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad// Reset The Current Modelview Matrix
rtri+=0.2f;
rquad-=0.15f;
return TRUE; // Everything Went OK
}
glColor3f(r,g,b)。括号中的三个参数依次是红、绿、蓝三色分量。取值范围可以从0,0f到1.0f。因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。
glRotatef(Angle,Xvector,Yvector,Zvector)负责让对象绕某个轴旋转。这个命令有很多用处。Angle通常是个变量代表对象转过的角度。Xvector、Yvector、Zvector三个参数则共同决定旋转轴的方向。比如(1,0,0)所描述的矢量经过X坐标轴的1个单位处并且方向向右。(-1,0,0)所描述的矢量经过X坐标轴的1个单位处,但方向向左。
代码中我们增加了另一个glLoadIdentity()调用。目的是为了重置模型观察矩阵。如果我们没有重置,直接调用glTranslate的话,会出现意料之外的结果。