opengl学习 nehe opengl lesson_5

opengl学习 nehe opengl lesson_5

nehe教程里头,该课的内容是利用多边形来组成三维图形.我只实现了一个利用三角形绘制四面体,而其中的正方体则是采用glut工具库自带的函数绘制的

#include 
" openglglut.h "

/**/ /*
**  使用四个三角形绘制成一个四面体,绕Y轴旋转
**  使用glut工具库绘制一个灰色正方体,绕XYZ轴旋转
**  nehe教程里头是采用绘制正方体的6个面来绘制正方体的.
*
*/

GLfloat yRot
= 0 ;                                  /**/ /** 四面体绕Y轴旋转角度*/
GLfloat xRot
= 0 ;                                  /**/ /** 正方体绕xyz轴旋转角度*/
int  main( int  argc, char **  argv)
{
    createWindow(
"opengl lesson_5",&argc,argv); /**//** 初始化并创建窗口*/

    glutDisplayFunc(glutDisplay);               
/**//** 注册重绘函数*/
    glutIdleFunc(glutIdle);                     
/**//** 注册空闲回调函数*/
    glutReshapeFunc(glutResize);                
/**//** 注册窗口调整函数*/
    glutSpecialFunc(glutSpecial);               
/**//** 注册特殊按键窗口*/
    glutKeyboardFunc(glutKeyboard);             
/**//** 注册键盘处理函数*/

    InitOpenGL();                               
/**//** 初始化opengl*/

    glutMainLoop();                             
/**//** 仿真循环*/
}

void  glutDisplay( void )                           /**/ /** 重绘函数*/
{
    glClear(GL_COLOR_BUFFER_BIT 
| GL_DEPTH_BUFFER_BIT);/**//** 清除颜色缓存和深度缓存*/
    
    glLoadIdentity();                           
/**//** 初始化当前矩阵为单位矩阵*/

    glPushMatrix();                             
/**//** 保存当前矩阵*/
    glTranslatef(
-1.5,0.0,-6.0);                /**//** 往左移动和往屏幕内部移动,绘制四面体*/
    glRotatef(yRot,
0.0,1.0,0.0);                /**//** 四面体绕y轴旋转yRot角度*/
    
    glBegin(GL_TRIANGLES);                      
/**//** 通过绘制四个三角形组成四面体*/
        glColor3f(
1.0f,0.0f,0.0f);              /**//** 红色*/
        glVertex3f( 
0.0f1.0f0.0f);          /**//** 三角形的上顶点 (前侧面)*/
        glColor3f(
0.0f,1.0f,0.0f);              /**//** 绿色*/
        glVertex3f(
-1.0f,-1.0f1.0f);          /**//** 三角形的左下顶点 (前侧面)*/
        glColor3f(
0.0f,0.0f,1.0f);              /**//** 蓝色*/
        glVertex3f( 
1.0f,-1.0f1.0f);          /**//** 三角形的右下顶点 (前侧面)*/

        glColor3f(
1.0f,0.0f,0.0f);                /**//**红色*/
        glVertex3f( 
0.0f1.0f0.0f);            /**//**三角形的上顶点 (右侧面)*/
        glColor3f(
0.0f,0.0f,1.0f);              /**//**  蓝色 */
        glVertex3f( 
1.0f,-1.0f1.0f);          /**//** 三角形的左下顶点 (右侧面)*/
        glColor3f(
0.0f,1.0f,0.0f);              /**//** 绿色*/
        glVertex3f( 
1.0f,-1.0f-1.0f);         /**//** 三角形的右下顶点 (右侧面)*/

        glColor3f(
1.0f,0.0f,0.0f);              /**//** 红色*/
        glVertex3f( 
0.0f1.0f0.0f);          /**//** 三角形的上顶点 (后侧面)*/
        glColor3f(
0.0f,1.0f,0.0f);              /**//** 绿色*/
        glVertex3f( 
1.0f,-1.0f-1.0f);         /**//** 三角形的左下顶点 (后侧面)*/
        glColor3f(
0.0f,0.0f,1.0f);              /**//** 蓝色*/
        glVertex3f(
-1.0f,-1.0f-1.0f);         /**//** 三角形的右下顶点 (后侧面)*/

        glColor3f(
1.0f,0.0f,0.0f);              /**//** 红色*/
        glVertex3f( 
0.0f1.0f0.0f);          /**//** 三角形的上顶点 (左侧面)*/
        glColor3f(
0.0f,0.0f,1.0f);              /**//** 蓝色*/
        glVertex3f(
-1.0f,-1.0f,-1.0f);          /**//** 三角形的左下顶点 (左侧面)*/
        glColor3f(
0.0f,1.0f,0.0f);                /**//** 绿色*/
        glVertex3f(
-1.0f,-1.0f1.0f);            /**//**三角形的右下顶点 (左侧面)*/
    glEnd();                                    
/**//**  四面体绘制结束*/
    
    glPopMatrix();                              
/**//** 恢复当前矩阵为保持前*/

    glTranslatef(
1.5,0.0,-6.0);                 /**//** 往右移动和往屏幕内部移动,绘制正方体*/
    glRotatef(xRot,
1.0,1.0,1.0);                /**//** 正方体绕XYZ轴旋转*/ 

    glColor3f(
0.5,0.5,0.5);                     /**//** 鉴于glut中提供了正方体的绘制*/
    glutSolidCube(
1.732);                       /**//** 故使用glut中方法绘制正方体*/

    glutSwapBuffers();

}

void  glutIdle( void )                              /**/ /** 回调函数*/
{
    yRot
+=0.5;                                  /**//** 绕y轴旋转角度递增*/
    xRot
-=0.5;                                  /**//** 绕x轴旋转角度递减*/
    glutPostRedisplay();                        
/**//** 使得函数完成后进行窗口重绘*/
}


void  glutKeyboard(unsigned  char  key, int  x, int  y)
{
    
switch(key)
    
{
        
case 27:                                /**//** Esc按键按下后退出程序 */
            exit(
0);
            
break;
        
default:
            
break;
    }

}

最终显示结果是:
opengl学习 nehe opengl lesson_5_第1张图片

末尾总结:
opengl当中,复杂的图形总是通过最基本的图元组成,当然绘制起来也不见得会非常的简单.通常很多复杂的模型等大多数是通过建模软件生成的.
这节课当中,并没有多大的内容,基本用到都是之前lesson当中学习过的.

你可能感兴趣的:(opengl学习 nehe opengl lesson_5)