stereoview
view src/stereoview.cc @ 3:993fdfd41a04
merged macosx makefile fixes
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 04 Mar 2011 15:51:37 +0200 |
parents | dc1723a8bf6f |
children |
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;
31 static float focus_dist = 1.0;
33 static std::list<std::string> scn_fnames;
34 static henge::Scene *scn;
37 int main(int argc, char **argv)
38 {
39 unsigned int init_flags = GLUT_RGB | GLUT_DOUBLE;
41 atexit(cleanup);
43 glutInitWindowSize(800, 600);
44 glutInit(&argc, argv);
46 if(parse_args(argc, argv) == -1) {
47 return 1;
48 }
49 if(use_stereo) {
50 init_flags |= GLUT_STEREO;
51 }
52 if(use_zbuf) {
53 init_flags |= GLUT_DEPTH;
54 }
56 glutInitDisplayMode(init_flags);
57 glutCreateWindow("stereoscopic scene viewer");
59 glutDisplayFunc(disp);
60 glutReshapeFunc(reshape);
61 glutKeyboardFunc(keyb);
62 glutMouseFunc(mouse);
63 glutMotionFunc(motion);
65 if(use_zbuf) {
66 glEnable(GL_DEPTH_TEST);
67 }
69 glEnable(GL_LIGHTING);
70 {
71 int i, num_lights;
72 struct Light lights[] = {
73 {{-0.85, 0.7, 1, 0}, {1.0, 1.0, 1.0, 1.0}},
74 {{1, -0.5, 0.9, 0}, {0.75, 0.75, 0.75, 1.0}},
75 {{0, 0, 1, 0}, {0.4, 0.4, 0.4, 1.0}}
76 };
78 num_lights = sizeof lights / sizeof *lights;
80 for(i=0; i<num_lights; i++) {
81 GLenum id = GL_LIGHT0 + i;
82 glLightfv(id, GL_POSITION, lights[i].pos);
83 glLightfv(id, GL_DIFFUSE, lights[i].color);
84 glLightfv(id, GL_SPECULAR, lights[i].color);
85 glEnable(id);
86 }
87 }
89 glEnable(GL_CULL_FACE);
91 if(!henge::init()) {
92 return 1;
93 }
95 if(use_zbuf) {
96 scn = new henge::Scene;
97 } else {
98 scn = new ZScene;
99 }
101 std::list<std::string>::iterator it = scn_fnames.begin();
102 while(it != scn_fnames.end()) {
103 if(!(scn->load(it->c_str()))) {
104 fprintf(stderr, "failed to load scene: %s\n", it->c_str());
105 return 1;
106 }
107 it++;
108 }
109 if(!scn->object_count()) {
110 fprintf(stderr, "didn't load any geometry, aborting\n");
111 return 1;
112 }
114 henge::Renderer *rend = henge::get_renderer();
115 rend->set_render_mask(henge::REND_ALL & ~henge::REND_CAM);
117 cam_reset();
118 cam_focus_dist(focus_dist);
120 glutMainLoop();
121 return 0;
122 }
124 static void cleanup()
125 {
126 delete scn;
127 }
129 static int parse_args(int argc, char **argv)
130 {
131 char *endptr;
133 for(int i=1; i<argc; i++) {
134 if(argv[i][0] == '-') {
135 if(argv[i][2]) {
136 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
137 return -1;
138 }
139 switch(argv[i][1]) {
140 case 's':
141 use_stereo = true;
142 break;
144 case 'd':
145 case 'z':
146 use_zbuf = false;
147 break;
149 case 'f':
150 focus_dist = strtof(argv[++i], &endptr);
151 if(endptr == argv[i]) {
152 fprintf(stderr, "-f must be followed by the focus distance\n");
153 return -1;
154 }
155 break;
157 case 'h':
158 printf("usage: %s [options]\n", argv[0]);
159 printf(" -s\tuse stereoscopic rendering\n");
160 printf(" -z\tdon't use z-buffer\n");
161 printf(" -f <num>\tstereo focus distance\n");
162 printf(" -h\tprint usage and exit\n");
163 exit(0);
165 default:
166 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
167 return -1;
168 }
169 } else {
170 scn_fnames.push_back(argv[i]);
171 }
172 }
174 return 0;
175 }
177 static void disp()
178 {
179 unsigned int clear_flags = GL_COLOR_BUFFER_BIT | (use_zbuf ? GL_DEPTH_BUFFER_BIT : 0);
181 glMatrixMode(GL_PROJECTION);
182 glLoadIdentity();
183 cam_stereo_proj_matrix(use_stereo ? CAM_LEFT : CAM_CENTER);
184 glMatrixMode(GL_MODELVIEW);
185 glLoadIdentity();
186 cam_stereo_view_matrix(use_stereo ? CAM_LEFT : CAM_CENTER);
188 if(!use_stereo) {
189 glClear(clear_flags);
190 render_scene();
191 } else {
192 glDrawBuffer(GL_BACK_LEFT);
193 glClear(clear_flags);
194 render_scene();
196 glMatrixMode(GL_PROJECTION);
197 glLoadIdentity();
198 cam_stereo_proj_matrix(CAM_RIGHT);
199 glMatrixMode(GL_MODELVIEW);
200 glLoadIdentity();
201 cam_stereo_view_matrix(CAM_RIGHT);
203 glDrawBuffer(GL_BACK_RIGHT);
204 glClear(clear_flags);
205 render_scene();
206 }
208 glutSwapBuffers();
209 assert(glGetError() == GL_NO_ERROR);
210 }
212 static void render_scene()
213 {
214 scn->render();
215 }
217 static void reshape(int x, int y)
218 {
219 glViewport(0, 0, x, y);
221 cam_aspect((float)x / (float)y);
222 }
224 static void keyb(unsigned char key, int x, int y)
225 {
226 switch(key) {
227 case 27:
228 exit(0);
230 case '-':
231 focus_dist -= 0.5;
232 cam_focus_dist(focus_dist);
233 glutPostRedisplay();
234 printf("focus_dist: %f\n", focus_dist);
235 break;
237 case '=':
238 focus_dist += 0.5;
239 cam_focus_dist(focus_dist);
240 glutPostRedisplay();
241 printf("focus_dist: %f\n", focus_dist);
242 break;
244 default:
245 break;
246 }
247 }
249 static int px = -1, py;
250 static int bnstate[32];
252 static void mouse(int bn, int state, int x, int y)
253 {
254 bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
256 if(state == GLUT_DOWN) {
257 px = x;
258 py = y;
259 } else {
260 px = -1;
261 }
262 }
264 static void motion(int x, int y)
265 {
266 int dx = x - px;
267 int dy = y - py;
269 px = x;
270 py = y;
272 if(bnstate[0]) {
273 cam_rotate(dx, dy);
274 glutPostRedisplay();
275 }
276 if(bnstate[1]) {
277 cam_pan(dx, dy);
278 glutPostRedisplay();
279 }
280 if(bnstate[2]) {
281 cam_zoom(dy);
282 glutPostRedisplay();
283 }
284 }