Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

Thread: First timy having a compilation error i cant fix in shader code. Can you please help

  1. #1
    Junior Member Newbie
    Join Date
    May 2015
    Posts
    26

    First timy having a compilation error i cant fix in shader code. Can you please help

    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.

    Code :
    #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

    Click image for larger version. 

Name:	glsl error.jpg 
Views:	162 
Size:	19.1 KB 
ID:	2022

    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
    Code :
    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)

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,529
    I suspect that 150 point lights is exceeding the size of the default uniform block. Try reducing that number.

    The errors from the compiler are referring to offsets in the compiled code, not line numbers in the source code.

    The value of GL_MAX_VERTEX_UNIFORM_COMPONENTS isn't required to be more than 1024, and GL_MAX_VERTEX_UNIFORM_VECTORS isn't required to be more than 256. Those 150 lights work out to at least 1200 components and potentially 600 vectors (if each field is stored in a separate vector rather than the float members being packed into the components left over by the vec3 members).

    If you need more uniform data than will fit into the default block, try using UBOs. If you need more data than will fit into UBOs, you can always use buffer textures (but they may be slower than uniforms).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •