5

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 ?

Community
  • 1
  • 1
tvoloshyn
  • 407
  • 8
  • 22
  • Ahhh, the fun old problem of texture mapping a sphere. One of the most universal solutions I've seen is detailed here (its a fantastic resource for graphics related stuff) Link: http://www.iquilezles.org/www/articles/patchedsphere/patchedsphere.htm – enhzflep Nov 23 '13 at 05:18

2 Answers2

2

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.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

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.

bjorke
  • 3,295
  • 1
  • 16
  • 20
  • The problem I have is that I can not really calculate uv within fragment shader as I have other objects which are not spheres. I know I ccould set ups ome "object type" variable, but i don't really want to overcomplicate my shader. I still don't get what is actually meant by duplicating vertices... Does that mean I have to "manually" add these vertcies and set their UV coordinates to 1.0 ? – tvoloshyn Nov 23 '13 at 10:51
  • 1
    Consider the case of a circle. Let's say that you simplify it to a pentagon. There would be five vertices, yes? But if you want to correctly UV map it (or in this simplified case, just "U" map it), you will need *SIX* vertices: [0.0, 0.2, 0.4, 0.6, 0.8, 1.0] -- if you didn't have the "duplicate" vertex at the end, then the mapping for the last span, from 0.8 to 1, would instead go from 0.8 to 0.0, "backwards." – bjorke Nov 23 '13 at 17:39
  • Now, it's possible that you already have your correct vertex count, but that you are simply miscalculating the value for the duplicated vertex in the final position, because your calcUV() trig says 0 degrees when you really want 360 degrees. The specific correct solution for that I leave to you, since the best solution will be one that understands the topology of your model and you've already stated that the *real* models you are using are not the ones you're showing. – bjorke Nov 23 '13 at 17:42