Skip to content

Commit

Permalink
Fixed ParticleSystem depth testing, needs log depth value
Browse files Browse the repository at this point in the history
  • Loading branch information
gue-ni committed Oct 11, 2023
1 parent 7852a75 commit 6c607dc
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
10 changes: 8 additions & 2 deletions OpenGL_Flightsim/shaders/mesh.frag
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,20 @@ 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;

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);

}
14 changes: 13 additions & 1 deletion OpenGL_Flightsim/shaders/particle.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
4 changes: 3 additions & 1 deletion OpenGL_Flightsim/shaders/particle.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
9 changes: 4 additions & 5 deletions OpenGL_Flightsim/shaders/terrain.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
}
13 changes: 7 additions & 6 deletions OpenGL_Flightsim/src/gfx/particles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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

0 comments on commit 6c607dc

Please sign in to comment.