Search code examples
javaglsllwjgl

Texture Wrapping, Weird Edges


I'm currently experiencing an issue with my textured VBO at the point where the coordinates begin to wrap around. I added a 30px yellow border all the way around my texture to make the effect stand out more. enter image description here

When looking at it closer, it looks almost as if the entire texture is squashed and smeared between the yellow lines.

enter image description here

Texture Coordinate Code (buffer[0] = vertex X, buffer[2] = vertex Y):

if ((buffer[0] * s) < 0) Logger.logDebug(buffer[8]+","+buffer[9]);
if ((buffer[2] * s) < 0) Logger.logDebug(buffer[8]+","+buffer[9]);
buffer[8] = (buffer[0] * s) % 1f; //tx
buffer[9] = (buffer[2] * s) % 1f; //ty
if (buffer[8] > 1f || buffer[9] > 1f) Logger.logDebug(buffer[8]+","+buffer[9]);
if (buffer[8] < 0f || buffer[9] < 0f) Logger.logDebug(buffer[8]+","+buffer[9]);

None of my checks for coordinates below 0 or above 1 passed, so they are in the right range.

Frag Shader:

#version 150 core

uniform sampler2D texture1;
in vec4 vColor;

in vec4 pass_Color;
in vec2 pass_texCoord;
out vec4 out_Color;

void main()
{
    out_Color = pass_Color;
    out_Color = texture(texture1, pass_texCoord.st);
}

Vert Shader:

uniform mat4 projection = mat4(
                        1.0, 1.0, 1.0, 1.0,
                        1.0, 1.0, 1.0, 1.0,
                        1.0, 1.0, 1.0, 1.0,
                        1.0, 1.0, 1.0, 1.0);
uniform mat4 view = mat4(
                        1.0, 1.0, 1.0, 1.0,
                        1.0, 1.0, 1.0, 1.0,
                        1.0, 1.0, 1.0, 1.0,
                        1.0, 1.0, 1.0, 1.0);
uniform vec4 overrideColor = vec4(-1,-1,-1,-1);

in vec4 in_Color;
in vec4 in_Position;
in vec2 texCoord;

out vec4 pass_Color;
out vec2 pass_texCoord;

void main()
{
    gl_Position = projection * view * gl_Vertex;
    if (overrideColor == vec4(-1,-1,-1,-1))
    {
        pass_Color = in_Color;
    }
    else
    {
        pass_Color = overrideColor;
    }
    pass_texCoord = texCoord;
}

Solution

  • You're using a fine grid of quads and trying to wrap a large texture across them, right? I think that the problem is that some quads are spanning across the wrap boundary. You should either make sure that the wrap boundary occurs right on the edge of the quads, or as I would recommend, just set up your texture to wrap:

    // when you set up your texture
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    

    Then don't use modulus for the texture coordinates; coords outside [0,1] will automatically wrap/repeat:

    buffer[8] = buffer[0] * s; //tx
    buffer[9] = buffer[2] * s; //ty
    

    See this and this for what texture wrapping looks like.