invisible

diff src/main.cc @ 23:b50ad2711f5f

fixed fov aspect projection et c, added shaders todo: segmentation
author Eleni Maria Stea <eleni@mutantstargoat.com>
date Sat, 16 Nov 2013 23:29:15 +0200
parents 531a814d4d6b
children 96b022f1210e
line diff
     1.1 --- a/src/main.cc	Sun Nov 10 14:32:29 2013 +0200
     1.2 +++ b/src/main.cc	Sat Nov 16 23:29:15 2013 +0200
     1.3 @@ -1,6 +1,7 @@
     1.4  #include <GL/glew.h>
     1.5  #include <GL/glut.h>
     1.6  
     1.7 +#include <math.h>
     1.8  #include <stdio.h>
     1.9  
    1.10  #include "kinect.h"
    1.11 @@ -9,7 +10,8 @@
    1.12  #include "tesquad.h"
    1.13  #include "texture.h"
    1.14  
    1.15 -#define ASPECT 1
    1.16 +#define FOV 45.0
    1.17 +#define QUAD_DIST 20.0 /*depth*/
    1.18  
    1.19  freenect_context *kin_ctx;
    1.20  freenect_device *kin_dev;
    1.21 @@ -17,16 +19,20 @@
    1.22  Frame *frame;
    1.23  
    1.24  static const char *filename = "data/textures/wallpaper.jpg";
    1.25 -//static const char *vsdr_path = "data/shaders/invisible.v.glsl";
    1.26 -//static const char *fsdr_path = "data/shaders/invisible.f.glsl";
    1.27 +static const char *vsdr_path = "data/shaders/invisible.v.glsl";
    1.28 +static const char *fsdr_path = "data/shaders/invisible.f.glsl";
    1.29 +static unsigned int sprog;
    1.30  static unsigned int drawing;
    1.31  static unsigned int debugging;
    1.32 -//static unsigned int sprog;
    1.33  static unsigned char tex;
    1.34 +static int tex_height;
    1.35 +static int tex_width;
    1.36 +static float aspect;
    1.37  static bool show;
    1.38  
    1.39  static void cleanup();
    1.40 -static void init();
    1.41 +static bool init();
    1.42 +static void init_tessquad();
    1.43  
    1.44  static void display();
    1.45  static void reshape(int w, int h);
    1.46 @@ -59,7 +65,9 @@
    1.47  	glutIdleFunc(idle);
    1.48  
    1.49  	atexit(cleanup);
    1.50 -	init();
    1.51 +	if(!init()) {
    1.52 +		return 1;
    1.53 +	}
    1.54  
    1.55  	glutMainLoop();
    1.56  }
    1.57 @@ -78,8 +86,32 @@
    1.58  	}
    1.59  	frame->process();
    1.60  
    1.61 -//	glUseProgram(sprog);
    1.62 +	glUseProgram(sprog);
    1.63 +
    1.64 +	// draw video frame
    1.65 +	glMatrixMode(GL_MODELVIEW);
    1.66 +	glPushMatrix();
    1.67 +	glTranslatef(0, 0, -QUAD_DIST);
    1.68 +
    1.69 +	glActiveTexture(GL_TEXTURE0);
    1.70 +	glEnable(GL_TEXTURE_2D);
    1.71 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.72 +	glActiveTexture(GL_TEXTURE1);
    1.73 +	glEnable(GL_TEXTURE_2D);
    1.74 +	glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
    1.75 +
    1.76  	glCallList(drawing);
    1.77 +
    1.78 +	glActiveTexture(GL_TEXTURE1);
    1.79 +	glDisable(GL_TEXTURE_2D);
    1.80 +	glActiveTexture(GL_TEXTURE0);
    1.81 +	glDisable(GL_TEXTURE_2D);
    1.82 +
    1.83 +	glMatrixMode(GL_MODELVIEW);
    1.84 +	glPopMatrix();
    1.85 +
    1.86 +	glUseProgram(0);
    1.87 +
    1.88  	if(show)
    1.89  		glCallList(debugging);
    1.90  	glFlush();
    1.91 @@ -89,10 +121,12 @@
    1.92  
    1.93  static void reshape(int w, int h)
    1.94  {
    1.95 +	aspect = (float)w / (float)h;
    1.96  	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    1.97  	glMatrixMode(GL_PROJECTION);
    1.98  	glLoadIdentity();
    1.99 -	gluPerspective(45, (float)w / (float)h, 1, 1000);
   1.100 +	gluPerspective(FOV, aspect, 1, 1000);
   1.101 +	init_tessquad();
   1.102  }
   1.103  
   1.104  static void keyb(unsigned char key, int x, int y)
   1.105 @@ -118,39 +152,37 @@
   1.106  
   1.107  static void cleanup()
   1.108  {
   1.109 +	glDeleteLists(drawing, 1);
   1.110 +	glDeleteLists(debugging, 1);
   1.111 +
   1.112  	stop_kinect_frames(kin_dev);
   1.113  	stop_kinect(kin_ctx, kin_dev);
   1.114  }
   1.115  
   1.116 -static void init()
   1.117 +static bool init()
   1.118  {
   1.119 -	glClearColor(1, 1, 1, 1);
   1.120 +	glClearColor(1, 0, 0, 1);
   1.121  
   1.122      frame = new Frame;
   1.123 -	if(!(tex = load_texture(filename))) {
   1.124 +	if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
   1.125  		fprintf(stderr, "Failed to load texture: %s\n", filename);
   1.126 -		exit(1);
   1.127 +		return false;
   1.128  	}
   1.129  
   1.130  	/* shaders setup */
   1.131 -//	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
   1.132 -//		fprintf(stderr, "Failed to create shader program!\n");
   1.133 -//		exit(1);
   1.134 -//	}
   1.135 +	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
   1.136 +		fprintf(stderr, "Failed to create shader program!\n");
   1.137 +		return false;
   1.138 +	}
   1.139  
   1.140 -	/* drawing */
   1.141 -	drawing = glGenLists(1);
   1.142 -	glNewList(drawing, GL_COMPILE);
   1.143 -	glMatrixMode(GL_MODELVIEW);
   1.144 -	glPushMatrix();
   1.145 -	glTranslatef(0, 0, -24);
   1.146 -	glEnable(GL_TEXTURE_2D);
   1.147 -	glBindTexture(GL_TEXTURE_2D, tex);
   1.148 -	draw_tess_quad(-14, -14 * 1/ASPECT, 28, 28 * 1 / ASPECT, 850, 850 * 1 / ASPECT);
   1.149 -	glDisable(GL_TEXTURE_2D);
   1.150 -	glPopMatrix();
   1.151 -	glMatrixMode(GL_MODELVIEW);
   1.152 -	glEndList();
   1.153 +	glUseProgram(sprog);
   1.154 +	int dloc = glGetUniformLocation(sprog, "depth_tex");
   1.155 +	if(dloc != -1)
   1.156 +		glUniform1i(dloc, 1);
   1.157 +	int tloc = glGetUniformLocation(sprog, "tex");
   1.158 +	if(tloc != -1)
   1.159 +		glUniform1i(tloc, 0);
   1.160 +	glUseProgram(0);
   1.161  
   1.162  	/* debugging */
   1.163  	debugging = glGenLists(1);
   1.164 @@ -162,4 +194,20 @@
   1.165  	draw_tess_quad(0, -1, 1, 2, 1, 1, true);
   1.166  	glDisable(GL_TEXTURE_2D);
   1.167  	glEndList();
   1.168 +
   1.169 +	return true;
   1.170  }
   1.171 +
   1.172 +static void init_tessquad()
   1.173 +{
   1.174 +	if(drawing)
   1.175 +		glDeleteLists(drawing, 1);
   1.176 +
   1.177 +	drawing = glGenLists(1);
   1.178 +	glNewList(drawing, GL_COMPILE);
   1.179 +	float fov_rads = FOV / 180.0 * M_PI;
   1.180 +	float ysz = QUAD_DIST * tan(fov_rads / 2.0);
   1.181 +	float xsz = ysz * aspect;
   1.182 +	draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
   1.183 +	glEndList();
   1.184 +}