Seam issue when mapping a texture to a sphere in OpenGL

前端 未结 6 1946
清歌不尽
清歌不尽 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:26

    You can also go a dirty way: interpolate X,Y positions in between vertex shader and fragment shader and recalculate correct texture coordinate in fragment shader. This may be somewhat slower, but it doesn't involve duplicate vertexes and it's simplier, I think.

    For example:
    vertex shader:

    #version 150 core
    uniform mat4 projM;
    uniform mat4 viewM;
    uniform mat4 modelM;
    in vec4 in_Position;
    in vec2 in_TextureCoord;
    out vec2 pass_TextureCoord;
    out vec2 pass_xy_position;
    void main(void) {
        gl_Position = projM * viewM * modelM * in_Position;
        pass_xy_position = in_Position.xy; // 2d spinning interpolates good!
        pass_TextureCoord = in_TextureCoord;
    }
    

    fragment shader:

    #version 150 core
    uniform sampler2D texture1;
    in vec2 pass_xy_position;
    in vec2 pass_TextureCoord;
    out vec4 out_Color;
    
    #define PI 3.141592653589793238462643383279
    
    void main(void) {
        vec2 tc = pass_TextureCoord;
        tc.x = (PI + atan(pass_xy_position.y, pass_xy_position.x)) / (2 * PI); // calculate angle and map it to 0..1
        out_Color = texture(texture1, tc);
    }
    

提交回复
热议问题