diff --git a/KREngine/kraken/KRAmbientZone.cpp b/KREngine/kraken/KRAmbientZone.cpp index 0d253dc..5770fe0 100644 --- a/KREngine/kraken/KRAmbientZone.cpp +++ b/KREngine/kraken/KRAmbientZone.cpp @@ -120,7 +120,7 @@ void KRAmbientZone::render(KRCamera *pCamera, std::vector &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)); } } } diff --git a/KREngine/kraken/KRAudioSource.cpp b/KREngine/kraken/KRAudioSource.cpp index 4e07b87..ee4df3c 100644 --- a/KREngine/kraken/KRAudioSource.cpp +++ b/KREngine/kraken/KRAudioSource.cpp @@ -231,7 +231,7 @@ void KRAudioSource::render(KRCamera *pCamera, std::vector &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)); } } } diff --git a/KREngine/kraken/KRBone.cpp b/KREngine/kraken/KRBone.cpp index b443c1d..921273d 100644 --- a/KREngine/kraken/KRBone.cpp +++ b/KREngine/kraken/KRBone.cpp @@ -70,7 +70,7 @@ void KRBone::render(KRCamera *pCamera, std::vector &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)); } } } diff --git a/KREngine/kraken/KRCamera.cpp b/KREngine/kraken/KRCamera.cpp index 479ad2a..919fb13 100644 --- a/KREngine/kraken/KRCamera.cpp +++ b/KREngine/kraken/KRCamera.cpp @@ -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(), std::vector(), std::vector(), 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(), std::vector(), std::vector(), 0, KRNode::RENDER_PASS_FORWARD_TRANSPARENT); diff --git a/KREngine/kraken/KRCollider.cpp b/KREngine/kraken/KRCollider.cpp index ef58845..b7dc674 100644 --- a/KREngine/kraken/KRCollider.cpp +++ b/KREngine/kraken/KRCollider.cpp @@ -193,7 +193,7 @@ void KRCollider::render(KRCamera *pCamera, std::vector &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; diff --git a/KREngine/kraken/KRPointLight.cpp b/KREngine/kraken/KRPointLight.cpp index 4275f9d..0ebd33c 100644 --- a/KREngine/kraken/KRPointLight.cpp +++ b/KREngine/kraken/KRPointLight.cpp @@ -138,7 +138,7 @@ void KRPointLight::render(KRCamera *pCamera, std::vector &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)); } } } diff --git a/KREngine/kraken/KRReverbZone.cpp b/KREngine/kraken/KRReverbZone.cpp index 45e56a2..a9f1ab5 100644 --- a/KREngine/kraken/KRReverbZone.cpp +++ b/KREngine/kraken/KRReverbZone.cpp @@ -119,7 +119,7 @@ void KRReverbZone::render(KRCamera *pCamera, std::vector &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)); } } } diff --git a/KREngine/kraken/KRScene.cpp b/KREngine/kraken/KRScene.cpp index d453b8f..14439ad 100644 --- a/KREngine/kraken/KRScene.cpp +++ b/KREngine/kraken/KRScene.cpp @@ -320,7 +320,7 @@ void KRScene::render(KROctreeNode *pOctreeNode, unordered_map &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 } diff --git a/KREngine/kraken_standard_assets_ios/Shaders/ObjectShader.fsh b/KREngine/kraken_standard_assets_ios/Shaders/ObjectShader.fsh index b6cdef4..0817637 100644 --- a/KREngine/kraken_standard_assets_ios/Shaders/ObjectShader.fsh +++ b/KREngine/kraken_standard_assets_ios/Shaders/ObjectShader.fsh @@ -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 }