view3d

view src/main.c @ 0:182bfd9f55c7

view3d
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 19 Jan 2012 00:17:31 +0200
parents
children 7e982a61852a
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 char *scene_fname;
30 int win_width, win_height;
31 int stereo;
32 int flip_winding;
33 int auto_rot = 1;
34 float cam_theta, cam_phi, cam_dist = 10;
35 float near_clip = 0.5;
36 float far_clip = 1000.0;
37 float fov = M_PI / 4.0;
38 float stereo_focus_dist = 1.0;
39 float stereo_eye_sep = 1.0 / 30.0;
41 struct scene scn;
43 int main(int argc, char **argv)
44 {
45 float ldir[] = {1, -1, -1, 0};
46 float dx, dy, dz, diag;
48 glutInitWindowSize(800, 600);
49 glutInit(&argc, argv);
51 if(parse_args(argc, argv) == -1) {
52 return 1;
53 }
55 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (stereo ? GLUT_STEREO : 0));
56 glutCreateWindow("OpenGL Logo");
58 glutDisplayFunc(disp);
59 glutReshapeFunc(reshape);
60 glutKeyboardFunc(keyb);
61 glutKeyboardUpFunc(keyb_up);
62 glutMouseFunc(mouse);
63 glutMotionFunc(motion);
64 glutSpaceballMotionFunc(sball_motion);
65 glutSpaceballRotateFunc(sball_rotate);
66 glutSpaceballButtonFunc(sball_button);
67 glutIdleFunc(glutPostRedisplay);
69 glewInit();
71 glEnable(GL_DEPTH_TEST);
72 glEnable(GL_CULL_FACE);
73 glEnable(GL_LIGHTING);
74 glEnable(GL_LIGHT0);
75 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
77 if((load_scene(&scn, scene_fname)) == -1) {
78 fprintf(stderr, "failed to load: %s\n", scene_fname);
79 return 1;
80 }
81 dx = scn.bbox.max[0] - scn.bbox.min[0];
82 dy = scn.bbox.max[1] - scn.bbox.min[1];
83 dz = scn.bbox.max[2] - scn.bbox.min[2];
84 diag = sqrt(dx * dx + dy * dy + dz * dz);
85 cam_dist = diag / fov;
86 printf("camera distance: %f\n", cam_dist);
88 glutMainLoop();
89 return 0;
90 }
93 void disp(void)
94 {
95 unsigned int tm = glutGet(GLUT_ELAPSED_TIME);
97 if(stereo) {
98 glDrawBuffer(GL_BACK_LEFT);
99 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
101 glMatrixMode(GL_PROJECTION);
102 glLoadIdentity();
103 proj_matrix(-1);
104 glMatrixMode(GL_MODELVIEW);
105 glLoadIdentity();
106 view_matrix(-1);
108 render(tm);
110 glDrawBuffer(GL_BACK_RIGHT);
111 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
113 glMatrixMode(GL_PROJECTION);
114 glLoadIdentity();
115 proj_matrix(1);
116 glMatrixMode(GL_MODELVIEW);
117 glLoadIdentity();
118 view_matrix(1);
120 render(tm);
121 } else {
122 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124 glMatrixMode(GL_PROJECTION);
125 glLoadIdentity();
126 proj_matrix(0);
127 glMatrixMode(GL_MODELVIEW);
128 glLoadIdentity();
129 view_matrix(0);
131 render(tm);
132 }
134 glutSwapBuffers();
135 }
137 void render(unsigned int msec)
138 {
139 if(auto_rot) {
140 glRotatef(msec / 10, 0, 1, 0);
141 }
142 render_scene(&scn);
143 }
145 void proj_matrix(float eye)
146 {
147 float vfov_rad = fov;
148 float top = near_clip * tan(vfov_rad * 0.5);
149 float right = top * (float)win_width / (float)win_height;
151 float frust_shift = eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
153 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
154 }
156 void view_matrix(float eye)
157 {
158 float offs = stereo_eye_sep * eye * 0.5;
159 glTranslatef(offs, 0, 0);
161 glTranslatef(0, 0, -cam_dist);
162 glRotatef(cam_phi, 1, 0, 0);
163 glRotatef(cam_theta, 0, 1, 0);
164 }
166 void reshape(int x, int y)
167 {
168 glViewport(0, 0, x, y);
169 win_width = x;
170 win_height = y;
171 }
173 static int stereo_shift_key;
175 void keyb(unsigned char key, int x, int y)
176 {
177 switch(key) {
178 case 'q':
179 case 27:
180 exit(0);
182 case 's':
183 stereo_shift_key = 1;
184 break;
186 case 'c':
187 {
188 static int flip;
189 glFrontFace((++flip & 1) ? GL_CW : GL_CCW);
190 glutPostRedisplay();
191 }
192 break;
194 case 'C':
195 {
196 static int cull = 1;
197 if(++cull & 1) {
198 glEnable(GL_CULL_FACE);
199 } else {
200 glDisable(GL_CULL_FACE);
201 }
202 glutPostRedisplay();
203 }
204 break;
206 case 'w':
207 {
208 static int wire;
209 glPolygonMode(GL_FRONT_AND_BACK, (++wire & 1) ? GL_LINE : GL_FILL);
210 glutPostRedisplay();
211 }
212 break;
214 case 'l':
215 {
216 static int lit = 1;
217 if(++lit & 1) {
218 glEnable(GL_LIGHTING);
219 } else {
220 glDisable(GL_LIGHTING);
221 }
222 glutPostRedisplay();
223 }
224 break;
226 case ' ':
227 auto_rot = !auto_rot;
228 if(auto_rot) {
229 glutIdleFunc(glutPostRedisplay);
230 } else {
231 glutIdleFunc(0);
232 }
233 glutPostRedisplay();
234 break;
236 default:
237 break;
238 }
239 }
241 void keyb_up(unsigned char key, int x, int y)
242 {
243 switch(key) {
244 case 's':
245 stereo_shift_key = 0;
246 break;
248 default:
249 break;
250 }
251 }
254 static int bnstate[32];
255 static int prev_x, prev_y;
256 void mouse(int bn, int state, int x, int y)
257 {
258 prev_x = x;
259 prev_y = y;
260 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN ? 1 : 0;
261 }
263 void motion(int x, int y)
264 {
265 int dx = x - prev_x;
266 int dy = y - prev_y;
267 prev_x = x;
268 prev_y = y;
270 if(stereo_shift_key && dy != 0) {
271 stereo_focus_dist += dy * 0.1;
272 stereo_eye_sep = stereo_focus_dist / 30.0;
273 glutPostRedisplay();
274 return;
275 }
277 if(bnstate[0]) {
278 cam_theta += dx * 0.5;
279 cam_phi += dy * 0.5;
281 if(cam_phi < -90)
282 cam_phi = -90;
283 if(cam_phi > 90)
284 cam_phi = 90;
286 glutPostRedisplay();
287 }
288 if(bnstate[2]) {
289 cam_dist += dy * 0.1;
290 glutPostRedisplay();
291 }
292 }
294 void sball_motion(int x, int y, int z)
295 {
296 }
298 void sball_rotate(int x, int y, int z)
299 {
300 cam_theta += y * 0.05;
301 cam_phi += x * 0.05;
303 if(cam_phi < -90)
304 cam_phi = -90;
305 if(cam_phi > 90)
306 cam_phi = 90;
308 glutPostRedisplay();
309 }
311 void sball_button(int bn, int state)
312 {
313 }
315 int parse_args(int argc, char **argv)
316 {
317 int i;
319 for(i=1; i<argc; i++) {
320 if(argv[i][0] == '-') {
321 if(argv[i][2] != 0)
322 goto inval;
323 switch(argv[i][1]) {
324 case 's':
325 stereo = 1;
326 break;
328 case 'c':
329 flip_winding = 1;
330 break;
332 default:
333 goto inval;
334 }
335 } else {
336 if(scene_fname) {
337 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
338 return -1;
339 }
340 scene_fname = argv[i];
341 }
342 }
344 if(!scene_fname) {
345 fprintf(stderr, "you must pass the filename of a scene to open\n");
346 return -1;
347 }
349 return 0;
351 inval:
352 fprintf(stderr, "invalid argument: %s\n", argv[i]);
353 return -1;
354 }