Shader cleanup, deferred lighting in progress

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%4045
This commit is contained in:
kearwood
2012-04-13 22:48:13 +00:00
parent 1ebdee1b3c
commit 0d7cdffd24
6 changed files with 471 additions and 390 deletions

View File

@@ -390,6 +390,12 @@ double const PI = 3.141592653589793f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Set source to buffers from pass 1
glActiveTexture(GL_TEXTURE6);
glBindTexture(GL_TEXTURE_2D, compositeColorTexture);
glActiveTexture(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, compositeDepthTexture);
// Enable additive blending
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
@@ -404,6 +410,10 @@ double const PI = 3.141592653589793f;
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Set source to buffers from pass 2
glActiveTexture(GL_TEXTURE6);
glBindTexture(GL_TEXTURE_2D, lightAccumulationBuffer);
// Enable backface culling
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
@@ -416,6 +426,12 @@ double const PI = 3.141592653589793f;
// Render the geometry
pScene->render(&m_camera, m_pContext, frustrumVolume, false, viewMatrix, cameraPosition, lightDirection, shadowmvpmatrix, shadowDepthTexture, m_cShadowBuffers, 3);
// Deactivate source buffer texture units
glActiveTexture(GL_TEXTURE6);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTexture(GL_TEXTURE7);
glBindTexture(GL_TEXTURE_2D, 0);
} else {
// ----====---- Opaque Geometry, Forward Rendering ----====----
@@ -588,11 +604,11 @@ double const PI = 3.141592653589793f;
// This needs to be done prior to linking.
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_VERTEX, "position");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUVA, "inputTextureCoordinate");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUVB, "shadowuv");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_VERTEX, "myVertex");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_NORMAL, "myNormal");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TANGENT, "myTangent");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUVA, "myUV");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUVB, "vertex_lightmap_uv");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_VERTEX, "vertex_position");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_NORMAL, "vertex_normal");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TANGENT, "vertex_tangent");
glBindAttribLocation(*programPointer, KRShader::KRENGINE_ATTRIB_TEXUVA, "vertex_uv");
// Link program.
if (![self linkProgram:*programPointer])
@@ -635,7 +651,7 @@ double const PI = 3.141592653589793f;
{
[self loadVertexShader:@"ShadowShader" fragmentShader:@"ShadowShader" forProgram:&m_shadowShaderProgram withOptions: NULL];
m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_shadowShaderProgram, "myShadowMVPMatrix1");
m_shadowUniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_shadowShaderProgram, "shadow_mvp1");
return TRUE;

View File

