Jelajahi Sumber

Merge pull request #170 from magestik/opengles

Add OpenGL ES shaders
TheOnlyJoey 6 tahun lalu
induk
melakukan
764f5ca5c0
4 mengubah file dengan 73 tambahan dan 0 penghapusan
  1. 2 0
      include/openhmd.h
  2. 6 0
      src/openhmd.c
  3. 62 0
      src/shaders.c
  4. 3 0
      src/shaders.h

+ 2 - 0
include/openhmd.h

@@ -63,6 +63,8 @@ typedef enum {
 	OHMD_GLSL_DISTORTION_FRAG_SRC = 1,
 	OHMD_GLSL_330_DISTORTION_VERT_SRC = 2,
 	OHMD_GLSL_330_DISTORTION_FRAG_SRC = 3,
+	OHMD_GLSL_ES_DISTORTION_VERT_SRC = 4,
+	OHMD_GLSL_ES_DISTORTION_FRAG_SRC = 5,
 } ohmd_string_description;
 
 /** Standard controls. Note that this is not an index into the control state. 

+ 6 - 0
src/openhmd.c

@@ -133,6 +133,12 @@ int OHMD_APIENTRY ohmd_gets(ohmd_string_description type, const char ** out)
 	case OHMD_GLSL_330_DISTORTION_FRAG_SRC:
 		*out = distortion_frag_330;
 		return OHMD_S_OK;
+	case OHMD_GLSL_ES_DISTORTION_VERT_SRC:
+		*out = distortion_vert_es;
+		return OHMD_S_OK;
+	case OHMD_GLSL_ES_DISTORTION_FRAG_SRC:
+		*out = distortion_frag_es;
+		return OHMD_S_OK;
 	default:
 		return OHMD_S_UNSUPPORTED;
 	}

+ 62 - 0
src/shaders.c

@@ -119,3 +119,65 @@ const char *const distortion_frag_330 =
     "//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"
 "}";
+
+const char * const distortion_vert_es =
+"#version 100\n"
+"\n"
+"attribute vec2 coords;"
+"uniform mat4 mvp;"
+"varying vec2 T;"
+"void main(void)\n"
+"{\n"
+	"T = coords;\n"
+	"gl_Position = mvp * vec4(coords, 0.0, 1.0);\n"
+"}";
+
+const char * const distortion_frag_es =
+"#version 100\n"
+"precision mediump float;"
+"\n"
+"varying vec2 T;"
+"//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"
+"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 = texture2D(warpTexture, tc_r).r;\n"
+	"float green = texture2D(warpTexture, tc_g).g;\n"
+	"float blue = texture2D(warpTexture, tc_b).b;\n"
+	"//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"
+"}";

+ 3 - 0
src/shaders.h

@@ -7,4 +7,7 @@ extern const char * const distortion_frag;
 extern const char * const distortion_vert_330;
 extern const char * const distortion_frag_330;
 
+extern const char * const distortion_vert_es;
+extern const char * const distortion_frag_es;
+
 #endif /* SHADERS_H */