Not working but shader compiles and example runs... not sure if deferred rendering could be supported in OpenGL ES 2.0.pull/4554/merge
@ -0,0 +1,59 @@ | |||
#version 100 | |||
precision mediump float; | |||
// Input vertex attributes (from vertex shader) | |||
varying vec2 fragTexCoord; | |||
varying vec4 fragColor; | |||
// Input uniform values | |||
uniform sampler2D gPosition; | |||
uniform sampler2D gNormal; | |||
uniform sampler2D gAlbedoSpec; | |||
struct Light { | |||
int enabled; | |||
int type; // Unused in this demo. | |||
vec3 position; | |||
vec3 target; // Unused in this demo. | |||
vec4 color; | |||
}; | |||
const int NR_LIGHTS = 4; | |||
uniform Light lights[NR_LIGHTS]; | |||
uniform vec3 viewPosition; | |||
const float QUADRATIC = 0.032; | |||
const float LINEAR = 0.09; | |||
void main() | |||
{ | |||
vec3 fragPosition = texture2D(gPosition, fragTexCoord).rgb; | |||
vec3 normal = texture2D(gNormal, fragTexCoord).rgb; | |||
vec3 albedo = texture2D(gAlbedoSpec, fragTexCoord).rgb; | |||
float specular = texture2D(gAlbedoSpec, fragTexCoord).a; | |||
vec3 ambient = albedo*vec3(0.1); | |||
vec3 viewDirection = normalize(viewPosition - fragPosition); | |||
for (int i = 0; i < NR_LIGHTS; ++i) | |||
{ | |||
if(lights[i].enabled == 0) continue; | |||
vec3 lightDirection = lights[i].position - fragPosition; | |||
vec3 diffuse = max(dot(normal, lightDirection), 0.0)*albedo*lights[i].color.xyz; | |||
vec3 halfwayDirection = normalize(lightDirection + viewDirection); | |||
float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0); | |||
vec3 specular = specular*spec*lights[i].color.xyz; | |||
// Attenuation | |||
float distance = length(lights[i].position - fragPosition); | |||
float attenuation = 1.0/(1.0 + LINEAR * distance + QUADRATIC*distance*distance); | |||
diffuse *= attenuation; | |||
specular *= attenuation; | |||
ambient += diffuse + specular; | |||
} | |||
gl_FragColor = vec4(ambient, 1.0); | |||
} | |||
@ -0,0 +1,16 @@ | |||
#version 100 | |||
// Input vertex attributes | |||
attribute vec3 vertexPosition; | |||
attribute vec2 vertexTexCoord; | |||
// Output vertex attributes (to fragment shader) | |||
varying vec2 fragTexCoord; | |||
void main() | |||
{ | |||
fragTexCoord = vertexTexCoord; | |||
// Calculate final vertex position | |||
gl_Position = vec4(vertexPosition, 1.0); | |||
} |
@ -0,0 +1,36 @@ | |||
#version 100 | |||
precision mediump float; | |||
// Input vertex attributes (from vertex shader) | |||
varying vec3 fragPosition; | |||
varying vec2 fragTexCoord; | |||
varying vec3 fragNormal; | |||
varying vec4 fragColor; | |||
// TODO: Is there some alternative for GLSL100 | |||
//layout (location = 0) out vec3 gPosition; | |||
//layout (location = 1) out vec3 gNormal; | |||
//layout (location = 2) out vec4 gAlbedoSpec; | |||
//uniform vec3 gPosition; | |||
//uniform vec3 gNormal; | |||
//uniform vec4 gAlbedoSpec; | |||
// Input uniform values | |||
uniform sampler2D texture0; // Diffuse texture | |||
uniform sampler2D specularTexture; | |||
void main() | |||
{ | |||
// Store the fragment position vector in the first gbuffer texture | |||
//gPosition = fragPosition; | |||
// Store the per-fragment normals into the gbuffer | |||
//gNormal = normalize(fragNormal); | |||
// Store the diffuse per-fragment color | |||
gl_FragColor.rgb = texture2D(texture0, fragTexCoord).rgb; | |||
// Store specular intensity in gAlbedoSpec's alpha component | |||
gl_FragColor.a = texture2D(specularTexture, fragTexCoord).r; | |||
} |
@ -0,0 +1,60 @@ | |||
#version 100 | |||
// Input vertex attributes | |||
attribute vec3 vertexPosition; | |||
attribute vec2 vertexTexCoord; | |||
attribute vec3 vertexNormal; | |||
attribute vec4 vertexColor; | |||
// Input uniform values | |||
uniform mat4 matModel; | |||
uniform mat4 matView; | |||
uniform mat4 matProjection; | |||
// Output vertex attributes (to fragment shader) | |||
varying vec3 fragPosition; | |||
varying vec2 fragTexCoord; | |||
varying vec3 fragNormal; | |||
varying vec4 fragColor; | |||
// https://github.com/glslify/glsl-inverse | |||
mat3 inverse(mat3 m) | |||
{ | |||
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2]; | |||
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2]; | |||
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2]; | |||
float b01 = a22*a11 - a12*a21; | |||
float b11 = -a22*a10 + a12*a20; | |||
float b21 = a21*a10 - a11*a20; | |||
float det = a00*b01 + a01*b11 + a02*b21; | |||
return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11), | |||
b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10), | |||
b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det; | |||
} | |||
// https://github.com/glslify/glsl-transpose | |||
mat3 transpose(mat3 m) | |||
{ | |||
return mat3(m[0][0], m[1][0], m[2][0], | |||
m[0][1], m[1][1], m[2][1], | |||
m[0][2], m[1][2], m[2][2]); | |||
} | |||
void main() | |||
{ | |||
// Calculate vertex attributes for fragment shader | |||
vec4 worldPos = matModel*vec4(vertexPosition, 1.0); | |||
fragPosition = worldPos.xyz; | |||
fragTexCoord = vertexTexCoord; | |||
fragColor = vertexColor; | |||
mat3 normalMatrix = transpose(inverse(mat3(matModel))); | |||
fragNormal = normalMatrix*vertexNormal; | |||
// Calculate final vertex position | |||
gl_Position = matProjection*matView*worldPos; | |||
} |