OpenGL Study 3

 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个正方形组成一个正方体。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值