Jump to content
  • Advertisement
Sign in to follow this  
Physix

OpenGL Sphere Tessellation wrong coordinates mapping

This topic is 1255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to apply a tessellation on the GPU for a simple Sphere. This tessellation is working perfectly for a simple plan, but it's not working for the sphere. Of course, I know the coordinates mapping aren't the same, I tried many ways to do it. For example, I tried to use the gl_TessCoord (x and y) in the tessellation Evaluation as longitude et latitude mapped into a plane. Then convert them to a spherical coordinates, but it 'really' didn't work. 

 

For the tessellation control, I'm just splitting all patches into 2 for the outer and 2 as well for the inner level. 

 

Here is my code to draw the sphere : 

glBindVertexArray(vertexArrayObject);
glPatchParameteri(GL_PATCH_VERTICES, 4);


glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesBuffer);
glDrawElements(GL_PATCHES, indices.length, GL_UNSIGNED_INT, 0);


glBindVertexArray(0);

Here my current code in the tessellation Evaluation : 

#version 430 


layout(quads, fractional_even_spacing, ccw) in;


uniform mat4 u_projectionMatrix; 
uniform mat4 u_viewMatrix; 
uniform mat4 u_transformMatrix;
uniform float u_radius;  
uniform vec3 u_cameraPosition; 




void main(void){
vec4 position = gl_in[0].gl_Position;
position.xz += gl_TessCoord.xy * 2.0 - 1.0; 
gl_Position = u_projectionMatrix * u_viewMatrix * u_transformMatrix * position; 
}

Here is the indices : 

int indPtr = 0; 


for(int r=0; r< mRings-1; r++)
for(int s=0; s<mSectors-1; s++){
indices[indPtr++] = r * mSectors + s; 
indices[indPtr++] = r * mSectors + (s+1); 
indices[indPtr++] = (r+1) * mSectors + (s+1); 
indices[indPtr++] = (r+1) * mSectors + s; 
}

To draw the sphere, I followed this example : http://stackoverflow.com/questions/5988686/creating-a-3d-sphere-in-opengl-using-visual-c

 

I attached two images for the result I'm getting. 

 

If you have any hint that could help me solve this problem, It'd be really cool. Thank you. 

Note : If you need any other informations, please ask me and I'll post them. 

 

 

Share this post


Link to post
Share on other sites
Advertisement

Hi, 

If anyone got the same problem, I have the solution for you : ) 

So the problem was in the mapping, and this is *tottaly* not the way of getting the coordinates : 

position.xz += gl_TessCoord.xy * 2.0 - 1.0;

Instead, you should do a linear interpolation from the positions sent by the Tessellation control shader with the u coordinate of gl_TessCoord like this : 

float u = gl_TessCoord.x;
float v = gl_TessCoord.y;
vec3 a = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, u);
vec3 b = mix(gl_in[2].gl_Position, gl_in[3].gl_Position, u);

And then, a second linear interpolation of the result using the v coordinates. 

vec4 position = mix(a, b, v); 

And you should get the right positions for you vertices now :D 

I hope this helped.

 

Here are the source : http://https://github.com/adamlwgriffiths/GLETools/blob/master/examples/tesselation/lod.shader

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!