初学OpenGL的程序(画球)

1. 利用Glut库,编写一个OpenGL程序,实现以下功能:

  • 仿照课本2.4.3的例子,绘制若干OpenGL基本体素(三角形、三角形带、四边形、四边形带)构成的球体,
  • 可以控制改变球的数量和球的体积,来改变基本体素的数量以及顶点的数量。
  • 可以通过改变glPolygonMode,切换是否填充所绘制的基本体素。
  • 用函数glutGet(GLUT_ELAPSED_TIME)(returns the time in milliseconds since
1
glutInit or the first invocation of the function)统计绘制时间

源程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include<GL/glut.h>
#include<math.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream.h>
#define pi 3.1415926
#define GLfloat float
#define GLdouble double
#define GLint int
double c=pi/180.0;
double radius=150.0;
int w=500,h=500;
double angle=10.0;
double a=10.0;
void drawSphere(void)
{
glViewport (0.0, 0.0, (GLsizei) w, (GLsizei) h);
float theta,phir,phi,thetar,phir20
;
float x,y,z;
for(phi=-90.0;phi<=90.0;phi+=a)
{
phir=cphi;
phir20=c(phi+a);
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glFrontFace(GL_CCW); //逆时针为正面
//glBegin(GL_TRIANGLES);
//glBegin(GL_LINE_LOOP);
glBegin(GL_TRIANGLE_STRIP);
//glBegin(GL_TRIANGLE_FAN);
//glBegin(GL_QUADS);
//glBegin(GL_QUAD_STRIP);
for(theta=-180.0;theta<=180.0;theta+=a)
{
thetar=ctheta;
x=radiussin(thetar)cos(phir);
y=radiuscos(thetar)cos(phir);
z=radiussin(phir);
glVertex3d(x,y,z);
//glVertex3f(x,y,z);
x=radiussin(thetar)cos(phir20);
y=radiuscos(thetar)cos(phir20);
z=radius*sin(phir20);
glVertex3d(x,y,z);
//glVertex3f(x,y,z);
}
glEnd();
}
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glFrontFace(GL_CCW);
//glBegin(GL_QUADS);
//glBegin(GL_QUAD_STRIP);
glBegin(GL_TRIANGLE_STRIP);
//glBegin(GL_TRIANGLES);
//glBegin(GL_TRIANGLE_FAN);
//glBegin(GL_LINE_LOOP);
//glBegin(GL_LINES);
//glVertex3f(0.0,0.0,radius);
glVertex3d(0.0,0.0,radius);
double c80=c90.0;
z=radiussin(c80);
for(theta=-180.0;theta<=180.0;theta+=a)
{
thetar=ctheta;
x=radiussin(thetar)cos(c80);
y=radiuscos(thetar)cos(c80);
glVertex3d(x,y,z);
//glVertex3f(x,y,z);
}
glEnd();
glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK, GL_LINE);
glFrontFace(GL_CCW);
//glBegin(GL_QUADS);
//glBegin(GL_QUAD_STRIP);
glBegin(GL_TRIANGLE_STRIP);
//glBegin(GL_TRIANGLES);
//glBegin(GL_TRIANGLE_FAN);
//glBegin(GL_LINE_LOOP);
//glBegin(GL_LINES);
glVertex3d(0.0,0.0,-radius);
//Vertex3f(0.0,0.0,-radius);
z=-radiussin(c80);
for(theta=-180.0;theta<=180.0;theta+=a)
{
thetar=ctheta;
x=radiussin(thetar)cos(c80);
y=radiuscos(thetar)*cos(c80);
glVertex3d(x,y,z);
//lVertex3f(x,y,z);
}
glEnd();
glFlush();
glutSwapBuffers();
}
void reshape(GLsizei ww, GLsizei hh)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble)w, 0.0,(GLdouble)h, -w, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
w=ww;
h=hh;
}
void init(void)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, (GLdouble)w, 0.0,(GLdouble)h, -500, 500);
glClearColor(0.0,0.0,0.0,0.0);
glColor3f(0.0,1.0,0.0);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
//glFlush();
}
void mouse(int btn,int state, int x,int y)
{
//if(btn==GLUT_RIGHT_BUTTON &&state==GLUT_DOWN) exit(0);
if(btn==GLUT_LEFT_BUTTON &&state==GLUT_DOWN)
{
radius=radius+15.0;
drawSphere();
printf("time is :%d\n",glutGet(GLUT_ELAPSED_TIME));
}
}
static void idle(void)
{
glutPostRedisplay();
}
void display(void)
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glTranslated(250,250,0);
glRotated(30,1,0,0);
glRotated(60,0,1,0);
glRotated(90,0,0,1);
angle = 12500.0/glutGet(GLUT_ELAPSED_TIME);
glRotated(angle,1,0,0);
glutPostRedisplay();
drawSphere();
printf("time is :%d\n",glutGet(GLUT_ELAPSED_TIME));
}
void submenu(int id)
{
switch(id)
{
case 1:
exit(0);
break;
case 2:
radius=radius+10.0;
break;
case 3:
if(radius>1.0) radius=radius/2;
break;
}
}
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(w,h);
glutInitWindowPosition(0,0);
glutCreateWindow("Swkkkk");
glutCreateMenu(submenu);
glutAddMenuEntry("quit",1);
glutAddMenuEntry("Increase radius",2);
glutAddMenuEntry("Decrease radius",3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init();
glutMouseFunc(mouse);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
//glutIdleFunc(idle);
glutMainLoop();
}

运行结果如下图:

望多多指教!!!!!

WHY·LIAM wechat
记录生活,研究产品,分享设计,分析数据,讨论科技和商业,不定期扯淡。
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!