Corrected lighting model for transparent surfaces. Reflections and specular are no longer masked by the alpha channel.

This commit is contained in:
2013-05-02 13:32:36 -07:00
parent 12842a0184
commit 30f39ea19f
9 changed files with 61 additions and 45 deletions

View File

@@ -120,7 +120,7 @@ void KRAmbientZone::render(KRCamera *pCamera, std::vector<KRPointLight *> &point
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
}
}

View File

@@ -231,7 +231,7 @@ void KRAudioSource::render(KRCamera *pCamera, std::vector<KRPointLight *> &point
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
}
}

View File

@@ -70,7 +70,7 @@ void KRBone::render(KRCamera *pCamera, std::vector<KRPointLight *> &point_lights
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
}
}

View File

@@ -312,7 +312,7 @@ void KRCamera::renderFrame(float deltaTime, GLint renderBufferWidth, GLint rende
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
// Render all transparent geometry
scene.render(this, m_viewport.getVisibleBounds(), m_viewport, KRNode::RENDER_PASS_FORWARD_TRANSPARENT, false);
@@ -846,7 +846,7 @@ void KRCamera::renderPost()
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
KRShader *fontShader = m_pContext->getShaderManager()->getShader("debug_font", this, std::vector<KRPointLight *>(), std::vector<KRDirectionalLight *>(), std::vector<KRSpotLight *>(), 0, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, KRNode::RENDER_PASS_FORWARD_TRANSPARENT);
getContext().getShaderManager()->selectShader(*this, fontShader, m_viewport, KRMat4(), std::vector<KRPointLight *>(), std::vector<KRDirectionalLight *>(), std::vector<KRSpotLight *>(), 0, KRNode::RENDER_PASS_FORWARD_TRANSPARENT);

View File

@@ -193,7 +193,7 @@ void KRCollider::render(KRCamera *pCamera, std::vector<KRPointLight *> &point_li
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
GL_POP_GROUP_MARKER;

View File

@@ -138,7 +138,7 @@ void KRPointLight::render(KRCamera *pCamera, std::vector<KRPointLight *> &point_
if(bVisualize) {
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
}
}

View File

@@ -119,7 +119,7 @@ void KRReverbZone::render(KRCamera *pCamera, std::vector<KRPointLight *> &point_
// Enable alpha blending
GLDEBUG(glEnable(GL_BLEND));
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
}
}
}

View File

@@ -320,7 +320,7 @@ void KRScene::render(KROctreeNode *pOctreeNode, unordered_map<KRAABB, int> &visi
if(renderPass != KRNode::RENDER_PASS_FORWARD_TRANSPARENT && renderPass != KRNode::RENDER_PASS_ADDITIVE_PARTICLES && renderPass != KRNode::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE) {
GLDEBUG(glDisable(GL_BLEND));
} else if(renderPass == KRNode::RENDER_PASS_FORWARD_TRANSPARENT) {
GLDEBUG(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA));
} else {
GLDEBUG(glBlendFunc(GL_ONE, GL_ONE)); // RENDER_PASS_FORWARD_TRANSPARENT and RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE
}

View File

