stereoview

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/stereoview.cc	Fri Mar 04 06:51:16 2011 +0200
     1.3 @@ -0,0 +1,268 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <assert.h>
     1.7 +#include <henge.h>
     1.8 +
     1.9 +#ifndef __APPLE__
    1.10 +#include <GL/glut.h>
    1.11 +#else
    1.12 +#include <GLUT/glut.h>
    1.13 +#endif
    1.14 +
    1.15 +#include "cam.h"
    1.16 +#include "zscn.h"
    1.17 +
    1.18 +struct Light {
    1.19 +	float pos[4];
    1.20 +	float color[4];
    1.21 +};
    1.22 +
    1.23 +static void cleanup();
    1.24 +static int parse_args(int argc, char **argv);
    1.25 +static void disp();
    1.26 +static void render_scene();
    1.27 +static void reshape(int x, int y);
    1.28 +static void keyb(unsigned char key, int x, int y);
    1.29 +static void mouse(int bn, int state, int x, int y);
    1.30 +static void motion(int x, int y);
    1.31 +
    1.32 +static bool use_stereo = false;
    1.33 +static bool use_zbuf = true;
    1.34 +
    1.35 +static std::list<std::string> scn_fnames;
    1.36 +static henge::Scene *scn;
    1.37 +
    1.38 +
    1.39 +int main(int argc, char **argv)
    1.40 +{
    1.41 +	unsigned int init_flags = GLUT_RGB | GLUT_DOUBLE;
    1.42 +
    1.43 +	atexit(cleanup);
    1.44 +
    1.45 +	glutInitWindowSize(800, 600);
    1.46 +	glutInit(&argc, argv);
    1.47 +
    1.48 +	if(parse_args(argc, argv) == -1) {
    1.49 +		return 1;
    1.50 +	}
    1.51 +	if(use_stereo) {
    1.52 +		init_flags |= GLUT_STEREO;
    1.53 +	}
    1.54 +	if(use_zbuf) {
    1.55 +		init_flags |= GLUT_DEPTH;
    1.56 +	}
    1.57 +
    1.58 +	glutInitDisplayMode(init_flags);
    1.59 +	glutCreateWindow("stereoscopic scene viewer");
    1.60 +
    1.61 +	glutDisplayFunc(disp);
    1.62 +	glutReshapeFunc(reshape);
    1.63 +	glutKeyboardFunc(keyb);
    1.64 +	glutMouseFunc(mouse);
    1.65 +	glutMotionFunc(motion);
    1.66 +
    1.67 +	if(use_zbuf) {
    1.68 +		glEnable(GL_DEPTH_TEST);
    1.69 +	}
    1.70 +
    1.71 +	glEnable(GL_LIGHTING);
    1.72 +	{
    1.73 +		int i, num_lights;
    1.74 +		struct Light lights[] = {
    1.75 +			{{-0.85, 0.7, 1, 0}, {1.0, 1.0, 1.0, 1.0}},
    1.76 +			{{1, -0.5, 0.9, 0}, {0.75, 0.75, 0.75, 1.0}},
    1.77 +			{{0, 0, 1, 0}, {0.4, 0.4, 0.4, 1.0}}
    1.78 +		};
    1.79 +
    1.80 +		num_lights = sizeof lights / sizeof *lights;
    1.81 +
    1.82 +		for(i=0; i<num_lights; i++) {
    1.83 +			GLenum id = GL_LIGHT0 + i;
    1.84 +			glLightfv(id, GL_POSITION, lights[i].pos);
    1.85 +			glLightfv(id, GL_DIFFUSE, lights[i].color);
    1.86 +			glLightfv(id, GL_SPECULAR, lights[i].color);
    1.87 +			glEnable(id);
    1.88 +		}
    1.89 +	}
    1.90 +
    1.91 +	glEnable(GL_CULL_FACE);
    1.92 +
    1.93 +	if(!henge::init()) {
    1.94 +		return 1;
    1.95 +	}
    1.96 +
    1.97 +	scn = new ZScene;
    1.98 +
    1.99 +	std::list<std::string>::iterator it = scn_fnames.begin();
   1.100 +	while(it != scn_fnames.end()) {
   1.101 +		if(!(scn->load(it->c_str()))) {
   1.102 +			fprintf(stderr, "failed to load scene: %s\n", it->c_str());
   1.103 +			return 1;
   1.104 +		}
   1.105 +		it++;
   1.106 +	}
   1.107 +	if(!scn->object_count()) {
   1.108 +		fprintf(stderr, "didn't load any geometry, aborting\n");
   1.109 +		return 1;
   1.110 +	}
   1.111 +
   1.112 +	henge::Renderer *rend = henge::get_renderer();
   1.113 +	rend->set_render_mask(henge::REND_ALL & ~henge::REND_CAM);
   1.114 +
   1.115 +	cam_reset();
   1.116 +
   1.117 +	glutMainLoop();
   1.118 +	return 0;
   1.119 +}
   1.120 +
   1.121 +static void cleanup()
   1.122 +{
   1.123 +	delete scn;
   1.124 +}
   1.125 +
   1.126 +static int parse_args(int argc, char **argv)
   1.127 +{
   1.128 +	for(int i=1; i<argc; i++) {
   1.129 +		if(argv[i][0] == '-') {
   1.130 +			if(argv[i][2]) {
   1.131 +				fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
   1.132 +				return -1;
   1.133 +			}
   1.134 +			switch(argv[i][1]) {
   1.135 +			case 's':
   1.136 +				use_stereo = true;
   1.137 +				break;
   1.138 +
   1.139 +			case 'd':
   1.140 +			case 'z':
   1.141 +				use_zbuf = false;
   1.142 +				break;
   1.143 +
   1.144 +			case 'h':
   1.145 +				printf("usage: %s [options]\n", argv[0]);
   1.146 +				printf(" -s\tuse stereoscopic rendering\n");
   1.147 +				printf(" -h\tprint usage and exit\n");
   1.148 +				exit(0);
   1.149 +
   1.150 +			default:
   1.151 +				fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
   1.152 +				return -1;
   1.153 +			}
   1.154 +		} else {
   1.155 +			scn_fnames.push_back(argv[i]);
   1.156 +		}
   1.157 +	}
   1.158 +
   1.159 +	return 0;
   1.160 +}
   1.161 +
   1.162 +static void disp()
   1.163 +{
   1.164 +	unsigned int clear_flags = GL_COLOR_BUFFER_BIT | (use_zbuf ? GL_DEPTH_BUFFER_BIT : 0);
   1.165 +
   1.166 +	glMatrixMode(GL_PROJECTION);
   1.167 +	glLoadIdentity();
   1.168 +	cam_stereo_proj_matrix(use_stereo ? CAM_LEFT : CAM_CENTER);
   1.169 +	glMatrixMode(GL_MODELVIEW);
   1.170 +	glLoadIdentity();
   1.171 +	cam_stereo_view_matrix(use_stereo ? CAM_LEFT : CAM_CENTER);
   1.172 +
   1.173 +	if(!use_stereo) {
   1.174 +		glClear(clear_flags);
   1.175 +		render_scene();
   1.176 +	} else {
   1.177 +		glDrawBuffer(GL_BACK_LEFT);
   1.178 +		glClear(clear_flags);
   1.179 +		render_scene();
   1.180 +
   1.181 +		glMatrixMode(GL_PROJECTION);
   1.182 +		glLoadIdentity();
   1.183 +		cam_stereo_proj_matrix(CAM_RIGHT);
   1.184 +		glMatrixMode(GL_MODELVIEW);
   1.185 +		glLoadIdentity();
   1.186 +		cam_stereo_view_matrix(CAM_RIGHT);
   1.187 +
   1.188 +		glDrawBuffer(GL_BACK_RIGHT);
   1.189 +		glClear(clear_flags);
   1.190 +		render_scene();
   1.191 +	}
   1.192 +
   1.193 +	glutSwapBuffers();
   1.194 +	assert(glGetError() == GL_NO_ERROR);
   1.195 +}
   1.196 +
   1.197 +static void render_scene()
   1.198 +{
   1.199 +	scn->render();
   1.200 +}
   1.201 +
   1.202 +static void reshape(int x, int y)
   1.203 +{
   1.204 +	glViewport(0, 0, x, y);
   1.205 +
   1.206 +	cam_aspect((float)x / (float)y);
   1.207 +}
   1.208 +
   1.209 +static void keyb(unsigned char key, int x, int y)
   1.210 +{
   1.211 +	static float focus_dist = 1.0;
   1.212 +
   1.213 +	switch(key) {
   1.214 +	case 27:
   1.215 +		exit(0);
   1.216 +
   1.217 +	case '-':
   1.218 +		focus_dist -= 0.5;
   1.219 +		cam_focus_dist(focus_dist);
   1.220 +		glutPostRedisplay();
   1.221 +		printf("focus_dist: %f\n", focus_dist);
   1.222 +		break;
   1.223 +
   1.224 +	case '=':
   1.225 +		focus_dist += 0.5;
   1.226 +		cam_focus_dist(focus_dist);
   1.227 +		glutPostRedisplay();
   1.228 +		printf("focus_dist: %f\n", focus_dist);
   1.229 +		break;
   1.230 +
   1.231 +	default:
   1.232 +		break;
   1.233 +	}
   1.234 +}
   1.235 +
   1.236 +static int px = -1, py;
   1.237 +static int bnstate[32];
   1.238 +
   1.239 +static void mouse(int bn, int state, int x, int y)
   1.240 +{
   1.241 +	bnstate[bn] = state == GLUT_DOWN ? 1 : 0;
   1.242 +
   1.243 +	if(state == GLUT_DOWN) {
   1.244 +		px = x;
   1.245 +		py = y;
   1.246 +	} else {
   1.247 +		px = -1;
   1.248 +	}
   1.249 +}
   1.250 +
   1.251 +static void motion(int x, int y)
   1.252 +{
   1.253 +	int dx = x - px;
   1.254 +	int dy = y - py;
   1.255 +
   1.256 +	px = x;
   1.257 +	py = y;
   1.258 +
   1.259 +	if(bnstate[0]) {
   1.260 +		cam_rotate(dx, dy);
   1.261 +		glutPostRedisplay();
   1.262 +	}
   1.263 +	if(bnstate[1]) {
   1.264 +		cam_pan(dx, dy);
   1.265 +		glutPostRedisplay();
   1.266 +	}
   1.267 +	if(bnstate[2]) {
   1.268 +		cam_zoom(dy);
   1.269 +		glutPostRedisplay();
   1.270 +	}
   1.271 +}