Browse Source

OpenGL 3.0 based shaders

BastiaanOlij 8 years ago
parent
commit
bab412304a
4 changed files with 79 additions and 3 deletions
  1. 2 0
      include/openhmd.h
  2. 6 0
      src/openhmd.c
  3. 67 3
      src/shaders.c
  4. 4 0
      src/shaders.h

+ 2 - 0
include/openhmd.h

@@ -61,6 +61,8 @@ typedef enum {
 typedef enum {
 	OHMD_GLSL_DISTORTION_VERT_SRC = 0,
 	OHMD_GLSL_DISTORTION_FRAG_SRC = 1,
+	OHMD_GLSL_330_DISTORTION_VERT_SRC = 2,
+	OHMD_GLSL_330_DISTORTION_FRAG_SRC = 3,
 } ohmd_string_description;
 
 /** A collection of float value information types, used for getting and setting information with

+ 6 - 0
src/openhmd.c

@@ -115,6 +115,12 @@ int OHMD_APIENTRY ohmd_gets(ohmd_string_description type, const char ** out)
 	case OHMD_GLSL_DISTORTION_FRAG_SRC:
 		*out = distortion_frag;
 		return OHMD_S_OK;
+	case OHMD_GLSL_330_DISTORTION_VERT_SRC:
+		*out = distortion_vert_330;
+		return OHMD_S_OK;
+	case OHMD_GLSL_330_DISTORTION_FRAG_SRC:
+		*out = distortion_frag_330;
+		return OHMD_S_OK;
 	default:
 		return OHMD_S_UNSUPPORTED;
 	}

+ 67 - 3
src/shaders.c

@@ -4,7 +4,7 @@ const char * const distortion_vert =
 "#version 120\n"
 "void main(void)\n"
 "{\n"
-	"gl_TexCoord[0] = gl_MultiTexCoord0;\n"
+    "gl_TexCoord[0] = gl_MultiTexCoord0;\n"
     "gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;\n"
 "}";
 
@@ -40,8 +40,8 @@ const char * const distortion_frag =
     "float r_mag = length(r);\n"
     "//offset for which fragment is sourced\n"
     "vec2 r_displaced = r * (HmdWarpParam.w + HmdWarpParam.z * r_mag +\n"
-		"HmdWarpParam.y * r_mag * r_mag +\n"
-		"HmdWarpParam.x * r_mag * r_mag * r_mag);\n"
+    "HmdWarpParam.y * r_mag * r_mag +\n"
+    "HmdWarpParam.x * r_mag * r_mag * r_mag);\n"
     "//back to world scale\n"
     "r_displaced *= WarpScale;\n"
     "//back to viewport co-ord\n"
@@ -55,3 +55,67 @@ const char * const distortion_frag =
     "//Black edges off the texture\n"
     "gl_FragColor = ((tc_g.x < 0.0) || (tc_g.x > 1.0) || (tc_g.y < 0.0) || (tc_g.y > 1.0)) ? vec4(0.0, 0.0, 0.0, 1.0) : vec4(red, green, blue, 1.0);\n"
 "}";
+
+const char *const distortion_vert_330 =
+"#version 330\n"
+"\n"
+"layout (location=0) in vec2 coords;"
+"uniform mat4 mvp;"
+"out vec2 T;"
+"\n"
+"void main(void)\n"
+"{\n"
+    "T = coords;\n"
+    "gl_Position = mvp * vec4(coords, 0.0, 1.0);\n"
+"}";
+
+const char *const distortion_frag_330 =
+"#version 330\n"
+"\n"
+"//per eye texture to warp for lens distortion\n"
+"uniform sampler2D warpTexture;\n"
+"\n"
+"//Position of lens center in m (usually eye_w/2, eye_h/2)\n"
+"uniform vec2 LensCenter;\n"
+"//Scale from texture co-ords to m (usually eye_w, eye_h)\n"
+"uniform vec2 ViewportScale;\n"
+"//Distortion overall scale in m (usually ~eye_w/2)\n"
+"uniform float WarpScale;\n"
+"//Distoriton coefficients (PanoTools model) [a,b,c,d]\n"
+"uniform vec4 HmdWarpParam;\n"
+"\n"
+"//chromatic distortion post scaling\n"
+"uniform vec3 aberr;\n"
+"\n"
+"in vec2 T;\n"
+"out vec4 color;\n"
+"\n"
+"void main()\n"
+"{\n"
+    "//output_loc is the fragment location on screen from [0,1]x[0,1]\n"
+    "vec2 output_loc = vec2(T.s, T.t);\n"
+    "//Compute fragment location in lens-centered co-ordinates at world scale\n"
+    "vec2 r = output_loc * ViewportScale - LensCenter;\n"
+    "//scale for distortion model\n"
+    "//distortion model has r=1 being the largest circle inscribed (e.g. eye_w/2)\n"
+    "r /= WarpScale;\n"
+    "\n"
+    "//|r|**2\n"
+    "float r_mag = length(r);\n"
+    "//offset for which fragment is sourced\n"
+    "vec2 r_displaced = r * (HmdWarpParam.w + HmdWarpParam.z * r_mag +\n"
+    "HmdWarpParam.y * r_mag * r_mag +\n"
+    "HmdWarpParam.x * r_mag * r_mag * r_mag);\n"
+    "//back to world scale\n"
+    "r_displaced *= WarpScale;\n"
+    "//back to viewport co-ord\n"
+    "vec2 tc_r = (LensCenter + aberr.r * r_displaced) / ViewportScale;\n"
+    "vec2 tc_g = (LensCenter + aberr.g * r_displaced) / ViewportScale;\n"
+    "vec2 tc_b = (LensCenter + aberr.b * r_displaced) / ViewportScale;\n"
+"\n"
+    "float red = texture(warpTexture, tc_r).r;\n"
+    "float green = texture(warpTexture, tc_g).g;\n"
+    "float blue = texture(warpTexture, tc_b).b;\n"
+    "//Black edges off the texture\n"
+    "color = ((tc_g.x < 0.0) || (tc_g.x > 1.0) || (tc_g.y < 0.0) || (tc_g.y > 1.0)) ? vec4(0.0, 0.0, 0.0, 1.0) : vec4(red, green, blue, 1.0);\n"
+"}";

+ 4 - 0
src/shaders.h

@@ -3,4 +3,8 @@
 
 extern const char * const distortion_vert;
 extern const char * const distortion_frag;
+
+extern const char * const distortion_vert_330;
+extern const char * const distortion_frag_330;
+
 #endif /* SHADERS_H */