GLUT提供的9种实体对象

  1 #define GLUT_DISABLE_ATEXIT_HACK
2 #include "gl/glut.h"
3 #include
4
5 #define RADIUS 50.0f
6 #define SIZE 100.0f
7
8 #define CONE 1
9 #define TETRAHEDRON 2
10 #define CUBE 3
11 #define DODECAHEDRON 4
12 #define ICONSAHEDRON 5
13 #define OCTAHEDRON 6
14 #define SPHERE 7
15 #define TOURUS 8
16 #define TEAPOT 9
17 #define WIRED 10
18
19 GLboolean bWire=false;
20 GLint nSelected=CONE;
21
22 void OnDisplay(void);
23 void OnReshape(int,int);
24 void OnMenu(int value);
25 void CreateMenu();
26 void SetupLights();
27
28 void main(int argc,char* argv[])
29 {
30 glutInit(&argc,argv); //初始化OpenGL
31 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); //设置显示模式
32 glutInitWindowSize(600,480);
33 glutCreateWindow("GLUT提供的9种实体对象");
34
35 glutCreateMenu(OnMenu);
36 glutReshapeFunc(OnReshape);
37 glutDisplayFunc(OnDisplay);
38
39 CreateMenu(); //实际生成菜单
40 SetupLights(); //设置光照
41
42
43 glutMainLoop(); //进入OpenGL主循环
44 }
45
46 void OnDisplay(void)
47 {
48 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
49
50 glEnable(GL_DEPTH_TEST); //使能深度检测
51
52 glPushMatrix();
53
54 glColor3f(1.0f,0.0f,0.0f);
55
56 glEnable(GL_NORMALIZE); //归一化法向量
57 switch(nSelected){
58 case(CONE): //圆锥体
59 if (bWire)
60 glutWireCone(RADIUS,2*RADIUS,30,30);
61 else
62 glutSolidCone(RADIUS,2*RADIUS,30,30);
63 break;
64
65 case(TETRAHEDRON): //四面体
66 glPushMatrix();
67 glScalef(RADIUS,RADIUS,RADIUS);
68 if(bWire)
69 glutWireTetrahedron();
70 else
71 glutSolidTetrahedron();
72 glPopMatrix();
73 break;
74
75 case(CUBE): //正方体
76 if(bWire)
77 glutWireCube(1.5 * RADIUS);
78 else
79 glutSolidCube(1.5 *RADIUS);
80 break;
81
82 case(DODECAHEDRON): //正十二面体
83 glPushMatrix();
84 glScalef(RADIUS/2,RADIUS/2,RADIUS/2);
85 if(bWire)
86 glutWireDodecahedron();
87 else
88 glutSolidDodecahedron();
89 glPopMatrix();
90 break;
91
92 case(ICONSAHEDRON): //正八面体
93 glPushMatrix();
94 glScalef(RADIUS,RADIUS,RADIUS);
95 if(bWire)
96 glutWireIcosahedron();
97 else
98 glutSolidIcosahedron();
99 glPopMatrix();
100 break;
101
102 case(OCTAHEDRON): //球体
103 glPushMatrix();
104 glScalef(RADIUS,RADIUS,RADIUS);
105 if(bWire)
106 glutWireOctahedron();
107 else
108 glutSolidOctahedron();
109 glPopMatrix();
110 break;
111
112 case(SPHERE): //圆环体
113 if(bWire)
114 glutWireSphere(RADIUS,30,30);
115 else
116 glutSolidSphere(RADIUS,30,30);
117 break;
118
119 case(TOURUS): // 茶壶
120 if(bWire)
121 glutWireTorus(RADIUS/2,RADIUS,30,30);
122 else
123 glutSolidTorus(RADIUS/2,RADIUS,30,30);
124 break;
125
126 case(TEAPOT):
127 if(bWire)
128 glutWireTeapot(RADIUS);
129 else
130 glutSolidTeapot(RADIUS);
131 }
132
133 glPopMatrix();
134 glutSwapBuffers();
135 }
136
137 void OnReshape(int w,int h)
138 {
139 glViewport(0,0,w,h);
140 glMatrixMode(GL_PROJECTION);
141
142 float aspect=(float)h/(float)w;
143
144 if(w<=h)
145 glOrtho(-SIZE,SIZE,-SIZE*aspect,SIZE*aspect,-SIZE,SIZE);
146 else
147 glOrtho(-SIZE/aspect,SIZE/aspect,-SIZE,SIZE,-SIZE,SIZE);
148
149 gluLookAt(10.0f,20.0f,25.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
150 }
151
152 void OnMenu(int value)
153 {
154 if(value==WIRED)
155 bWire=!bWire;
156 else
157 nSelected=value;
158
159 glutPostRedisplay();
160 }
161
162 void CreateMenu()
163 {
164 glutAddMenuEntry("圆锥体",CONE);
165 glutAddMenuEntry("四面体",TETRAHEDRON);
166 glutAddMenuEntry("正方体",CUBE);
167 glutAddMenuEntry("正十二面体",DODECAHEDRON);
168 glutAddMenuEntry("正二十面体",ICONSAHEDRON);
169 glutAddMenuEntry("正八面体",OCTAHEDRON);
170 glutAddMenuEntry("球体",SPHERE);
171 glutAddMenuEntry("圆环体",TOURUS);
172 glutAddMenuEntry("茶壶",TEAPOT);
173 glutAddMenuEntry("线框或实体",WIRED);
174
175 glutAttachMenu(GLUT_RIGHT_BUTTON); //指定菜单事件由鼠标右键单击产生
176
177 }
178
179 void SetupLights()
180 {
181 GLfloat ambientLight[]={0.2f,0.2f,0.2f,1.0f};//环境光
182 GLfloat diffuseLight[]={0.9f,0.9f,0.9f,1.0f};//漫反射
183 GLfloat specularLight[]={1.0f,1.0f,1.0f,1.0f};//镜面光
184 GLfloat lightPos[]={50.0f,80.0f,60.0f,1.0f};//光源位置
185
186 glEnable(GL_LIGHTING); //启用光照
187 glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);//设置环境光源
188 glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);//设置漫反射光源
189 glLightfv(GL_LIGHT0,GL_SPECULAR,specularLight);//设置镜面光源
190 glLightfv(GL_LIGHT0,GL_POSITION,lightPos);//设置灯光位置
191 glEnable(GL_LIGHT0); //打开第一个灯光
192
193 glEnable(GL_COLOR_MATERIAL); //启用材质的颜色跟踪
194 glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); //指定材料着色的面
195 glMaterialfv(GL_FRONT,GL_SPECULAR,specularLight); //指定材料对镜面光的反应
196 glMateriali(GL_FRONT,GL_SHININESS,100); //指定反射系数
197
198
199 }

转载于:https://www.cnblogs.com/xiacl/archive/2012/03/25/2416802.html

你可能感兴趣的:(GLUT提供的9种实体对象)