添加新纹理opengl

时间:2017-06-27 22:30:31

标签: c++ opengl textures shader 3dsmax

我尝试在opengl中加载相同的模型(在3Ds max中建模),但是使用不同的纹理。问题是,当我尝试使用glBindTexture绑定Texture后,我已经激活了纹理,然后它就消失了。在我更改3Ds max中的纹理之前,它总是以黑色显示模型(仅在opengl中)。但我甚至没有为我的模特分配黑色。我的图片“model-image.png”也包含我分配给我的对象的所有纹理。我错过了什么吗? 感谢您的帮助。

void initDrawing()
{
glEnable(GL_DEPTH_TEST);
program = glCreateProgram();
    std::string shaderV = Utilities::loadFile("shader.vert");
    std::string shaderF = Utilities::loadFile("shader.frag");
    program = Utilities::compileShader(shaderV, shaderF);
    Utilities::loadObj("model.obj", obj);
    Utilities::loadPNG("model-image.png", diffuse);

    glEnable(GL_TEXTURE_2D);
    glGenVertexArrays(1, &vertexArrayObject);
    glBindVertexArray(vertexArrayObject);

    glGenBuffers(1, &vPosition);
    glBindBuffer(GL_ARRAY_BUFFER, vPosition);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4)*obj.vertices.size(), &obj.vertices[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vPosition);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

    GLuint vCoordinates;
    glGenBuffers(1, &vCoordinates);
    glBindBuffer(GL_ARRAY_BUFFER, vCoordinates);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2)*obj.textureCoordinates.size(), &obj.textureCoordinates[0], GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, vCoordinates);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, NULL);

    glGenTextures(2, &texture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, diffuse.width, diffuse.height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
        &diffuse.colors[0]);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


    glUseProgram(program);

    ModelView = glGetUniformLocation(program, "ModelView");
    Projection = glGetUniformLocation(program, "Projection");
    Diffuse = glGetUniformLocation(program, "Diffuse");
}
void display()
{

    // background color
    const GLfloat color[] = { 0.5, 0.5, 0.5, 1 };
    glClear(GL_DEPTH_BUFFER_BIT);
    glClearBufferfv(GL_COLOR, 0, color);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    modelView = view * model;
    glUniformMatrix4fv(ModelView, 1, GL_FALSE, glm::value_ptr(modelView));
    glUniformMatrix4fv(Projection, 1, GL_FALSE, glm::value_ptr(projection));
    glUniform1i(Diffuse, 0);
    glDrawArrays(GL_TRIANGLES, 0, obj.vertices.size());

glutSwapBuffers();
}

我的着色器:

片段着色器:

uniform sampler2D Diffuse;

in vec2 fUV;
out vec3 color;

//main for the color
void main(void)
{
    color = texture(Diffuse, fUV).rgb; 
}

顶点着色器:

layout (location = 0) in vec4 vPosition;
layout (location = 2) in vec2 vCoordinates;

uniform mat4 ModelView;
uniform mat4 Projection;

out vec2 fUV;

void main(void)
{
    gl_Position = Projection * ModelView * vPosition;
    fUV = vCoordinates;
}

1 个答案:

答案 0 :(得分:0)

我的猜测是,当你说

layout(location = 2) in vec2 vCoordinates;

你真的想说

layout(location = 1) in vec2 vCoordinates;

考虑到你从不启用顶点属性2.