Heyy!
I’ve been learning about tessellation shaders recently and I got stuck implementing the Levels Of Details (LOD) technique! :sorrow:
I implemented a tessellation shader program on one of my triangles in my scene. I am trying to control the LOD where I want the quality of the triangle to drop as the camera moves farther away.
I’ll just show some shaders:
Vertex Shader:
#version 430 core
// Attributes
layout (location = 0) in vec3 vertexPos;
layout (location = 1) in vec3 vertexColor;
// Output
out vec3 Color;
out float Distance; // To store the distance between the camera and the triangle
// Uniforms
uniform mat4 Model;
uniform mat4 View;
uniform mat4 Projection;
uniform vec3 CameraPos; // Passing the camera's position from CPU
uniform vec3 TrianglePos; // Passing the triangle's position from CPU
void main()
{
Distance = distance(TrianglePos.xyz, CameraPos); // Calculating the distance using glsl's distance() function
gl_Position = Projection * View * Model * vec4(vertexPos, 1.0f); // Transform the object from model space to world space to view space...
Color = vertexColor; // Store the color...
}
Tessellation Control Shader:
#version 430 core
layout (vertices = 3) out;
in float Distance; // This is meant to hold the distance between the camera and the triangle
// Note: I'm not doing anything with it just yet
void main()
{
gl_TessLevelInner[0] = 5.0;
gl_TessLevelOuter[0] = 3.0;
gl_TessLevelOuter[1] = 3.0;
gl_TessLevelOuter[2] = 3.0;
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
}
In the TCS above, I want to be able to adjust the tessellation based on the distance, so I was thinking maybe something like this would work…?
if (Distance > 15.0f)
{
// Adjust gl_TessLevelInner and gl_TessLevelOuter here maybe...? :confused:
} else if (Distance > 25.0f)
{
// ...
}
When I tried doing that, the triangle disappears if the distance is > 0 or even < 0, honestly, I don’t even know whether the glsl’s distance() function is doing the right thing or not.
I don’t even know whether the TCS is being called every frame in order to update the adjustments I am making, but it does make sense to do these things in the TCS instead of the TES no?
Tessellation Evaluation Shader:
#version 430 core
layout (triangles, equal_spacing, cw) in;
void main()
{
gl_Position = (gl_TessCoord.x * gl_in[0].gl_Position +
gl_TessCoord.y * gl_in[1].gl_Position +
gl_TessCoord.z * gl_in[2].gl_Position);
}
Sorry for the lengthy post, I hope someone could me understand things better.
Thank you!