view3d

annotate src/main.c @ 10:d0896caa3e5b

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