rev |
line source |
nuclear@0
|
1 #include <stdio.h>
|
nuclear@0
|
2 #include <stdlib.h>
|
nuclear@0
|
3 #include <math.h>
|
nuclear@0
|
4
|
nuclear@0
|
5 #include <GL/glew.h>
|
nuclear@0
|
6 #ifndef __APPLE__
|
nuclear@0
|
7 #include <GL/glut.h>
|
nuclear@0
|
8 #else
|
nuclear@0
|
9 #include <GLUT/glut.h>
|
nuclear@0
|
10 #endif
|
nuclear@0
|
11
|
nuclear@0
|
12 #include "scene.h"
|
nuclear@0
|
13
|
nuclear@0
|
14 void disp(void);
|
nuclear@0
|
15 void render(unsigned int msec);
|
nuclear@0
|
16 void proj_matrix(float eye);
|
nuclear@0
|
17 void view_matrix(float eye);
|
nuclear@0
|
18
|
nuclear@0
|
19 void reshape(int x, int y);
|
nuclear@0
|
20 void keyb(unsigned char key, int x, int y);
|
nuclear@0
|
21 void keyb_up(unsigned char key, int x, int y);
|
nuclear@0
|
22 void mouse(int bn, int state, int x, int y);
|
nuclear@0
|
23 void motion(int x, int y);
|
nuclear@0
|
24 void sball_motion(int x, int y, int z);
|
nuclear@0
|
25 void sball_rotate(int x, int y, int z);
|
nuclear@0
|
26 void sball_button(int bn, int state);
|
nuclear@0
|
27 int parse_args(int argc, char **argv);
|
nuclear@0
|
28
|
nuclear@0
|
29 char *scene_fname;
|
nuclear@0
|
30 int win_width, win_height;
|
nuclear@0
|
31 int stereo;
|
nuclear@0
|
32 int flip_winding;
|
nuclear@3
|
33 int auto_rot;
|
nuclear@0
|
34 float cam_theta, cam_phi, cam_dist = 10;
|
nuclear@0
|
35 float near_clip = 0.5;
|
nuclear@0
|
36 float far_clip = 1000.0;
|
nuclear@0
|
37 float fov = M_PI / 4.0;
|
nuclear@0
|
38 float stereo_focus_dist = 1.0;
|
nuclear@0
|
39 float stereo_eye_sep = 1.0 / 30.0;
|
nuclear@0
|
40
|
nuclear@3
|
41 int verbose = 0;
|
nuclear@3
|
42
|
nuclear@0
|
43 struct scene scn;
|
nuclear@0
|
44
|
nuclear@0
|
45 int main(int argc, char **argv)
|
nuclear@0
|
46 {
|
nuclear@3
|
47 float ldir[] = {-1, 1, 1, 0};
|
nuclear@0
|
48 float dx, dy, dz, diag;
|
nuclear@0
|
49
|
nuclear@0
|
50 glutInitWindowSize(800, 600);
|
nuclear@0
|
51 glutInit(&argc, argv);
|
nuclear@0
|
52
|
nuclear@0
|
53 if(parse_args(argc, argv) == -1) {
|
nuclear@0
|
54 return 1;
|
nuclear@0
|
55 }
|
nuclear@0
|
56
|
nuclear@0
|
57 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
|
nuclear@0
|
58 glutCreateWindow("OpenGL Logo");
|
nuclear@0
|
59
|
nuclear@0
|
60 glutDisplayFunc(disp);
|
nuclear@0
|
61 glutReshapeFunc(reshape);
|
nuclear@0
|
62 glutKeyboardFunc(keyb);
|
nuclear@0
|
63 glutKeyboardUpFunc(keyb_up);
|
nuclear@0
|
64 glutMouseFunc(mouse);
|
nuclear@0
|
65 glutMotionFunc(motion);
|
nuclear@0
|
66 glutSpaceballMotionFunc(sball_motion);
|
nuclear@0
|
67 glutSpaceballRotateFunc(sball_rotate);
|
nuclear@0
|
68 glutSpaceballButtonFunc(sball_button);
|
nuclear@3
|
69 if(auto_rot) {
|
nuclear@3
|
70 glutIdleFunc(glutPostRedisplay);
|
nuclear@3
|
71 }
|
nuclear@0
|
72
|
nuclear@0
|
73 glewInit();
|
nuclear@0
|
74
|
nuclear@0
|
75 glEnable(GL_DEPTH_TEST);
|
nuclear@0
|
76 glEnable(GL_CULL_FACE);
|
nuclear@0
|
77 glEnable(GL_LIGHTING);
|
nuclear@0
|
78 glEnable(GL_LIGHT0);
|
nuclear@0
|
79 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
|
nuclear@0
|
80
|
nuclear@0
|
81 if((load_scene(&scn, scene_fname)) == -1) {
|
nuclear@0
|
82 fprintf(stderr, "failed to load: %s\n", scene_fname);
|
nuclear@0
|
83 return 1;
|
nuclear@0
|
84 }
|
nuclear@0
|
85 dx = scn.bbox.max[0] - scn.bbox.min[0];
|
nuclear@0
|
86 dy = scn.bbox.max[1] - scn.bbox.min[1];
|
nuclear@0
|
87 dz = scn.bbox.max[2] - scn.bbox.min[2];
|
nuclear@0
|
88 diag = sqrt(dx * dx + dy * dy + dz * dz);
|
nuclear@0
|
89 cam_dist = diag / fov;
|
nuclear@0
|
90 printf("camera distance: %f\n", cam_dist);
|
nuclear@0
|
91
|
nuclear@0
|
92 glutMainLoop();
|
nuclear@0
|
93 return 0;
|
nuclear@0
|
94 }
|
nuclear@0
|
95
|
nuclear@0
|
96
|
nuclear@0
|
97 void disp(void)
|
nuclear@0
|
98 {
|
nuclear@0
|
99 unsigned int tm = glutGet(GLUT_ELAPSED_TIME);
|
nuclear@0
|
100
|
nuclear@0
|
101 if(stereo) {
|
nuclear@0
|
102 glDrawBuffer(GL_BACK_LEFT);
|
nuclear@0
|
103 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
104
|
nuclear@0
|
105 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
106 glLoadIdentity();
|
nuclear@0
|
107 proj_matrix(-1);
|
nuclear@0
|
108 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
109 glLoadIdentity();
|
nuclear@0
|
110 view_matrix(-1);
|
nuclear@0
|
111
|
nuclear@0
|
112 render(tm);
|
nuclear@0
|
113
|
nuclear@0
|
114 glDrawBuffer(GL_BACK_RIGHT);
|
nuclear@0
|
115 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
116
|
nuclear@0
|
117 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
118 glLoadIdentity();
|
nuclear@0
|
119 proj_matrix(1);
|
nuclear@0
|
120 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
121 glLoadIdentity();
|
nuclear@0
|
122 view_matrix(1);
|
nuclear@0
|
123
|
nuclear@0
|
124 render(tm);
|
nuclear@0
|
125 } else {
|
nuclear@0
|
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
nuclear@0
|
127
|
nuclear@0
|
128 glMatrixMode(GL_PROJECTION);
|
nuclear@0
|
129 glLoadIdentity();
|
nuclear@0
|
130 proj_matrix(0);
|
nuclear@0
|
131 glMatrixMode(GL_MODELVIEW);
|
nuclear@0
|
132 glLoadIdentity();
|
nuclear@0
|
133 view_matrix(0);
|
nuclear@0
|
134
|
nuclear@0
|
135 render(tm);
|
nuclear@0
|
136 }
|
nuclear@0
|
137
|
nuclear@0
|
138 glutSwapBuffers();
|
nuclear@0
|
139 }
|
nuclear@0
|
140
|
nuclear@0
|
141 void render(unsigned int msec)
|
nuclear@0
|
142 {
|
nuclear@0
|
143 if(auto_rot) {
|
nuclear@0
|
144 glRotatef(msec / 10, 0, 1, 0);
|
nuclear@0
|
145 }
|
nuclear@0
|
146 render_scene(&scn);
|
nuclear@0
|
147 }
|
nuclear@0
|
148
|
nuclear@0
|
149 void proj_matrix(float eye)
|
nuclear@0
|
150 {
|
nuclear@0
|
151 float vfov_rad = fov;
|
nuclear@0
|
152 float top = near_clip * tan(vfov_rad * 0.5);
|
nuclear@0
|
153 float right = top * (float)win_width / (float)win_height;
|
nuclear@0
|
154
|
nuclear@0
|
155 float frust_shift = eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
|
nuclear@0
|
156
|
nuclear@0
|
157 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
|
nuclear@0
|
158 }
|
nuclear@0
|
159
|
nuclear@0
|
160 void view_matrix(float eye)
|
nuclear@0
|
161 {
|
nuclear@0
|
162 float offs = stereo_eye_sep * eye * 0.5;
|
nuclear@0
|
163 glTranslatef(offs, 0, 0);
|
nuclear@0
|
164
|
nuclear@0
|
165 glTranslatef(0, 0, -cam_dist);
|
nuclear@0
|
166 glRotatef(cam_phi, 1, 0, 0);
|
nuclear@0
|
167 glRotatef(cam_theta, 0, 1, 0);
|
nuclear@0
|
168 }
|
nuclear@0
|
169
|
nuclear@0
|
170 void reshape(int x, int y)
|
nuclear@0
|
171 {
|
nuclear@0
|
172 glViewport(0, 0, x, y);
|
nuclear@0
|
173 win_width = x;
|
nuclear@0
|
174 win_height = y;
|
nuclear@0
|
175 }
|
nuclear@0
|
176
|
nuclear@0
|
177 static int stereo_shift_key;
|
nuclear@0
|
178
|
nuclear@0
|
179 void keyb(unsigned char key, int x, int y)
|
nuclear@0
|
180 {
|
nuclear@0
|
181 switch(key) {
|
nuclear@0
|
182 case 'q':
|
nuclear@0
|
183 case 27:
|
nuclear@0
|
184 exit(0);
|
nuclear@0
|
185
|
nuclear@0
|
186 case 's':
|
nuclear@0
|
187 stereo_shift_key = 1;
|
nuclear@0
|
188 break;
|
nuclear@0
|
189
|
nuclear@0
|
190 case 'c':
|
nuclear@0
|
191 {
|
nuclear@0
|
192 static int flip;
|
nuclear@0
|
193 glFrontFace((++flip & 1) ? GL_CW : GL_CCW);
|
nuclear@0
|
194 glutPostRedisplay();
|
nuclear@0
|
195 }
|
nuclear@0
|
196 break;
|
nuclear@0
|
197
|
nuclear@0
|
198 case 'C':
|
nuclear@0
|
199 {
|
nuclear@0
|
200 static int cull = 1;
|
nuclear@0
|
201 if(++cull & 1) {
|
nuclear@0
|
202 glEnable(GL_CULL_FACE);
|
nuclear@0
|
203 } else {
|
nuclear@0
|
204 glDisable(GL_CULL_FACE);
|
nuclear@0
|
205 }
|
nuclear@0
|
206 glutPostRedisplay();
|
nuclear@0
|
207 }
|
nuclear@0
|
208 break;
|
nuclear@0
|
209
|
nuclear@0
|
210 case 'w':
|
nuclear@0
|
211 {
|
nuclear@0
|
212 static int wire;
|
nuclear@0
|
213 glPolygonMode(GL_FRONT_AND_BACK, (++wire & 1) ? GL_LINE : GL_FILL);
|
nuclear@0
|
214 glutPostRedisplay();
|
nuclear@0
|
215 }
|
nuclear@0
|
216 break;
|
nuclear@0
|
217
|
nuclear@0
|
218 case 'l':
|
nuclear@0
|
219 {
|
nuclear@0
|
220 static int lit = 1;
|
nuclear@0
|
221 if(++lit & 1) {
|
nuclear@0
|
222 glEnable(GL_LIGHTING);
|
nuclear@0
|
223 } else {
|
nuclear@0
|
224 glDisable(GL_LIGHTING);
|
nuclear@0
|
225 }
|
nuclear@0
|
226 glutPostRedisplay();
|
nuclear@0
|
227 }
|
nuclear@0
|
228 break;
|
nuclear@0
|
229
|
nuclear@0
|
230 case ' ':
|
nuclear@0
|
231 auto_rot = !auto_rot;
|
nuclear@0
|
232 if(auto_rot) {
|
nuclear@0
|
233 glutIdleFunc(glutPostRedisplay);
|
nuclear@0
|
234 } else {
|
nuclear@0
|
235 glutIdleFunc(0);
|
nuclear@0
|
236 }
|
nuclear@0
|
237 glutPostRedisplay();
|
nuclear@0
|
238 break;
|
nuclear@0
|
239
|
nuclear@0
|
240 default:
|
nuclear@0
|
241 break;
|
nuclear@0
|
242 }
|
nuclear@0
|
243 }
|
nuclear@0
|
244
|
nuclear@0
|
245 void keyb_up(unsigned char key, int x, int y)
|
nuclear@0
|
246 {
|
nuclear@0
|
247 switch(key) {
|
nuclear@0
|
248 case 's':
|
nuclear@0
|
249 stereo_shift_key = 0;
|
nuclear@0
|
250 break;
|
nuclear@0
|
251
|
nuclear@0
|
252 default:
|
nuclear@0
|
253 break;
|
nuclear@0
|
254 }
|
nuclear@0
|
255 }
|
nuclear@0
|
256
|
nuclear@0
|
257
|
nuclear@0
|
258 static int bnstate[32];
|
nuclear@0
|
259 static int prev_x, prev_y;
|
nuclear@0
|
260 void mouse(int bn, int state, int x, int y)
|
nuclear@0
|
261 {
|
nuclear@0
|
262 prev_x = x;
|
nuclear@0
|
263 prev_y = y;
|
nuclear@0
|
264 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN ? 1 : 0;
|
nuclear@0
|
265 }
|
nuclear@0
|
266
|
nuclear@0
|
267 void motion(int x, int y)
|
nuclear@0
|
268 {
|
nuclear@0
|
269 int dx = x - prev_x;
|
nuclear@0
|
270 int dy = y - prev_y;
|
nuclear@0
|
271 prev_x = x;
|
nuclear@0
|
272 prev_y = y;
|
nuclear@0
|
273
|
nuclear@0
|
274 if(stereo_shift_key && dy != 0) {
|
nuclear@0
|
275 stereo_focus_dist += dy * 0.1;
|
nuclear@0
|
276 stereo_eye_sep = stereo_focus_dist / 30.0;
|
nuclear@0
|
277 glutPostRedisplay();
|
nuclear@0
|
278 return;
|
nuclear@0
|
279 }
|
nuclear@0
|
280
|
nuclear@0
|
281 if(bnstate[0]) {
|
nuclear@0
|
282 cam_theta += dx * 0.5;
|
nuclear@0
|
283 cam_phi += dy * 0.5;
|
nuclear@0
|
284
|
nuclear@0
|
285 if(cam_phi < -90)
|
nuclear@0
|
286 cam_phi = -90;
|
nuclear@0
|
287 if(cam_phi > 90)
|
nuclear@0
|
288 cam_phi = 90;
|
nuclear@0
|
289
|
nuclear@0
|
290 glutPostRedisplay();
|
nuclear@0
|
291 }
|
nuclear@0
|
292 if(bnstate[2]) {
|
nuclear@0
|
293 cam_dist += dy * 0.1;
|
nuclear@0
|
294 glutPostRedisplay();
|
nuclear@0
|
295 }
|
nuclear@0
|
296 }
|
nuclear@0
|
297
|
nuclear@0
|
298 void sball_motion(int x, int y, int z)
|
nuclear@0
|
299 {
|
nuclear@0
|
300 }
|
nuclear@0
|
301
|
nuclear@0
|
302 void sball_rotate(int x, int y, int z)
|
nuclear@0
|
303 {
|
nuclear@0
|
304 cam_theta += y * 0.05;
|
nuclear@0
|
305 cam_phi += x * 0.05;
|
nuclear@0
|
306
|
nuclear@0
|
307 if(cam_phi < -90)
|
nuclear@0
|
308 cam_phi = -90;
|
nuclear@0
|
309 if(cam_phi > 90)
|
nuclear@0
|
310 cam_phi = 90;
|
nuclear@0
|
311
|
nuclear@0
|
312 glutPostRedisplay();
|
nuclear@0
|
313 }
|
nuclear@0
|
314
|
nuclear@0
|
315 void sball_button(int bn, int state)
|
nuclear@0
|
316 {
|
nuclear@0
|
317 }
|
nuclear@0
|
318
|
nuclear@0
|
319 int parse_args(int argc, char **argv)
|
nuclear@0
|
320 {
|
nuclear@0
|
321 int i;
|
nuclear@0
|
322
|
nuclear@0
|
323 for(i=1; i<argc; i++) {
|
nuclear@0
|
324 if(argv[i][0] == '-') {
|
nuclear@0
|
325 if(argv[i][2] != 0)
|
nuclear@0
|
326 goto inval;
|
nuclear@0
|
327 switch(argv[i][1]) {
|
nuclear@0
|
328 case 's':
|
nuclear@0
|
329 stereo = 1;
|
nuclear@0
|
330 break;
|
nuclear@0
|
331
|
nuclear@0
|
332 case 'c':
|
nuclear@0
|
333 flip_winding = 1;
|
nuclear@0
|
334 break;
|
nuclear@0
|
335
|
nuclear@3
|
336 case 'v':
|
nuclear@3
|
337 verbose = !verbose;
|
nuclear@3
|
338 break;
|
nuclear@3
|
339
|
nuclear@0
|
340 default:
|
nuclear@0
|
341 goto inval;
|
nuclear@0
|
342 }
|
nuclear@0
|
343 } else {
|
nuclear@0
|
344 if(scene_fname) {
|
nuclear@0
|
345 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
|
nuclear@0
|
346 return -1;
|
nuclear@0
|
347 }
|
nuclear@0
|
348 scene_fname = argv[i];
|
nuclear@0
|
349 }
|
nuclear@0
|
350 }
|
nuclear@0
|
351
|
nuclear@0
|
352 if(!scene_fname) {
|
nuclear@0
|
353 fprintf(stderr, "you must pass the filename of a scene to open\n");
|
nuclear@0
|
354 return -1;
|
nuclear@0
|
355 }
|
nuclear@0
|
356
|
nuclear@0
|
357 return 0;
|
nuclear@0
|
358
|
nuclear@0
|
359 inval:
|
nuclear@0
|
360 fprintf(stderr, "invalid argument: %s\n", argv[i]);
|
nuclear@0
|
361 return -1;
|
nuclear@0
|
362 }
|