Skip to main content
GameDev.net gamedev.net
🔒 Locked

Shadow map question

Started by oz02 Jul 1, 2009 at 3:16 AM 2 replies 750+ views
Original Post
oz02
oz02
Sorry my native language is not English, I can only use translation tools. This is the rendering: http://oz01.512j.com/p/shadowerror.PNG ----------------------------------------------------------------------- PASS1VS:

//Vertex Shader
struct vertex
{
float4 position : POSITION;
float4 color0 : COLOR0;
float2 texcoord0 : TEXCOORD0;
};

struct fragment
{
float4 position : POSITION;
float4 color0 : TEXCOORD1;
float2 texcoord0 : TEXCOORD0;
};

uniform float4x4 modelViewProj : state.matrix.mvp;
fragment main( vertex IN )
{
fragment OUT;
float4 colortmp=mul( modelViewProj, IN.position );
OUT.position = colortmp;

OUT.color0 = colortmp;
OUT.texcoord0 = IN.texcoord0;

return OUT;
}
-------------------------------------------------------------------------- PASS1PS:

//Fragment Shader

// Note how the fragment struct was simply copied from our vertex shader code.
// This is necessary if we want to use a vertex and pixel shader together.

struct fragment
{
float4 position : POSITION;
float4 color0 : TEXCOORD1;
float2 texcoord0 : TEXCOORD0;
};

struct pixel
{
float4 color : COLOR;
};

pixel main( fragment IN )
{
pixel OUT;


OUT.color.r=floor(IN.color0.b)/255.0;
OUT.color.g=frac(IN.color0.b);
OUT.color.b=frac(IN.color0.b*255.0);
OUT.color.a=1.0;
return OUT;
}
------------------------------------------------------------ PASS2VS:

//Vertex Shader
struct vertex
{
float4 position	:	POSITION;
float3 normal	:	NORMAL;
float4 color0	:	COLOR0;
float2 texcoord0	:	TEXCOORD0;
};

struct fragment
{
float4 position	:	POSITION;
float3 oNormal	:	TEXCOORD1;
float4 color0	:	COLOR0;
float2 texcoord0	:	TEXCOORD0;
float3 objectPos	:	TEXCOORD2;
float2 texcoord3	:	TEXCOORD3;
float4 objectPos2	:	TEXCOORD4;
float3 Light2color	:	TEXCOORD5;
};

// This binding semantic requires CG_PROFILE_ARBVP1 or higher.
uniform float4x4 modelViewProj : state.matrix.mvp;

fragment main(
			  vertex IN,
			  uniform float4x4 ShadowMapMVPmatrix,
			  uniform float4 MissleLightDirection,
			  uniform float3 paraLightColor
			  )
{
	fragment OUT;
	float4 objectPosShadowMap=mul( ShadowMapMVPmatrix, IN.position );
	OUT.texcoord3.x=objectPosShadowMap.x/(objectPosShadowMap.w*2)+0.5;
	OUT.texcoord3.y=objectPosShadowMap.y/(objectPosShadowMap.w*2)+0.5;
	OUT.objectPos2 = objectPosShadowMap;

	
	OUT.position = mul( modelViewProj, IN.position );
	OUT.color0 = IN.color0;
	OUT.texcoord0 = IN.texcoord0;
	OUT.oNormal = IN.normal;
	OUT.objectPos = IN.position.xyz;
	

    float3 P = IN.position.xyz;
    float3 N = normalize(IN.normal);
	float3 L = normalize(MissleLightDirection.xyz-P);
    float diffuseLight = max(dot(N, L), 0);
    float3 diffuse = paraLightColor * diffuseLight*10;

	OUT.Light2color = diffuse;


	return OUT;
}
------------------------------------------------------------------- PASS2PS:

//Fragment Shader

// Note how the fragment struct was simply copied from our vertex shader code.
// This is necessary if we want to use a vertex and pixel shader together.

struct fragment
{
    float4 position	:	POSITION;
    float3 color0	:	COLOR0;
    float2 texcoord0	:	TEXCOORD0;
    float3 normal	:	TEXCOORD1;
    float4 objectPos	:	TEXCOORD2;
    float2 texcoord3	:	TEXCOORD3;
	float4 objectPos2	:	TEXCOORD4;
	float3 Light2color	:	TEXCOORD5;
};

struct pixel
{
    float4 color : COLOR;
};

pixel main( 
    fragment IN, 
    uniform float3	globalAmbient,
    uniform float3	paraLightColor,
    uniform float3	paraLightDirection,
    uniform float3	eyePosition,
    uniform float4x4 Worldmatrix,

    uniform sampler2D	testTexture,
    uniform sampler2D	ShadowMapTexture,
	uniform samplerCUBE	AmbientReflectiveTexture,
	uniform sampler2D	NormalMapTexture,
	uniform sampler2D	SpecularMapTexture
)
{
	float3 T = normalize(IN.color0);
	float3 Nm = normalize(IN.normal);
	float3 B = normalize(cross (T,Nm));
	float3x3 TBN;
	TBN[0]=T;
	TBN[1]=B;
	TBN[2]=Nm;
	float3 NN = tex2D( NormalMapTexture, IN.texcoord0 ).xyz;
	float3 NN2;
	NN2.x=(NN.x-0.5)*2.0;
	NN2.y=(NN.y-0.5)*2.0;
	NN2.z=(NN.z-0.5)*2.0;
    float3 P = IN.position.xyz;
    float3 N = mul(NN2,TBN);

    float3 L = normalize(paraLightDirection-P);
    float diffuseLight = max(dot(N, L), 0);
    float3 diffuse = paraLightColor * diffuseLight;

    float3 V = normalize(eyePosition - P);
    float3 H = normalize(L + V);
    float specularLight = pow(max(dot(normalize(N), H), 0), 50.0);
	float4 SpecularMapcolor;
	SpecularMapcolor=tex2D( SpecularMapTexture, IN.texcoord0 );


    if(diffuseLight<=0) specularLight=0;
    float4 Ocolor;
    Ocolor.xyz = globalAmbient  +IN.Light2color+ diffuse+specularLight*8*SpecularMapcolor.xyz;
    Ocolor.w = 1.0;
    pixel OUT;
	float4 colorposZ=tex2D( ShadowMapTexture, IN.texcoord3 );
	float posz=colorposZ.r*255.0+colorposZ.g+colorposZ.b/255.0;
	if(IN.objectPos2.z>posz) Ocolor.xyz=globalAmbient +IN.Light2color;

	float4 R2;
	R2.xyz= reflect((eyePosition-IN.objectPos.xyz/IN.objectPos.w), N);
	R2.w=0;
	R2=mul(Worldmatrix,R2);


	OUT.color = tex2D( testTexture, IN.texcoord0 )*Ocolor*(texCUBE( AmbientReflectiveTexture, R2)+1.0);
	OUT.color.w=0.5;

    return OUT;
}

Enalis
Enalis
This seems to me to be a polygon offset issue. Are you setting a polygon offset when rendering the light/shadow buffer?
Douglas Eugene Reisinger II
Projects/Profile Site
dpadam450
dpadam450
glPolygonOffset()
NBA2K, Madden, Maneater, Killing Floor, Sims 
oz02
oz02
I think the problem has nothing to do with polygon offset.
I think that in the package pass1 a relatively large Zpos precision error.
Is here:
OUT.color.r=floor(IN.color0.b)/255.0;

OUT.color.g=frac(IN.color0.b);

OUT.color.b=frac(IN.color0.b*255.0);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.