Hello I am VERY new to opengl. I am working on modifying another shader I found online. The version I found online works just great, however somewhere along in my code I made a mistake while editing. The problem is I have combed through it and can not see it, I was hoping for some fresh eyes.
To be clear it is not that what is being drawn is incorrect, it flat out wont compile. BTW i am absolutely sure the vertex shader isnt the problem, I havent edited it in awhile. Also some of this code might seem a bit stupid… I’m not done yet… I have only finished the multiple light part.
#version 330 core
struct Material {
sampler2D diffuse;
sampler2D specular;
float shininess;
float ambient;
};
struct DirLight {
vec3 direction;
vec3 color;
float strength;
};
struct PointLight {
vec3 position;
vec3 color;
float strength;
float active;
};
struct SpotLight {
vec3 position;
vec3 direction;
float cutOff;
float outerCutOff;
float constant;
float linear;
float quadratic;
vec3 ambient;
vec3 diffuse;
vec3 specular;
};
#define NR_POINT_LIGHTS 150
in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoords;
out vec4 color;
uniform vec3 viewPos;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];
uniform SpotLight spotLight;
uniform Material material;
// Function prototypes
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 fcolor);
vec3 CalcSpotLight(SpotLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
void main()
{
// Properties
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);
vec3 fragColor = texture(material.diffuse, TexCoords).rgb;
// == ======================================
// Our lighting is set up in 3 phases: directional, point lights and an optional flashlight
// For each phase, a calculate function is defined that calculates the corresponding color
// per lamp. In the main() function we take all the calculated colors and sum them up for
// this fragment's final color.
// == ======================================
// Phase 1: Directional lighting
vec3 result = vec3(0.0f);
//result = CalcDirLight(dirLight, norm, viewDir);
// Phase 2: Point lights
for(int i = 0; i < NR_POINT_LIGHTS; i++)
{
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir, fragColor);
}
// Phase 3: Spot light
//result += CalcSpotLight(spotLight, norm, FragPos, viewDir);
result += material.ambient * fragColor;
color = vec4(result, 1.0);
}
// Calculates the color when using a directional light.
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
// 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), material.shininess);
// Combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
return (diffuse + specular);
}
// Calculates the color when using a point light.
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir, vec3 fcolor)
{
if (light.active == 1)
{
// Lighting
vec3 lighting = vec3(0.0f);
// Diffuse
vec3 lightDir = normalize(light.position - fragPos);
float diff = max(dot(lightDir, normal), 0.0);
vec3 result = light.color * diff * fcolor;
// Attenuation (use quadratic as we have gamma correction)
float distance = (length(fragPos - light.position)/light.strength);
result *= 1.0 / (distance * distance);
lighting += result;
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess);
vec3 specular = spec * vec3(texture(material.specular, TexCoords)) * light.color;
specular *= material.shininess;
specular *= 1.0 / (distance * distance);
vec3 toReturn = lighting + specular;
return toReturn;
}
else
{
return vec3(0.0f, 0.0f, 0.0f);
}
}
// Calculates the color when using a spot light.
vec3 CalcSpotLight(SpotLight light, 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), material.shininess);
// Attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0f / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// Spotlight intensity
float theta = dot(lightDir, normalize(-light.direction));
float epsilon = light.cutOff - light.outerCutOff;
float intensity = clamp((theta - light.outerCutOff) / epsilon, 0.0, 1.0);
// Combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
ambient *= attenuation * intensity;
diffuse *= attenuation * intensity;
specular *= attenuation * intensity;
return (diffuse + specular);
}
And thankfully my program does print out errors on compilation. This is what is spat out. Unfortunately the lines it points to are not real… like they exist but they dont have any problems, heck one of the errors pointed to a line with no code on it… I havent changed the fragment shader from its origional part so it can’t be the issue
[ATTACH=CONFIG]1197[/ATTACH]
I have tried glslDevil but i cant figure out how to import my shaders.
Thanks much!
EDIT:
Intersting thing i noticed…
An interesting thing i have noticed is that if you define the Directional light function as so
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);
// 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), material.shininess);
// Combine results
vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
vec3 specular = light.specular * spec * vec3(texture(material.specular, TexCoords));
return (diffuse + specular);
}
It only throws an error that diffuse, specular , and ambient arent a member of that struct (which is intentional)