OpenGL Study 3

来源:互联网 发布:大淘客cms怎么样 编辑:程序博客网 时间:2024/06/05 09:18

 Lesson04

实现图形的旋转

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);

    
//rotate around the y axis
    glRotatef(rtri,0.0f,1.0f,0.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();

    
//reset the current modleview matrix
    glLoadIdentity();

    
//move from (0.0,0.0,0.0) to (1.5,0.0,-6.0)
    glTranslatef(1.5f,0.0f,-6.0f);

    
//rotate around the x axis
    glRotatef(rquad,1.0f,0.0f,0.0f);

    
//draw a quad,the vertex is represented in the model coordinate
    
//with flat coloring
    glColor3f(0.5f,0.5f,1.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();

    rtri
+=0.2f;
    rquad
-=0.15f;
    
return true;
}

使用glRotatef(r,x,y,z)使图形绕向量(x,y,z)旋转r弧度,r>0时,正对环绕轴来看,逆时针旋转。

代码中两次出现了glLoadIdentity()函数,在每绘制一个图形之前都将模型视图矩阵置一,然后在移动,旋转进行必要的变换,再绘制图形,这样可以防止不同图形之间变换矩阵的影响。

Lesson05

绘制3D物体

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);

    
//rotate around the y axis
    glRotatef(rtri,0.0f,1.0f,0.0f);

    
//draw a diamond made of 4 triangles,with smooth coloring
    glBegin(GL_TRIANGLES);
        
//front
        glColor3f(1.0f,0.0f,0.0f);//red
        glVertex3f(0.0f,1.0f,0.0f);
        glColor3f(
0.0f,1.0f,0.0f);//green
        glVertex3f(-1.0f,-1.0f,1.0f);
        glColor3f(
0.0f,0.0f,1.0f);//blue
        glVertex3f(1.0f,-1.0f,1.0f);

        
//right
        glColor3f(1.0f,0.0f,0.0f);//red
        glVertex3f(0.0f,1.0f,0.0f);
        glColor3f(
0.0f,0.0f,1.0f);//blue
        glVertex3f(1.0f,-1.0f,1.0f);
        glColor3f(
0.5f,0.5f,0.0f);//rg
        glVertex3f(1.0f,-1.0f,-1.0f);

        
//back
        glColor3f(1.0f,0.0f,0.0f);//red
        glVertex3f(0.0f,1.0f,0.0f);
        glColor3f(
0.5f,0.5f,0.0f);//rg
        glVertex3f(1.0f,-1.0f,-1.0f);
        glColor3f(
0.5f,0.0f,0.5f);//rb
        glVertex3f(-1.0f,-1.0f,-1.0f);

        
//back
        glColor3f(1.0f,0.0f,0.0f);//red
        glVertex3f(0.0f,1.0f,0.0f);
        glColor3f(
0.5f,0.0f,0.5f);//rb
        glVertex3f(-1.0f,-1.0f,-1.0f);
        glColor3f(
0.0f,1.0f,0.0f);//green
        glVertex3f(-1.0f,-1.0f,1.0f);

    glEnd();

    
//reset the current modleview matrix
    glLoadIdentity();

    
//move from (0.0,0.0,0.0) to (1.5,0.0,-6.0)
    glTranslatef(1.5f,0.0f,-6.0f);

    
//rotate around the x axis
    glRotatef(rquad,1.0f,0.0f,0.0f);

    
//draw a cubic made of 6 quads,the vertex is represented in the model coordinate
    
//with flat coloring
    glBegin(GL_QUADS);
        
//top,y=1
        glColor3f(1.0f,0.0f,0.0f);
        glVertex3f(
-1.0f,1.0f,1.0f);
        glVertex3f(
1.0f,1.0f,1.0f);
        glVertex3f(
1.0f,1.0f,-1.0f);
        glVertex3f(
-1.0f,1.0f,-1.0f);

        
//bottom,y=-1
        glColor3f(0.0f,1.0f,0.0f);
        glVertex3f(
-1.0f,-1.0f,1.0f);
        glVertex3f(
-1.0f,-1.0f,-1.0f);
        glVertex3f(
1.0f,-1.0f,-1.0f);
        glVertex3f(
1.0f,-1.0f,1.0f);

        
//front,z=1
        glColor3f(0.0f,0.0f,1.0f);
        glVertex3f(
-1.0f,-1.0f,1.0f);
        glVertex3f(
1.0f,-1.0f,1.0f);
        glVertex3f(
1.0f,1.0f,1.0f);
        glVertex3f(
-1.0f,1.0f,1.0f);

        
//right,x=1
        glColor3f(1.0f,1.0f,0.0f);
        glVertex3f(
1.0f,-1.0f,1.0f);
        glVertex3f(
1.0f,-1.0f,-1.0f);
        glVertex3f(
1.0f,1.0f,-1.0f);
        glVertex3f(
1.0f,1.0f,1.0f);

        
//back,z=-1
        glColor3f(0.0f,1.0f,1.0f);
        glVertex3f(
-1.0f,-1.0f,-1.0f);
        glVertex3f(
-1.0f,1.0f,-1.0f);
        glVertex3f(
1.0f,1.0f,-1.0f);
        glVertex3f(
1.0f,-1.0f,-1.0f);

        
//left,x=-1
        glColor3f(1.0f,0.0f,1.0f);
        glVertex3f(
-1.0f,-1.0f,1.0f);
        glVertex3f(
-1.0f,1.0f,1.0f);
        glVertex3f(
-1.0f,1.0f,-1.0f);
        glVertex3f(
-1.0f,-1.0f,-1.0f);

    glEnd();

    rtri
+=0.2f;
    rquad
-=0.15f;
    
return true;
}

使用4个三角形组成一个金字塔,采用6个正方形组成一个正方体。

原创粉丝点击