oculus1

changeset 15:402cbb6d9ce3

added the shader as a separate header file
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 04:15:20 +0300
parents cceffea995a4
children f3672317e5c2
files src/vr.cc src/vr_impl.h src/vr_sdr.h
diffstat 3 files changed, 52 insertions(+), 29 deletions(-) [+]
line diff
     1.1 --- a/src/vr.cc	Sat Sep 21 03:35:53 2013 +0300
     1.2 +++ b/src/vr.cc	Sat Sep 21 04:15:20 2013 +0300
     1.3 @@ -2,35 +2,7 @@
     1.4  #include <GL/glew.h>
     1.5  #include "vr.h"
     1.6  #include "vr_impl.h"
     1.7 -
     1.8 -static const char *sdr_src =
     1.9 -	"uniform sampler2D tex;\n"
    1.10 -	"uniform vec2 lens_center;\n"
    1.11 -	"uniform vec2 scr_center;\n"
    1.12 -	"uniform vec2 scale;\n"
    1.13 -	"uniform vec2 scale_in;\n"
    1.14 -	"uniform vec4 warp_param;\n"
    1.15 -	"\n"
    1.16 -	"vec2 warp(in vec2 pt, out float dbgrad)\n"
    1.17 -	"{\n"
    1.18 -	"    vec2 theta = (pt - lens_center) * scale_in;\n"
    1.19 -	"    float rsq = dot(theta, theta);\n"
    1.20 -	"    vec2 rvec = theta * (warp_param.x + \n"
    1.21 -	"                         warp_param.y * rsq +\n"
    1.22 -	"                         warp_param.z * rsq * rsq +\n"
    1.23 -	"                         warp_param.w * rsq * rsq * rsq);\n"
    1.24 -	"    dbgrad = length(theta);\n"
    1.25 -	"    return lens_center + scale * rvec;\n"
    1.26 -	"}\n"
    1.27 -	"\n"
    1.28 -	"void main()\n"
    1.29 -	"{\n"
    1.30 -	"    float dbgrad;\n"
    1.31 -	"    vec2 tc = warp(gl_TexCoord[0].xy, dbgrad);\n"
    1.32 -	"    gl_FragColor.rgb = texture2D(tex, tc).rgb;\n"
    1.33 -	"    gl_FragColor.rgb += mix(vec3(1.0, 1.0, 0.0), vec3(0.0), smoothstep(0.01, 0.05, dbgrad));\n"
    1.34 -	"    gl_FragColor.a = 1.0;\n"
    1.35 -	"}\n";
    1.36 +#include "vr_sdr.h"
    1.37  
    1.38  static bool init_ovr();
    1.39  static bool init_sdr();
    1.40 @@ -55,6 +27,7 @@
    1.41  
    1.42  extern "C" void vr_shutdown(void)
    1.43  {
    1.44 +	delete [] vr_ctx.info.display;
    1.45  	//System::Destroy();
    1.46  }
    1.47  
    1.48 @@ -124,6 +97,10 @@
    1.49  		float center_dist_meters = info.HScreenSize * 0.25;
    1.50  		float proj_shift = center_dist_meters - info.LensSeparationDistance * 0.5;
    1.51  		vr_ctx.info.proj_center_offset = 4.0 * proj_shift / info.HScreenSize;
    1.52 +
    1.53 +		// grab the display name
    1.54 +		vr_ctx.info.display = new char[strlen(info.DisplayDeviceName) + 1];
    1.55 +		strcpy(vr_ctx.info.display, info.DisplayDeviceName);
    1.56  	}
    1.57  
    1.58  	// get the sensor device
     2.1 --- a/src/vr_impl.h	Sat Sep 21 03:35:53 2013 +0300
     2.2 +++ b/src/vr_impl.h	Sat Sep 21 04:15:20 2013 +0300
     2.3 @@ -12,6 +12,7 @@
     2.4  	SensorFusion ovr_sfusion;
     2.5  
     2.6  	struct {
     2.7 +		char *display;
     2.8  		// the full width and height of the display (both eyes)
     2.9  		int width, height;
    2.10  		float fov;
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/vr_sdr.h	Sat Sep 21 04:15:20 2013 +0300
     3.3 @@ -0,0 +1,45 @@
     3.4 +static const char *sdr_src =
     3.5 +	"uniform sampler2D tex;\n"
     3.6 +	"uniform float aspect, scale;\n"
     3.7 +	"uniform float lens_center_offset;\n"
     3.8 +	"uniform vec4 dist_factors;\n"
     3.9 +	"\n"
    3.10 +	"vec2 distort_texcoords(in vec2 tc);\n"
    3.11 +	"float barrel_scale(float x, in vec4 k);\n"
    3.12 +	"\n"
    3.13 +	"void main()\n"
    3.14 +	"{\n"
    3.15 +	"	vec2 tc = distort_texcoords(gl_TexCoord[0].xy);\n"
    3.16 +	"\n"
    3.17 +	"	float vis = any(greaterThan(tc, vec2(1.0)) || lessThan(tc, vec2(0.0))) ? 0.0 : 1.0;\n"
    3.18 +	"\n"
    3.19 +	"	gl_FragColor.rgb = texture2D(tex, tc).rgb * vis;\n"
    3.20 +	"	gl_FragColor.a = 1.0;\n"
    3.21 +	"}\n"
    3.22 +	"\n"
    3.23 +	"vec2 distort_texcoords(in vec2 tc)\n"
    3.24 +	"{\n"
    3.25 +	"	// map tc [0, 1] -> [-1, 1]\n"
    3.26 +	"	vec2 pt = tc * 2.0 - 1.0;\n"
    3.27 +	"\n"
    3.28 +	"	pt.x += lens_center_offset * 2.0;\n"
    3.29 +	"	pt.y /= aspect;	// correct for aspect ratio\n"
    3.30 +	"\n"
    3.31 +	"	float rad = barrel_scale(dot(pt, pt), dist_factors);\n"
    3.32 +	"	pt *= rad;	// scale the point by the computer distortion radius\n"
    3.33 +	"\n"
    3.34 +	"	pt /= scale;\n"
    3.35 +	"	pt.y *= aspect;\n"
    3.36 +	"	pt.x -= lens_center_offset * 2.0;\n"
    3.37 +	"\n"
    3.38 +	"	// map back to range [0, 1]\n"
    3.39 +	"	return pt * 0.5 + 0.5;\n"
    3.40 +	"}\n"
    3.41 +	"\n"
    3.42 +	"float barrel_scale(float rad, in vec4 k)\n"
    3.43 +	"{\n"
    3.44 +	"	float radsq = rad * rad;\n"
    3.45 +	"	float radquad = radsq * radsq;\n"
    3.46 +	"	return k.x + k.y * radsq + k.z * radquad + k.w * radquad * radsq;\n"
    3.47 +	"}\n";
    3.48 +