Soft Shadow

Hi everybody.
I already much days agonize on problem: can not do the soft shade by means of shadow map.
Its fp:

  
   if(shadowValue.z < projectiveBiased.z)
{
      gl_FragColor = ambient;
}
   else
{
      gl_FragColor = gl_Color + ambient;
}

As from this possible dispose?

Thanks.

problem is concluded in that that shade is drawn by small ladder.

As possible soften the shade?

Post whole shader. You can use sampler2DShadow and shadow2DProj and result is 0 or 1 dependeing if fragment are lited or not. Use more samples and sum and divide results win number of samples.

yooyo

yooyo
Its is source:

  
// vp
varying vec4 projShadow;
attribute vec3 lightpos;     // light position 
attribute vec3 lightnorm;    // light vector 

varying float LightIntensity;
varying float ShadowIntensity;


mat4 stq = mat4(1.0,0.0,0.0,0.0, 0.0,1.0,0.0,0.0, 0.0,0.0,1.0,0.0, 0.0,0.0,0.0,1.0);

void main()
{
   vec4 realPos = stq * gl_Vertex;


float rasst = length(gl_Vertex-lightpos);

LightIntensity = dot(normalize(gl_Vertex-lightpos), normalize(lightnorm-gl_Normal)) / rasst*400;

   projShadow      = gl_TextureMatrix[2] * realPos;

   gl_Position = ftransform();

}


//fp
uniform sampler2D shadowMap;

varying vec4 projShadow;

varying float LightIntensity;

const vec4 ambient = vec4(0.13), boost = vec4(1.06);

void main()
{ 
varying float res;

   vec3 projectiveBiased = (projShadow.xyz / projShadow.q);
   projectiveBiased = projectiveBiased * 0.1;

   vec4 shadowValue = texture2DProj(shadowMap, projShadow);


   if(shadowValue.z < projectiveBiased.z)
      gl_FragColor =  ambient;
   else
      gl_FragColor = LightIntensity + ambient;


}

I at all not got smooth shade. As possible solve this problem ?

This is one of your first shaders? Right? :slight_smile:
There is a lot of error. Correct shader should look like:

  
// vp
// *attribute* are per vertex attribute. you have only one light so use uniform

uniform vec3 lightpos;     // light position in worldspace
uniform vec3 lightnorm;    // light vector in worldspace

// use varings to pass variables
// between vp and fp. it will interpolated
varying vec4 projShadow;
varying float LightIntensity;

void main()
{
	 // calc worldspace vertex position and normal
	 vec4 realPos = gl_ModelViewMatrix * gl_Vertex;
	 vec3 realNorm = gl_NormalMatrix * gl_Normal;

	 // assume lightpos are in world space
	 float lightdist = length(realPos.xyz - lightpos);
	 LightIntensity = dot(lightnorm, realNorm) * lightdist/400.0;

	 // assume light mating in gl_TextureMatrix[2]
	 projShadow = gl_TextureMatrix[2] * realPos;
	 gl_Position = ftransform();
}

//fp
// a shadowmap. Use this kind of sample for shadow map
uniform sampler2DShadow shadowMap; 

// a spotlight texture. Usualy a white filled smooth cicrcle on black background
uniform sampler2D spotlight; 

varying vec4 projShadow;
varying float LightIntensity;

const vec4 ambient = vec4(0.13);

void main()
{ 
	 // check for shadowing. This func compare fragment against depth in shadow map. result is 0 or 1!
	 float isShadow = shadow2DProj(shadowMap, projShadow).r;

	 // fetch spotlight mask
	 float spot = texture2DProj(spotlight, projShadow).r;

	 gl_FragColor =  (vec4(LightIntensity) + ambient) * spot * isShadow;
}

Dont forget to bind spotlight to some of tex units. Im not sure will shader work. It is code from a head right now, so it might be a few errors.

yooyo

yooyo
Thank you very much.
You right: I really while beginner in glsl :slight_smile:
On picture are seen bugs: stairs from shade:


What known the cheap methods of their removal? Can be you will give the references?

Well it’s a known shadow mapping problem. It’s happens only in situations where light source are very close to (infinite) triangle plane. To avoid this try to use bigger resolution of shadow map, smaller light frustum or shade such tris as dark as possible.

yooyo

can i ask how gl_TextureMatrix[2] must look?

	
glActiveTextureARB(GL_TEXTURE2_ARB);
glMatrixMode(GL_TEXTURE);	
glLoadIdentity();
glTranslatef(0.5, 0.5, 0.5);
glScalef(0.5, 0.5, 0.5);
gluPerspective(m_Fov, 1, m_Near, m_Far);

// Set the projector's position and orientation:
// Pos & Targets are light pos and light targer (just like camera)
gluLookAt(m_Pos.v.x, m_Pos.v.y, m_Pos.v.z,  m_Target.v.x, m_Target.v.y, m_Target.v.z,	0,1,0);

glMatrixMode(GL_MODELVIEW);	

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.