Lesson02:
在前一课建立的Opengl窗口中绘制一个三角形和一个正方形。
//
here we do all the drawings
bool DrawGLScene(GLvoid)
{
// clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// reset the current modelview matrix
glLoadIdentity();
// move left 1.5 units,deep 6.0 units from current position
// move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef( - 1.5f , 0.0f , - 6.0f );
// draw a triangle
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f , 1.0f , 0.0f );
glVertex3f( - 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , - 1.0f , 0.0f );
glEnd();
// move from (-1.5,0.0,-6.0) to (1.5,0.0,-6.0)
glTranslatef( 3.0f , 0.0f , 0.0f );
// draw a quad,the vertex is represented in the model coordinate
glBegin(GL_QUADS);
glVertex3f( - 1.0f , 1.0f , 0.0f );
glVertex3f( - 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , 1.0f , 0.0f );
glEnd();
return true ;
}
bool DrawGLScene(GLvoid)
{
// clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// reset the current modelview matrix
glLoadIdentity();
// move left 1.5 units,deep 6.0 units from current position
// move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef( - 1.5f , 0.0f , - 6.0f );
// draw a triangle
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f , 1.0f , 0.0f );
glVertex3f( - 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , - 1.0f , 0.0f );
glEnd();
// move from (-1.5,0.0,-6.0) to (1.5,0.0,-6.0)
glTranslatef( 3.0f , 0.0f , 0.0f );
// draw a quad,the vertex is represented in the model coordinate
glBegin(GL_QUADS);
glVertex3f( - 1.0f , 1.0f , 0.0f );
glVertex3f( - 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , 1.0f , 0.0f );
glEnd();
return true ;
}
绘制前先清除颜色缓存和深度缓存,将当前矩阵置为单位矩阵。
在该代码中没有使用视图变换,而是采用模型变换实现的。对于每个要绘制的图形,现使用glTranslatef()将局部坐标系的原点移动到适当的位置,然后在绘制时,就可以在局部坐标系中以原点为中心绘制相应图形了。
注意,glTranslatef()的参数是移动增量,是将当前位置移动指定的增量到目标位置,移动后当前矩阵值为目标位置的值。
绘制图形:在glBegin()和glEnd()指定图形的顶点坐标。
绘制三角形:采用GL_TRIANGLES参数,使用三个顶点绘制一个三角形,顶点要按逆时针的顺序列出;
绘制四边形:采用GL_QUADS参数,使用四个顶点,顶点按逆时针顺序列出
Lesson03
给图形着色,采用Flat coloring 和 Smooth Coloring 两种着色模式
//
here we do all the drawings
bool DrawGLScene(GLvoid)
{
// clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// reset the current modelview matrix
glLoadIdentity();
// move left 1.5 units,deep 6.0 units from current position
// move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef( - 1.5f , 0.0f , - 6.0f );
// draw a triangle,with smooth coloring
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();
// move from (-1.5,0.0,-6.0) to (1.5,0.0,-6.0)
glTranslatef( 3.0f , 0.0f , 0.0f );
// draw a quad,the vertex is represented in the model coordinate
// with flat coloring
glColor3f( 0.5f , 0.5f , 0.0f );
glBegin(GL_QUADS);
glVertex3f( - 1.0f , 1.0f , 0.0f );
glVertex3f( - 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , 1.0f , 0.0f );
glEnd();
return true ;
}
bool DrawGLScene(GLvoid)
{
// clear the screen and the depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// reset the current modelview matrix
glLoadIdentity();
// move left 1.5 units,deep 6.0 units from current position
// move the model from (0,0,0) to (-1.5,0.0,-6.0),it can also be implemented by move the view point
glTranslatef( - 1.5f , 0.0f , - 6.0f );
// draw a triangle,with smooth coloring
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();
// move from (-1.5,0.0,-6.0) to (1.5,0.0,-6.0)
glTranslatef( 3.0f , 0.0f , 0.0f );
// draw a quad,the vertex is represented in the model coordinate
// with flat coloring
glColor3f( 0.5f , 0.5f , 0.0f );
glBegin(GL_QUADS);
glVertex3f( - 1.0f , 1.0f , 0.0f );
glVertex3f( - 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , - 1.0f , 0.0f );
glVertex3f( 1.0f , 1.0f , 0.0f );
glEnd();
return true ;
}
对每个顶点都设置颜色后,Opengl会自动在不同顶点的不同颜色间进行插值,从而使颜色线性变换;
如果对某个顶点不设置颜色,则应用之前最后设置的颜色,例如在绘制四边形时,只在绘制第一个顶点时设置了颜色,则其他顶点采用相同颜色。
在Opengl中,视图矩阵,投影矩阵,颜色矩阵和纹理矩阵都只记录最后一次修改后的值。