aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRay <raysan5@gmail.com>2016-06-10 08:58:37 +0200
committerGitHub <noreply@github.com>2016-06-10 08:58:37 +0200
commit296378299259c86af58d5bda5d4654680ef87f08 (patch)
tree5d5b4ca2dc079b74c9228b8f1174b7e54891fe1b
parentfb9047735465537d97c78fdf56bccf7b74ba0bb7 (diff)
parent77f599885dade4930fb01baca22db6e2ae0c9f20 (diff)
downloadraylib-296378299259c86af58d5bda5d4654680ef87f08.tar.gz
raylib-296378299259c86af58d5bda5d4654680ef87f08.zip
Merge pull request #128 from victorfisac/develop
Adapted shaders for OpenGL ES 2.0
-rw-r--r--examples/resources/shaders/glsl100/bloom.fs2
-rw-r--r--examples/resources/shaders/glsl100/grayscale.fs2
-rw-r--r--shaders/glsl100/bloom.fs2
-rw-r--r--shaders/glsl100/grayscale.fs2
-rw-r--r--src/rlgl.c7
-rw-r--r--src/standard_shader.h40
6 files changed, 34 insertions, 21 deletions
diff --git a/examples/resources/shaders/glsl100/bloom.fs b/examples/resources/shaders/glsl100/bloom.fs
index 280d2fb6..128736f2 100644
--- a/examples/resources/shaders/glsl100/bloom.fs
+++ b/examples/resources/shaders/glsl100/bloom.fs
@@ -26,7 +26,7 @@ void main()
}
// Texel color fetching from texture sampler
- vec4 texelColor = texture(texture0, fragTexCoord);
+ vec4 texelColor = texture2D(texture0, fragTexCoord);
// Calculate final fragment color
if (texelColor.r < 0.3) tc = sum*sum*0.012 + texelColor;
diff --git a/examples/resources/shaders/glsl100/grayscale.fs b/examples/resources/shaders/glsl100/grayscale.fs
index f92ec335..cf857488 100644
--- a/examples/resources/shaders/glsl100/grayscale.fs
+++ b/examples/resources/shaders/glsl100/grayscale.fs
@@ -15,7 +15,7 @@ uniform vec4 fragTintColor;
void main()
{
// Texel color fetching from texture sampler
- vec4 texelColor = texture(texture0, fragTexCoord)*fragTintColor*fragColor;
+ vec4 texelColor = texture2D(texture0, fragTexCoord)*fragTintColor*fragColor;
// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
diff --git a/shaders/glsl100/bloom.fs b/shaders/glsl100/bloom.fs
index 280d2fb6..128736f2 100644
--- a/shaders/glsl100/bloom.fs
+++ b/shaders/glsl100/bloom.fs
@@ -26,7 +26,7 @@ void main()
}
// Texel color fetching from texture sampler
- vec4 texelColor = texture(texture0, fragTexCoord);
+ vec4 texelColor = texture2D(texture0, fragTexCoord);
// Calculate final fragment color
if (texelColor.r < 0.3) tc = sum*sum*0.012 + texelColor;
diff --git a/shaders/glsl100/grayscale.fs b/shaders/glsl100/grayscale.fs
index c76dd8af..15174ea5 100644
--- a/shaders/glsl100/grayscale.fs
+++ b/shaders/glsl100/grayscale.fs
@@ -15,7 +15,7 @@ uniform vec4 colDiffuse;
void main()
{
// Texel color fetching from texture sampler
- vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor;
+ vec4 texelColor = texture2D(texture0, fragTexCoord)*colDiffuse*fragColor;
// Convert texel color to grayscale using NTSC conversion weights
float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114));
diff --git a/src/rlgl.c b/src/rlgl.c
index c9944806..9a88a818 100644
--- a/src/rlgl.c
+++ b/src/rlgl.c
@@ -1795,8 +1795,13 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform)
// NOTE: standard shader specific locations are got at render time to keep Shader struct as simple as possible (with just default shader locations)
if (material.shader.id == standardShader.id)
{
+ // Transpose and inverse model transformations matrix for fragment normal calculations
+ Matrix transInvTransform = transform;
+ MatrixTranspose(&transInvTransform);
+ MatrixInvert(&transInvTransform);
+
// Send model transformations matrix to shader
- glUniformMatrix4fv(glGetUniformLocation(material.shader.id, "modelMatrix"), 1, false, MatrixToFloat(transform));
+ glUniformMatrix4fv(glGetUniformLocation(material.shader.id, "modelMatrix"), 1, false, MatrixToFloat(transInvTransform));
// Send view transformation matrix to shader. View matrix 8, 9 and 10 are view direction vector axis values (target - position)
glUniform3f(glGetUniformLocation(material.shader.id, "viewDir"), matView.m8, matView.m9, matView.m10);
diff --git a/src/standard_shader.h b/src/standard_shader.h
index b2b8980c..a4bc9694 100644
--- a/src/standard_shader.h
+++ b/src/standard_shader.h
@@ -85,13 +85,13 @@ static const char fStandardShaderStr[] =
"{\n"
" vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1));\n"
" vec3 surfaceToLight = l.position - surfacePos;\n"
-" float brightness = clamp(dot(n, surfaceToLight)/(length(surfaceToLight)*length(n)), 0, 1);\n"
+" float brightness = clamp(float(dot(n, surfaceToLight)/(length(surfaceToLight)*length(n))), 0.0, 1.0);\n"
" float diff = 1.0/dot(surfaceToLight/l.radius, surfaceToLight/l.radius)*brightness*l.intensity;\n"
" float spec = 0.0;\n"
" if (diff > 0.0)\n"
" {\n"
" vec3 h = normalize(-l.direction + v);\n"
-" spec = pow(dot(n, h), 3 + glossiness)*s;\n"
+" spec = pow(dot(n, h), 3.0 + glossiness)*s;\n"
" }\n"
" return (diff*l.diffuse.rgb + spec*colSpecular.rgb);\n"
"}\n"
@@ -99,23 +99,23 @@ static const char fStandardShaderStr[] =
"vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v, float s)\n"
"{\n"
" vec3 lightDir = normalize(-l.direction);\n"
-" float diff = clamp(dot(n, lightDir), 0.0, 1.0)*l.intensity;\n"
+" float diff = clamp(float(dot(n, lightDir)), 0.0, 1.0)*l.intensity;\n"
" float spec = 0.0;\n"
" if (diff > 0.0)\n"
" {\n"
" vec3 h = normalize(lightDir + v);\n"
-" spec = pow(dot(n, h), 3 + glossiness)*s;\n"
+" spec = pow(dot(n, h), 3.0 + glossiness)*s;\n"
" }\n"
" return (diff*l.intensity*l.diffuse.rgb + spec*colSpecular.rgb);\n"
"}\n"
"\n"
"vec3 CalcSpotLight(Light l, vec3 n, vec3 v, float s)\n"
"{\n"
-" vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1));\n"
+" vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1.0));\n"
" vec3 lightToSurface = normalize(surfacePos - l.position);\n"
" vec3 lightDir = normalize(-l.direction);\n"
-" float diff = clamp(dot(n, lightDir), 0.0, 1.0)*l.intensity;\n"
-" float attenuation = clamp(dot(n, lightToSurface), 0.0, 1.0);\n"
+" float diff = clamp(float(dot(n, lightDir)), 0.0, 1.0)*l.intensity;\n"
+" float attenuation = clamp(float(dot(n, lightToSurface)), 0.0, 1.0);\n"
" attenuation = dot(lightToSurface, -lightDir);\n"
" float lightToSurfaceAngle = degrees(acos(attenuation));\n"
" if (lightToSurfaceAngle > l.coneAngle) attenuation = 0.0;\n"
@@ -125,37 +125,45 @@ static const char fStandardShaderStr[] =
" if (diffAttenuation > 0.0)\n"
" {\n"
" vec3 h = normalize(lightDir + v);\n"
-" spec = pow(dot(n, h), 3 + glossiness)*s;\n"
+" spec = pow(dot(n, h), 3.0 + glossiness)*s;\n"
" }\n"
" return (falloff*(diffAttenuation*l.diffuse.rgb + spec*colSpecular.rgb));\n"
"}\n"
"\n"
"void main()\n"
"{\n"
-" mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));\n"
+" mat3 normalMatrix = mat3(modelMatrix);\n"
" vec3 normal = normalize(normalMatrix*fragNormal);\n"
" vec3 n = normalize(normal);\n"
" vec3 v = normalize(viewDir);\n"
+#if defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_21)
+" vec4 texelColor = texture2D(texture0, fragTexCoord);\n"
+#elif defined(GRAPHICS_API_OPENGL_33)
" vec4 texelColor = texture(texture0, fragTexCoord);\n"
+#endif
" vec3 lighting = colAmbient.rgb;\n"
" if (useNormal == 1)\n"
" {\n"
+#if defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_21)
+" n *= texture2D(texture1, fragTexCoord).rgb;\n"
+#elif defined(GRAPHICS_API_OPENGL_33)
" n *= texture(texture1, fragTexCoord).rgb;\n"
+#endif
" n = normalize(n);\n"
" }\n"
" float spec = 1.0;\n"
+#if defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_21)
+" if (useSpecular == 1) spec *= normalize(texture2D(texture2, fragTexCoord).r);\n"
+#elif defined(GRAPHICS_API_OPENGL_33)
" if (useSpecular == 1) spec *= normalize(texture(texture2, fragTexCoord).r);\n"
+#endif
" for (int i = 0; i < lightsCount; i++)\n"
" {\n"
" if (lights[i].enabled == 1)\n"
" {\n"
-" switch (lights[i].type)\n"
-" {\n"
-" case 0: lighting += CalcPointLight(lights[i], n, v, spec); break;\n"
-" case 1: lighting += CalcDirectionalLight(lights[i], n, v, spec); break;\n"
-" case 2: lighting += CalcSpotLight(lights[i], n, v, spec); break;\n"
-" default: break;\n"
-" }\n"
+" if(lights[i].type == 0) lighting += CalcPointLight(lights[i], n, v, spec);\n"
+" else if(lights[i].type == 1) lighting += CalcDirectionalLight(lights[i], n, v, spec);\n"
+" else if(lights[i].type == 2) lighting += CalcSpotLight(lights[i], n, v, spec);\n"
" }\n"
" }\n"
#if defined(GRAPHICS_API_OPENGL_33)