view3d

annotate src/main.c @ 1:7650e941805c

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