# OpenGL Sphere Tessellation wrong coordinates mapping

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

## 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

uniform mat4 u_projectionMatrix;
uniform mat4 u_viewMatrix;
uniform mat4 u_transformMatrix;
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 on other sites

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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013601
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!