Không load được texture trên Cube

Mày mò làm theo tutorials ở trong này http://learnopengl.com/#!Getting-started/OpenGL
thì những ví dụ của người ta em đều chạy được. Chỉ có mỗi phần Texture là không xuất hiện trên vật thể.
Cụ thể là trong tutorial này: http://learnopengl.com/#!Lighting/Lighting-maps khiến em bị bí mấy ngày nay rồi :dizzy_face:

Một số hình ảnh về kết quả không mong đợi:

Nếu làm đúng thì nó phải như thế này:

Đây là vertex shader:

#version 330 core

struct Material
{
    sampler2D    diffuse;
    sampler2D    specular;
    float        shininess;
};

struct Light
{
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;

uniform vec3 viewPos;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

uniform Material material;
uniform Light light;

varying vec4 LightningColor;

void main()    
{
    gl_Position = projection * view * model * vec4(position, 1.0f);

    vec3 fragPos = vec3(model * vec4(position, 1.0f));

    // Ambient
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, texCoord));

    // Diffuse
    vec3 norm = normalize(mat3(transpose(inverse(model))) * normal); // prevent non-uniform scale
    vec3 lightDirection = normalize(light.position - fragPos);
    float diff = max(dot(norm, lightDirection), 0.0f); //dot-product of normal vector and light vector
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, texCoord));

    // Specular
    vec3 viewDirection = normalize(viewPos - fragPos); 
    vec3 reflectDirection = reflect(-lightDirection, norm);
    float spec = pow(max(dot(viewDirection, reflectDirection), 0.0f), material.shininess);
    vec3 specular = light.specular * spec * vec3(texture(material.specular, texCoord));

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

Tiếp theo là Fragment shader

#version 330 core

varying vec4 LightningColor;

void main()    
{
    gl_FragColor = LightningColor;
}

Sử dụng class Texture2D để lưu texture id

//////////////////////////////////////////
    // load texture
    Texture2D *texture = new Texture2D(2); //2 texture id sẽ được tạo ra
    texture->activeTexture(0); // kích hoạt texture[0] trong đối tượng texture
    texture->bind(0); // ràng buộc texture[0]
    texture->loadImage("images/container.png", 0); // dùng SOIL_load_image function để load image
    texture->textureParameteri(0, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    texture->textureParameteri(0, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture->textureParameteri(0, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture->textureParameteri(0, GL_TEXTURE_WRAP_T, GL_REPEAT);
    texture->generateMipmap(0);

    texture->activeTexture(1); // Tương tự như texture[0]
    texture->bind(1);
    texture->loadImage("images/container_specular.png", 1);
    texture->textureParameteri(0, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    texture->textureParameteri(0, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    texture->textureParameteri(0, GL_TEXTURE_WRAP_S, GL_REPEAT);
    texture->textureParameteri(0, GL_TEXTURE_WRAP_T, GL_REPEAT);
    texture->generateMipmap(1);
    texture->unbind();

    /*----------------------------------------------------------------------*/

Và dùng nó trong main loop

// Using shader
        gouraud->use();


        glm::mat4 view;
        view = camera.getViewMatrix();
        
        glm::mat4 projection;
        projection = glm::perspective(camera.fieldOfView, (GLfloat)(WIDTH / HEIGHT), 0.1f, 100.0f);


        // shader Gouraud location
        GLint viewLocation = glGetUniformLocation(gouraud->getProgramID(), "view");
        GLint projectionLocation = glGetUniformLocation(gouraud->getProgramID(), "projection");
        GLint lightColorLocation = glGetUniformLocation(gouraud->getProgramID(), "lightColor");
        GLint viewPosLocation = glGetUniformLocation(gouraud->getProgramID(), "viewPos");

        //GLint materialAmbientLocation = glGetUniformLocation(gouraud->getProgramID(), "material.ambient");
        GLint materialDiffuseLocation = glGetUniformLocation(gouraud->getProgramID(), "material.diffuse");
        GLint materialSpecularLocation = glGetUniformLocation(gouraud->getProgramID(), "material.specular");
        GLint materialShininessLocation = glGetUniformLocation(gouraud->getProgramID(), "material.shininess");

        GLint lightPositionLocation = glGetUniformLocation(gouraud->getProgramID(), "light.position");
        GLint lightAmbientLocation = glGetUniformLocation(gouraud->getProgramID(), "light.ambient");
        GLint lightDiffuseLocation = glGetUniformLocation(gouraud->getProgramID(), "light.diffuse");
        GLint lightSpecularLocation = glGetUniformLocation(gouraud->getProgramID(), "light.specular");

        glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, glm::value_ptr(projection));
        glUniform3f(lightColorLocation, 1.0f, 0.5f, 0.4f);
        glUniform3f(viewPosLocation, camera.getCameraPosition().x, camera.getCameraPosition().y, camera.getCameraPosition().z);
        
       *// input for material uniform
        texture->activeTexture(0);
        texture->bind(0);
        glUniform1i(materialDiffuseLocation, 0);
        texture->activeTexture(1);
        texture->bind(1);
        glUniform1i(materialSpecularLocation, 1);
        glUniform1f(materialShininessLocation, 32.0f);

        // input for the lamp
        glm::vec3 lightColor(1.0f, 1.0f, 1.0f);
        glm::vec3 ambient = lightColor * glm::vec3(0.2f);
        glm::vec3 diffuse = lightColor * glm::vec3(0.5f);
        glUniform3f(lightPositionLocation, lightPos.x, lightPos.y, lightPos.z);
        glUniform3f(lightAmbientLocation, ambient.x, ambient.y, ambient.z);
        glUniform3f(lightDiffuseLocation, diffuse.x, diffuse.y, diffuse.z);
        glUniform3f(lightSpecularLocation, 1.0f, 1.0f, 1.0f);

        drawCube(shader->getProgramID(), VAO, VBO, glm::vec3(0.0f, 0.0f, 0.0f));

        glfwSwapBuffers(window);

Về chi tiết class Texture2D và một số source code khác lưu ở đây https://github.com/nguyenchiemminhvu/GLFW-projects/tree/master/GLFW_CubeTexture

Rõ ràng là shader program nó có tác dụng vào màu sắc của Cube, nhưng không hiển thị ra đúng chất liệu gỗ như trong tutorial.

Mọi người ai biết về GLFW 3.3 vào giúp với :dizzy_face:

1 Like

Lâu rồi mình ko code openGL nhưng bạn thử đổi texturesize (resize file ảnh) thành 512x512 xem.

2 Likes

Đã đổi size từ 500x500 thành 512x512 nhưng kết quả vẫn thế anh :joy:
Để em thử làm lại theo cách load texture bằng SOIL_load_OGL_texture thay vì SOIL_load_image xem thử thế nào.

1 Like

Làm được rồi.
Dùng shader theo Phong model thì load đc texture, dùng theo Gouraud model thì lại không được.

Phong model vertex shader:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 normal;
layout (location = 2) in vec2 texCoord;

varying vec3 Normal;
varying vec3 FragPos;
varying vec2 TexCoord;

uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    gl_Position = projection * view * model * vec4(position, 1.0f);
    FragPos = vec3(model * vec4(position, 1.0f));
    Normal = mat3(transpose(inverse(model))) * normal;
    TexCoord = texCoord;
}

Phong model fragment shader

#version 330 core

struct Material
{
    sampler2D diffuse;
    sampler2D specular;
    float shininess;
};

struct Light
{
    vec3 position;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

varying vec3 Normal;
varying vec3 FragPos;
varying vec2 TexCoord;

uniform vec3 viewPos;

uniform Material material;
uniform Light light;

void main()
{
    // Ambient
    vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoord));
    
    // Diffuse
    vec3 norm = normalize(Normal);
    vec3 lightDirection = normalize(light.position - FragPos);
    float diff = max(dot(norm, lightDirection), 0.0f);
    vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoord));

    // Specular
    vec3 viewDirection = normalize(viewPos - FragPos);
    vec3 reflectDirection = reflect(-lightDirection, norm);
    float spec = pow(max(dot(viewDirection, reflectDirection), 0.0), material.shininess);
    vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoord));

    vec3 resultColor = ambient + diffuse + specular;
    gl_FragColor = vec4(resultColor, 1.0f);
}
1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?