invisible

changeset 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 8b81571dd4dd
files data/shaders/invisible.f.glsl data/shaders/invisible.v.glsl src/main.cc src/sdr.cc src/tesquad.cc src/texture.cc src/texture.h
diffstat 7 files changed, 111 insertions(+), 38 deletions(-) [+]
line diff
     1.1 --- a/data/shaders/invisible.f.glsl	Sun Nov 10 14:32:29 2013 +0200
     1.2 +++ b/data/shaders/invisible.f.glsl	Sat Nov 16 23:29:15 2013 +0200
     1.3 @@ -0,0 +1,6 @@
     1.4 +uniform sampler2D tex;
     1.5 +
     1.6 +void main()
     1.7 +{
     1.8 +	gl_FragColor = texture2D(tex, gl_TexCoord[0].xy);
     1.9 +}
     2.1 --- a/data/shaders/invisible.v.glsl	Sun Nov 10 14:32:29 2013 +0200
     2.2 +++ b/data/shaders/invisible.v.glsl	Sat Nov 16 23:29:15 2013 +0200
     2.3 @@ -0,0 +1,11 @@
     2.4 +uniform sampler2D depth_tex;
     2.5 +
     2.6 +void main()
     2.7 +{
     2.8 +	float depth = texture2D(depth_tex, gl_MultiTexCoord0.xy).r;
     2.9 +	vec4 vpos = gl_Vertex;
    2.10 +	vpos.z = depth;
    2.11 +
    2.12 +	gl_Position = gl_ModelViewProjectionMatrix * vpos;
    2.13 +	gl_TexCoord[0] = gl_MultiTexCoord0;
    2.14 +}
     3.1 --- a/src/main.cc	Sun Nov 10 14:32:29 2013 +0200
     3.2 +++ b/src/main.cc	Sat Nov 16 23:29:15 2013 +0200
     3.3 @@ -1,6 +1,7 @@
     3.4  #include <GL/glew.h>
     3.5  #include <GL/glut.h>
     3.6  
     3.7 +#include <math.h>
     3.8  #include <stdio.h>
     3.9  
    3.10  #include "kinect.h"
    3.11 @@ -9,7 +10,8 @@
    3.12  #include "tesquad.h"
    3.13  #include "texture.h"
    3.14  
    3.15 -#define ASPECT 1
    3.16 +#define FOV 45.0
    3.17 +#define QUAD_DIST 20.0 /*depth*/
    3.18  
    3.19  freenect_context *kin_ctx;
    3.20  freenect_device *kin_dev;
    3.21 @@ -17,16 +19,20 @@
    3.22  Frame *frame;
    3.23  
    3.24  static const char *filename = "data/textures/wallpaper.jpg";
    3.25 -//static const char *vsdr_path = "data/shaders/invisible.v.glsl";
    3.26 -//static const char *fsdr_path = "data/shaders/invisible.f.glsl";
    3.27 +static const char *vsdr_path = "data/shaders/invisible.v.glsl";
    3.28 +static const char *fsdr_path = "data/shaders/invisible.f.glsl";
    3.29 +static unsigned int sprog;
    3.30  static unsigned int drawing;
    3.31  static unsigned int debugging;
    3.32 -//static unsigned int sprog;
    3.33  static unsigned char tex;
    3.34 +static int tex_height;
    3.35 +static int tex_width;
    3.36 +static float aspect;
    3.37  static bool show;
    3.38  
    3.39  static void cleanup();
    3.40 -static void init();
    3.41 +static bool init();
    3.42 +static void init_tessquad();
    3.43  
    3.44  static void display();
    3.45  static void reshape(int w, int h);
    3.46 @@ -59,7 +65,9 @@
    3.47  	glutIdleFunc(idle);
    3.48  
    3.49  	atexit(cleanup);
    3.50 -	init();
    3.51 +	if(!init()) {
    3.52 +		return 1;
    3.53 +	}
    3.54  
    3.55  	glutMainLoop();
    3.56  }
    3.57 @@ -78,8 +86,32 @@
    3.58  	}
    3.59  	frame->process();
    3.60  
    3.61 -//	glUseProgram(sprog);
    3.62 +	glUseProgram(sprog);
    3.63 +
    3.64 +	// draw video frame
    3.65 +	glMatrixMode(GL_MODELVIEW);
    3.66 +	glPushMatrix();
    3.67 +	glTranslatef(0, 0, -QUAD_DIST);
    3.68 +
    3.69 +	glActiveTexture(GL_TEXTURE0);
    3.70 +	glEnable(GL_TEXTURE_2D);
    3.71 +	glBindTexture(GL_TEXTURE_2D, tex);
    3.72 +	glActiveTexture(GL_TEXTURE1);
    3.73 +	glEnable(GL_TEXTURE_2D);
    3.74 +	glBindTexture(GL_TEXTURE_2D, frame->depth_tex);
    3.75 +
    3.76  	glCallList(drawing);
    3.77 +
    3.78 +	glActiveTexture(GL_TEXTURE1);
    3.79 +	glDisable(GL_TEXTURE_2D);
    3.80 +	glActiveTexture(GL_TEXTURE0);
    3.81 +	glDisable(GL_TEXTURE_2D);
    3.82 +
    3.83 +	glMatrixMode(GL_MODELVIEW);
    3.84 +	glPopMatrix();
    3.85 +
    3.86 +	glUseProgram(0);
    3.87 +
    3.88  	if(show)
    3.89  		glCallList(debugging);
    3.90  	glFlush();
    3.91 @@ -89,10 +121,12 @@
    3.92  
    3.93  static void reshape(int w, int h)
    3.94  {
    3.95 +	aspect = (float)w / (float)h;
    3.96  	glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    3.97  	glMatrixMode(GL_PROJECTION);
    3.98  	glLoadIdentity();
    3.99 -	gluPerspective(45, (float)w / (float)h, 1, 1000);
   3.100 +	gluPerspective(FOV, aspect, 1, 1000);
   3.101 +	init_tessquad();
   3.102  }
   3.103  
   3.104  static void keyb(unsigned char key, int x, int y)
   3.105 @@ -118,39 +152,37 @@
   3.106  
   3.107  static void cleanup()
   3.108  {
   3.109 +	glDeleteLists(drawing, 1);
   3.110 +	glDeleteLists(debugging, 1);
   3.111 +
   3.112  	stop_kinect_frames(kin_dev);
   3.113  	stop_kinect(kin_ctx, kin_dev);
   3.114  }
   3.115  
   3.116 -static void init()
   3.117 +static bool init()
   3.118  {
   3.119 -	glClearColor(1, 1, 1, 1);
   3.120 +	glClearColor(1, 0, 0, 1);
   3.121  
   3.122      frame = new Frame;
   3.123 -	if(!(tex = load_texture(filename))) {
   3.124 +	if(!(tex = load_texture(filename, &tex_width, &tex_height))) {
   3.125  		fprintf(stderr, "Failed to load texture: %s\n", filename);
   3.126 -		exit(1);
   3.127 +		return false;
   3.128  	}
   3.129  
   3.130  	/* shaders setup */
   3.131 -//	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
   3.132 -//		fprintf(stderr, "Failed to create shader program!\n");
   3.133 -//		exit(1);
   3.134 -//	}
   3.135 +	if(!(sprog = sdr_getprog(vsdr_path, fsdr_path))) {
   3.136 +		fprintf(stderr, "Failed to create shader program!\n");
   3.137 +		return false;
   3.138 +	}
   3.139  
   3.140 -	/* drawing */
   3.141 -	drawing = glGenLists(1);
   3.142 -	glNewList(drawing, GL_COMPILE);
   3.143 -	glMatrixMode(GL_MODELVIEW);
   3.144 -	glPushMatrix();
   3.145 -	glTranslatef(0, 0, -24);
   3.146 -	glEnable(GL_TEXTURE_2D);
   3.147 -	glBindTexture(GL_TEXTURE_2D, tex);
   3.148 -	draw_tess_quad(-14, -14 * 1/ASPECT, 28, 28 * 1 / ASPECT, 850, 850 * 1 / ASPECT);
   3.149 -	glDisable(GL_TEXTURE_2D);
   3.150 -	glPopMatrix();
   3.151 -	glMatrixMode(GL_MODELVIEW);
   3.152 -	glEndList();
   3.153 +	glUseProgram(sprog);
   3.154 +	int dloc = glGetUniformLocation(sprog, "depth_tex");
   3.155 +	if(dloc != -1)
   3.156 +		glUniform1i(dloc, 1);
   3.157 +	int tloc = glGetUniformLocation(sprog, "tex");
   3.158 +	if(tloc != -1)
   3.159 +		glUniform1i(tloc, 0);
   3.160 +	glUseProgram(0);
   3.161  
   3.162  	/* debugging */
   3.163  	debugging = glGenLists(1);
   3.164 @@ -162,4 +194,20 @@
   3.165  	draw_tess_quad(0, -1, 1, 2, 1, 1, true);
   3.166  	glDisable(GL_TEXTURE_2D);
   3.167  	glEndList();
   3.168 +
   3.169 +	return true;
   3.170  }
   3.171 +
   3.172 +static void init_tessquad()
   3.173 +{
   3.174 +	if(drawing)
   3.175 +		glDeleteLists(drawing, 1);
   3.176 +
   3.177 +	drawing = glGenLists(1);
   3.178 +	glNewList(drawing, GL_COMPILE);
   3.179 +	float fov_rads = FOV / 180.0 * M_PI;
   3.180 +	float ysz = QUAD_DIST * tan(fov_rads / 2.0);
   3.181 +	float xsz = ysz * aspect;
   3.182 +	draw_tess_quad(-xsz, -ysz, xsz * 2, ysz * 2, tex_width/8 - 1, tex_height/8 - 1);
   3.183 +	glEndList();
   3.184 +}
     4.1 --- a/src/sdr.cc	Sun Nov 10 14:32:29 2013 +0200
     4.2 +++ b/src/sdr.cc	Sat Nov 16 23:29:15 2013 +0200
     4.3 @@ -25,14 +25,16 @@
     4.4  	ctr = ftell(fp);
     4.5  	rewind(fp);
     4.6  
     4.7 -	char *glsl = NULL;
     4.8 +	char *glsl;
     4.9  	if(!(glsl = (char*) malloc(ctr + 1))) {
    4.10  		perror("Failed to allocate memory");
    4.11  		return 0;
    4.12  	}
    4.13 -	ctr = fread(glsl, 1, ctr, fp);
    4.14 +	fread(glsl, 1, ctr, fp);
    4.15  	fclose(fp);
    4.16  
    4.17 +	glsl[ctr] = 0;
    4.18 +
    4.19  	return glsl;
    4.20  }
    4.21  
     5.1 --- a/src/tesquad.cc	Sun Nov 10 14:32:29 2013 +0200
     5.2 +++ b/src/tesquad.cc	Sat Nov 16 23:29:15 2013 +0200
     5.3 @@ -3,11 +3,11 @@
     5.4  
     5.5  void draw_tess_quad(float x, float y, float w, float h, int usub, int vsub, bool ortho)
     5.6  {
     5.7 +	glPushAttrib(GL_ENABLE_BIT);
     5.8 +	glDisable(GL_DEPTH_TEST);
     5.9 +	glDisable(GL_LIGHTING);
    5.10 +
    5.11  	if(ortho) {
    5.12 -		glPushAttrib(GL_ENABLE_BIT);
    5.13 -		glDisable(GL_DEPTH_TEST);
    5.14 -		glDisable(GL_LIGHTING);
    5.15 -
    5.16  		glMatrixMode(GL_PROJECTION);
    5.17  		glPushMatrix();
    5.18  		glLoadIdentity();
    5.19 @@ -41,6 +41,6 @@
    5.20  
    5.21  	if(ortho) {
    5.22  		glPopMatrix();
    5.23 -		glPopAttrib();
    5.24  	}
    5.25 +	glPopAttrib();
    5.26  }
     6.1 --- a/src/texture.cc	Sun Nov 10 14:32:29 2013 +0200
     6.2 +++ b/src/texture.cc	Sat Nov 16 23:29:15 2013 +0200
     6.3 @@ -3,7 +3,7 @@
     6.4  #include <cv.h>
     6.5  #include "texture.h"
     6.6  
     6.7 -unsigned int load_texture(const char* fname)
     6.8 +unsigned int load_texture(const char* fname, int *xsz, int *ysz)
     6.9  {
    6.10  	IplImage* img = cvLoadImage(fname);
    6.11  	if(!img) {
    6.12 @@ -22,5 +22,11 @@
    6.13  	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, mat.cols, mat.rows,
    6.14  			0, GL_BGR, GL_UNSIGNED_BYTE, mat.data);
    6.15  
    6.16 +	if(xsz) {
    6.17 +		*xsz = mat.cols;
    6.18 +	}
    6.19 +	if(ysz) {
    6.20 +		*ysz = mat.rows;
    6.21 +	}
    6.22  	return tex;
    6.23  }
     7.1 --- a/src/texture.h	Sun Nov 10 14:32:29 2013 +0200
     7.2 +++ b/src/texture.h	Sat Nov 16 23:29:15 2013 +0200
     7.3 @@ -1,6 +1,6 @@
     7.4  #ifndef TEXTURE_H_
     7.5  #define TEXTURE_H_
     7.6  
     7.7 -unsigned int load_texture(const char *fname);
     7.8 +unsigned int load_texture(const char *fname, int *xsz = 0, int *ysz = 0);
     7.9  
    7.10  #endif // TEXTURE_H_