怪异的切成纹理

时间:2019-01-17 19:24:27

标签: opengl nim

我尝试在我的opengl程序中实现纹理化,但是生成的映射是意外的。四边形的右侧会显示左侧的切片。

enter image description here

我无法弄清楚代码中有什么问题。这是坐标。

var 
    vertices = @[OGLfloat(0), 0, 0, # Position 
                            0, 0, # Texcoords
                      225, 0, 0,
                            1.0, 0.0,
                      225, 225, 0,
                            1.0, 1.0,
                      0, 225, 0,
                            0.0, 1.0
                      ]

    indices = @[OGLuint(0), 1, 2, 2, 3, 0]

这是与纹理有关的代码。

var 
    textureID:OGLuint
    texWidth:OGLint
    texHeight:OGLint
    channelCount:OGLint
    imageData = stbi_load("awesome_face.png", addr texWidth, addr texHeight, addr channelCount, 0)

#texture
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_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, addr imageData[0])

还有着色器。

var
shadID:OGLuint
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);
    }

    """

完整的示例程序(如果需要)。 https://bitbucket.org/Neotry/2d-rendering-test./src/e1081893f0f61dcbca39c959b869bc27e6ae31d2/WIPtest.nim?at=master

0 个答案:

没有答案