[NEHE Couse] 05.3D Objects

     在这节课中终于从二维平面拓展到三维平面上来了,程序中绘制了两个3D物体,一个是四面体棱锥,一个是一个正方体,为了看起来效果好些,四棱锥每个顶点的颜色都不一样,然后颜色样式采用GL_SMOOTH来控制,通过键盘控制,按下键'S',可以控制四棱锥绕Y轴旋转,按下键'S',可以控制正方体绕点(0,0,0)和点(1,1,1)的连线所在的直线旋转,Esc键控制退出,空格键控制全屏与否。
     此外为了让这两个3D物体看起来不至于变形,我在投影方式上采用函数glOrtho来控制。(投影矩阵应该由你在屏幕中显示物体的大小来控制,这与物体的坐标有直接的关系,同时这也取决于你,如果你想让物体看起来大些,那么投影区域你应该设置稍大于显示物体的包围盒即可,如果你想让物体看起来小些,投影区域应该大大于包围盒)。
程序如下:

  1/*
  2Introduction:Written by krisdy,finished at 2009.1.10
  3Apply to:NEHE Couse 05:3D Objects
  4*/

  5#include <gl/glut.h>
  6#include <stdlib.h>
  7
  8#define WinWidth 500        //the width of the window
  9#define WinHeight 500        //the height of the window
 10
 11static GLint t;
 12static GLfloat spin1,spin2;        //control the rotate range
 13
 14void init(void)
 15{
 16    glShadeModel(GL_SMOOTH);
 17    glClearColor(0.0f,0.0f,0.0f,0.0f);
 18    glClearDepth(1.0f);
 19    glEnable(GL_DEPTH_TEST);
 20}

 21void display(void)
 22{
 23    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 24    glLoadIdentity();
 25    glPushMatrix();
 26    glTranslatef(-1.5f,0.0f,-6.0f);
 27    glRotatef(spin1,0.0f,1.0f,0.0f);    //revolve round y-axis
 28    glBegin(GL_TRIANGLES);
 29        glColor3f(1.0f,0.0f,0.0f);
 30        glVertex3f(0.0f,1.0f,0.0f);
 31        glColor3f(0.0f,0.0f,1.0f);
 32        glVertex3f(1.0f,-1.0f,1.0f);
 33        glColor3f(0.0f,1.0f,0.0f);
 34        glVertex3f(1.0f,-1.0f,-1.0f);
 35        glColor3f(1.0f,0.0f,0.0f);
 36        glVertex3f(0.0f,1.0f,0.0f);
 37        glColor3f(0.0f,1.0f,0.0f);
 38        glVertex3f(1.0f,-1.0f,-1.0f);
 39        glColor3f(0.0f,0.0f,1.0f);
 40        glVertex3f(-1.0f,-1.0f,-1.0f);
 41        glColor3f(1.0f,0.0f,0.0f);
 42        glVertex3f(0.0f,1.0f,0.0f);
 43        glColor3f(0.0f,0.0f,1.0f);
 44        glVertex3f(-1.0f,-1.0f,-1.0f);
 45        glColor3f(0.0f,1.0f,0.0f);
 46        glVertex3f(-1.0f,-1.0f,1.0f);
 47    glEnd();
 48    glPopMatrix();
 49    glPushMatrix();
 50    glTranslatef(1.5f,0.0f,-7.0f);
 51    //rotate round the line which connect (0,0,0) and (1,1,1) and extended..
 52    glRotatef(spin2,1.0f,1.0f,1.0f);    
 53    glBegin(GL_QUADS);
 54        glColor3f(0.0f,1.0f,0.0f);            
 55        glVertex3f( 1.0f1.0f,-1.0f);            
 56        glVertex3f(-1.0f1.0f,-1.0f);            
 57        glVertex3f(-1.0f1.0f1.0f);            
 58        glVertex3f( 1.0f1.0f1.0f);
 59        glColor3f(1.0f,0.5f,0.0f);            
 60        glVertex3f( 1.0f,-1.0f1.0f);        
 61        glVertex3f(-1.0f,-1.0f1.0f);            
 62        glVertex3f(-1.0f,-1.0f,-1.0f);            
 63        glVertex3f( 1.0f,-1.0f,-1.0f);
 64        glColor3f(1.0f,0.0f,0.0f);            
 65        glVertex3f( 1.0f1.0f1.0f);            
 66        glVertex3f(-1.0f1.0f1.0f);            
 67        glVertex3f(-1.0f,-1.0f1.0f);            
 68        glVertex3f( 1.0f,-1.0f1.0f);
 69        glColor3f(1.0f,1.0f,0.0f);            
 70        glVertex3f( 1.0f,-1.0f,-1.0f);            
 71        glVertex3f(-1.0f,-1.0f,-1.0f);            
 72        glVertex3f(-1.0f1.0f,-1.0f);            
 73        glVertex3f( 1.0f1.0f,-1.0f);
 74        glColor3f(0.0f,0.0f,1.0f);            
 75        glVertex3f(-1.0f1.0f1.0f);            
 76        glVertex3f(-1.0f1.0f,-1.0f);            
 77        glVertex3f(-1.0f,-1.0f,-1.0f);            
 78        glVertex3f(-1.0f,-1.0f1.0f);
 79        glColor3f(1.0f,0.0f,1.0f);            
 80        glVertex3f( 1.0f1.0f,-1.0f);            
 81        glVertex3f( 1.0f1.0f1.0f);            
 82        glVertex3f( 1.0f,-1.0f1.0f);            
 83        glVertex3f( 1.0f,-1.0f,-1.0f);            
 84    glEnd();    
 85    glPopMatrix();
 86    glutSwapBuffers();
 87}

 88void reshape(int w,int h)
 89{
 90    GLfloat range = 5.0f;
 91    if(h == 0)
 92        h = 1;
 93    glViewport(0,0,w,h);
 94    glMatrixMode(GL_PROJECTION);
 95    glLoadIdentity();
 96    if(w < h)
 97        glOrtho(-range,range,-GLfloat(range*h)/w,GLfloat(range*h)/w,-10.0f,10.0f);
 98    else
 99        glOrtho(-GLfloat(range*w)/h,GLfloat(range*w)/h,-range,range,-10.0f,10.0f);
100    glMatrixMode(GL_MODELVIEW);
101    glLoadIdentity();
102}

