2017-06-27 59 views
-2

我試圖在opengl中加載相同的模型(建模爲3Ds max),但是紋理不同。問題是,當我在創建紋理後嘗試將紋理與glBindTexture綁定時,它會消失。在我更改3Ds max中的紋理之前,它始終以黑色顯示模型(僅在opengl中)。但我甚至沒有給我的模型分配黑色。我的圖像「model-image.png」也包含我分配給我的對象的所有紋理。所以我錯過了什麼? 感謝您的幫助提前。添加新紋理時模型消失opengl

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; 
} 

回答

0

我的猜測是,當你說

layout(location = 2) in vec2 vCoordinates; 

你真正的意思是說

layout(location = 1) in vec2 vCoordinates; 

考慮你從來沒有啓用頂點屬性2.

+0

哦謝謝@TheKitchenSink,是我的意思是位置= 1個you're權。這解決了這個問題,所以對象不再是黑色了。但不幸的是,它並沒有像預期的那樣顯示出這個模型。現在紋理混合。我也認爲,我已經正確導出了對象。所以這個問題不是通過3Ds Max出口我猜。 – Bibi90

+0

這可能是由於OpenGL加載紋理「顛倒」的事實造成的。在頂點着色器中,嘗試將fUV.y設置爲(1 - vCoordinates.y)。 – TheKitchenSink