From 6c607dcb015ae0cf0f9f274622344b869e3af140 Mon Sep 17 00:00:00 2001 From: Jakob Date: Wed, 11 Oct 2023 10:45:34 +0200 Subject: [PATCH] Fixed ParticleSystem depth testing, needs log depth value --- OpenGL_Flightsim/shaders/mesh.frag | 10 ++++++++-- OpenGL_Flightsim/shaders/particle.frag | 14 +++++++++++++- OpenGL_Flightsim/shaders/particle.vert | 4 +++- OpenGL_Flightsim/shaders/terrain.frag | 9 ++++----- OpenGL_Flightsim/src/gfx/particles.cpp | 13 +++++++------ 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/OpenGL_Flightsim/shaders/mesh.frag b/OpenGL_Flightsim/shaders/mesh.frag index 5f19aba..4b634d1 100644 --- a/OpenGL_Flightsim/shaders/mesh.frag +++ b/OpenGL_Flightsim/shaders/mesh.frag @@ -91,9 +91,11 @@ void main() { return; } - float farPlane = 150000.0; - float coeff = 2.0 / (log2(farPlane + 1.0) / 0.693); +#if 1 + float far = 150000.0; + float coeff = 2.0 / (log2(far + 1.0) / 0.693); gl_FragDepth = log2(FragDepth) * coeff * 0.5; +#endif vec3 texColor = texture(u_Texture_01, TexCoords).rgb; vec3 reflectedColor = texture(u_EnvMap, ReflectedVector).rgb; @@ -101,4 +103,8 @@ void main() { vec3 color = phongLighting(mix(texColor, reflectedColor, u_Shininess), u_LightDir, u_LightColor); gl_FragColor = vec4(color, u_Opacity); + + + //gl_FragColor = vec4(vec3(gl_FragCoord.z), 1.0); + } \ No newline at end of file diff --git a/OpenGL_Flightsim/shaders/particle.frag b/OpenGL_Flightsim/shaders/particle.frag index d5e2310..2ab6f74 100644 --- a/OpenGL_Flightsim/shaders/particle.frag +++ b/OpenGL_Flightsim/shaders/particle.frag @@ -5,9 +5,21 @@ out vec4 FragColor; in vec2 TexCoords; in vec4 Color; +in float FragDepth; void main() { - //FragColor = texture(u_Texture, TexCoords) * vec4(u_Color, 1.0f); +#if 0 + FragColor = texture(u_Texture, TexCoords) * Color; + #else FragColor = Color; +#endif + +#if 1 + // log depth buffer + float far = 150000.0; + float coeff = 2.0 / (log2(far + 1.0) / 0.693); + gl_FragDepth = log2(FragDepth) * coeff * 0.5; +#endif + } \ No newline at end of file diff --git a/OpenGL_Flightsim/shaders/particle.vert b/OpenGL_Flightsim/shaders/particle.vert index fae28b5..becc46f 100644 --- a/OpenGL_Flightsim/shaders/particle.vert +++ b/OpenGL_Flightsim/shaders/particle.vert @@ -12,10 +12,10 @@ uniform vec3 u_Right; out vec2 TexCoords; out vec4 Color; +out float FragDepth; void main() { - vec3 WorldPos = a_WorldPosAndSize.xyz; float particleSize = a_WorldPosAndSize.w; @@ -25,6 +25,8 @@ void main() gl_Position = u_Projection * u_View * vec4(vertexPos, 1.0); + FragDepth = 1.0 + gl_Position.w; + TexCoords = a_VertexPos.xy + vec2(0.5, 0.5); Color = a_Color; } \ No newline at end of file diff --git a/OpenGL_Flightsim/shaders/terrain.frag b/OpenGL_Flightsim/shaders/terrain.frag index 75e7b9e..97f6401 100644 --- a/OpenGL_Flightsim/shaders/terrain.frag +++ b/OpenGL_Flightsim/shaders/terrain.frag @@ -90,7 +90,6 @@ void main() //vec4 terrainColor = vec4(calculateDirLight(lightDir, Normal, texture(u_Texture_01, TexCoord).rgb), 1.0); - vec3 LightDir = vec3(0,1,0); vec3 LightColor = vec3(1.0); @@ -103,9 +102,9 @@ void main() FragColor = mix(vec4(Color, 1.0), terrainColor, 0.5); #endif - float farPlane = 150000.0; - float coeff = 2.0 / (log2(farPlane + 1.0) / 0.693); +#if 1 + float far = 150000.0; + float coeff = 2.0 / (log2(far + 1.0) / 0.693); gl_FragDepth = log2(FragDepth) * coeff * 0.5; - - //FragColor = terrainColor; +#endif } diff --git a/OpenGL_Flightsim/src/gfx/particles.cpp b/OpenGL_Flightsim/src/gfx/particles.cpp index 6142dc9..0bfaf7c 100644 --- a/OpenGL_Flightsim/src/gfx/particles.cpp +++ b/OpenGL_Flightsim/src/gfx/particles.cpp @@ -138,10 +138,9 @@ void ParticleSystem::draw_self(RenderContext& context) // m_texture->bind(5); - // glEnable(GL_BLEND); - // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_DEPTH_TEST); - glDepthMask(GL_FALSE); + glEnable(GL_BLEND); + //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + //glDepthMask(GL_TRUE); shader->bind(); shader->set_uniform("u_View", view); @@ -179,8 +178,10 @@ void ParticleSystem::draw_self(RenderContext& context) m_vao.unbind(); shader->unbind(); - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); + + //glDepthMask(GL_TRUE); + //glDisable(GL_BLEND); + } } // namespace gfx