103void keyboard(unsigned char key,int x,int y)
104{
105    switch(key)
106    {
107    //use the space key to decide whether the window will be full-screen displayed.
108    case 32:                                            
109        if(t%2 == 0)
110            //requests that the current window be made full screen
111            glutFullScreen();                            
112        else
113            //requests a change to the size of the current window
114            glutReshapeWindow(WinWidth,WinHeight);        
115        t++;
116        break;
117    //use the key w to control the change of spin1
118    case 'w':
119    case 'W':
120        spin1 = spin1 + 2.0f;
121        if(spin1 > 360.0f)
122            spin1 -= 360.0f;
123        glutPostRedisplay();
124        break;
125    //use the key s to control the change of spin2
126    case 's':
127    case 'S':
128        spin2 = spin2 + 2.0f;
129        if(spin2 > 360.0f)
130            spin2 -= 360.0f;
131        glutPostRedisplay();
132        break;
133    //use the Esc key to escape the application
134    case 27:
135        exit(NULL);
136        break;
137    default:
138        break;
139    }

140}

141int main(int argc,char *argv[])
142{
143    glutInit(&argc,argv);
144    glutInitDisplayMode(GLUT_DEPTH|GLUT_RGB|GLUT_DOUBLE);
145    glutInitWindowSize(WinWidth,WinHeight);
146    glutInitWindowPosition(100,100);
147    glutCreateWindow("Lesson 05");
148
149    init();
150    glutDisplayFunc(display);
151    glutReshapeFunc(reshape);
152    glutKeyboardFunc(keyboard);
153    glutMainLoop();
154    return 0;
155}

 

程序效果:

posted on 2009-01-10 19:45  笔记  阅读(332)  评论(0编辑  收藏  举报

导航