PDA

View Full Version : Syntax error: parse error when defining a struct



OneCleverGorilla
06-04-2017, 08:53 AM
So I'm trying to define a struct to organize all variables related to my directional light and I'm getting a fragment shader compilation error:


Fragment shader failed to compile with the following errors:
ERROR: 0:2: error(#132) Syntax error: "DirLight" parse error
ERROR: error(#273) 1 compilation errors. No code generated

I have other issues with structs as well like not being able to define uniforms in structs but I have worked around that. Anyone know what could cause my fragment shader to fail to compile?


#version 330
vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir);
vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir);
#define NR_POINT_LIGHTS 4

in vec2 texCoordOut;
in vec3 Normal;
in vec3 FragPos;

out vec4 outColor;

struct Material {
float shininess;
};

struct DirLight {
vec3 direction;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};

struct PointLight {
vec3 position;

float constant;
float linear;
float quadratic;

vec3 ambient;
vec3 diffuse;
vec3 specular;
};

uniform sampler2D diffuse;
uniform sampler2D specular;
uniform sampler2D emission;

uniform vec3 viewPos;

uniform Material material;
uniform DirLight dirLight;
uniform PointLight pointLights[NR_POINT_LIGHTS];

void main()
{
//properties
vec3 norm = normalize(Normal);
vec3 viewDir = normalize(viewPos - FragPos);

//Directional
vec3 result = CalcDirLight(dirLight, norm, viewDir);

//Point
for(int i = 0; i < NR_POINT_LIGHTS; i++)
result += CalcPointLight(pointLights[i], norm, FragPos, viewDir);

outColor = vec4(result, 1.0)
}

vec3 CalcDirLight(DirLight light, vec3 normal, vec3 viewDir)
{
vec3 lightDir = normalize(-light.direction);

//diffuse
float diff = max(dot(normal, lightDir), 0.0);
//specular
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);

//net
vec3 ambient = light.ambient * vec3(texture(diffuse, texCoordOut));
vec3 diffuse = light.diffuse * diff * vec3(texture(diffuse, texCoordOut));
vec3 specular = light.specular * spec * vec3(texture(specular, texCoordOut));
return (ambient + diffuse + specular);
}

vec3 CalcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir)
{
vec3 lightDir = normalize(light.position - fragPos);

//diffuse
float diff = max(dot(normal, lightDir), 0.0);
//specular
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);

//attenuation
float distance = length(light.position - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));

//net
vec3 ambient = light.ambient * vec3(texture(diffuse, texCoordOut));
vec3 diffuse = light.diffuse * diff * vec3(texture(diffuse, texCoordOut));
vec3 specular = light.specular * spec * vec3(texture(specular, texCoordOut));
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}

Silence
06-04-2017, 11:24 AM
Try to define the function using your structures after the definition of the structures.

OneCleverGorilla
06-04-2017, 11:43 AM
Thanks this was one of the issues I found. Also had to declare my struct uniforms after defining each struct. And fixed the bug where I called on a shininess variable when it should have been material.shininess. GLSL seems to be pretty picky about the order in which you define structs, functions, and variables.