GLSL shaders

Started by TheDude, October 19, 2014, 00:58:02

Previous topic - Next topic

TheDude

In my code I added shaders and started to add in shadow mapping. I am nearly done but as you'll see one line says:
    vec3 ProjectionCoords = toLightVector.xyz / toLightVector; where it should be     vec3 ProjectionCoords = toLightVector.xyz / toLightVector.w;
But other parts of the code does not allow toLightVector to be a vec4 for for what ever reason. Here is my fragmentShader.txt file:

#version 400 core

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec3 toLightVector;
in vec3 toCameraVector;
uniform sampler2D inShadowMap;
out vec4 out_Colour;

uniform sampler2D textureSampler;
uniform vec3 lightColor;
uniform float shineDamper;
uniform float reflectivity;

float CalcShadowFactor(vec3 toLightVector)
{

    vec3 ProjectionCoords = toLightVector.xyz / toLightVector;

    vec2 UVCoords;
    UVCoords.x = 0.5 * ProjectionCoords.x + 0.5;
    UVCoords.y = 0.5 * ProjectionCoords.y + 0.5;

    float Depth = texture(inShadowMap, UVCoords).x;
    if(Depth < (ProjectionCoords.z + 0.001)) return 0.5;
    else return 1.0;
}


void main(void) {

   vec3 unitNormal = normalize(surfaceNormal);
   vec3 unitLightVector = normalize(toLightVector);
   float shadowFactor = CalcShadowFactor(toLightVector);
   float nDot1 = dot(unitNormal,unitLightVector);
   float brightness = max(nDot1,0.2) * shadowFactor + 0.2;
   vec3 diffuse = brightness * lightColor;

   vec3 unitVectorToCamera = normalize(toCameraVector);
   vec3 lightDirection = -unitLightVector;
   vec3 reflectedLightDirection = reflect(toLightVector,unitNormal);
   float specularFactor;
   specularFactor = max(0.0,dot(reflectedLightDirection,unitVectorToCamera));
   float dampedFactor = pow(specularFactor,shineDamper);
   vec3 finalSpecular = dampedFactor * reflectivity * lightColor;
    if(shadowFactor < 0.9){
        specularFactor = pow(specularFactor, 128.0);
        out_Colour += specularFactor;
    }
   out_Colour = vec4(diffuse,1.0) * texture(textureSampler,pass_textureCoords) + vec4(finalSpecular,1.0);

}


also all toLightVector would be vec4. The that should allow of proper shadows? Currently i thing it casts the shadow on itself(3d object).

quew8

I don't understand what the problem is.

If you need a vec3 then you can reference it with "toLightVector.xyz", if you need a vec4 then reference it with "toLightVector".

TheDude

My problem is that if I change toLightVector to a vec4 and the unitLightVector to vec4 also, the dot() function won't work because I get this error:
ERROR: 0:34: No matching function for call to dot(vec3, float)
ERROR: 0:35: Use of undeclared identifier 'nDot1'
ERROR: 0:36: Use of undeclared identifier 'brightness'
ERROR: 0:52: Use of undeclared identifier 'diffuse'

Because the shadows only work properly if it's:
    vec3 ProjectionCoords = toLightVector.xyz / toLightVector.w;