1、OpenGL 绘制的几类三维实体EXAM304_H/防止重复嵌入#if !defined _EXAM304_H_#define _EXAM304_H_/指定嵌入文件#include#include/宏#define RADIUS 50.0f#define SIZE 100.0f#define CONE 1#define TETRAHEDRON 2#define CUBE 3#define DODECAHEDRON 4#define ICOSAHEDRON 5#define OCTAHEDRON 6#define SPHERE 7#define TORUS 8#define TEAPOT 9#d
2、efine WIRED 10/全局变量GLboolean bWire=false;GLint nSelected=CONE;/函数原型void OnDisplay(void);void OnReshape(int,int);void OnMenu(int value);void CreateMenu();void SetupLights();#endifEXAM304.cpp#include “EXAM304.H“void main(int argc,char *argv)glutInit(glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH)
3、;glutInitWindowSize(640,480);glutInitWindowPosition(10,10);glutCreateWindow(“EXAM304“);glutCreateMenu(OnMenu);glutDisplayFunc(OnDisplay);glutReshapeFunc(OnReshape);CreateMenu();SetupLights();glutMainLoop();void OnDisplay(void)glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glEnable(GL_DEPTH_TEST);g
4、lPushMatrix();glColor3f(0.0f,1.0f,1.0f);glEnable(GL_NORMALIZE);switch(nSelected)case(CONE):if(bWire)glutWireCone(RADIUS,2*RADIUS,30,30);elseglutSolidCone(RADIUS,2*RADIUS,30,30);break;case(TETRAHEDRON):glPushMatrix();glScalef(RADIUS,RADIUS,RADIUS);if(bWire)glutWireTetrahedron();elseglutSolidTetrahedr
5、on();glPopMatrix();break;case(CUBE):if(bWire)glutWireCube(1.5*RADIUS);elseglutSolidCube(1.5*RADIUS);break;case(DODECAHEDRON):glPushMatrix();glScalef(RADIUS/2,RADIUS/2,RADIUS/2);if(bWire)glutWireDodecahedron();elseglutSolidDodecahedron();glPopMatrix();break;case(ICOSAHEDRON):glPushMatrix();glScalef(R
6、ADIUS,RADIUS,RADIUS);if(bWire)glutWireIcosahedron();elseglutSolidIcosahedron();glPopMatrix();break;case(OCTAHEDRON):glPushMatrix();glScalef(RADIUS,RADIUS,RADIUS);if(bWire)glutWireOctahedron();elseglutSolidOctahedron();glPopMatrix();break;case(SPHERE):if(bWire)glutWireSphere(RADIUS,30,30);elseglutSol
7、idSphere(RADIUS,30,30);break;case(TORUS):if(bWire)glutWireTorus(RADIUS/2,RADIUS,30,30);elseglutSolidTorus(RADIUS/2,RADIUS,30,30);break;case(TEAPOT):if(bWire)glutWireTeapot(RADIUS);elseglutSolidTeapot(RADIUS);glPopMatrix();glutSwapBuffers();/窗口尺寸变化后的回调函数void OnReshape(int w,int h)glViewport(0,0,w,h);
8、glMatrixMode(GL_PROJECTION);glLoadIdentity();float aspect=(float)h/(float)w;if(w=h)glOrtho(-SIZE,SIZE,-SIZE*aspect,SIZE*aspect,-SIZE,SIZE);elseglOrtho(-SIZE/aspect,SIZE/aspect,-SIZE,SIZE,-SIZE,SIZE);gluLookAt(10.0f,20.0f,25.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);void OnMenu(int value)if(value=WIRED)bWire
9、=!bWire;elsenSelected=value;glutPostRedisplay();void CreateMenu()glutAddMenuEntry(“圆锥体“,CONE);glutAddMenuEntry(“四面体“,TETRAHEDRON);glutAddMenuEntry(“正方体“,CUBE);glutAddMenuEntry(“正十二面体“,DODECAHEDRON);glutAddMenuEntry(“正二十面体“,ICOSAHEDRON);glutAddMenuEntry(“正八面体“,OCTAHEDRON);glutAddMenuEntry(“球体“,SPHERE
10、);glutAddMenuEntry(“圆环体“,TORUS);glutAddMenuEntry(“茶壶“,TEAPOT);glutAddMenuEntry(“线框或实体“,WIRED);glutAttachMenu(GLUT_RIGHT_BUTTON);void SetupLights()GLfloat ambientLight=0.2f,0.2f,0.2f,1.0f;GLfloat diffuseLight=0.9f,0.9f,0.9f,1.0f;GLfloat specularLight=1.0f,1.0f,1.0f,1.0f;GLfloat lightPos=50.0f,100.0f,
11、80.0f,1.0f;glEnable(GL_LIGHTING);glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);glLightfv(GL_LIGHT0,GL_POSITION,lightPos);glEnable(GL_LIGHT0);glEnable(GL_COLOR_MATERIAL);glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);glMaterialfv(GL_FRONT,GL_SPECULAR,specularLight);glMateriali(GL_FRONT,GL_SHININESS,100);