@@ -322,9 +322,35 @@ void main()
// -------------------- Add diffuse light --------------------
gl_FragColor += diffuseMaterial * vec4(material_diffuse, 1.0) * vec4(vec3(lamberFactor), 1.0);
#endif
// -------------------- Switch to Pre-multiplied Alpha and Apply material_alpha --------------------
#if ALPHA_BLEND == 1
gl_FragColor.a = gl_FragColor.a * material_alpha; // Apply material_alpha
gl_FragColor.rgb *= gl_FragColor.a; // Switch to pre-multiplied alpha
#endif
// -------------------- Add specular light --------------------
// Additive, not masked against diffuse alpha
#if ENABLE_SPECULAR == 1
#if HAS_SPEC_MAP == 1 && ENABLE_PER_PIXEL == 1
gl_FragColor.rgb += material_specular * vec3(texture2D(specularTexture, spec_uv)) * specularFactor;
#else
gl_FragColor.rgb += material_specular * specularFactor;
#endif
#endif
// -------------------- Multiply light map --------------------
#if HAS_LIGHT_MAP == 1
mediump vec3 lightMapColor = vec3(texture2D(lightmapTexture, lightmap_uv));
//gl_FragColor = vec4(gl_FragColor.r * lightMapColor.r, gl_FragColor.g * lightMapColor.g, gl_FragColor.b * lightMapColor.b, gl_FragColor.a);
gl_FragColor.rgb *= lightMapColor;
#endif
// -------------------- Add reflected light --------------------
#if HAS_REFLECTION_CUBE_MAP == 1
// -------------------- Add reflected light --------------------
// Reflected light is additive and not modulated by the light map
#if HAS_NORMAL_MAP == 1
// Calculate reflection vector as I - 2.0 * dot(N, I) * N
mediump vec3 incidenceVec = -normalize(eyeVec);
@@ -338,43 +364,33 @@ void main()
#endif
#endif
// -------------------- Add specular light --------------------
#if ENABLE_SPECULAR == 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);
// -------------------- Apply Fog --------------------
#if FOG_TYPE == 1 || FOG_TYPE == 2 || FOG_TYPE == 3
#if FOG_TYPE == 1
// Linear fog
lowp float fog_alpha = clamp((fog_far - gl_FragCoord.z / gl_FragCoord.w) * fog_scale, 0.0, 1.0);
#endif
#if FOG_TYPE == 2
// Exponential fog
mediump float fog_z = gl_FragCoord.z / gl_FragCoord.w - fog_near;
lowp float fog_alpha = clamp(exp2(fog_density_premultiplied_exponential * fog_z), 0.0, 1.0);
#endif
#if FOG_TYPE == 3
// Exponential squared fog
mediump float fog_z = max(gl_FragCoord.z / gl_FragCoord.w - fog_near, 0.0);
lowp float fog_alpha = clamp(exp2(fog_density_premultiplied_squared * fog_z * fog_z), 0.0, 1.0);
#endif
#if ALPHA_BLEND == 1
gl_FragColor.rgb = mix(fog_color.rgb * gl_FragColor.a, gl_FragColor.rgb, fog_alpha);
#else
gl_FragColor.rgb = mix(fog_color.rgb, gl_FragColor.rgb, fog_alpha);
#endif
#endif
#if ALPHA_BLEND == 1
gl_FragColor.a = gl_FragColor.a * material_alpha;
#endif
// -------------------- Multiply light map --------------------
#if HAS_LIGHT_MAP == 1
mediump vec3 lightMapColor = vec3(texture2D(lightmapTexture, lightmap_uv));
gl_FragColor = vec4(gl_FragColor.r * lightMapColor.r, gl_FragColor.g * lightMapColor.g, gl_FragColor.b * lightMapColor.b, gl_FragColor.a);
#endif
// -------------------- Apply Fog --------------------
#if FOG_TYPE == 1
// Linear fog
gl_FragColor.rgb = mix(fog_color.rgb, gl_FragColor.rgb, clamp((fog_far - gl_FragCoord.z / gl_FragCoord.w) * fog_scale, 0.0, 1.0));
#endif
#if FOG_TYPE == 2
// Exponential fog
mediump float fog_z = gl_FragCoord.z / gl_FragCoord.w - fog_near;
gl_FragColor.rgb = mix(fog_color.rgb, gl_FragColor.rgb, clamp(exp2(fog_density_premultiplied_exponential * fog_z), 0.0, 1.0));
#endif
#if FOG_TYPE == 3
// Exponential squared fog
mediump float fog_z = max(gl_FragCoord.z / gl_FragCoord.w - fog_near, 0.0);
gl_FragColor.rgb = mix(fog_color.rgb, gl_FragColor.rgb, clamp(exp2(fog_density_premultiplied_squared * fog_z * fog_z), 0.0, 1.0));
#endif
#endif
}