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