Home Game Development c++ – Attempt to repair sprite sheet pixel bleeding in OpenGL 2D inflicting sprite distortion

c++ – Attempt to repair sprite sheet pixel bleeding in OpenGL 2D inflicting sprite distortion

0
c++ – Attempt to repair sprite sheet pixel bleeding in OpenGL 2D inflicting sprite distortion

[ad_1]

While engaged on a challenge, I encountered the frequent downside of pixel bleeding when making an attempt to attract subregions of my sprite sheet. This brought about kind of “seams” to look on the edges of my sprites. You can see the problem right here, on the proper and prime of the sprite .

Doing some looking, I discovered others with the same downside, and a recommended answer (right here, and right here for instance) was to offset my texture coordinates by a bit, corresponding to 0.5. I attempted this, and it appeared to work. But I’ve seen that typically, relying on the place the sprite or digicam is, I get a little bit of distortion on the sprites. Here, the left aspect seems to be minimize off, and right here, the underside appears to have expanded. (I ought to observe, the distortion occurs on all sides, I simply occurred to take screenshots of it taking place on the underside and left.) It could also be just a little tough to see in screenshots, however it’s undoubtedly noticeable in movement. For reference, right here is the a part of the sprite sheet that’s being displayed right here

Does anyone have any thought what’s going on right here? I did not really discover this situation till lately. I initially got down to resolve the pixel bleeding after I noticed it occurring between my tile sprites. This new situation doesn’t happen with them utilizing my present half-pixel offset answer (or if it does, it is not noticeable).

Code:

Texture parameters

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Texture coordinate calculation

std::vector<glm::vec4> Texture2D::GetUVs(int w, int h)
{
std::vector<glm::vec4> uvs;
int rows = Width/ w;
int columns = Height / h;

for(int c = 0; c < columns; c ++)
{
    for(int i = 0; i < rows; i ++)
    {
        float offset = 0.5;
        uvs.emplace_back(glm::vec4(float(((i) * w + offset))/Width,
                              float(((1 + i) * w - offset))/Width,
                              float(((c) * h + offset))/Height,
                              float(((1 + c) * h - offset))/Height));
    }
}
return uvs;

Where Width and Height are the size of the sprite sheet, and w and h are the size of the subregion, on this case 32 and 32.

How I cross the uvs to the shader

GLfloat verticies[] =
{
    uv.x, uv.w,
    uv.y, uv.z,
    uv.x, uv.z,

    uv.x, uv.w,
    uv.y, uv.w,
    uv.y, uv.z
};

this->shader.Use().SetVector2fv("uvs", 12, verticies);

Where uv is the uv at an index within the uvs vector that was returned above within the GetUVs perform.

Vertex shader

#model 330 core
format (location = 0) in vec2 vertex; 

out vec2 TextureCoordinates;

uniform vec2 uvs[6];
uniform mat4 mannequin;
uniform mat4 projection;

void essential()
{
    const vec2 place [6] = vec2[]
    (
        vec2(0.0f, 1.0f),
        vec2(1.0f, 0.0f),
        vec2(0.0f, 0.0f),

        vec2(0.0f, 1.0f),
        vec2(1.0f, 1.0f),
        vec2(1.0f, 0.0f)
    );

   TextureCoordinates = uvs[gl_VertexID];
   gl_Position = projection * mannequin * vec4(place[gl_VertexID], 0.0, 1.0);
}

Fragment shader

#model 330 core
in vec2 TextureCoordinates;
out vec4 colour;

uniform sampler2D picture;
uniform vec4 spriteColor;

void essential()
{    
    colour = vec4(spriteColor) * texture(picture, TextureCoordinates);
}  

Thanks for studying. I’ve requested this query a number of locations and never gotten any response, so any assistance is tremendously appreciated.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here