oculus1

diff src/vr.cc @ 12:d797639e0234

moving on to the distortion... not correct yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 10:14:29 +0300
parents 3265970a7315
children 464e1d135d68
line diff
     1.1 --- a/src/vr.cc	Fri Sep 20 07:00:18 2013 +0300
     1.2 +++ b/src/vr.cc	Fri Sep 20 10:14:29 2013 +0300
     1.3 @@ -1,16 +1,51 @@
     1.4  #include <stdio.h>
     1.5 +#include <GL/glew.h>
     1.6  #include "vr.h"
     1.7  #include "vr_impl.h"
     1.8  
     1.9 +static const char *sdr_src =
    1.10 +	"uniform sampler2D tex;\n"
    1.11 +	"uniform vec2 lens_center;\n"
    1.12 +	"uniform vec2 scr_center;\n"
    1.13 +	"uniform vec2 scale;\n"
    1.14 +	"uniform vec2 scale_in;\n"
    1.15 +	"uniform vec4 warp_param;\n"
    1.16 +	"\n"
    1.17 +	"vec2 warp(in vec2 pt)\n"
    1.18 +	"{\n"
    1.19 +	"    vec2 theta = (pt - lens_center) * scale_in;\n"
    1.20 +	"    float rsq = length(theta);\n"
    1.21 +	"    vec2 rvec = theta * (warp_param.x + warp_param.y * rsq +\n"
    1.22 +	"                         warp_param.z * rsq * rsq +\n"
    1.23 +	"                         warp_param.w * rsq * rsq * rsq);\n"
    1.24 +	"    return lens_center + scale * rvec;\n"
    1.25 +	"}\n"
    1.26 +	"\n"
    1.27 +	"void main()\n"
    1.28 +	"{\n"
    1.29 +	"    vec2 tc = warp(gl_TexCoord[0].xy);\n"
    1.30 +	"    gl_FragColor.rgb = texture2D(tex, tc).rgb;\n"
    1.31 +	"    gl_FragColor.a = 1.0;\n"
    1.32 +	"}\n";
    1.33 +
    1.34  static bool init_ovr();
    1.35 +static bool init_sdr();
    1.36  
    1.37  VRContext vr_ctx;
    1.38 +static unsigned int sdrprog;
    1.39  
    1.40  extern "C" int vr_init(enum vr_init_mode mode)
    1.41  {
    1.42 +	glewInit();
    1.43 +
    1.44  	if(!init_ovr()) {
    1.45  		return -1;
    1.46  	}
    1.47 +
    1.48 +	if(!init_sdr()) {
    1.49 +		return -1;
    1.50 +	}
    1.51 +
    1.52  	return 0;
    1.53  }
    1.54  
    1.55 @@ -70,6 +105,8 @@
    1.56  		for(int i=0; i<4; i++) {
    1.57  			vr_ctx.info.distort[i] = info.DistortionK[i];
    1.58  		}
    1.59 +
    1.60 +		vr_ctx.info.lens_center = info.LensSeparationDistance / info.HScreenSize;
    1.61  	}
    1.62  
    1.63  	// get the sensor device
    1.64 @@ -95,6 +132,55 @@
    1.65  	return true;
    1.66  }
    1.67  
    1.68 +static bool init_sdr()
    1.69 +{
    1.70 +	int status;
    1.71 +
    1.72 +	unsigned int sdr = glCreateShader(GL_FRAGMENT_SHADER);
    1.73 +	glShaderSource(sdr, 1, &sdr_src, 0);
    1.74 +	glCompileShader(sdr);
    1.75 +	glGetShaderiv(sdr, GL_COMPILE_STATUS, &status);
    1.76 +	if(!status) {
    1.77 +		fprintf(stderr, "failed to compile distortion shader\n");
    1.78 +		return false;
    1.79 +	}
    1.80 +
    1.81 +	sdrprog = glCreateProgram();
    1.82 +	glAttachShader(sdrprog, sdr);
    1.83 +	glLinkProgram(sdrprog);
    1.84 +	if(!status) {
    1.85 +		fprintf(stderr, "failed to link distortion shader program\n");
    1.86 +		glDeleteShader(sdr);
    1.87 +		return false;
    1.88 +	}
    1.89 +
    1.90 +	int loc;
    1.91 +
    1.92 +	glUseProgram(sdrprog);
    1.93 +
    1.94 +	if((loc = glGetUniformLocation(sdrprog, "tex")) != -1) {
    1.95 +		glUniform1i(loc, 0);
    1.96 +	}
    1.97 +	if((loc = glGetUniformLocation(sdrprog, "lens_center")) != -1) {
    1.98 +		glUniform2f(loc, 0.5, 0.5);
    1.99 +	}
   1.100 +	if((loc = glGetUniformLocation(sdrprog, "scr_center")) != -1) {
   1.101 +		glUniform2f(loc, 0, 0);
   1.102 +	}
   1.103 +	if((loc = glGetUniformLocation(sdrprog, "scale")) != -1) {
   1.104 +		glUniform2f(loc, 1, 1);
   1.105 +	}
   1.106 +	if((loc = glGetUniformLocation(sdrprog, "scale_in")) != -1) {
   1.107 +		glUniform2f(loc, 1, 1);
   1.108 +	}
   1.109 +	if((loc = glGetUniformLocation(sdrprog, "warp_param")) != -1) {
   1.110 +		glUniform4f(loc, vr_ctx.info.distort[0], vr_ctx.info.distort[1],
   1.111 +				vr_ctx.info.distort[2], vr_ctx.info.distort[3]);
   1.112 +	}
   1.113 +
   1.114 +	return true;
   1.115 +}
   1.116 +
   1.117  extern "C" int vr_get_width(void)
   1.118  {
   1.119  	return vr_ctx.info.width;
   1.120 @@ -165,3 +251,52 @@
   1.121  	Quatf oq = vr_ctx.ovr_sfusion.GetPredictedOrientation();
   1.122  	oq.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(euler + 1, euler, euler + 2);
   1.123  }
   1.124 +
   1.125 +extern "C" void vr_draw_eye(unsigned int tex, int eye)
   1.126 +{
   1.127 +	static const float rects[3][4] = {
   1.128 +		{-1, -1, 1, 1},
   1.129 +		{-1, -1, 0, 1},
   1.130 +		{0, -1, 1, 1}
   1.131 +	};
   1.132 +	static const float offs_scale[3] = {0.0, -1.0, 1.0};
   1.133 +
   1.134 +	glPushAttrib(GL_ENABLE_BIT);
   1.135 +	glDisable(GL_DEPTH_TEST);
   1.136 +	glDisable(GL_LIGHTING);
   1.137 +	glEnable(GL_TEXTURE_2D);
   1.138 +
   1.139 +	glMatrixMode(GL_MODELVIEW);
   1.140 +	glPushMatrix();
   1.141 +	glLoadIdentity();
   1.142 +
   1.143 +	glMatrixMode(GL_PROJECTION);
   1.144 +	glPushMatrix();
   1.145 +	glLoadIdentity();
   1.146 +
   1.147 +	glUseProgram(sdrprog);
   1.148 +
   1.149 +	int loc;
   1.150 +	if((loc = glGetUniformLocation(sdrprog, "lens_center")) != -1) {
   1.151 +		float lens_center = 0.5 - vr_ctx.info.lens_center * offs_scale[eye];
   1.152 +		glUniform2f(loc, lens_center, 0.5);
   1.153 +		printf("lens_center = %f\n", lens_center);
   1.154 +	}
   1.155 +
   1.156 +	glBindTexture(GL_TEXTURE_2D, tex);
   1.157 +	glBegin(GL_QUADS);
   1.158 +	glColor4f(1, 1, 1, 1);
   1.159 +	glTexCoord2f(0, 0); glVertex2f(rects[eye][0], rects[eye][1]);
   1.160 +	glTexCoord2f(1, 0); glVertex2f(rects[eye][2], rects[eye][1]);
   1.161 +	glTexCoord2f(1, 1); glVertex2f(rects[eye][2], rects[eye][3]);
   1.162 +	glTexCoord2f(0, 1); glVertex2f(rects[eye][0], rects[eye][3]);
   1.163 +	glEnd();
   1.164 +
   1.165 +	glUseProgram(0);
   1.166 +
   1.167 +	glPopMatrix();
   1.168 +	glMatrixMode(GL_MODELVIEW);
   1.169 +	glPopMatrix();
   1.170 +
   1.171 +	glPopAttrib();
   1.172 +}