opengltessellationtesselation

Sphere tessellation new primitves position


I'm trying to get an LOD working with the tessellation shader. I have a simple sphere which is tessellated with a 5 rings et 5 sectors at the begining. I would like the sphere to increase its details when the camera is approching. But the new primitves generated by the tessellation are mapped in a flat plane, I tried to change there position, but I couldn't manage to get it working. Here is an illustration of the problem :
enter image description here enter image description here enter image description here enter image description here

As you can see, I'm not getting a sphere when the camera is approroching. This is what I would like to get when I'm near the sphere :

enter image description here

Here is the code in the tessellation evaluation shader :

void main(void){
    float u = gl_TessCoord.x; 
    float v = gl_TessCoord.y; 

    vec4 pos0 = gl_in[0].gl_Position; 
    vec4 pos1 = gl_in[1].gl_Position; 
    vec4 pos2 = gl_in[2].gl_Position; 
    vec4 pos3 = gl_in[3].gl_Position; 

    vec4 a = mix(pos1,pos0, u);
    vec4 b = mix(pos2, pos3, u);

    float l = length(a - b); 
    vec4 position = mix(a, b, v);
    gl_Position = u_transformMatrix * position; 
  tes_positions = (u_transformMatrix * position).xyz;
}

geometry shader :

layout(triangles) in; 
layout(triangle_strip, max_vertices = 3) out;
void main(void){

    for(int i=0; i<3; i++){
        vec4 pos = gl_in[i].gl_Position;
        vec4 normal = normalize(pos);   

        pos = normal * u_radius; 
        gl_Position = u_projectionMatrix * u_viewMatrix * pos; 
        EmitVertex();
    }

   EndPrimitive();
}

Thank you for your help ! And if you need anything else, please ask me and I'll post it.


Solution

  • So the @slicer4ever find the answer, all credits go to him. (Thank you by the way !). He doesn't have an SO account so he can't post It himself, unfortunately. I quote him : your normalizing the vec4, which might be messing up the w component of your vertex?

    And that was it, the w coordinate was the problem.

    And here is the output now : enter image description here