Ok, I admit, back then the first time I read about tessellation a few years ago, when I was still using OpenGl immediate mode and thought it was difficult, I was quite intimidated and thought it was some fancy black - magic thing that is impossible to grasp, let alone do.
Well, it turned out a lot less intimidating than I thought at first.
A quad, dressed in a heightmap, that was getting its height only from the heightmap, and getting tesselated depending on its distance from the screen.
After a lot of studying and understanding the three stages of the pipeline that it uses, the implementation itself turned out to be three hour's worth of coding.
Either I was getting better, or grGraphicsGl had really hit the rapid-prototyping sweetspot I was planning it for.
Hopefully both...
The heurestic used here to determine tessellation levels is ugly, exaggerated, untested, unresearched, with magic numbers, and completely unproven - it is just something super simple that worked after some trial and error that worked with the data I had at the time I wrote the shader.
In other words, it was perfect in order to prototype my first Tessellation Control Shader.
#version 420
layout(std140) uniform;
layout(vertices = 3) out;
in vec3 position_vsout[];
in vec2 texUv_vsout[];
in vec3 normal_vsout[];
out vec3 position_csout[];
out vec2 texUv_csout[];
out vec3 normal_csout[];
#pragma include matricesuniform.glsl
#define ID gl_InvocationID
void main()
{
//Magic numbers, ugly hacks and cooking that are dependent on vertex-to-camera
//distance. This is one of the places where you should put real effort to find an
//algorithm that gives you optimal tessellation levels - I'm not kidding, DON'T
//COPY PASTE IT FOR YOUR OWN GOOD.
float tesslevel = clamp(70.f-10.f*abs(modelToEye[3].z),2.f,1000.f);
// Set the control points of the output patch
position_csout[ID] = position_vsout[ID];
texUv_csout[ID] = texUv_vsout[ID];
normal_csout[ID] = normal_vsout[ID];
//Don't forget that you can cull vertices here, so a few visibility tests are generally in order.
// Calculate the tessellation levels.
gl_TessLevelOuter[0] = tesslevel;
gl_TessLevelOuter[1] = tesslevel;
gl_TessLevelOuter[2] = tesslevel;
gl_TessLevelInner[0] = tesslevel;
}
No comments:
Post a Comment