Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


GLSL shader not working at close range


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 28 August 2012 - 11:49 PM

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.
Posted Image

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

Proof that it works at long distance.
Posted Image


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);


Sponsor:

#2 Ignifex   Members   -  Reputation: 507

Like
0Likes
Like

Posted 29 August 2012 - 03:32 AM

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.

#3 dpadam450   Members   -  Reputation: 943

Like
0Likes
Like

Posted 29 August 2012 - 08:41 AM

normalize the light.

#4 Ignifex   Members   -  Reputation: 507

Like
0Likes
Like

Posted 29 August 2012 - 09:54 AM

Ah, that too yes. Posted Image

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.

#5 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 29 August 2012 - 11:48 AM

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;
}


#6 GeneralQuery   Crossbones+   -  Reputation: 1263

Like
0Likes
Like

Posted 29 August 2012 - 12:01 PM

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.

#7 Shawn619   Members   -  Reputation: 342

Like
0Likes
Like

Posted 29 August 2012 - 03:28 PM

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;
}

Posted Image




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS