Hi, I've been poking around GLSL, making some shaders to solve the seams problem in ComplexBody. So far it's going ok, but I'm having trouble with one fragment shader that's making me crazy.

The shader itself is ok, but It does not cast the light, and I don't know why.

This is where the problem lies (I think):

**Code:**

vec4 clrDiffuse = gl_Color;

vec4 clrNormal = vec4(.9, .9, .9, .9);

vec4 clrSpecular = gl_Color;

vec4 finalColor = vec4(.0, .0, .0, clrDiffuse.a);

vec2 pos = mod(worldSpacePos.xy, vec2(Size)) - vec2(Size / 2);

float dist_squared = dot(pos, pos);

// Color

for(int i = 0; i < 36; i++)

{

pos = mod((worldSpacePos.xy * i), vec2(Size)) - vec2(Size / 2.0);

dist_squared = dot(pos, pos);

clrDiffuse *= dist_squared < Size * 6.0 ? vec4(.9, .9, .9, .9) - ((.75 / i) / dist_squared) : vec4(.8, .8, .8, .8);

clrNormal -= dist_squared / Size;

}

And this is the full .frag code:

**Code:**

uniform vec3 CameraPosition;

uniform float Size;

uniform float Brightness;

uniform int _lightCount;

uniform vec4 _lightPos[8];

uniform vec4 _lightDir[8];

uniform vec3 _lightColor[8];

varying vec3 worldSpacePos;

varying mat2 objTransform;

void main()

{

vec3 eyeDir = normalize(CameraPosition - worldSpacePos);

vec4 clrDiffuse = gl_Color;

vec4 clrNormal = vec4(.9, .9, .9, .9);

vec4 clrSpecular = gl_Color;

vec4 finalColor = vec4(.0, .0, .0, clrDiffuse.a);

vec2 pos = mod(worldSpacePos.xy, vec2(Size)) - vec2(Size / 2);

float dist_squared = dot(pos, pos);

// Color

for(int i = 0; i < 36; i++)

{

pos = mod((worldSpacePos.xy * i), vec2(Size)) - vec2(Size / 2.0);

dist_squared = dot(pos, pos);

clrDiffuse *= dist_squared < Size * 6.0 ? vec4(.9, .9, .9, .9) - ((.75 / i) / dist_squared) : vec4(.8, .8, .8, .8);

clrNormal -= dist_squared / Size;

}

// Lighting START (copied from the DynamicLightingSample)

vec3 normal = normalize(clrNormal.xyz - vec3(.5, .5, .5));

normal.z = -normal.z;

vec3 lightDir;

float attenFactor;

for (int i = 0; i < _lightCount; i++)

{

if (_lightPos[i].w > .0)

{

// positional light source (pos.w encodes range)

float dist = distance(_lightPos[i].xyz, worldSpacePos);

attenFactor = 1.0 - min(dist / _lightPos[i].w, 1.0);

lightDir = normalize(_lightPos[i].xyz - worldSpacePos);

attenFactor = attenFactor * pow(max(dot(lightDir, -_lightDir[i].xyz), 0.000001), _lightDir[i].w);

}

else

{

// directional light source (pos.xyz encodes an ambient term)

attenFactor = 1.0;

lightDir = -_lightDir[i].xyz;

finalColor.rgb += _lightPos[i].xyz * clrDiffuse.rgb;

}

// Apply rotation to the light direction to match rotated normal map.

lightDir.xy = lightDir.xy * objTransform;

// Diffuse lighting

float diffuseFactor = max(dot(normal, lightDir), 0.0);

finalColor.rgb += attenFactor * _lightColor[i] * clrDiffuse.rgb * diffuseFactor;

// Specular lighting

float specularFactor = pow(max(dot(normal, normalize(eyeDir + lightDir)), 0.000001), clrSpecular.a * 64.0);

finalColor.rgb += _lightColor[i] * clrSpecular.rgb * specularFactor * diffuseFactor * attenFactor;

}

finalColor.rgb = max(finalColor.rgb, mix(clrDiffuse.rgb, finalColor.rgb, clrNormal.a));

// Lighting END

// A final touch to apply the Brightness parameter

gl_FragColor = vec4(finalColor.rgb * Brightness, gl_Color.a);

}

As you can see, the lighting code is the same as the one in the DynamicLightingSample.

The vertex shader is also the same:

**Code:**

uniform float CameraFocusDist;

uniform vec3 CameraPosition;

varying vec3 worldSpacePos;

varying mat2 objTransform;

attribute vec4 objTrAttrib;

void main()

{

gl_Position = ftransform();

gl_TexCoord[0] = gl_MultiTexCoord0;

gl_FrontColor = gl_Color;

float camDistScaleInv = gl_Vertex.z / CameraFocusDist;

worldSpacePos = CameraPosition + vec3(gl_Vertex.xy * camDistScaleInv, gl_Vertex.z);

objTransform = mat2(objTrAttrib.x, objTrAttrib.y, objTrAttrib.z, objTrAttrib.w);

}

Any idea why the light does not work for this shader?

I'm new to GLSL, so maybe I'm missing something obvious.

Thanks!