@@ -47,11 +47,31 @@ KRShader::KRShader(char *szKey, std::string options, const GLchar *szVertShaderS
glShaderSource(vertexShader, 2, vertSource, NULL);
glCompileShader(vertexShader);
// Report any compile issues to stderr
GLint logLength;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(vertexShader, logLength, &logLength, log);
fprintf(stderr, "KREngine - Failed to compile vertex shader: %s\nShader compile log:\n%s", szKey, log);
free(log);
}
// Create and compile vertex shader.
fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 2, fragSource, NULL);
glCompileShader(fragShader);
// Report any compile issues to stderr
glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
GLchar *log = (GLchar *)malloc(logLength);
glGetShaderInfoLog(vertexShader, logLength, &logLength, log);
fprintf(stderr, "KREngine - Failed to compile fragment shader: %s\nShader compile log:\n%s", szKey, log);
free(log);
}
// Attach vertex shader to program.
glAttachShader(m_iProgram, vertexShader);
@@ -62,15 +82,25 @@ KRShader::KRShader(char *szKey, std::string options, const GLchar *szVertShaderS
// This needs to be done prior to linking.
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_VERTEX, "position");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUVA, "inputTextureCoordinate");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUVB, "shadowuv");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_VERTEX, "myVertex");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_NORMAL, "myNormal");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TANGENT, "myTangent");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUVA, "myUV");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUVB, "vertex_lightmap_uv");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_VERTEX, "vertex_position");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_NORMAL, "vertex_normal");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TANGENT, "vertex_tangent");
glBindAttribLocation(m_iProgram, KRENGINE_ATTRIB_TEXUVA, "vertex_uv");
// Link program.
glLinkProgram(m_iProgram);
// Report any linking issues to stderr
glGetProgramiv(m_iProgram, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0)
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(m_iProgram, logLength, &logLength, log);
fprintf(stderr, "KREngine - Failed to link shader program: %s\nProgram link log:\n%s", szKey, log);
free(log);
}
// Get uniform locations
m_uniforms[KRENGINE_UNIFORM_MATERIAL_AMBIENT] = glGetUniformLocation(m_iProgram, "material_ambient");
m_uniforms[KRENGINE_UNIFORM_MATERIAL_DIFFUSE] = glGetUniformLocation(m_iProgram, "material_diffuse");
@@ -78,11 +108,11 @@ KRShader::KRShader(char *szKey, std::string options, const GLchar *szVertShaderS
m_uniforms[KRENGINE_UNIFORM_MATERIAL_ALPHA] = glGetUniformLocation(m_iProgram, "material_alpha");
m_uniforms[KRENGINE_UNIFORM_MATERIAL_SHININESS] = glGetUniformLocation(m_iProgram, "material_shininess");
m_uniforms[KRENGINE_UNIFORM_MVP] = glGetUniformLocation(m_iProgram, "myMVPMatrix");
m_uniforms[KRENGINE_UNIFORM_M2V] = glGetUniformLocation(m_iProgram, "model_to_view");
m_uniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_iProgram, "myShadowMVPMatrix1");
m_uniforms[KRENGINE_UNIFORM_SHADOWMVP2] = glGetUniformLocation(m_iProgram, "myShadowMVPMatrix2");
m_uniforms[KRENGINE_UNIFORM_SHADOWMVP3] = glGetUniformLocation(m_iProgram, "myShadowMVPMatrix3");
m_uniforms[KRENGINE_UNIFORM_MVP] = glGetUniformLocation(m_iProgram, "mvp_matrix");
m_uniforms[KRENGINE_UNIFORM_M2V] = glGetUniformLocation(m_iProgram, "model_to_view_matrix");
m_uniforms[KRENGINE_UNIFORM_SHADOWMVP1] = glGetUniformLocation(m_iProgram, "shadow_mvp1");
m_uniforms[KRENGINE_UNIFORM_SHADOWMVP2] = glGetUniformLocation(m_iProgram, "shadow_mvp2");
m_uniforms[KRENGINE_UNIFORM_SHADOWMVP3] = glGetUniformLocation(m_iProgram, "shadow_mvp3");
m_uniforms[KRENGINE_UNIFORM_LIGHTDIRECTION] = glGetUniformLocation(m_iProgram, "lightDirection");
m_uniforms[KRENGINE_UNIFORM_CAMERAPOS] = glGetUniformLocation(m_iProgram, "cameraPosition");
@@ -104,6 +134,9 @@ KRShader::KRShader(char *szKey, std::string options, const GLchar *szVertShaderS
m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE3] = glGetUniformLocation(m_iProgram, "shadowTexture3");
m_uniforms[KRENGINE_UNIFORM_GBUFFER_FRAME] = glGetUniformLocation(m_iProgram, "gbuffer_frame");
m_uniforms[KRENGINE_UNIFORM_GBUFFER_DEPTH] = glGetUniformLocation(m_iProgram, "gbuffer_depth");
} catch(...) {
if(vertexShader) {
glDeleteShader(vertexShader);
@@ -180,6 +213,9 @@ void KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix
glUniform1i(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE2], 4);
glUniform1i(m_uniforms[KRENGINE_UNIFORM_SHADOWTEXTURE3], 5);
glUniform1i(m_uniforms[KRENGINE_UNIFORM_GBUFFER_FRAME], 6);
glUniform1i(m_uniforms[KRENGINE_UNIFORM_GBUFFER_DEPTH], 7);
#if defined(DEBUG)
GLint logLength;
@@ -189,7 +225,7 @@ void KRShader::bind(KRCamera *pCamera, KRMat4 &matModelToView, KRMat4 &mvpMatrix
{
GLchar *log = (GLchar *)malloc(logLength);
glGetProgramInfoLog(m_iProgram, logLength, &logLength, log);
fprintf(stderr, "Program validate log:\n%s", log);
fprintf(stderr, "KREngine - Failed to validate shader program: %s\n Program validate log:\n%s", m_szKey, log);
free(log);
}
#endif

View File

@@ -89,6 +89,8 @@ public:
KRENGINE_UNIFORM_SHADOWTEXTURE1,
KRENGINE_UNIFORM_SHADOWTEXTURE2,
KRENGINE_UNIFORM_SHADOWTEXTURE3,
KRENGINE_UNIFORM_GBUFFER_FRAME,
KRENGINE_UNIFORM_GBUFFER_DEPTH,
KRENGINE_NUM_UNIFORMS
};

View File

@@ -25,172 +25,195 @@
// or implied, of Kearwood Gilbert.
//
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
uniform lowp float material_alpha;
#if ENABLE_PER_PIXEL == 1 || GBUFFER_PASS == 1
uniform mediump float material_shininess;
#if HAS_NORMAL_MAP == 1
uniform sampler2D normalTexture;
#else
varying mediump vec3 normal;
#endif
#if HAS_DIFFUSE_MAP == 1 || HAS_NORMAL_MAP == 1 || HAS_SPEC_MAP == 1
varying highp vec2 texCoord;
#endif
#if HAS_NORMAL_MAP_OFFSET == 1 || HAS_NORMAL_MAP_SCALE == 1
varying highp vec2 normal_uv;
#else
#define normal_uv texCoord
#endif
#else
#if HAS_DIFFUSE_MAP == 1
varying highp vec2 texCoord;
#endif
#endif
#if GBUFFER_PASS == 2 || GBUFFER_PASS == 3
uniform sampler2D gbuffer_frame;
uniform sampler2D gbuffer_depth;
varying mediump vec2 gbuffer_uv;
#endif
#if GBUFFER_PASS == 1
#if HAS_NORMAL_MAP == 1
varying highp mat3 tangent_to_view;
#if HAS_NORMAL_MAP == 1
varying highp mat3 tangent_to_view_matrix;
#else
uniform highp mat4 model_to_view_matrix;
#endif
#else
uniform highp mat4 model_to_view;
#endif
#endif
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
uniform lowp float material_alpha;
#if HAS_DIFFUSE_MAP == 1
uniform sampler2D diffuseTexture;
#endif
#if HAS_SPEC_MAP == 1
uniform sampler2D specularTexture;
#endif
#if ENABLE_PER_PIXEL == 1
uniform mediump float material_shininess;
#endif
#if SHADOW_QUALITY >= 1
uniform sampler2D shadowTexture1;
varying highp vec4 shadowMapCoord1;
#endif
#if HAS_DIFFUSE_MAP == 1
uniform sampler2D diffuseTexture;
#endif
#if HAS_LIGHT_MAP == 1
uniform sampler2D shadowTexture1;
varying mediump vec2 lightmap_uv;
#endif
#if HAS_SPEC_MAP == 1
uniform sampler2D specularTexture;
#endif
#if SHADOW_QUALITY >= 2
uniform sampler2D shadowTexture2;
varying highp vec4 shadowMapCoord2;
#endif
#if HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1
uniform sampler2D normalTexture;
#endif
#if SHADOW_QUALITY >= 3
uniform sampler2D shadowTexture3;
varying highp vec4 shadowMapCoord3;
#endif
#if SHADOW_QUALITY >= 1
uniform sampler2D shadowTexture1;
varying highp vec4 shadowMapCoord1;
#endif
#if ENABLE_PER_PIXEL == 1
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
#else
varying mediump float lamberFactor;
varying mediump float specularFactor;
#endif
#if HAS_LIGHT_MAP == 1
uniform sampler2D shadowTexture1;
varying mediump vec2 shadowCoord;
#endif
#if (HAS_SPEC_MAP_OFFSET == 1|| HAS_SPEC_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
varying mediump vec2 spec_uv;
#else
#define spec_uv texCoord
#endif
#if SHADOW_QUALITY >= 2
uniform sampler2D shadowTexture2;
varying highp vec4 shadowMapCoord2;
#endif
#if HAS_DIFFUSE_MAP_OFFSET == 1 || HAS_DIFFUSE_MAP_SCALE == 1
varying highp vec2 diffuse_uv;
#else
#define diffuse_uv texCoord
#endif
#if SHADOW_QUALITY >= 3
uniform sampler2D shadowTexture3;
varying highp vec4 shadowMapCoord3;
#endif
#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || HAS_SPEC_MAP == 1
varying highp vec2 texCoord;
#endif
#if HAS_NORMAL_MAP == 0 && ENABLE_PER_PIXEL == 1
varying mediump vec3 normal;
#endif
#if ENABLE_PER_PIXEL == 1
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
#else
varying mediump float lamberFactor;
varying mediump float specularFactor;
#endif
#if (HAS_NORMAL_MAP_OFFSET == 1 || HAS_NORMAL_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
varying highp vec2 normal_uv;
#else
#define normal_uv texCoord
#endif
#if (HAS_SPEC_MAP_OFFSET == 1|| HAS_SPEC_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
varying mediump vec2 spec_uv;
#else
#define spec_uv texCoord
#endif
#if HAS_DIFFUSE_MAP_OFFSET == 1 || HAS_DIFFUSE_MAP_SCALE == 1
varying highp vec2 diffuse_uv;
#else
#define diffuse_uv texCoord
#endif
void main()
{
#if HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1
#if GBUFFER_PASS == 2
lowp vec4 gbuffer_sample = texture2D(gbuffer_frame, gbuffer_uv);
mediump vec3 gbuffer_normal = normalize(2.0 * gbuffer_sample.rgb - 1.0);
mediump float gbuffer_specular_exponent = gbuffer_sample.a;
#endif
#if GBUFFER_PASS == 3
lowp vec4 gbuffer_sample = texture2D(gbuffer_frame, gbuffer_uv);
lowp vec3 gbuffer_lamber_factor = gbuffer_sample.rgb;
lowp float gbuffer_specular_factor = gbuffer_sample.a;
#endif
#if GBUFFER_PASS == 1
#if HAS_NORMAL_MAP == 1
// lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize
mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,normal_uv).rgb - 1.0);
#endif
mediump vec3 view_space_normal = tangent_to_view_matrix * normal;
#else
mediump vec3 view_space_normal = vec3(model_to_view_matrix * vec4(normal, 1.0));
#endif
gl_FragColor = vec4(view_space_normal * 0.5 + 0.5, material_shininess / 100.0);
#else
#if HAS_DIFFUSE_MAP == 1
mediump vec4 diffuseMaterial = vec4(vec3(texture2D(diffuseTexture, diffuse_uv)), material_alpha);
#else
mediump vec4 diffuseMaterial = vec4(vec3(1.0), material_alpha);
#endif
#if ENABLE_PER_PIXEL == 1
#if ENABLE_PER_PIXEL == 1
#if HAS_NORMAL_MAP == 1
// lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize
mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,normal_uv).rgb - 1.0);
#endif
mediump float lamberFactor = max(0.0,dot(lightVec, normal));
mediump float specularFactor = 0.0;
if(material_shininess > 0.0) {
specularFactor = max(0.0,pow(dot(halfVec,normal), material_shininess));
}
#endif
#if HAS_DIFFUSE_MAP == 1
mediump vec4 diffuseMaterial = vec4(vec3(texture2D(diffuseTexture, diffuse_uv)), material_alpha);
#else
mediump vec4 diffuseMaterial = vec4(vec3(1.0), material_alpha);
#endif
#if SHADOW_QUALITY == 1
#if SHADOW_QUALITY == 1
highp float shadowMapDepth = 1.0;
highp float vertexShadowDepth = 1.0;
highp vec2 shadowMapPos = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).st;
if(shadowMapCoord1.x >= -1.0 && shadowMapCoord1.x <= 1.0 && shadowMapCoord1.y >= -1.0 && shadowMapCoord1.y <= 1.0 && shadowMapCoord1.z >= 0.0 && shadowMapCoord1.z <= 1.0) {
#if DEBUG_PSSM == 1
#if DEBUG_PSSM == 1
diffuseMaterial = diffuseMaterial * vec4(0.75, 0.75, 0.5, 1.0) + vec4(0.0, 0.0, 0.5, 0.0);
#endif
#endif
shadowMapDepth = texture2D(shadowTexture1, shadowMapPos).z;
vertexShadowDepth = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).z;
}
#endif
#endif
#if SHADOW_QUALITY >= 2
#if SHADOW_QUALITY >= 2
highp float shadowMapDepth = 1.0;
highp float vertexShadowDepth = 1.0;
if(shadowMapCoord1.x >= -1.0 && shadowMapCoord1.x <= 1.0 && shadowMapCoord1.y >= -1.0 && shadowMapCoord1.y <= 1.0 && shadowMapCoord1.z >= 0.0 && shadowMapCoord1.z <= 1.0) {
#if DEBUG_PSSM == 1
#if DEBUG_PSSM == 1
diffuseMaterial = diffuseMaterial * vec4(0.75, 0.75, 0.5, 1.0) + vec4(0.0, 0.0, 0.5, 0.0);
#endif
#endif
highp vec2 shadowMapPos = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).st;
shadowMapDepth = texture2D(shadowTexture1, shadowMapPos).z;
vertexShadowDepth = ((shadowMapCoord1 / shadowMapCoord1.w + 1.0) / 2.0).z;
}
else if(shadowMapCoord2.s >= -1.0 && shadowMapCoord2.s <= 1.0 && shadowMapCoord2.t >= -1.0 && shadowMapCoord2.t <= 1.0 && shadowMapCoord2.z >= 0.0 && shadowMapCoord2.z <= 1.0) {
#if DEBUG_PSSM == 1
} else if(shadowMapCoord2.s >= -1.0 && shadowMapCoord2.s <= 1.0 && shadowMapCoord2.t >= -1.0 && shadowMapCoord2.t <= 1.0 && shadowMapCoord2.z >= 0.0 && shadowMapCoord2.z <= 1.0) {
#if DEBUG_PSSM == 1
diffuseMaterial = diffuseMaterial * vec4(0.75, 0.50, 0.75, 1.0) + vec4(0.0, 0.5, 0.0, 0.0);
#endif
#endif
highp vec2 shadowMapPos = ((shadowMapCoord2 / shadowMapCoord2.w + 1.0) / 2.0).st;
shadowMapDepth = texture2D(shadowTexture2, shadowMapPos).z;
vertexShadowDepth = ((shadowMapCoord2 / shadowMapCoord2.w + 1.0) / 2.0).z;
}
#if SHADOW_QUALITY >= 3
#if SHADOW_QUALITY >= 3
else if(shadowMapCoord3.s >= -1.0 && shadowMapCoord3.s <= 1.0 && shadowMapCoord3.t >= -1.0 && shadowMapCoord3.t <= 1.0 && shadowMapCoord3.z >= 0.0 && shadowMapCoord3.z <= 1.0) {
#if DEBUG_PSSM == 1
#if DEBUG_PSSM == 1
diffuseMaterial = diffuseMaterial * vec4(0.50, 0.75, 0.75, 1.0) + vec4(0.5, 0.0, 0.0, 0.0);
#endif
#endif
highp vec2 shadowMapPos = ((shadowMapCoord3 / shadowMapCoord3.w + 1.0) / 2.0).st;
shadowMapDepth = texture2D(shadowTexture3, shadowMapPos).z;
vertexShadowDepth = ((shadowMapCoord3 / shadowMapCoord3.w + 1.0) / 2.0).z;
}
#endif
#endif
#endif
#endif
#if SHADOW_QUALITY >= 1
#if SHADOW_QUALITY >= 1
if(vertexShadowDepth >= shadowMapDepth && shadowMapDepth < 1.0) {
lamberFactor = 0.0;
specularFactor = 0.0;
}
#endif
#if GBUFFER_PASS == 0 || GBUFFER_PASS == 2
#endif
#endif
#if ENABLE_AMBIENT
// -------------------- Add ambient light and alpha component --------------------
@@ -207,7 +230,7 @@ void main()
#if ENABLE_SPECULAR
// -------------------- Add specular light --------------------
#if HAS_SPEC_MAP == 1
#if HAS_SPEC_MAP == 1 && ENABLE_PER_PIXEL == 1
gl_FragColor += vec4(material_specular * vec3(texture2D(specularTexture, spec_uv)) * specularFactor, 0.0);
#else
gl_FragColor += vec4(material_specular * specularFactor, 0.0);
@@ -219,19 +242,21 @@ void main()
// -------------------- Multiply light map --------------------
#if HAS_LIGHT_MAP
mediump vec3 shadowColor = vec3(texture2D(shadowTexture1, shadowCoord));
gl_FragColor = vec4(gl_FragColor.r * shadowColor.r, gl_FragColor.g * shadowColor.g, gl_FragColor.b * shadowColor.b, 1.0);
mediump vec3 lightMapColor = vec3(texture2D(shadowTexture1, lightmap_uv));
gl_FragColor = vec4(gl_FragColor.r * lightMapColor.r, gl_FragColor.g * lightMapColor.g, gl_FragColor.b * lightMapColor.b, 1.0);
#endif
#endif
#if GBUFFER_PASS == 2
// mediump vec3 gbuffer_normal = normalize(2.0 * gbuffer_sample.rgb - 1.0);
// mediump float gbuffer_specular_exponent = gbuffer_sample.a;
#endif
#if GBUFFER_PASS == 1
#if HAS_NORMAL_MAP == 1
mediump vec3 view_space_normal = tangent_to_view * normal;
#else
mediump vec3 view_space_normal = vec3(model_to_view * vec4(normal, 1.0));
#endif
gl_FragColor = vec4(view_space_normal * 0.5 + 0.5, material_shininess / 100.0);
#if GBUFFER_PASS == 3
// lowp vec3 gbuffer_lamber_factor = gbuffer_sample.rgb;
// lowp float gbuffer_specular_factor = gbuffer_sample.a;
gl_FragColor = vec4(vec3(gbuffer_lamber_factor), 1.0);
gl_FragColor = vec4(0.0, 0.0, 1.0, 1.0);
#endif
}

View File

@@ -29,204 +29,210 @@
// or implied, of Kearwood Gilbert.
//
attribute highp vec3 myVertex, myNormal;
attribute highp vec3 myTangent;
attribute mediump vec2 myUV;
attribute mediump vec2 shadowuv;
uniform highp mat4 myMVPMatrix, myShadowMVPMatrix1,myShadowMVPMatrix2,myShadowMVPMatrix3; // mvpmatrix is the result of multiplying the model, view, and projection matrices
attribute highp vec3 vertex_position, vertex_normal, vertex_tangent;
attribute mediump vec2 vertex_uv;
uniform highp mat4 mvp_matrix; // mvp_matrix is the result of multiplying the model, view, and projection matrices
#if GBUFFER_PASS == 1 && HAS_NORMAL_MAP == 1
uniform highp mat4 model_to_view;
varying highp mat3 tangent_to_view;
#if ENABLE_PER_PIXEL == 1 || GBUFFER_PASS == 1
#if HAS_DIFFUSE_MAP == 1 || HAS_NORMAL_MAP == 1 || HAS_SPEC_MAP == 1
varying highp vec2 texCoord;
#endif
#if HAS_NORMAL_MAP == 1
#if HAS_NORMAL_MAP_SCALE == 1
uniform highp vec2 normalTexture_Scale;
#endif
#endif
#if HAS_NORMAL_MAP_OFFSET == 1
uniform highp vec2 normalTexture_Offset;
#endif
// uniform lowp vec3 material_ambient, material_diffuse, material_specular;
uniform highp vec3 lightDirection; // Must be normalized before entering shader
uniform highp vec3 cameraPosition;
#if ENABLE_PER_PIXEL == 0
uniform mediump float material_shininess;
#endif
#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || HAS_SPEC_MAP == 1
varying highp vec2 texCoord;
#endif
#if HAS_LIGHT_MAP == 1
varying mediump vec2 shadowCoord;
#endif
#if ENABLE_PER_PIXEL == 1
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
#if HAS_NORMAL_MAP_OFFSET == 1 || HAS_NORMAL_MAP_SCALE == 1
varying highp vec2 normal_uv;
#endif
#else
varying mediump vec3 normal;
#endif
#else
varying mediump float lamberFactor;
varying mediump float specularFactor;
uniform mediump float material_shininess;
#if HAS_DIFFUSE_MAP == 1
varying highp vec2 texCoord;
#endif
#endif
#if HAS_DIFFUSE_MAP_SCALE == 1
uniform highp vec2 diffuseTexture_Scale;
#if GBUFFER_PASS == 2 || GBUFFER_PASS == 3
varying mediump vec2 gbuffer_uv;
#endif
#if HAS_NORMAL_MAP_SCALE == 1
uniform highp vec2 normalTexture_Scale;
#if GBUFFER_PASS == 1
#if HAS_NORMAL_MAP == 1
uniform highp mat4 model_to_view_matrix;
varying highp mat3 tangent_to_view_matrix;
#endif
#else
uniform highp vec3 lightDirection; // Must be normalized before entering shader
uniform highp vec3 cameraPosition;
#if HAS_LIGHT_MAP == 1
attribute mediump vec2 vertex_lightmap_uv;
varying mediump vec2 lightmap_uv;
#endif
#if ENABLE_PER_PIXEL == 1
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
#if HAS_SPEC_MAP_OFFSET == 1 || HAS_SPEC_MAP_SCALE == 1
varying highp vec2 spec_uv;
#endif
#if HAS_SPEC_MAP_SCALE == 1
uniform highp vec2 specularTexture_Scale;
#endif
#if HAS_SPEC_MAP_OFFSET == 1
uniform highp vec2 specularTexture_Offset;
#endif
#if SHADOW_QUALITY >= 1
uniform highp mat4 shadow_mvp1;
varying highp vec4 shadowMapCoord1;
#endif
#if SHADOW_QUALITY >= 2
uniform highp mat4 shadow_mvp2;
varying highp vec4 shadowMapCoord2;
#endif
#if SHADOW_QUALITY >= 3
uniform highp mat4 shadow_mvp3;
varying highp vec4 shadowMapCoord3;
#endif
#else
varying mediump float lamberFactor;
varying mediump float specularFactor;
#endif
#if HAS_DIFFUSE_MAP_SCALE == 1
uniform highp vec2 diffuseTexture_Scale;
#endif
#if HAS_DIFFUSE_MAP_OFFSET == 1
uniform highp vec2 diffuseTexture_Offset;
#endif
#if HAS_DIFFUSE_MAP_OFFSET == 1 || HAS_DIFFUSE_MAP_SCALE == 1
varying highp vec2 diffuse_uv;
#endif
#endif
#if HAS_SPEC_MAP_SCALE == 1
uniform highp vec2 specularTexture_Scale;
#endif
#if HAS_NORMAL_MAP_OFFSET == 1
uniform highp vec2 normalTexture_Offset;
#endif
#if HAS_SPEC_MAP_OFFSET == 1
uniform highp vec2 specularTexture_Offset;
#endif
#if HAS_DIFFUSE_MAP_OFFSET == 1
uniform highp vec2 diffuseTexture_Offset;
#endif
#if HAS_NORMAL_MAP == 0 && ENABLE_PER_PIXEL == 1
varying mediump vec3 normal;
#endif
#if SHADOW_QUALITY >= 1
varying highp vec4 shadowMapCoord1;
#endif
#if SHADOW_QUALITY >= 2
varying highp vec4 shadowMapCoord2;
#endif
#if SHADOW_QUALITY >= 3
varying highp vec4 shadowMapCoord3;
#endif
#if (HAS_NORMAL_MAP_OFFSET == 1 || HAS_NORMAL_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
varying highp vec2 normal_uv;
#endif
#if (HAS_SPEC_MAP_OFFSET == 1|| HAS_SPEC_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
varying highp vec2 spec_uv;
#endif
#if HAS_DIFFUSE_MAP_OFFSET == 1 || HAS_DIFFUSE_MAP_SCALE == 1
varying highp vec2 diffuse_uv;
#endif
void main()
{
// Transform position
gl_Position = myMVPMatrix * vec4(myVertex,1.0);
gl_Position = mvp_matrix * vec4(vertex_position,1.0);
#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || HAS_SPEC_MAP == 1
// Pass UV co-ordinates
texCoord = myUV.st;
#if GBUFFER_PASS == 2 || GBUFFER_PASS == 3
gbuffer_uv = gl_Position.xy;
#endif
#if HAS_DIFFUSE_MAP == 1 || (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || (HAS_SPEC_MAP == 1 && ENABLE_PER_PIXEL == 1)
// Pass UV co-ordinates
texCoord = vertex_uv.st;
#endif
// Scaled and translated normal map UV's
#if (HAS_NORMAL_MAP_OFFSET == 1 || HAS_NORMAL_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
// Scaled and translated normal map UV's
#if (HAS_NORMAL_MAP_OFFSET == 1 || HAS_NORMAL_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
normal_uv = texCoord;
#if HAS_NORMAL_MAP_OFFSET == 1
#if HAS_NORMAL_MAP_OFFSET == 1
normal_uv + normalTexture_Offset;
#endif
#endif
#if HAS_NORMAL_MAP_SCALE == 1
#if HAS_NORMAL_MAP_SCALE == 1
normal_uv *= normalTexture_Scale;
#endif
#endif
#endif
#endif
#if GBUFFER_PASS == 1
#if HAS_NORMAL_MAP == 1
mediump vec3 a_bitangent = cross(vertex_normal, vertex_tangent);
tangent_to_view_matrix[0] = vec3(model_to_view_matrix * vec4(vertex_tangent, 1.0));
tangent_to_view_matrix[1] = vec3(model_to_view_matrix * vec4(a_bitangent, 1.0));
tangent_to_view_matrix[2] = vec3(model_to_view_matrix * vec4(vertex_normal, 1.0));
#else
normal = vertex_normal;
#endif
#else
#if HAS_LIGHT_MAP == 1
// Pass shadow UV co-ordinates
lightmap_uv = vertex_lightmap_uv.st;
#endif
// Scaled and translated diffuse map UV's
#if HAS_DIFFUSE_MAP_OFFSET == 1 || HAS_DIFFUSE_MAP_SCALE == 1
// Scaled and translated diffuse map UV's
#if HAS_DIFFUSE_MAP_OFFSET == 1 || HAS_DIFFUSE_MAP_SCALE == 1
diffuse_uv = texCoord;
#if HAS_DIFFUSE_MAP_OFFSET == 1
#if HAS_DIFFUSE_MAP_OFFSET == 1
diffuse_uv + diffuseTexture_Offset;
#endif
#endif
#if HAS_DIFFUSE_MAP_SCALE == 1
#if HAS_DIFFUSE_MAP_SCALE == 1
diffuse_uv *= diffuseTexture_Scale;
#endif
#endif
#endif
#endif
// Scaled and translated specular map UV's
#if (HAS_SPEC_MAP_OFFSET == 1 || HAS_SPEC_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
#if ENABLE_PER_PIXEL == 1
// Scaled and translated specular map UV's
#if (HAS_SPEC_MAP_OFFSET == 1 || HAS_SPEC_MAP_SCALE == 1) && ENABLE_PER_PIXEL == 1
spec_uv = texCoord;
#if HAS_SPEC_MAP_OFFSET == 1
#if HAS_SPEC_MAP_OFFSET == 1
spec_uv + specularTexture_Offset;
#endif
#endif
#if HAS_SPEC_MAP_SCALE == 1
#if HAS_SPEC_MAP_SCALE == 1
spec_uv *= specularTexture_Scale;
#endif
#endif
#endif
#endif
#if SHADOW_QUALITY >= 1
shadowMapCoord1 = shadow_mvp1 * vec4(vertex_position,1.0);
#endif
#if HAS_LIGHT_MAP == 1
// Pass shadow UV co-ordinates
shadowCoord = shadowuv.st;
#endif
#if SHADOW_QUALITY >= 2
shadowMapCoord2 = shadow_mvp2 * vec4(vertex_position,1.0);
#endif
#if SHADOW_QUALITY >= 1
shadowMapCoord1 = myShadowMVPMatrix1 * vec4(myVertex,1.0);
#endif
#if SHADOW_QUALITY >= 2
shadowMapCoord2 = myShadowMVPMatrix2 * vec4(myVertex,1.0);
#endif
#if SHADOW_QUALITY >= 3
shadowMapCoord3 = myShadowMVPMatrix3 * vec4(myVertex,1.0);
#endif
#if (HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1) || GBUFFER_PASS == 1
mediump vec3 a_bitangent = cross(myNormal, myTangent);
#endif
#if SHADOW_QUALITY >= 3
shadowMapCoord3 = shadow_mvp3 * vec4(vertex_position,1.0);
#endif
// ----------- Directional Light (Sun) -----------
#if HAS_NORMAL_MAP == 1 && ENABLE_PER_PIXEL == 1
#if HAS_NORMAL_MAP == 1
// ----- Calculate per-pixel lighting in tangent space, for normal mapping ------
mediump vec3 eyeVec = normalize(cameraPosition - myVertex);
mediump vec3 a_bitangent = cross(vertex_normal, vertex_tangent);
mediump vec3 eyeVec = normalize(cameraPosition - vertex_position);
lightVec = normalize(vec3(dot(lightDirection, myTangent), dot(lightDirection, a_bitangent), dot(lightDirection, myNormal)));
halfVec = normalize(vec3(dot(eyeVec, myTangent), dot(eyeVec, a_bitangent), dot(eyeVec, myNormal)));
lightVec = normalize(vec3(dot(lightDirection, vertex_tangent), dot(lightDirection, a_bitangent), dot(lightDirection, vertex_normal)));
halfVec = normalize(vec3(dot(eyeVec, vertex_tangent), dot(eyeVec, a_bitangent), dot(eyeVec, vertex_normal)));
halfVec = normalize(halfVec + lightVec); // Normalizing anyways, no need to divide by 2
#else
#if ENABLE_PER_PIXEL == 1
#else
// ------ Calculate per-pixel lighting without normal mapping ------
normal = myNormal;
normal = vertex_normal;
lightVec = lightDirection;
halfVec = normalize((normalize(cameraPosition - myVertex) + lightVec)); // Normalizing anyways, no need to divide by 2
#else
halfVec = normalize((normalize(cameraPosition - vertex_position) + lightVec)); // Normalizing anyways, no need to divide by 2
#endif
#else
// ------ Calculate per-vertex lighting ------
mediump vec3 halfVec = normalize((normalize(cameraPosition - myVertex) + lightDirection)); // Normalizing anyways, no need to divide by 2
lamberFactor = max(0.0,dot(lightDirection, myNormal));
specularFactor = max(0.0,pow(dot(halfVec,myNormal), material_shininess));
#endif
#endif
#if GBUFFER_PASS == 1 && HAS_NORMAL_MAP == 1
tangent_to_view[0] = vec3(model_to_view * vec4(myTangent, 1.0));
tangent_to_view[1] = vec3(model_to_view * vec4(a_bitangent, 1.0));
tangent_to_view[2] = vec3(model_to_view * vec4(myNormal, 1.0));
#endif
mediump vec3 halfVec = normalize((normalize(cameraPosition - vertex_position) + lightDirection)); // Normalizing anyways, no need to divide by 2
lamberFactor = max(0.0,dot(lightDirection, vertex_normal));
specularFactor = max(0.0,pow(dot(halfVec,vertex_normal), material_shininess));
#endif
#endif
}

View File

@@ -28,12 +28,8 @@
#define SHADOW_BIAS 0.01
attribute highp vec3 myVertex;
/*
attribute mediump vec2 myUV;
varying mediump vec2 texCoord;
*/
uniform highp mat4 myShadowMVPMatrix1; // Shadowmvpmatrix is the result of multiplying the model, view, and projection matrices
attribute highp vec3 vertex_position;
uniform highp mat4 shadow_mvp1; // Shadowmvpmatrix is the result of multiplying the model, view, and projection matrices
@@ -41,13 +37,13 @@ void main()
{
// Transform position
/*
position = myShadowMVPMatrix1 * vec4(myVertex,1.0);
position = shadow_mvp1 * vec4(vertex_position,1.0);
*/
gl_Position = myShadowMVPMatrix1 * vec4(myVertex,1.0);
gl_Position = shadow_mvp1 * vec4(vertex_position,1.0);
gl_Position.z += SHADOW_BIAS;
/*
// Pass UV co-ordinates
texCoord = myUV.st;
texCoord = vertex_uv.st;
*/
}