OpenGL - 纹理只显示部分和泥泞

时间:2018-02-15 10:11:36

标签: c++ opengl

我想在正方形上显示纹理。我创建具有6x5浮点值的数组(6个顶点,每个顶点具有x,y,z位置和x,y UV位置),设置顶点属性指针,使用CImg库加载位图并使用glTexImage2D加载它。

图片已正确读取,可以src.display()显示。

但是我的图像看起来像放大了一小部分图像。

这是结果(它应该是一个清晰的Lenna图像):Texture result

顶点着色器:

#version 330

layout (location = 0) in vec3 position;
layout (location = 1) in vec2 uvPos;

out vec2 uvPosition;

void main() {
    uvPosition = uvPos;
    gl_Position = vec4(position, 1.0);
}

片段着色器:

#version 330

in vec2 uvPosition;

out vec4 color;

uniform sampler2D tileTex;

void main() {
    color = texture(tileTex, uvPosition);
}

加载纹理的代码:

imageShader = new OpenGL::OpenGLShader(Common::GetShaderResource(IDR_SHADERS_BASIC_VERTEX), Common::GetShaderResource(IDR_SHADERS_BASIC_FRAGMENT));
        imageShader->bind();

        float verticesImage[6][5] = {
            0.0f,  0.0f, 0.0f, 0.0f,  0.0f, // Top-left
            3.0f,  0.0f, 0.0f, 1.0f,  0.0f, // Top-right
            3.0f,  5.0f, 0.0f, 1.0f,  1.0f, // Bottom-right
            0.0f,  0.0f, 0.0f, 0.0f,  0.0f, // Top-left
            0.0f,  5.0f, 0.0f, 0.0f,  1.0f, // Bottom-left
            3.0f,  5.0f, 0.0f, 1.0f,  1.0f // Bottom-right
        };
        unsigned int vboIndexImage;

        glGenVertexArrays(1, &vaoIndexImage);
        glGenBuffers(1, &vboIndexImage);

        glBindVertexArray(vaoIndexImage);
        glBindBuffer(GL_ARRAY_BUFFER, vboIndexImage);
        glBufferData(GL_ARRAY_BUFFER, sizeof(verticesImage), &verticesImage[0][0], GL_STATIC_DRAW);

        GLint position = glGetAttribLocation(imageShader->shader_id, "position");
        GLint uvPos = glGetAttribLocation(imageShader->shader_id, "uvPos");
        glEnableVertexAttribArray(position);
        glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);

        glEnableVertexAttribArray(uvPos);
        glVertexAttribPointer(uvPos, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));

        CImg<unsigned char> src("images\\lena512.bmp");
        int width = src.width();
        int height = src.height();

        GLuint textureID;
        glGenTextures(1, &textureID);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureID);

        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, src);

        glEnable(GL_TEXTURE_2D);

        imageShader->unbind();

1 个答案:

答案 0 :(得分:3)

glVertexAttribPointer函数中的最后一个参数是指向第一个元素的指针。在您的情况下,uvPos已偏移3 * sizeof(float),但您有2 * sizeof(float)

你有:

glVertexAttribPointer(uvPos, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(2 * sizeof(float)));

必须:

glVertexAttribPointer(uvPos, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));