view3d

view src/main.c @ 3:7e982a61852a

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