Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL UV mapping for a sphere (having seam)

I have the following issue when trying to map UV-coordinates to a sphere enter image description here

Here is the code I'm using to get my UV-coordinates

glm::vec2 calcUV( glm::vec3 p)
{
    p = glm::normalize(p);

    const float PI = 3.1415926f;

    float u = ((glm::atan(p.x, p.z) / PI) + 1.0f) * 0.5f;
    float v = (asin(p.y) / PI) + 0.5f;

    return glm::vec2(u, v);
}

The issue was very well explained at this stackoverflow question, although, I still don't get how can I fix it. From what I've been reading, I have to create a duplicate pair of vertices. Does anyone know some good and effcient way of doing it ?

like image 393
tvoloshyn Avatar asked Oct 21 '22 20:10

tvoloshyn


2 Answers

The problem you have is, that at the seam your texture coordinates "roll" back to 0, so you get the whole texture mapped, mirrored onto the seam. To avoid this you should use GL_WRAP repeat mode and at the seam finish with vertices with texture coordinates >= 1 (don't roll back to 0). Remember that a vertex consists of the whole tuple of all its attributes and vertices with different attribute values are different in the whole, so there's no point in trying to "share" the vertices.

like image 74
datenwolf Avatar answered Oct 24 '22 11:10

datenwolf


Another way to do it is simply to pass the object coordinates of the sphere into the pixel shader, and calculate the UV in "perfect" spherical space.

Be aware that you will need to pass the local derivatives so that you don't merely reduce your seam from several pixels to one.

Otherwise, yes. You need to duplicate vertices along the same edge as u=0, and likewise repeat the vertices at the poles. In this way, your object topology will become a rectangle: just like your texture.

like image 21
bjorke Avatar answered Oct 24 '22 13:10

bjorke