-1

I have been following www.learnopengl.com tutorial and tried to load models using the method described. It works until the very end where it asks you to load the nanosuit with 2 points lights. I have copied the exact code from the tutorial with: camera, mesh, model, and shader classes however the issue is with the fragment shader. If I try a simple one like:

#version 330 core

in vec2 TexCoords;

out vec4 color;

uniform sampler2D texture_diffuse1;

void main()
{    
    color = vec4(texture(texture_diffuse1, TexCoords));
}

, the model is loaded and the textures too. But if try the solution given for 2 points lighting the texture is black:

#version 330 core
struct Material {
    sampler2D texture_diffuse1;
    sampler2D texture_specular1;
    float shininess;
}; 
/* Note: because we now use a material struct again you want to change your
mesh class to bind all the textures using material.texture_diffuseN instead of
texture_diffuseN. */

struct PointLight {
    vec3 position;

    float constant;
    float linear;
    float quadratic;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

#define NR_POINT_LIGHTS 2

in vec3 fragPosition;
in vec3 Normal;
in vec2 TexCoords;

out vec4 color;

uniform vec3 viewPos;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform Material material;

// Function prototypes
vec3 CalcPointLight(PointLight light, Material mat, vec3 normal, vec3 fragPos, vec3 viewDir);

void main()
{    
    vec3 result;
    vec3 viewDir = normalize(viewPos - fragPosition);
    vec3 norm = normalize(Normal);

    for(int i = 0; i < NR_POINT_LIGHTS; i++)
        result += CalcPointLight(pointLights[i], material, norm, fragPosition, viewDir);

    color = vec4(result, 1.0f);
}


// Calculates the color when using a point light.
vec3 CalcPointLight(PointLight light, Material mat, vec3 normal, vec3 fragPos, vec3 viewDir)
{
    vec3 lightDir = normalize(light.position - fragPos);
    // Diffuse shading
    float diff = max(dot(normal, lightDir), 0.0);
    // Specular shading
    vec3 reflectDir = reflect(-lightDir, normal);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), mat.shininess);
    // Attenuation
    float distance = length(light.position - fragPos);
    float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));    
    // Combine results
    vec3 ambient = light.ambient * vec3(texture(mat.texture_diffuse1, TexCoords));
    vec3 diffuse = light.diffuse * diff * vec3(texture(mat.texture_diffuse1, TexCoords));
    vec3 specular = light.specular * spec * vec3(texture(mat.texture_specular1, TexCoords));
    ambient *= attenuation;
    diffuse *= attenuation;
    specular *= attenuation;
    return (ambient + diffuse + specular);
}

I also modified the mesh class as described in the fragment shader from:

glUniform1i(glGetUniformLocation(shader.Program, (name + number).c_str()), i);

to:

glUniform1i(glGetUniformLocation(shader.Program, ("material." + name + number).c_str()), i);

Is anyone having the same issue?

2A-66-42
  • 554
  • 1
  • 5
  • 18

1 Answers1

0

It seems like GLSL does not like function. If I take CalcPointLight() code and place it in main(), it works!

#version 330 core
struct Material {
    sampler2D texture_diffuse1;
    sampler2D texture_specular1;
    float shininess;
};
/* Note: because we now use a material struct again you want to change your
mesh class to bind all the textures using material.texture_diffuseN instead of
texture_diffuseN. */

struct PointLight {
    vec3 position;

    float constant;
    float linear;
    float quadratic;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

#define NR_POINT_LIGHTS 2

in vec3 fragPosition;
in vec3 Normal;
in vec2 TexCoords;

out vec4 color;

uniform vec3 viewPos;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform Material material;

void main()
{
    vec3 result;
    vec3 viewDir = normalize(viewPos - fragPosition);
    vec3 norm = normalize(Normal);

    vec3 lightDir = normalize(pointLights[0].position - fragPosition);

    // Diffuse shading
    float diff = max(dot(norm, lightDir), 0.0);

    // Specular shading
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

    // Attenuation
    float distance = length(pointLights[0].position - fragPosition);
    float attenuation = 1.0f / (pointLights[0].constant + pointLights[0].linear * distance + pointLights[0].quadratic * (distance * distance));

    // Combine results
    vec3 ambient = pointLights[0].ambient * vec3(texture(material.texture_diffuse1, TexCoords));
    vec3 diffuse = pointLights[0].diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));
    vec3 specular = pointLights[0].specular * spec * vec3(texture(material.texture_specular1, TexCoords));
    ambient *= attenuation;
    diffuse *= attenuation;
    specular *= attenuation;
    result = ambient+diffuse+specular;

    color = vec4(result, 1.0f);
}

Edit: Found the answer there: GLSL sampler2D in struct. You can't instanciate a struct that contains an opaque type such as sampler2D.

Edit1: You can actually use those variables into a function without passing them as arguments since they are defined globally:

#version 330 core
struct Material {
    sampler2D texture_diffuse1;
    sampler2D texture_specular1;
    float shininess;
};
/* Note: because we now use a material struct again you want to change your
mesh class to bind all the textures using material.texture_diffuseN instead of
texture_diffuseN. */

struct PointLight {
    vec3 position;

    float constant;
    float linear;
    float quadratic;

    vec3 ambient;
    vec3 diffuse;
    vec3 specular;
};

#define NR_POINT_LIGHTS 2

in vec3 fragPosition;
in vec3 Normal;
in vec2 TexCoords;

out vec4 color;

uniform vec3 viewPos;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform Material material;

vec3 CalcLights(int i);

void main()
{
    vec3 result;
    for(int i=0; i<NR_POINT_LIGHTS; i++)
        result += CalcLights(i);
    color = vec4(result, 1.0f);
}

vec3 CalcLights(int i) //viewPos fragPosition pointLights[] Normal material TexCoords
{
    vec3 viewDir = normalize(viewPos - fragPosition);
    vec3 norm = normalize(Normal);

    vec3 lightDir = normalize(pointLights[i].position - fragPosition);

    // Diffuse shading
    float diff = max(dot(norm, lightDir), 0.0);

    // Specular shading
    vec3 reflectDir = reflect(-lightDir, norm);
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);

    // Attenuation
    float distance = length(pointLights[i].position - fragPosition);
    float attenuation = 1.0f / (pointLights[i].constant + pointLights[i].linear * distance + pointLights[i].quadratic * (distance * distance));

    // Combine results
    vec3 ambient = pointLights[0].ambient * vec3(texture(material.texture_diffuse1, TexCoords));
    vec3 diffuse = pointLights[0].diffuse * diff * vec3(texture(material.texture_diffuse1, TexCoords));
    vec3 specular = pointLights[0].specular * spec * vec3(texture(material.texture_specular1, TexCoords));
    ambient *= attenuation;
    diffuse *= attenuation;
    specular *= attenuation;

    return (ambient+diffuse+specular);
}
Community
  • 1
  • 1
2A-66-42
  • 554
  • 1
  • 5
  • 18