invisible

diff src/main.cc @ 26:61d593076c56

foo
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 17 Nov 2013 23:02:40 +0200
parents 96b022f1210e
children
line diff
     1.1 --- a/src/main.cc	Sun Nov 17 12:46:45 2013 +0200
     1.2 +++ b/src/main.cc	Sun Nov 17 23:02:40 2013 +0200
     1.3 @@ -18,7 +18,7 @@
     1.4  KinectParams kin_params;
     1.5  Frame *frame;
     1.6  
     1.7 -static const char *filename = "data/textures/wallpaper.jpg";
     1.8 +static const char *filename = "data/textures/wp2.jpg";
     1.9  static const char *vsdr_path = "data/shaders/invisible.v.glsl";
    1.10  static const char *fsdr_path = "data/shaders/invisible.f.glsl";
    1.11  static unsigned char tex;
    1.12 @@ -30,6 +30,13 @@
    1.13  static float aspect;
    1.14  static bool show;
    1.15  static bool wireframe;
    1.16 +static bool fullscreen;
    1.17 +
    1.18 +static float phi = 0;
    1.19 +static float theta = 0;
    1.20 +static float distance = 10;
    1.21 +static int prev_x = -1, prev_y = -1;
    1.22 +static int bn;
    1.23  
    1.24  static void cleanup();
    1.25  static bool init();
    1.26 @@ -38,6 +45,8 @@
    1.27  static void display();
    1.28  static void reshape(int w, int h);
    1.29  static void keyb(unsigned char key, int x, int y);
    1.30 +static void mouse(int button, int status, int x, int y);
    1.31 +static void motion(int x, int y);
    1.32  static void idle();
    1.33  
    1.34  bool has_video;
    1.35 @@ -63,6 +72,8 @@
    1.36  	glutDisplayFunc(display);
    1.37  	glutReshapeFunc(reshape);
    1.38  	glutKeyboardFunc(keyb);
    1.39 +	glutMouseFunc(mouse);
    1.40 +	glutMotionFunc(motion);
    1.41  	glutIdleFunc(idle);
    1.42  
    1.43  	atexit(cleanup);
    1.44 @@ -73,13 +84,63 @@
    1.45  	glutMainLoop();
    1.46  }
    1.47  
    1.48 +static bool init()
    1.49 +{
    1.50 +	glClearColor(1, 0, 0, 1);
    1.51 +
    1.52 +    frame = new Frame;
    1.53 +	if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
    1.54 +		fprintf(stderr, "Failed to load texture: %s\n", filename);
    1.55 +		return false;
    1.56 +	}
    1.57 +
    1.58 +	/* shaders setup */
    1.59 +	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
    1.60 +		fprintf(stderr, "Failed to create shader program!\n");
    1.61 +		return false;
    1.62 +	}
    1.63 +
    1.64 +	glUseProgram(sprog);
    1.65 +	int dloc = glGetUniformLocation(sprog, "depth_tex");
    1.66 +	if(dloc != -1)
    1.67 +		glUniform1i(dloc, 1);
    1.68 +	int tloc = glGetUniformLocation(sprog, "tex");
    1.69 +	if(tloc != -1)
    1.70 +		glUniform1i(tloc, 0);
    1.71 +	glUseProgram(0);
    1.72 +
    1.73 +	/* debugging */
    1.74 +	debugging = glGenLists(1);
    1.75 +	glNewList(debugging, GL_COMPILE);
    1.76 +	glEnable(GL_TEXTURE_2D);
    1.77 +	glBindTexture(GL_TEXTURE_2D, frame->video_tex);
    1.78 +	draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
    1.79 +	glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
    1.80 +	draw_tess_quad(0, -1, 1, 2, 1, 1, true);
    1.81 +	glDisable(GL_TEXTURE_2D);
    1.82 +	glEndList();
    1.83 +
    1.84 +	return true;
    1.85 +}
    1.86 +
    1.87 +static void init_tessquad()
    1.88 +{
    1.89 +	if(drawing)
    1.90 +		glDeleteLists(drawing, 1);
    1.91 +
    1.92 +	drawing = glGenLists(1);
    1.93 +	glNewList(drawing, GL_COMPILE);
    1.94 +	float fov_rads = FOV / 180.0 * M_PI;
    1.95 +	float ysz = QUAD_DIST * tan(fov_rads / 2.0);
    1.96 +	float xsz = ysz * aspect;
    1.97 +	draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
    1.98 +	glEndList();
    1.99 +}
   1.100 +
   1.101  static void display()
   1.102  {
   1.103  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.104  
   1.105 -	glMatrixMode(GL_MODELVIEW);
   1.106 -	glLoadIdentity();
   1.107 -
   1.108  	has_depth = false; has_video = false;
   1.109  	if(freenect_process_events(kin_ctx) != 0) {
   1.110  		fprintf(stderr, "Failed to process events.\n");
   1.111 @@ -87,6 +148,14 @@
   1.112  	}
   1.113  	frame->process();
   1.114  
   1.115 +	glMatrixMode(GL_MODELVIEW);
   1.116 +	glLoadIdentity();
   1.117 +
   1.118 +	// camera
   1.119 +	glRotatef(phi, 1, 0, 0);
   1.120 +	glRotatef(theta, 0, 1, 0);
   1.121 +	glTranslatef(0, 0, -distance);
   1.122 +
   1.123  	glUseProgram(sprog);
   1.124  
   1.125  	// draw video frame
   1.126 @@ -146,6 +215,14 @@
   1.127  	case 'w':
   1.128  		wireframe = wireframe ? false : true;
   1.129  		break;
   1.130 +	case 'r':
   1.131 +		theta = 0;
   1.132 +		phi = 0;
   1.133 +		distance = 0;
   1.134 +		break;
   1.135 +	case 'f':
   1.136 +		glutFullScreen();
   1.137 +		break;
   1.138  	case 27:
   1.139  		exit(0);
   1.140  	default:
   1.141 @@ -153,6 +230,42 @@
   1.142  	}
   1.143  }
   1.144  
   1.145 +static void mouse(int button, int state, int x, int y)
   1.146 +{
   1.147 +	bn = button;
   1.148 +	prev_x = x;
   1.149 +	prev_y = y;
   1.150 +}
   1.151 +
   1.152 +static void motion(int x, int y)
   1.153 +{
   1.154 +	switch(bn) {
   1.155 +		case GLUT_LEFT_BUTTON:
   1.156 +		theta += x - prev_x;
   1.157 +		phi += y - prev_y;
   1.158 +
   1.159 +		theta = theta < 0 ? 360 + theta : theta;
   1.160 +		theta = theta > 360 ? theta - 360 : theta;
   1.161 +
   1.162 +		phi = phi < -90 ? 90 + phi : phi;
   1.163 +		phi = phi > 90 ? phi - 90 : phi;
   1.164 +		break;
   1.165 +
   1.166 +	case GLUT_RIGHT_BUTTON:
   1.167 +		distance *= (y - prev_y) * 0.01 + 1;
   1.168 +		if(distance < 0.0) {
   1.169 +			distance = 0.0;
   1.170 +		}
   1.171 +		break;
   1.172 +
   1.173 +	default:
   1.174 +		break;
   1.175 +	}
   1.176 +
   1.177 +	prev_x = x;
   1.178 +	prev_y = y;
   1.179 +}
   1.180 +
   1.181  static void idle()
   1.182  {
   1.183  	glutPostRedisplay();
   1.184 @@ -166,56 +279,3 @@
   1.185  	stop_kinect_frames(kin_dev);
   1.186  	stop_kinect(kin_ctx, kin_dev);
   1.187  }
   1.188 -
   1.189 -static bool init()
   1.190 -{
   1.191 -	glClearColor(1, 0, 0, 1);
   1.192 -
   1.193 -    frame = new Frame;
   1.194 -	if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
   1.195 -		fprintf(stderr, "Failed to load texture: %s\n", filename);
   1.196 -		return false;
   1.197 -	}
   1.198 -
   1.199 -	/* shaders setup */
   1.200 -	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
   1.201 -		fprintf(stderr, "Failed to create shader program!\n");
   1.202 -		return false;
   1.203 -	}
   1.204 -
   1.205 -	glUseProgram(sprog);
   1.206 -	int dloc = glGetUniformLocation(sprog, "depth_tex");
   1.207 -	if(dloc != -1)
   1.208 -		glUniform1i(dloc, 1);
   1.209 -	int tloc = glGetUniformLocation(sprog, "tex");
   1.210 -	if(tloc != -1)
   1.211 -		glUniform1i(tloc, 0);
   1.212 -	glUseProgram(0);
   1.213 -
   1.214 -	/* debugging */
   1.215 -	debugging = glGenLists(1);
   1.216 -	glNewList(debugging, GL_COMPILE);
   1.217 -	glEnable(GL_TEXTURE_2D);
   1.218 -	glBindTexture(GL_TEXTURE_2D, frame->video_tex);
   1.219 -	draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
   1.220 -	glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
   1.221 -	draw_tess_quad(0, -1, 1, 2, 1, 1, true);
   1.222 -	glDisable(GL_TEXTURE_2D);
   1.223 -	glEndList();
   1.224 -
   1.225 -	return true;
   1.226 -}
   1.227 -
   1.228 -static void init_tessquad()
   1.229 -{
   1.230 -	if(drawing)
   1.231 -		glDeleteLists(drawing, 1);
   1.232 -
   1.233 -	drawing = glGenLists(1);
   1.234 -	glNewList(drawing, GL_COMPILE);
   1.235 -	float fov_rads = FOV / 180.0 * M_PI;
   1.236 -	float ysz = QUAD_DIST * tan(fov_rads / 2.0);
   1.237 -	float xsz = ysz * aspect;
   1.238 -	draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
   1.239 -	glEndList();
   1.240 -}