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