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