Images showing in weird colors

Random Coder :

I am creating a new 2d game engine in lwjgl 3 using OpenGL to try out lwjgl 3 as this is the first project I'm using lwjgl 3 in all my other projects have had lwjgl 2. But when I render an image using that new engine all the colors change and some don't even show up.

Code for loading textures

    public Texture(String filename){
    IntBuffer width = BufferUtils.createIntBuffer(1);
    IntBuffer height = BufferUtils.createIntBuffer(1);
    IntBuffer comp = BufferUtils.createIntBuffer(1);

    ByteBuffer data = stbi_load(filename, width, height, comp, 4);

    id = glGenTextures();
    this.width = width.get();
    this.height = height.get();

    glBindTexture(GL_TEXTURE_2D, id);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_BYTE, data);
    stbi_image_free(data);

}

Texture i tried to render (with a resolution of 16x16)
input

Render
result

If you know how i could this prevent from happening please let me know.

derhass :
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_BYTE, data);
                                                                             ^^^^^^^

GL_BYTE is a signed 8 Bit integer type, so all your values > 127 will end up being interpreted as negative values (assuming your platform uses standard two's complement representation at least) and be clamped to 0.

Just use GL_UNSIGNED_BYTE.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=130204&siteId=1