view3d

view src/main.c @ 8:5562a637e5aa

load multiple files and concatenate them
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 23 Jan 2012 08:51:59 +0200
parents 58ddd42848f9
children d2e283764fca
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <math.h>
5 #include <GL/glew.h>
6 #ifndef __APPLE__
7 #include <GL/glut.h>
8 #else
9 #include <GLUT/glut.h>
10 #endif
12 #include "scene.h"
14 void disp(void);
15 void render(unsigned int msec);
16 void proj_matrix(float eye);
17 void view_matrix(float eye);
19 void reshape(int x, int y);
20 void keyb(unsigned char key, int x, int y);
21 void keyb_up(unsigned char key, int x, int y);
22 void mouse(int bn, int state, int x, int y);
23 void motion(int x, int y);
24 void sball_motion(int x, int y, int z);
25 void sball_rotate(int x, int y, int z);
26 void sball_button(int bn, int state);
27 int parse_args(int argc, char **argv);
29 int win_width, win_height;
30 int stereo;
31 int flip_winding;
32 int auto_rot;
33 float cam_theta, cam_phi, cam_dist = 10;
34 float near_clip = 0.5;
35 float far_clip = 1000.0;
36 float fov = M_PI / 4.0;
37 float stereo_focus_dist = 1.0;
38 float stereo_eye_sep = 1.0 / 30.0;
40 int verbose = 0;
42 struct scene scn;
44 int main(int argc, char **argv)
45 {
46 float amb[] = {0.01, 0.01, 0.01, 1};
47 float ldir[] = {-1, 1, 1, 0};
48 float dx, dy, dz, diag;
51 glutInitWindowSize(800, 600);
52 glutInit(&argc, argv);
53 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
54 glutCreateWindow("OpenGL 3D viewer");
56 glutDisplayFunc(disp);
57 glutReshapeFunc(reshape);
58 glutKeyboardFunc(keyb);
59 glutKeyboardUpFunc(keyb_up);
60 glutMouseFunc(mouse);
61 glutMotionFunc(motion);
62 glutSpaceballMotionFunc(sball_motion);
63 glutSpaceballRotateFunc(sball_rotate);
64 glutSpaceballButtonFunc(sball_button);
65 if(auto_rot) {
66 glutIdleFunc(glutPostRedisplay);
67 }
69 glewInit();
71 glEnable(GL_NORMALIZE);
72 glEnable(GL_DEPTH_TEST);
73 glEnable(GL_CULL_FACE);
74 glEnable(GL_LIGHTING);
75 glEnable(GL_LIGHT0);
76 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
78 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
81 init_scene(&scn);
82 if(parse_args(argc, argv) == -1) {
83 return 1;
84 }
85 if(verbose) {
86 printf("scene bounds: %.2f %.2f %.2f -> %.2f %.2f %.2f\n", scn.bbox.min[0], scn.bbox.min[1],
87 scn.bbox.min[2], scn.bbox.max[0], scn.bbox.max[1], scn.bbox.max[2]);
88 }
90 dx = scn.bbox.max[0] - scn.bbox.min[0];
91 dy = scn.bbox.max[1] - scn.bbox.min[1];
92 dz = scn.bbox.max[2] - scn.bbox.min[2];
93 diag = sqrt(dx * dx + dy * dy + dz * dz);
94 cam_dist = diag / fov;
95 printf("camera distance: %f\n", cam_dist);
97 glutMainLoop();
98 return 0;
99 }
102 void disp(void)
103 {
104 unsigned int tm = glutGet(GLUT_ELAPSED_TIME);
106 if(stereo) {
107 glDrawBuffer(GL_BACK_LEFT);
108 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110 glMatrixMode(GL_PROJECTION);
111 glLoadIdentity();
112 proj_matrix(-1);
113 glMatrixMode(GL_MODELVIEW);
114 glLoadIdentity();
115 view_matrix(-1);
117 render(tm);
119 glDrawBuffer(GL_BACK_RIGHT);
120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 glMatrixMode(GL_PROJECTION);
123 glLoadIdentity();
124 proj_matrix(1);
125 glMatrixMode(GL_MODELVIEW);
126 glLoadIdentity();
127 view_matrix(1);
129 render(tm);
130 } else {
131 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
133 glMatrixMode(GL_PROJECTION);
134 glLoadIdentity();
135 proj_matrix(0);
136 glMatrixMode(GL_MODELVIEW);
137 glLoadIdentity();
138 view_matrix(0);
140 render(tm);
141 }
143 glutSwapBuffers();
144 }
146 void render(unsigned int msec)
147 {
148 if(auto_rot) {
149 glRotatef(msec / 10, 0, 1, 0);
150 }
151 render_scene(&scn);
152 }
154 void proj_matrix(float eye)
155 {
156 float vfov_rad = fov;
157 float top = near_clip * tan(vfov_rad * 0.5);
158 float right = top * (float)win_width / (float)win_height;
160 float frust_shift = eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
162 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
163 }
165 void view_matrix(float eye)
166 {
167 float offs = stereo_eye_sep * eye * 0.5;
168 glTranslatef(offs, 0, 0);
170 glTranslatef(0, 0, -cam_dist);
171 glRotatef(cam_phi, 1, 0, 0);
172 glRotatef(cam_theta, 0, 1, 0);
173 }
175 void reshape(int x, int y)
176 {
177 glViewport(0, 0, x, y);
178 win_width = x;
179 win_height = y;
180 }
182 static int stereo_shift_key;
184 void keyb(unsigned char key, int x, int y)
185 {
186 switch(key) {
187 case 'q':
188 case 27:
189 exit(0);
191 case 's':
192 stereo_shift_key = 1;
193 break;
195 case 'c':
196 {
197 static int flip;
198 glFrontFace((++flip & 1) ? GL_CW : GL_CCW);
199 glutPostRedisplay();
200 }
201 break;
203 case 'C':
204 {
205 static int cull = 1;
206 if(++cull & 1) {
207 glEnable(GL_CULL_FACE);
208 } else {
209 glDisable(GL_CULL_FACE);
210 }
211 glutPostRedisplay();
212 }
213 break;
215 case 'w':
216 {
217 static int wire;
218 glPolygonMode(GL_FRONT_AND_BACK, (++wire & 1) ? GL_LINE : GL_FILL);
219 glutPostRedisplay();
220 }
221 break;
223 case 'l':
224 {
225 static int lit = 1;
226 if(++lit & 1) {
227 glEnable(GL_LIGHTING);
228 } else {
229 glDisable(GL_LIGHTING);
230 }
231 glutPostRedisplay();
232 }
233 break;
235 case ' ':
236 auto_rot = !auto_rot;
237 if(auto_rot) {
238 glutIdleFunc(glutPostRedisplay);
239 } else {
240 glutIdleFunc(0);
241 }
242 glutPostRedisplay();
243 break;
245 case 'f':
246 {
247 static int fullscr;
248 if(++fullscr & 1) {
249 glutFullScreen();
250 } else {
251 glutPositionWindow(20, 20);
252 }
253 }
254 break;
256 {
257 static float bgval;
258 case '=':
259 bgval += 0.1;
260 if(bgval > 1.0)
261 bgval = 1.0;
262 glClearColor(bgval, bgval, bgval, 1.0);
263 glutPostRedisplay();
264 break;
266 case '-':
267 bgval -= 0.1;
268 if(bgval < 0.0)
269 bgval = 0.0;
270 glClearColor(bgval, bgval, bgval, 1.0);
271 glutPostRedisplay();
272 break;
273 }
275 default:
276 break;
277 }
278 }
280 void keyb_up(unsigned char key, int x, int y)
281 {
282 switch(key) {
283 case 's':
284 stereo_shift_key = 0;
285 break;
287 default:
288 break;
289 }
290 }
293 static int bnstate[32];
294 static int prev_x, prev_y;
295 void mouse(int bn, int state, int x, int y)
296 {
297 prev_x = x;
298 prev_y = y;
299 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN ? 1 : 0;
300 }
302 void motion(int x, int y)
303 {
304 int dx = x - prev_x;
305 int dy = y - prev_y;
306 prev_x = x;
307 prev_y = y;
309 if(stereo_shift_key && dy != 0) {
310 stereo_focus_dist += dy * 0.1;
311 stereo_eye_sep = stereo_focus_dist / 30.0;
312 glutPostRedisplay();
313 return;
314 }
316 if(bnstate[0]) {
317 cam_theta += dx * 0.5;
318 cam_phi += dy * 0.5;
320 if(cam_phi < -90)
321 cam_phi = -90;
322 if(cam_phi > 90)
323 cam_phi = 90;
325 glutPostRedisplay();
326 }
327 if(bnstate[2]) {
328 cam_dist += dy * 0.1;
329 glutPostRedisplay();
330 }
331 }
333 void sball_motion(int x, int y, int z)
334 {
335 }
337 void sball_rotate(int x, int y, int z)
338 {
339 cam_theta += y * 0.05;
340 cam_phi += x * 0.05;
342 if(cam_phi < -90)
343 cam_phi = -90;
344 if(cam_phi > 90)
345 cam_phi = 90;
347 glutPostRedisplay();
348 }
350 void sball_button(int bn, int state)
351 {
352 }
354 int parse_args(int argc, char **argv)
355 {
356 int i, num_loaded = 0;
358 for(i=1; i<argc; i++) {
359 if(argv[i][0] == '-') {
360 if(argv[i][2] != 0)
361 goto inval;
362 switch(argv[i][1]) {
363 case 's':
364 stereo = 1;
365 break;
367 case 'c':
368 flip_winding = 1;
369 break;
371 case 'v':
372 verbose = !verbose;
373 break;
375 default:
376 goto inval;
377 }
378 } else {
379 if((load_scene(&scn, argv[i])) == -1) {
380 fprintf(stderr, "failed to load: %s\n", argv[1]);
381 return -1;
382 }
383 num_loaded++;
384 }
385 }
387 if(num_loaded == 0) {
388 fprintf(stderr, "you must pass the filename of a scene to open\n");
389 return -1;
390 }
392 return 0;
394 inval:
395 fprintf(stderr, "invalid argument: %s\n", argv[i]);
396 return -1;
397 }