首页 > 解决方案 > Images showing in weird colors

问题描述

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.

标签: javaopengllwjgl

解决方案


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

GL_BYTE是有符号的8 位整数类型,因此所有大于 127 的值最终都将被解释为负值(假设您的平台至少使用标准二进制补码表示)并被限制为 0。

只需使用GL_UNSIGNED_BYTE.


推荐阅读