今天主要学习二维图形的着色和旋转。
None.gifint DrawGLScene(GLvoid)                                    // Here's Where We Do All The Drawing
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    glClear(GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT);    // Clear Screen And Depth Buffer
InBlock.gif
    glLoadIdentity();    
InBlock.gif    glTranslatef(
-1.5f,0.0f,-6.0f);                        // Move Left 1.5 Units And Into The Screen 6.0
InBlock.gif
    glRotatef(rtri,0.0f,1.0f,0.0f);
InBlock.gif    glBegin(GL_TRIANGLES);                                
// Drawing Using Triangles
InBlock.gif
        glColor3f(1.0f,0.0f,0.0f);
InBlock.gif        glVertex3f( 
0.0f1.0f0.0f);                    // Top
InBlock.gif
        glColor3f(0.0f,1.0f,0.0f);
InBlock.gif        glVertex3f(
-1.0f,-1.0f0.0f);                    // Bottom Left
InBlock.gif
        glColor3f(0.0f,0.0f,1.0f);
InBlock.gif        glVertex3f( 
1.0f,-1.0f0.0f);                    // Bottom Right
InBlock.gif
    glEnd();                                            // Finished Drawing The Triangle
InBlock.gif
    glLoadIdentity();
InBlock.gif    glTranslatef(
1.5f,0.0f,-6.0f);                        // Move Right 3 Units
InBlock.gif
    glRotatef(rquad,1.0f,0.0f,0.0f);
InBlock.gif    glBegin(GL_QUADS);                                    
// Draw A Quad
InBlock.gif
        glColor3f(1.0f,0.0f,0.0f);
InBlock.gif        glVertex3f(
-1.0f1.0f0.0f);                    // Top Left
InBlock.gif
        glColor3f(0.0f,1.0f,0.0f);
InBlock.gif        glVertex3f( 
1.0f1.0f0.0f);                    // Top Right
InBlock.gif
        glColor3f(0.0f,0.0f,1.0f);
InBlock.gif        glVertex3f( 
1.0f,-1.0f0.0f);                    // Bottom Right
InBlock.gif
        glColor3f(1.0f,1.0f,1.0f);
InBlock.gif        glVertex3f(
-1.0f,-1.0f0.0f);                    // Bottom Left
InBlock.gif
    glEnd();                                            // Done Drawing The Quad// Reset The Current Modelview Matrix
InBlock.gif
    rtri+=0.2f;
InBlock.gif    rquad
-=0.15f;
InBlock.gif    
return TRUE;                                        // Everything Went OK
ExpandedBlockEnd.gif
}

None.gif

200741702.JPG


glColor3f(r,g,b)。括号中的三个参数依次是红、绿、蓝三色分量。取值范围可以从0,0f1.0f。因为每个顶点有不同的颜色,因此看起来颜色从每个角喷出,并刚好在三角形的中心汇合,三种颜色相互混合。

glRotatef(Angle,Xvector,Yvector,Zvector)负责让对象绕某个轴旋转。这个命令有很多用处。Angle通常是个变量代表对象转过的角度。XvectorYvectorZvector三个参数则共同决定旋转轴的方向。比如(1,0,0)所描述的矢量经过X坐标轴的1个单位处并且方向向右。(-1,0,0)所描述的矢量经过X坐标轴的1个单位处,但方向向左。

代码中我们增加了另一个glLoadIdentity()调用。目的是为了重置模型观察矩阵。如果我们没有重置,直接调用glTranslate的话,会出现意料之外的结果。