stereoview

annotate src/stereoview.cc @ 0:dc1723a8bf6f

initial import
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 04 Mar 2011 06:51:16 +0200
parents
children 30c7a5df0523
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <assert.h>
nuclear@0 4 #include <henge.h>
nuclear@0 5
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 "cam.h"
nuclear@0 13 #include "zscn.h"
nuclear@0 14
nuclear@0 15 struct Light {
nuclear@0 16 float pos[4];
nuclear@0 17 float color[4];
nuclear@0 18 };
nuclear@0 19
nuclear@0 20 static void cleanup();
nuclear@0 21 static int parse_args(int argc, char **argv);
nuclear@0 22 static void disp();
nuclear@0 23 static void render_scene();
nuclear@0 24 static void reshape(int x, int y);
nuclear@0 25 static void keyb(unsigned char key, int x, int y);
nuclear@0 26 static void mouse(int bn, int state, int x, int y);
nuclear@0 27 static void motion(int x, int y);
nuclear@0 28
nuclear@0 29 static bool use_stereo = false;
nuclear@0 30 static bool use_zbuf = true;
nuclear@0 31
nuclear@0 32 static std::list<std::string> scn_fnames;
nuclear@0 33 static henge::Scene *scn;
nuclear@0 34
nuclear@0 35
nuclear@0 36 int main(int argc, char **argv)
nuclear@0 37 {
nuclear@0 38 unsigned int init_flags = GLUT_RGB | GLUT_DOUBLE;
nuclear@0 39
nuclear@0 40 atexit(cleanup);
nuclear@0 41
nuclear@0 42 glutInitWindowSize(800, 600);
nuclear@0 43 glutInit(&argc, argv);
nuclear@0 44
nuclear@0 45 if(parse_args(argc, argv) == -1) {
nuclear@0 46 return 1;
nuclear@0 47 }
nuclear@0 48 if(use_stereo) {
nuclear@0 49 init_flags |= GLUT_STEREO;
nuclear@0 50 }
nuclear@0 51 if(use_zbuf) {
nuclear@0 52 init_flags |= GLUT_DEPTH;
nuclear@0 53 }
nuclear@0 54
nuclear@0 55 glutInitDisplayMode(init_flags);
nuclear@0 56 glutCreateWindow("stereoscopic scene viewer");
nuclear@0 57
nuclear@0 58 glutDisplayFunc(disp);
nuclear@0 59 glutReshapeFunc(reshape);
nuclear@0 60 glutKeyboardFunc(keyb);
nuclear@0 61 glutMouseFunc(mouse);
nuclear@0 62 glutMotionFunc(motion);
nuclear@0 63
nuclear@0 64 if(use_zbuf) {
nuclear@0 65 glEnable(GL_DEPTH_TEST);
nuclear@0 66 }
nuclear@0 67
nuclear@0 68 glEnable(GL_LIGHTING);
nuclear@0 69 {
nuclear@0 70 int i, num_lights;
nuclear@0 71 struct Light lights[] = {
nuclear@0 72 {{-0.85, 0.7, 1, 0}, {1.0, 1.0, 1.0, 1.0}},
nuclear@0 73 {{1, -0.5, 0.9, 0}, {0.75, 0.75, 0.75, 1.0}},
nuclear@0 74 {{0, 0, 1, 0}, {0.4, 0.4, 0.4, 1.0}}
nuclear@0 75 };
nuclear@0 76
nuclear@0 77 num_lights = sizeof lights / sizeof *lights;
nuclear@0 78
nuclear@0 79 for(i=0; i<num_lights; i++) {
nuclear@0 80 GLenum id = GL_LIGHT0 + i;
nuclear@0 81 glLightfv(id, GL_POSITION, lights[i].pos);
nuclear@0 82 glLightfv(id, GL_DIFFUSE, lights[i].color);
nuclear@0 83 glLightfv(id, GL_SPECULAR, lights[i].color);
nuclear@0 84 glEnable(id);
nuclear@0 85 }
nuclear@0 86 }
nuclear@0 87
nuclear@0 88 glEnable(GL_CULL_FACE);
nuclear@0 89
nuclear@0 90 if(!henge::init()) {
nuclear@0 91 return 1;
nuclear@0 92 }
nuclear@0 93
nuclear@0 94 scn = new ZScene;
nuclear@0 95
nuclear@0 96 std::list<std::string>::iterator it = scn_fnames.begin();
nuclear@0 97 while(it != scn_fnames.end()) {
nuclear@0 98 if(!(scn->load(it->c_str()))) {
nuclear@0 99 fprintf(stderr, "failed to load scene: %s\n", it->c_str());
nuclear@0 100 return 1;
nuclear@0 101 }
nuclear@0 102 it++;
nuclear@0 103 }
nuclear@0 104 if(!scn->object_count()) {
nuclear@0 105 fprintf(stderr, "didn't load any geometry, aborting\n");
nuclear@0 106 return 1;
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 henge::Renderer *rend = henge::get_renderer();
nuclear@0 110 rend->set_render_mask(henge::REND_ALL & ~henge::REND_CAM);
nuclear@0 111
nuclear@0 112 cam_reset();
nuclear@0 113
nuclear@0 114 glutMainLoop();
nuclear@0 115 return 0;
nuclear@0 116 }
nuclear@0 117
nuclear@0 118 static void cleanup()
nuclear@0 119 {
nuclear@0 120 delete scn;
nuclear@0 121 }
nuclear@0 122
nuclear@0 123 static int parse_args(int argc, char **argv)
nuclear@0 124 {
nuclear@0 125 for(int i=1; i<argc; i++) {
nuclear@0 126 if(argv[i][0] == '-') {
nuclear@0 127 if(argv[i][2]) {
nuclear@0 128 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
nuclear@0 129 return -1;
nuclear@0 130 }
nuclear@0 131 switch(argv[i][1]) {
nuclear@0 132 case 's':
nuclear@0 133 use_stereo = true;
nuclear@0 134 break;
nuclear@0 135
nuclear@0 136 case 'd':
nuclear@0 137 case 'z':
nuclear@0 138 use_zbuf = false;
nuclear@0 139 break;
nuclear@0 140
nuclear@0 141 case 'h':
nuclear@0 142 printf("usage: %s [options]\n", argv[0]);
nuclear@0 143 printf(" -s\tuse stereoscopic rendering\n");
nuclear@0 144 printf(" -h\tprint usage and exit\n");
nuclear@0 145 exit(0);
nuclear@0 146
nuclear@0 147 default:
nuclear@0 148 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
nuclear@0 149 return -1;
nuclear@0 150 }
nuclear@0 151 } else {
nuclear@0 152 scn_fnames.push_back(argv[i]);
nuclear@0 153 }
nuclear@0 154 }
nuclear@0 155
nuclear@0 156 return 0;
nuclear@0 157 }
nuclear@0 158
nuclear@0 159 static void disp()
nuclear@0 160 {
nuclear@0 161 unsigned int clear_flags = GL_COLOR_BUFFER_BIT | (use_zbuf ? GL_DEPTH_BUFFER_BIT : 0);
nuclear@0 162
nuclear@0 163 glMatrixMode(GL_PROJECTION);
nuclear@0 164 glLoadIdentity();
nuclear@0 165 cam_stereo_proj_matrix(use_stereo ? CAM_LEFT : CAM_CENTER);
nuclear@0 166 glMatrixMode(GL_MODELVIEW);
nuclear@0 167 glLoadIdentity();
nuclear@0 168 cam_stereo_view_matrix(use_stereo ? CAM_LEFT : CAM_CENTER);
nuclear@0 169
nuclear@0 170 if(!use_stereo) {
nuclear@0 171 glClear(clear_flags);
nuclear@0 172 render_scene();
nuclear@0 173 } else {
nuclear@0 174 glDrawBuffer(GL_BACK_LEFT);
nuclear@0 175 glClear(clear_flags);
nuclear@0 176 render_scene();
nuclear@0 177
nuclear@0 178 glMatrixMode(GL_PROJECTION);
nuclear@0 179 glLoadIdentity();
nuclear@0 180 cam_stereo_proj_matrix(CAM_RIGHT);
nuclear@0 181 glMatrixMode(GL_MODELVIEW);
nuclear@0 182 glLoadIdentity();
nuclear@0 183 cam_stereo_view_matrix(CAM_RIGHT);
nuclear@0 184
nuclear@0 185 glDrawBuffer(GL_BACK_RIGHT);
nuclear@0 186 glClear(clear_flags);
nuclear@0 187 render_scene();
nuclear@0 188 }
nuclear@0 189
nuclear@0 190 glutSwapBuffers();
nuclear@0 191 assert(glGetError() == GL_NO_ERROR);
nuclear@0 192 }
nuclear@0 193
nuclear@0 194 static void render_scene()
nuclear@0 195 {
nuclear@0 196 scn->render();
nuclear@0 197 }
nuclear@0 198
nuclear@0 199 static void reshape(int x, int y)
nuclear@0 200 {
nuclear@0 201 glViewport(0, 0, x, y);
nuclear@0 202
nuclear@0 203 cam_aspect((float)x / (float)y);
nuclear@0 204 }
nuclear@0 205
nuclear@0 206 static void keyb(unsigned char key, int x, int y)
nuclear@0 207 {
nuclear@0 208 static float focus_dist = 1.0;
nuclear@0 209
nuclear@0 210 switch(key) {
nuclear@0 211 case 27:
nuclear@0 212 exit(0);
nuclear@0 213
nuclear@0 214 case '-':
nuclear@0 215 focus_dist -= 0.5;
nuclear@0 216 cam_focus_dist(focus_dist);
nuclear@0 217 glutPostRedisplay();
nuclear@0 218 printf("focus_dist: %f\n", focus_dist);
nuclear@0 219 break;
nuclear@0 220
nuclear@0 221 case '=':
nuclear@0 222 focus_dist += 0.5;
nuclear@0 223 cam_focus_dist(focus_dist);
nuclear@0 224 glutPostRedisplay();
nuclear@0 225 printf("focus_dist: %f\n", focus_dist);
nuclear@0 226 break;
nuclear@0 227
nuclear@0 228 default:
nuclear@0 229 break;
nuclear@0 230 }
nuclear@0 231 }
nuclear@0 232
nuclear@0 233 static int px = -1, py;
nuclear@0 234 static int bnstate[32];
nuclear@0 235
nuclear@0 236 static void mouse(int bn, int state, int x, int y)
nuclear@0 237 {
nuclear@0 238 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
nuclear@0 239
nuclear@0 240 if(state == GLUT_DOWN) {
nuclear@0 241 px = x;
nuclear@0 242 py = y;
nuclear@0 243 } else {
nuclear@0 244 px = -1;
nuclear@0 245 }
nuclear@0 246 }
nuclear@0 247
nuclear@0 248 static void motion(int x, int y)
nuclear@0 249 {
nuclear@0 250 int dx = x - px;
nuclear@0 251 int dy = y - py;
nuclear@0 252
nuclear@0 253 px = x;
nuclear@0 254 py = y;
nuclear@0 255
nuclear@0 256 if(bnstate[0]) {
nuclear@0 257 cam_rotate(dx, dy);
nuclear@0 258 glutPostRedisplay();
nuclear@0 259 }
nuclear@0 260 if(bnstate[1]) {
nuclear@0 261 cam_pan(dx, dy);
nuclear@0 262 glutPostRedisplay();
nuclear@0 263 }
nuclear@0 264 if(bnstate[2]) {
nuclear@0 265 cam_zoom(dy);
nuclear@0 266 glutPostRedisplay();
nuclear@0 267 }
nuclear@0 268 }