Seam issue when mapping a texture to a sphere in OpenGL

前端 未结 6 1942
清歌不尽
清歌不尽 2020-12-25 08:51

I\'m trying to create geometry to represent the Earth in OpenGL. I have what\'s more or less a sphere (closer to the elliptical geoid that Earth is though). I map a texture

6条回答
  •  别那么骄傲
    2020-12-25 09:33

    One approach is like in the accepted answer. In the code generating the array of vertex attributes you will have a code like this:

    // FOR EVERY TRIANGLE
    const float threshold = 0.7;
    if(tcoords_1.s > threshold || tcoords_2.s > threshold || tcoords_3.s > threshold)
    {
        if(tcoords_1.s < 1. - threshold)
        {
            tcoords_1.s += 1.;
        }
        if(tcoords_2.s < 1. - threshold)
        {
            tcoords_2.s += 1.;
        }
        if(tcoords_3.s < 1. - threshold)
        {
            tcoords_3.s += 1.;
        }
    }
    

    If you have triangles which are not meridian-aligned you will also want glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);. You also need to use glDrawArrays since vertices with the same position will have different texture coords.

    I think the better way to go is to eliminate the root of all evil, which is texture coords interpolation in this case. Since you know basically all about your sphere/ellipsoid, you can calculate texture coords, normals, etc. in the fragment shader based on position. This means that your CPU code generating vertex attributes will be much simpler and you can use indexed drawing again. And I don't think this approach is dirty. It's clean.

提交回复
热议问题