view3d

annotate src/main.c @ 5:58ddd42848f9

foo
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 19 Jan 2012 07:37:31 +0200
parents 0aee5df08cfc
children 5562a637e5aa
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@5 244 case 'f':
nuclear@5 245 {
nuclear@5 246 static int fullscr;
nuclear@5 247 if(++fullscr & 1) {
nuclear@5 248 glutFullScreen();
nuclear@5 249 } else {
nuclear@5 250 glutPositionWindow(20, 20);
nuclear@5 251 }
nuclear@5 252 }
nuclear@5 253 break;
nuclear@5 254
nuclear@5 255 {
nuclear@5 256 static float bgval;
nuclear@5 257 case '=':
nuclear@5 258 bgval += 0.1;
nuclear@5 259 if(bgval > 1.0)
nuclear@5 260 bgval = 1.0;
nuclear@5 261 glClearColor(bgval, bgval, bgval, 1.0);
nuclear@5 262 glutPostRedisplay();
nuclear@5 263 break;
nuclear@5 264
nuclear@5 265 case '-':
nuclear@5 266 bgval -= 0.1;
nuclear@5 267 if(bgval < 0.0)
nuclear@5 268 bgval = 0.0;
nuclear@5 269 glClearColor(bgval, bgval, bgval, 1.0);
nuclear@5 270 glutPostRedisplay();
nuclear@5 271 break;
nuclear@5 272 }
nuclear@5 273
nuclear@0 274 default:
nuclear@0 275 break;
nuclear@0 276 }
nuclear@0 277 }
nuclear@0 278
nuclear@0 279 void keyb_up(unsigned char key, int x, int y)
nuclear@0 280 {
nuclear@0 281 switch(key) {
nuclear@0 282 case 's':
nuclear@0 283 stereo_shift_key = 0;
nuclear@0 284 break;
nuclear@0 285
nuclear@0 286 default:
nuclear@0 287 break;
nuclear@0 288 }
nuclear@0 289 }
nuclear@0 290
nuclear@0 291
nuclear@0 292 static int bnstate[32];
nuclear@0 293 static int prev_x, prev_y;
nuclear@0 294 void mouse(int bn, int state, int x, int y)
nuclear@0 295 {
nuclear@0 296 prev_x = x;
nuclear@0 297 prev_y = y;
nuclear@0 298 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN ? 1 : 0;
nuclear@0 299 }
nuclear@0 300
nuclear@0 301 void motion(int x, int y)
nuclear@0 302 {
nuclear@0 303 int dx = x - prev_x;
nuclear@0 304 int dy = y - prev_y;
nuclear@0 305 prev_x = x;
nuclear@0 306 prev_y = y;
nuclear@0 307
nuclear@0 308 if(stereo_shift_key && dy != 0) {
nuclear@0 309 stereo_focus_dist += dy * 0.1;
nuclear@0 310 stereo_eye_sep = stereo_focus_dist / 30.0;
nuclear@0 311 glutPostRedisplay();
nuclear@0 312 return;
nuclear@0 313 }
nuclear@0 314
nuclear@0 315 if(bnstate[0]) {
nuclear@0 316 cam_theta += dx * 0.5;
nuclear@0 317 cam_phi += dy * 0.5;
nuclear@0 318
nuclear@0 319 if(cam_phi < -90)
nuclear@0 320 cam_phi = -90;
nuclear@0 321 if(cam_phi > 90)
nuclear@0 322 cam_phi = 90;
nuclear@0 323
nuclear@0 324 glutPostRedisplay();
nuclear@0 325 }
nuclear@0 326 if(bnstate[2]) {
nuclear@0 327 cam_dist += dy * 0.1;
nuclear@0 328 glutPostRedisplay();
nuclear@0 329 }
nuclear@0 330 }
nuclear@0 331
nuclear@0 332 void sball_motion(int x, int y, int z)
nuclear@0 333 {
nuclear@0 334 }
nuclear@0 335
nuclear@0 336 void sball_rotate(int x, int y, int z)
nuclear@0 337 {
nuclear@0 338 cam_theta += y * 0.05;
nuclear@0 339 cam_phi += x * 0.05;
nuclear@0 340
nuclear@0 341 if(cam_phi < -90)
nuclear@0 342 cam_phi = -90;
nuclear@0 343 if(cam_phi > 90)
nuclear@0 344 cam_phi = 90;
nuclear@0 345
nuclear@0 346 glutPostRedisplay();
nuclear@0 347 }
nuclear@0 348
nuclear@0 349 void sball_button(int bn, int state)
nuclear@0 350 {
nuclear@0 351 }
nuclear@0 352
nuclear@0 353 int parse_args(int argc, char **argv)
nuclear@0 354 {
nuclear@0 355 int i;
nuclear@0 356
nuclear@0 357 for(i=1; i<argc; i++) {
nuclear@0 358 if(argv[i][0] == '-') {
nuclear@0 359 if(argv[i][2] != 0)
nuclear@0 360 goto inval;
nuclear@0 361 switch(argv[i][1]) {
nuclear@0 362 case 's':
nuclear@0 363 stereo = 1;
nuclear@0 364 break;
nuclear@0 365
nuclear@0 366 case 'c':
nuclear@0 367 flip_winding = 1;
nuclear@0 368 break;
nuclear@0 369
nuclear@3 370 case 'v':
nuclear@3 371 verbose = !verbose;
nuclear@3 372 break;
nuclear@3 373
nuclear@0 374 default:
nuclear@0 375 goto inval;
nuclear@0 376 }
nuclear@0 377 } else {
nuclear@0 378 if(scene_fname) {
nuclear@0 379 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
nuclear@0 380 return -1;
nuclear@0 381 }
nuclear@0 382 scene_fname = argv[i];
nuclear@0 383 }
nuclear@0 384 }
nuclear@0 385
nuclear@0 386 if(!scene_fname) {
nuclear@0 387 fprintf(stderr, "you must pass the filename of a scene to open\n");
nuclear@0 388 return -1;
nuclear@0 389 }
nuclear@0 390
nuclear@0 391 return 0;
nuclear@0 392
nuclear@0 393 inval:
nuclear@0 394 fprintf(stderr, "invalid argument: %s\n", argv[i]);
nuclear@0 395 return -1;
nuclear@0 396 }