不算重新开博……临时发的……
用了Alpha混合,透视投影,旋转变换,+简单的键盘控制
1 #include <stdio.h>
2 #include <math.h>
3 #include <GL/glut.h>
4
5 GLint keytable[128] = {0};
6
7 void resize(int w, int h);
8 void onkey(unsigned char key, int x, int y);
9 void onkeyup(unsigned char key, int x, int y);
10 void onspeckey(int key, int x, int y);
11 void onspeckeyup(int key, int x, int y);
12 void draw();
13 void ontimer(int timer);
14
15 void fire_key_event(int key);
16
17 GLdouble rotate = 0.0;
18 GLdouble rotate2 = 10.0;
19
20 /**
21 * the enterance of the application
22 */
23 int main(int argc, char** argv)
24 {
25 /* initialize glut */
26 glutInit(&argc, argv);
27
28 /* initialize the window */
29 glutInitWindowPosition(200, 100);
30 glutInitWindowSize(800, 600);
31 glutCreateWindow("sence editor");
32
33 /* initialize display mode
34 we use DOUBLE BUFFERS, DEPTH, RGBA colors */
35 glutInitDisplayMode(GLUT_DOUBLE | GL_DEPTH | GL_RGBA);
36
37 /* enable blend */
38 glEnable(GL_BLEND);
39 glDisable(GL_DEPTH_TEST);
40
41 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
42
43 /* handle events */
44 glutDisplayFunc(draw);
45 glutReshapeFunc(resize);
46 glutKeyboardFunc(onkey);
47 glutKeyboardUpFunc(onkeyup);
48 glutSpecialFunc(onspeckey);
49 glutSpecialUpFunc(onspeckeyup);
50
51 /* main loop */
52 glutTimerFunc(10, ontimer, 1);
53
54 /* glut main loop */
55 glutMainLoop();
56
57 return 0;
58 }
59
60 /**
61 * the application's main loop
62 */
63 void ontimer(int timer)
64 {
65 key_rotate();
66
67 glutPostRedisplay();
68 glutTimerFunc(10, ontimer, 1);
69 }
70
71 /**
72 * paint the sence
73 */
74 void draw()
75 {
76 /* clear the sence */
77 glClearColor(0.0, 0.0, 0.0, 1.0);
78 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
79
80 glLoadIdentity();
81 glRotated(rotate, 0.0, 0.0, 1.0);
82
83 glPushMatrix();
84
85 GLint i;
86 for (i = 0; i < 10; i++) {
87 glRotated(rotate2, 0.0, 0.0, 1.0);
88
89 /* paint a triangle */
90 glBegin(GL_TRIANGLE_STRIP);
91
92 glColor4d(1.0, 0.0, 0.0, 0.2);
93 glVertex3d(0.0, 1.0, -1.0 * i);
94
95 glColor4d(0.0, 1.0, 0.0, 0.2);
96 glVertex3d(-1.0, 0.0, -1.0 * i);
97
98 glColor4d(0.0, 0.0, 1.0, 0.2);
99 glVertex3d(1.0, 0.0, -1.0 * i);
100
101 glEnd();
102 }
103
104 glPopMatrix();
105
106 /* flush and swap the buffers */
107 glFlush();
108 glutSwapBuffers();
109 }
110
111 /**
112 * handle the window resized event
113 * we reset the viewport settings and projection matrix
114 */
115 void resize(int w, int h)
116 {
117 glViewport(0, 0, w, h);
118
119 glMatrixMode(GL_PROJECTION);
120 glLoadIdentity();
121 /* glFrustum(- w/(GLdouble)h, w/(GLdouble)h, 1.0, -1.0, 1.0, 100.0); */
122 glFrustum(1.0, -1.0,- h/(GLdouble)w, h/(GLdouble)w, 1.0, 100.0);
123
124 glMatrixMode(GL_MODELVIEW);
125 }
126
127 /**
128 * map key state to the keytable
129 */
130 void onkey(unsigned char key, int x, int y)
131 {
132 keytable[key] = 1;
133 fire_key_event(key);
134 }
135
136 void onkeyup(unsigned char key, int x, int y)
137 {
138 keytable[key] = 0;
139 fire_key_event(key);
140 }
141
142 void onspeckey(int key, int x, int y)
143 {
144 keytable[key] = 1;
145 fire_key_event(key);
146 }
147
148 void onspeckeyup(int key, int x, int y)
149 {
150 keytable[key] = 0;
151 fire_key_event(key);
152 }
153
154 /**
155 * handle the key event
156 */
157 void fire_key_event(int key)
158 {
159 if (keytable[27] || keytable['q'] || keytable['Q']) {
160 exit(0);
161 }
162 }
163
164 /**
165 * keyboard controlled rotate
166 */
167 void key_rotate()
168 {
169 if (keytable[GLUT_KEY_LEFT]) {
170 rotate += 1.0;
171 }
172
173 if (keytable[GLUT_KEY_RIGHT]) {
174 rotate -= 1.0;
175 }
176
177 if (keytable[GLUT_KEY_UP]) {
178 rotate2 += 1.0;
179 }
180
181 if (keytable[GLUT_KEY_DOWN]) {
182 rotate2 -= 1.0;
183 }
184 }
|