以奇怪的颜色显示的图像

Images showing in weird colors

我正在使用 OpenGL 在 lwjgl 3 中创建一个新的 2d 游戏引擎来试用 lwjgl 3,因为这是我在所有其他项目中使用 lwjgl 3 的第一个项目都有 lwjgl 2。但是当我渲染一个使用该新引擎的图像所有颜色都发生变化,有些甚至不显示。

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)

Render

如果您知道如何防止这种情况发生,请告诉我。

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

GL_BYTE 是一个 signed 8 位整数类型,所以你所有大于 127 的值最终都会被解释为负值(假设你的平台使用标准的二进制补码表示至少)并被限制为 0.

只需使用GL_UNSIGNED_BYTE.