GLSL shader not working at close range

Started by
5 comments, last by Shawn619 11 years, 7 months ago
I took a program about lighting from the opengl red book(i altered it slighty to include more primitives and include a shader). The OGL fixed-function lighting works fine with at all distance(distance as in from light sources to vertex), but the shader copes out completely and stops working when in range of 0-5 units of distance.

This is without shader (works at all distances away from lightsource). Blue cube is a positional lightsource.
bvqx.jpg

This is the problem, custom shader isn't working at short range (~5 units).
2vvklg9.jpg

Proof that it works at long distance.
sc6gko.jpg


I don't know if its a problem w/ my shader, but it seems like thats the case. My (toon) shader is from http://www.lighthous...ng-version-iii/

.vert

varying vec3 normal;
void main()
{
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}


.frag

varying vec3 normal;
void main()
{
float intensity;
vec4 color;
vec3 n = normalize(normal);
intensity = dot(vec3(gl_LightSource[0].position),n);
if (intensity > 0.95)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.25)
color = vec4(0.4,0.2,0.2,1.0);
else
color = vec4(0.2,0.1,0.1,1.0);
gl_FragColor = color;
}


My enables:

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
Advertisement
I would say the problem is with this line:
intensity = dot(vec3(gl_LightSource[0].position),n);
Here you are taking the dot product between the normal and the light source position vector, instead of the vector to the light source. This should have the form:
intensity = dot(vec3(gl_LightSource[0].position) - position,n);
where position is the position of the fragment (pixel) you are shading. You should pass this position as a second varying vec3.

From far away, this difference will be neglible, since the light source position vector is so much bigger, but the result is still slightly incorrect.
normalize the light.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Ah, that too yes. wink.png

That would result in something like:
vec3 lightvector = normalize(vec3(gl_LightSource[0].position) - position);
intensity = dot(lightvector, n);

And you should consider applying some attenuation if you are using a point light source.
I assume by "position" you ment the vertex coords. If so, i tried to pass the vertex coords from .vert to .frag and got this error:


ERROR: 0:9: 'dot' : no matching overloaded function found (using implicit conversion)

intensity = dot(vec3(gl_LightSource[0].position) - vertPos,n);


.vert

varying vec3 normal;
varying vec4 vertPos;
void main()
{
vertPos = gl_Vertex;
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}


.frag

varying vec3 normal;
varying vec4 vertPos;
void main()
{
float intensity;
vec4 color;
vec3 n = normalize(normal);
intensity = dot(vec3(gl_LightSource[0].position) - vertPos,n);


if (intensity > 0.90)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.2)
color = vec4(0.4,0.2,0.2,1.0);
else
color = vec4(0.2,0.1,0.1,1.0);
gl_FragColor = color;
}

I assume by "position" you ment the vertex coords. If so, i tried to pass the vertex coords from .vert to .frag and got this error:


ERROR: 0:9: 'dot' : no matching overloaded function found (using implicit conversion)


You're attempting to mix 4d vectors (light, vert) with a 3d vector (n). There is no overload for this, hence the error. Use light.xyz and vert.xyz.
Solved!

I found the best method to be just normalizing the light & normal.

.vert

varying vec3 normal;
void main()
{
normal = gl_NormalMatrix * gl_Normal;
gl_Position = ftransform();
}


.frag

varying vec3 normal;
void main()
{
float intensity;
vec4 color;
vec3 n = normalize(normal);
vec3 lightvector = normalize(vec3(gl_LightSource[0].position.xyz));
intensity = dot(lightvector, n);

if (intensity > 0.90)
color = vec4(1.0,0.5,0.5,1.0);
else if (intensity > 0.5)
color = vec4(0.6,0.3,0.3,1.0);
else if (intensity > 0.2)
color = vec4(0.4,0.2,0.2,1.0);
else
color = vec4(0.2,0.1,0.1,1.0);
gl_FragColor = color;
}


34jasgk.jpg

This topic is closed to new replies.

Advertisement