纹理颜色被紫色调取代

Texture colors replaced by purple tones

我尝试在四边形上显示纹理,但颜色被一些紫色所取代。

代码灵感来自"learnopengl"网站,但我没有找到我失败的地方。 这里是着色器。

vertSrc:cstring = """
    #version 330 core

    layout (location = 0) in vec3 aPos;
    layout (location = 1) in vec2 aTexCoord;

    out vec2 texCoord;
    uniform mat4 projection;

    void main()
    {
        gl_Position = projection * vec4(aPos, 1.0f);
        texCoord = aTexCoord;
    }
    """
fragSrc:cstring = """
    #version 330 core

    out vec4 FragColor;
    in vec2 texCoord;

    uniform sampler2D matTexture;

    void main()
    {
        FragColor = texture(matTexture, texCoord);
    }

    """

纹理的 opengl 代码。

glGenTextures(1, addr textureID)
glBindTexture(GL_TEXTURE_2D, textureID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, addr imageData[0])
glGenerateMipmap(GL_TEXTURE_2D)

图像加载感谢 stb_image 库。

var 
textureID:OGLuint
texWidth:OGLint
texHeight:OGLint
channelCount:OGLint
imageData = stbi_load("Test/2d-rendering-test./stack.png", addr texWidth, addr texHeight, addr channelCount, 0)

以及主循环中的opengl代码。

glBindTexture(GL_TEXTURE_2D, textureID)

如果您对此处显示问题的完整 nim 文件感到好奇。 https://bitbucket.org/Neotry/2d-rendering-test./src/master/WIPtest.nim

Portable Network Graphics (PNG) 文件可能包含 32 位 RGBA 颜色。

通过显式将 4 传递给最后一个参数,强制 stbi_load 生成具有 4 个颜色通道的图像:

imageData = stbi_load("Test/2d-rendering-test./stack.png",
                      addr texWidth, addr texHeight, addr channelCount, 4)

参见stb_image.h

Basic usage (see HDR discussion below for HDR usage):
      int x,y,n;
      unsigned char *data = stbi_load(filename, &x, &y, &n, 0);
      // ... process data if not NULL ...
      // ... x = width, y = height, n = # 8-bit components per pixel ...

// ... replace '0' with '1'..'4' to force that many components per pixel

      // ... but 'n' will always be the number that it would have been if you said 0
      stbi_image_free(data)

最后glTexImage2D的格式参数必须是GL_RGBAGL_BGRA。对于内部格式可以保持GL_RGB

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGBA,
             GL_UNSIGNED_BYTE, addr imageData[0])