invisible

changeset 26:61d593076c56 tip

foo
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sun, 17 Nov 2013 23:02:40 +0200
parents 96b022f1210e
children
files data/shaders/invisible.v.glsl src/main.cc
diffstat 2 files changed, 118 insertions(+), 58 deletions(-) [+]
line diff
     1.1 --- a/data/shaders/invisible.v.glsl	Sun Nov 17 12:46:45 2013 +0200
     1.2 +++ b/data/shaders/invisible.v.glsl	Sun Nov 17 23:02:40 2013 +0200
     1.3 @@ -4,7 +4,7 @@
     1.4  void main()
     1.5  {
     1.6  	const float threshold = 0.8;
     1.7 -	const float scale = 4.0;
     1.8 +	const float scale = 5.0;
     1.9  
    1.10  	float texel = texture2D(depth_tex, gl_MultiTexCoord0.xy).r;
    1.11  	vec4 vpos = gl_Vertex;
     2.1 --- a/src/main.cc	Sun Nov 17 12:46:45 2013 +0200
     2.2 +++ b/src/main.cc	Sun Nov 17 23:02:40 2013 +0200
     2.3 @@ -18,7 +18,7 @@
     2.4  KinectParams kin_params;
     2.5  Frame *frame;
     2.6  
     2.7 -static const char *filename = "data/textures/wallpaper.jpg";
     2.8 +static const char *filename = "data/textures/wp2.jpg";
     2.9  static const char *vsdr_path = "data/shaders/invisible.v.glsl";
    2.10  static const char *fsdr_path = "data/shaders/invisible.f.glsl";
    2.11  static unsigned char tex;
    2.12 @@ -30,6 +30,13 @@
    2.13  static float aspect;
    2.14  static bool show;
    2.15  static bool wireframe;
    2.16 +static bool fullscreen;
    2.17 +
    2.18 +static float phi = 0;
    2.19 +static float theta = 0;
    2.20 +static float distance = 10;
    2.21 +static int prev_x = -1, prev_y = -1;
    2.22 +static int bn;
    2.23  
    2.24  static void cleanup();
    2.25  static bool init();
    2.26 @@ -38,6 +45,8 @@
    2.27  static void display();
    2.28  static void reshape(int w, int h);
    2.29  static void keyb(unsigned char key, int x, int y);
    2.30 +static void mouse(int button, int status, int x, int y);
    2.31 +static void motion(int x, int y);
    2.32  static void idle();
    2.33  
    2.34  bool has_video;
    2.35 @@ -63,6 +72,8 @@
    2.36  	glutDisplayFunc(display);
    2.37  	glutReshapeFunc(reshape);
    2.38  	glutKeyboardFunc(keyb);
    2.39 +	glutMouseFunc(mouse);
    2.40 +	glutMotionFunc(motion);
    2.41  	glutIdleFunc(idle);
    2.42  
    2.43  	atexit(cleanup);
    2.44 @@ -73,13 +84,63 @@
    2.45  	glutMainLoop();
    2.46  }
    2.47  
    2.48 +static bool init()
    2.49 +{
    2.50 +	glClearColor(1, 0, 0, 1);
    2.51 +
    2.52 +    frame = new Frame;
    2.53 +	if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
    2.54 +		fprintf(stderr, "Failed to load texture: %s\n", filename);
    2.55 +		return false;
    2.56 +	}
    2.57 +
    2.58 +	/* shaders setup */
    2.59 +	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
    2.60 +		fprintf(stderr, "Failed to create shader program!\n");
    2.61 +		return false;
    2.62 +	}
    2.63 +
    2.64 +	glUseProgram(sprog);
    2.65 +	int dloc = glGetUniformLocation(sprog, "depth_tex");
    2.66 +	if(dloc != -1)
    2.67 +		glUniform1i(dloc, 1);
    2.68 +	int tloc = glGetUniformLocation(sprog, "tex");
    2.69 +	if(tloc != -1)
    2.70 +		glUniform1i(tloc, 0);
    2.71 +	glUseProgram(0);
    2.72 +
    2.73 +	/* debugging */
    2.74 +	debugging = glGenLists(1);
    2.75 +	glNewList(debugging, GL_COMPILE);
    2.76 +	glEnable(GL_TEXTURE_2D);
    2.77 +	glBindTexture(GL_TEXTURE_2D, frame->video_tex);
    2.78 +	draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
    2.79 +	glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
    2.80 +	draw_tess_quad(0, -1, 1, 2, 1, 1, true);
    2.81 +	glDisable(GL_TEXTURE_2D);
    2.82 +	glEndList();
    2.83 +
    2.84 +	return true;
    2.85 +}
    2.86 +
    2.87 +static void init_tessquad()
    2.88 +{
    2.89 +	if(drawing)
    2.90 +		glDeleteLists(drawing, 1);
    2.91 +
    2.92 +	drawing = glGenLists(1);
    2.93 +	glNewList(drawing, GL_COMPILE);
    2.94 +	float fov_rads = FOV / 180.0 * M_PI;
    2.95 +	float ysz = QUAD_DIST * tan(fov_rads / 2.0);
    2.96 +	float xsz = ysz * aspect;
    2.97 +	draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
    2.98 +	glEndList();
    2.99 +}
   2.100 +
   2.101  static void display()
   2.102  {
   2.103  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   2.104  
   2.105 -	glMatrixMode(GL_MODELVIEW);
   2.106 -	glLoadIdentity();
   2.107 -
   2.108  	has_depth = false; has_video = false;
   2.109  	if(freenect_process_events(kin_ctx) != 0) {
   2.110  		fprintf(stderr, "Failed to process events.\n");
   2.111 @@ -87,6 +148,14 @@
   2.112  	}
   2.113  	frame->process();
   2.114  
   2.115 +	glMatrixMode(GL_MODELVIEW);
   2.116 +	glLoadIdentity();
   2.117 +
   2.118 +	// camera
   2.119 +	glRotatef(phi, 1, 0, 0);
   2.120 +	glRotatef(theta, 0, 1, 0);
   2.121 +	glTranslatef(0, 0, -distance);
   2.122 +
   2.123  	glUseProgram(sprog);
   2.124  
   2.125  	// draw video frame
   2.126 @@ -146,6 +215,14 @@
   2.127  	case 'w':
   2.128  		wireframe = wireframe ? false : true;
   2.129  		break;
   2.130 +	case 'r':
   2.131 +		theta = 0;
   2.132 +		phi = 0;
   2.133 +		distance = 0;
   2.134 +		break;
   2.135 +	case 'f':
   2.136 +		glutFullScreen();
   2.137 +		break;
   2.138  	case 27:
   2.139  		exit(0);
   2.140  	default:
   2.141 @@ -153,6 +230,42 @@
   2.142  	}
   2.143  }
   2.144  
   2.145 +static void mouse(int button, int state, int x, int y)
   2.146 +{
   2.147 +	bn = button;
   2.148 +	prev_x = x;
   2.149 +	prev_y = y;
   2.150 +}
   2.151 +
   2.152 +static void motion(int x, int y)
   2.153 +{
   2.154 +	switch(bn) {
   2.155 +		case GLUT_LEFT_BUTTON:
   2.156 +		theta += x - prev_x;
   2.157 +		phi += y - prev_y;
   2.158 +
   2.159 +		theta = theta < 0 ? 360 + theta : theta;
   2.160 +		theta = theta > 360 ? theta - 360 : theta;
   2.161 +
   2.162 +		phi = phi < -90 ? 90 + phi : phi;
   2.163 +		phi = phi > 90 ? phi - 90 : phi;
   2.164 +		break;
   2.165 +
   2.166 +	case GLUT_RIGHT_BUTTON:
   2.167 +		distance *= (y - prev_y) * 0.01 + 1;
   2.168 +		if(distance < 0.0) {
   2.169 +			distance = 0.0;
   2.170 +		}
   2.171 +		break;
   2.172 +
   2.173 +	default:
   2.174 +		break;
   2.175 +	}
   2.176 +
   2.177 +	prev_x = x;
   2.178 +	prev_y = y;
   2.179 +}
   2.180 +
   2.181  static void idle()
   2.182  {
   2.183  	glutPostRedisplay();
   2.184 @@ -166,56 +279,3 @@
   2.185  	stop_kinect_frames(kin_dev);
   2.186  	stop_kinect(kin_ctx, kin_dev);
   2.187  }
   2.188 -
   2.189 -static bool init()
   2.190 -{
   2.191 -	glClearColor(1, 0, 0, 1);
   2.192 -
   2.193 -    frame = new Frame;
   2.194 -	if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
   2.195 -		fprintf(stderr, "Failed to load texture: %s\n", filename);
   2.196 -		return false;
   2.197 -	}
   2.198 -
   2.199 -	/* shaders setup */
   2.200 -	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
   2.201 -		fprintf(stderr, "Failed to create shader program!\n");
   2.202 -		return false;
   2.203 -	}
   2.204 -
   2.205 -	glUseProgram(sprog);
   2.206 -	int dloc = glGetUniformLocation(sprog, "depth_tex");
   2.207 -	if(dloc != -1)
   2.208 -		glUniform1i(dloc, 1);
   2.209 -	int tloc = glGetUniformLocation(sprog, "tex");
   2.210 -	if(tloc != -1)
   2.211 -		glUniform1i(tloc, 0);
   2.212 -	glUseProgram(0);
   2.213 -
   2.214 -	/* debugging */
   2.215 -	debugging = glGenLists(1);
   2.216 -	glNewList(debugging, GL_COMPILE);
   2.217 -	glEnable(GL_TEXTURE_2D);
   2.218 -	glBindTexture(GL_TEXTURE_2D, frame->video_tex);
   2.219 -	draw_tess_quad(-1, -1, 1, 2, 1, 1, true);
   2.220 -	glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
   2.221 -	draw_tess_quad(0, -1, 1, 2, 1, 1, true);
   2.222 -	glDisable(GL_TEXTURE_2D);
   2.223 -	glEndList();
   2.224 -
   2.225 -	return true;
   2.226 -}
   2.227 -
   2.228 -static void init_tessquad()
   2.229 -{
   2.230 -	if(drawing)
   2.231 -		glDeleteLists(drawing, 1);
   2.232 -
   2.233 -	drawing = glGenLists(1);
   2.234 -	glNewList(drawing, GL_COMPILE);
   2.235 -	float fov_rads = FOV / 180.0 * M_PI;
   2.236 -	float ysz = QUAD_DIST * tan(fov_rads / 2.0);
   2.237 -	float xsz = ysz * aspect;
   2.238 -	draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
   2.239 -	glEndList();
   2.240 -}