2016-08-11 165 views
-1

我有一個簡單的函數來加載紋理,但問題是當我加載2紋理第二紋理是相同的第一紋理。紋理複製opengl

我認爲這可能是紋理加載或渲染的問題?

紋理加載:

GLuint loadTexture(std::string path) 
{ 

    GLuint load; 

    glGenTextures(1, &load); 
    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, load); 

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); 

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

    float borderColor[] = { 1.0f, 1.0f, 1.0f, 0.0f }; 

    glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); 


    int tw, th; 
    unsigned char* image = SOIL_load_image(path.c_str(), &tw, &th, 0, SOIL_LOAD_RGB); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB, GL_UNSIGNED_BYTE, image); 
    glGenerateMipmap(GL_TEXTURE_2D); 
    SOIL_free_image_data(image); 

    glBindTexture(GL_TEXTURE_2D, 0); 
    return load; 
} 

渲染及其它:

class TexturedCube { 
    static GLuint vbo; 
    static GLuint vao; 
    bool spec; 
    GLuint texture, specular; 
    glm::vec3 ambient, diffuse, Lspecular,viewPos; 
    float shiny; 
public: 
    TexturedCube(); 

    void render(Shader* shader, glm::mat4* model, glm::mat4* view, glm::mat4* projection,glm::vec3 cam); 
    void setTexture(std::string path); 
    void setSpecular(std::string path); 
    void useSpecular(bool x); 
    void setAttribs(glm::vec3 amb, glm::vec3 diff, glm::vec3 specs, float shine); 
}; 

GLuint loadTexture(std::string); 

const GLfloat cubeverts[] = { 
    // Positions   // Normals   // Texture Coords 
    -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.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 
    0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 
    0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 
    0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 

    -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 
    0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, 
    0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 
    0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, 
    -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 
    -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, 

    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 
    0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 
    0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 
    0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 
    -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 
    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f 
}; 

CPP

void TexturedCube::render(Shader * shader, glm::mat4 * model, glm::mat4 * view, glm::mat4 * projection, glm::vec3 cam) 
{ 
    glBindVertexArray(vao); 
    shader->Use(); 

    glActiveTexture(GL_TEXTURE0); 
    glBindTexture(GL_TEXTURE_2D, texture); 
    glUniform1i(glGetUniformLocation(shader->Program, "texture0"), GL_TEXTURE0); 

    if (spec) 
    { 
     glActiveTexture(GL_TEXTURE1); 
     glBindTexture(GL_TEXTURE_2D, specular); 
     glUniform1i(glGetUniformLocation(shader->Program, "texture1"), GL_TEXTURE1); 
    } 

    glUniformMatrix4fv(glGetUniformLocation(shader->Program, "model"), 1, GL_FALSE, glm::value_ptr(*model)); 
    glUniformMatrix4fv(glGetUniformLocation(shader->Program, "view"), 1, GL_FALSE, glm::value_ptr(*view)); 
    glUniformMatrix4fv(glGetUniformLocation(shader->Program, "projection"), 1, GL_FALSE, glm::value_ptr(*projection)); 

    glUniform1f(glGetUniformLocation(shader->Program, "material.shininess"), shiny); 
    glUniform3f(glGetUniformLocation(shader->Program, "material.ambient"), ambient.r, ambient.g, ambient.b); 
    glUniform3f(glGetUniformLocation(shader->Program, "material.diffuse"), diffuse.r, diffuse.g, diffuse.b); 
    glUniform3f(glGetUniformLocation(shader->Program, "material.specular"), Lspecular.r, Lspecular.g, Lspecular.b); 
    glUniform3f(glGetUniformLocation(shader->Program, "viewPos"), cam.x, cam.y, cam.z); 

    glDrawArrays(GL_TRIANGLES, 0, 36); 
    glBindVertexArray(0); 
} 

void TexturedCube::setTexture(std::string path) 
{ 
    texture = loadTexture(path); 
} 

void TexturedCube::setSpecular(std::string path) 
{ 
    specular = loadTexture(path); 
    spec = true; 
} 

破片着色器

#version 330 core 
struct Material { 
    sampler2D texture0; 
    sampler2D texture1; 
    float  shininess; 

    vec3 ambient; 
    vec3 diffuse; 
    vec3 specular; 
}; 

struct PointLight { 
    vec3 position; 
    vec3 color; 
}; 

in vec3 FragPos; 
in vec3 Normal; 
in vec2 TexCoords; 

out vec4 color; 

uniform vec3 viewPos; 
uniform Material material; 
uniform PointLight light; 

void main() 
{ 
    // Ambient 
    vec3 ambient = material.ambient * vec3(texture(material.texture0, TexCoords)); 

    // Diffuse 
    vec3 norm = normalize(Normal); 
    vec3 lightDir = normalize(light.position - FragPos); 
    float diff = max(dot(norm, lightDir), 0.0); 
    vec3 diffuse = material.diffuse * diff * vec3(texture(material.texture0, TexCoords)); 

    // Specular 
    vec3 viewDir = normalize(viewPos - FragPos); 
    vec3 reflectDir = reflect(-lightDir, norm); 
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); 
    vec3 specular = material.specular * spec * vec3(texture(material.texture1, TexCoords)); 

    color = vec4(ambient + diffuse + specular, 1.0f); 

} 

然後,我只需在渲染循環中調用SetSpecular()和SetTexture()然後render()。

感謝您的幫助。

+1

什麼是'texture'的聲明? –

+0

GLuint紋理,鏡面;在頭文件中。 – Adrian

+0

它是某個班的成員嗎?哪一堂課?發佈適當的[mcve]。 –

回答

2

問題是,你將錯誤的值傳遞給制服。你必須做的是傳遞紋理單元索引而不是OpenGL常量。所以

glActiveTexture(GL_TEXTURE0); 
glBindTexture(GL_TEXTURE_2D, texture); 
glUniform1i(glGetUniformLocation(shader->Program, "texture0"), GL_TEXTURE0); 

必須改變以

glUniform1i(glGetUniformLocation(shader->Program, "texture0"), 0); 

同爲其他質地:

glActiveTexture(GL_TEXTURE1); 
glBindTexture(GL_TEXTURE_2D, specular); 
glUniform1i(glGetUniformLocation(shader->Program, "texture1"), 1); 
+0

仍然沒有工作,但無論如何感謝。 – Adrian

+0

我試圖做一些調試,它看起來像第一個紋理覆蓋第二個紋理,但我不知道如何。 – Adrian

+1

我很確定它需要合格的「material.texture0」和「material.texture1」。現在你很可能爲這兩者獲得** - 1 **的值,並且統一結構的'sampler2D'成員都被賦予相同的未定義值。 –