Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
2 registered members (TipmyPip, 1 invisible), 18,758 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
I want to use mtl_terraintex+mtlShadow from Shadowmapping exam. #370065
05/10/11 19:39
05/10/11 19:39
Joined: Aug 2008
Posts: 394
Germany
Benni003 Offline OP
Senior Member
Benni003  Offline OP
Senior Member

Joined: Aug 2008
Posts: 394
Germany
Hi, I have a problem with using the shadowmapping example from conitec.
The terrain in my level is using the material mtl_terraintex from the mtlFX.c . Now I want to use the shadowmapping code from the samples folder. It's need to apply the material mtlShadow to the Entities to show shadow. Also on my terrain. But how can I combine this both materials? Please could someone help me?

here the Schaders:
First the terrainshader and then the shadow shader

MATERIAL* mtl_terraintex =
{
effect = "terraintex.fx";
flags = PASS_SOLID; // prevent transparency of 32 bit skins
event = mtl_terrain_init;
}

Code:
//////////////////////////////////////////////////////////////////////
// terraintex.fx
// Terrain material for an unlimited number of terrain textures
// Tiled texture on RGB, mask on alpha 

Texture entSkin1; // basic tiled terrain texture
Texture LightMap; // lightmap created by the map compiler

bool PASS_SOLID; // enforce rendering on the solid pass
//bool AUTORELOAD;

//////////////////////////////////////////////////////////////////////

// technique without lightmap
technique terraintex
{
	pass multi_repeat11
	{
    ZWriteEnable = True;
		AlphaBlendEnable = True;
		SrcBlend = SrcAlpha;
    DestBlend = InvSrcAlpha;
		
		Texture[0] = <entSkin1>;
		TexCoordIndex[0] = 0;
		AlphaOp[0] = SelectArg1;
		AlphaArg1[0] = Texture;
		
		Texture[1] = <entSkin1>;
		TexCoordIndex[1] = 1;
    ColorArg1[1] = Texture; 
	  ColorArg2[1]= Diffuse;
	  ColorOp[1] = Modulate2x;
	  AlphaArg1[1] = Current;
	  AlphaOp[1] = SelectArg1;

	  ColorOp[2] = Disable;
	  AlphaOp[2] = Disable;
	}
}

// technique with lightmap
technique terraintex_lm
{
	pass multi_repeat11
	{
      ZWriteEnable = True;
		AlphaBlendEnable = True;
		SrcBlend = SrcAlpha;
      DestBlend = InvSrcAlpha;
		
		Texture[0] = <entSkin1>;
		TexCoordIndex[0] = 0;
		AlphaOp[0] = SelectArg1;
		AlphaArg1[0] = Texture;
		
		Texture[1] = <LightMap>;
		TexCoordIndex[1] = 0;
      ColorArg1[1] = Texture; 
	   ColorArg2[1]= Diffuse;
	   ColorOp[1] = AddSigned;
	   AlphaArg1[1] = Current;
	   AlphaOp[1] = SelectArg1;

		Texture[2] = <entSkin1>;
		TexCoordIndex[2] = 1;
      ColorArg1[2] = Texture; 
	   ColorArg2[2]= Current;
	   ColorOp[2] = Modulate2x;
	   AlphaArg1[2] = Current;
	   AlphaOp[2] = SelectArg1;

	   ColorOp[3] = Disable;
	   AlphaOp[3] = Disable;
	}
}

// fallback if nothing works
technique fallback { pass one { } }



MATERIAL* mtlShadow =
{
effect = "Shadow.fx";
flags = AUTORELOAD;
}

Code:
////////////////////////////////////////////////////
// Simple shadow mapping shader
// Copyright (c) 2007 Conitec.
// (with reduced darkness..)
// based on modifications by Jibb Smart 
////////////////////////////////////////////////////

#define USE_PCF // use percentage closer filtering

//Tweakables
static const float fDark = 0.6;
static const float fDarkDiffuse = 0.15;
//static const float fDarkDiffuse = 0.15;
static const float fBright = 1.4;
static const float fDepthOffset = 0.99;
static const float fPCF = 0.9;

// Application fed data:
const float4x4 matWorldViewProj;	// World*view*projection matrix.
const float4x4 matWorld; // World matrix.
const float4x4 matMtl;   // Precalculated texture projection matrix
const float4 vecSunDir;	// Sun direction vector.

texture TargetMap;
texture entSkin1;
sampler DepthSampler = sampler_state { Texture = <TargetMap>; };
sampler TexSampler = sampler_state { Texture = <entSkin1>; Mipfilter = Linear; };

// Shadow mapping vertex shader
void ShadowVS (in float4 inPos: POSITION,
		in float2 inTex: TEXCOORD0,
		in float3 inNormal: NORMAL,
		out float4 outPos: POSITION,
		out float2 outTex: TEXCOORD0,
		out float3 outNormal: TEXCOORD1,
		out float4 outDepth: TEXCOORD2)
{
// Transform the vertex from object space to clip space:
	outPos = mul(inPos, matWorldViewProj);
	
// Transform the normal from object space to world space:
	outNormal = normalize(mul(inNormal,matWorld));
	
// Pass the texture coordinate to the pixel shader:
	outTex = inTex;
	
// Output the projective texture coordinates
	outDepth = mul( mul(inPos,matWorld), matMtl );
}

// distance comparison function
float fDist(float4 DepthCoord,float fDepth)
{
	return 
		tex2Dproj(DepthSampler,DepthCoord).r < (fDepth*fDepthOffset)? fDark : fBright;
}

#ifdef USE_PCF
static const float4 fTaps_PCF[9] = {
	{-1.0,-1.0, 0.0, 0.0},
	{-1.0, 0.0, 0.0, 0.0},
	{-1.0, 1.0, 0.0, 0.0},
	{ 0.0,-1.0, 0.0, 0.0},
	{ 0.0, 0.0, 0.0, 0.0},
	{ 0.0, 1.0, 0.0, 0.0},
	{ 1.0,-1.0, 0.0, 0.0},
	{ 1.0, 0.0, 0.0, 0.0},
	{ 1.0, 1.0, 0.0, 0.0}};
#endif

// Shadow mapping pixel shader
float4 ShadowPS (in float4 inPos: POSITION,
					in float2 inTex: TEXCOORD0,
					in float3 inNormal: TEXCOORD1,
					in float4 inDepth: TEXCOORD2) : COLOR0
{
// Calculate the diffuse term:

	float fDiffuse = lerp(fDarkDiffuse, fBright, saturate(dot(-vecSunDir, normalize(inNormal))));


// Calculate the shadow term
#ifdef USE_PCF
	float fShadow = 0.0;
	for (int i=0; i < 9; i++)
	{
		float4 fTap = inDepth + fPCF*fTaps_PCF[i];
		fShadow += fDist(fTap,inDepth.z)/9;
	}
#else
	float fShadow = fDist(inDepth,inDepth.z);
#endif		


		return tex2D(TexSampler,inTex) * min(fShadow, fDiffuse);
}

technique techShadow
{
	pass p0
	{
		VertexShader = compile vs_2_0 ShadowVS();
		PixelShader  = compile ps_2_0 ShadowPS();
	}
}





Last edited by Benni003; 05/11/11 08:51.
Re: I want to use mtl_terraintex+mtlShadow from Shadowmapping exam. [Re: Benni003] #370200
05/11/11 15:44
05/11/11 15:44
Joined: Jan 2011
Posts: 797
Da wo du nicht bist! Muhahaha!
xxxxxxx Offline
User
xxxxxxx  Offline
User

Joined: Jan 2011
Posts: 797
Da wo du nicht bist! Muhahaha!
I wuoldn't combine this shaders, if you do that you'll see that you have to do it for every (objekt)shader in your game. I've changed to code of the shadowmapping.c in the samples folder for you. It uses a different View to create the shadows and the camera just uses a pp shader to add these shadows to the scene, normaly it renders every model with an objekt shader so other shaders, like your terrain shader aren't visible there.
here is the changed shadowmapping.c
Code:
///////////////////////////////
#include <acknex.h>
#include <default.c>
#include <mtlView.c>
///////////////////////////////

function mtlDepth_event();
BMAP* shadowT;

MATERIAL* mtlDepth =
{
	effect = "Depth.fx";
	event = mtlDepth_event;
	flags = ENABLE_VIEW;
}

MATERIAL* mtlShadowAdd =
{
	effect = "ShadowFilter.fx";
}

MATERIAL* mtlShadow =
{
	effect = "Shadow.fx";
	flags = AUTORELOAD;
}

VIEW* viewDepth = 
{
	bmap = "#2048x2048x14";
	material = mtlDepth;
	flags = SHOW;
}

VIEW* viewShadow = 
{
	flags = AUTORELOAD;
}

function mtlDepth_event()
{
	float fTexAdj[16] =
	{
		0.5, 0.0, 0.0, 0.0,
		0.0,-0.5,	0.0, 0.0,
		0.0, 0.0, 1.0, 0.0,
		0.0, 0.0, 0.0, 1.0
	};
	fTexAdj[12] = 0.5 + (0.5/(float)viewDepth.bmap.width);
	fTexAdj[13] = 0.5 + (0.5/(float)viewDepth.bmap.height);
	mat_set(mtlShadow.matrix,matViewProj);
	mat_multiply(mtlShadow.matrix,fTexAdj);
	return 0;	
}

function main()
{
	d3d_antialias = 4;
	level_load("small.hmp");
	ENTITY* ent = ent_create("blob.mdl",vector(0,0,30),NULL);
	
	shadowT = bmap_createblack(screen_size.x, screen_size.y, 24);
	viewShadow.bmap = shadowT;
	
	vec_set(sun_angle,vector(90,60,1000)); 
	vec_set(camera.x,vector(-250,0,100));
	camera.tilt = -15;
	pp_set(camera,mtlShadowAdd);
	viewShadow.material = mtlShadow;
	viewDepth.stage = viewShadow;
	
	while(1)
	{
		ent.pan -= 2 * time_step;
		ent.tilt -= time_step;	
		vec_set(viewDepth.x,sun_pos);
		vec_set(viewDepth.pan,vector(180+sun_angle.pan,-sun_angle.tilt,0));
		
		draw_text(str_printf(NULL,
		"Simple shadow mapping demo (Shader-Workshop 6)
		Edit Shadow.fx and observe changes immediately in the scene"),
		15,5,COLOR_WHITE);
		vec_set(viewShadow.x,camera.x);
		vec_set(viewShadow.pan,camera.pan);
		wait(1);
	}
}


the cahnged shadow.fx
Code:
#define USE_PCF // use percentage closer filtering

//Tweakables
static const float fDark = 0.5;
static const float fBright = 1.5;
static const float fDepthOffset = 0.99;
static const float fPCF = 0.5;

// Application fed data:
const float4x4 matWorldViewProj;	// World*view*projection matrix.
const float4x4 matWorld; // World matrix.
const float4x4 matMtl;   // Precalculated texture projection matrix
const float4 vecSunDir;	// Sun direction vector.

texture TargetMap;
sampler DepthSampler = sampler_state { Texture = <TargetMap>; };

// Shadow mapping vertex shader
void ShadowVS (in float4 inPos: POSITION,
		in float2 inTex: TEXCOORD0,
		in float3 inNormal: NORMAL,
		out float4 outPos: POSITION,
		out float3 outNormal: TEXCOORD0,
		out float4 outDepth: TEXCOORD1)
{
// Transform the vertex from object space to clip space:
	outPos = mul(inPos, matWorldViewProj);
	
// Transform the normal from object space to world space:
	outNormal = normalize(mul(inNormal,matWorld));
	
// Output the projective texture coordinates
	outDepth = mul( mul(inPos,matWorld), matMtl );
}

// distance comparison function
float fDist(float4 DepthCoord,float fDepth)
{
	return 
		tex2Dproj(DepthSampler,DepthCoord).r < (fDepth*fDepthOffset)? fDark : fBright;
}

#ifdef USE_PCF
static const float4 fTaps_PCF[9] = {
	{-1.0,-1.0, 0.0, 0.0},
	{-1.0, 0.0, 0.0, 0.0},
	{-1.0, 1.0, 0.0, 0.0},
	{ 0.0,-1.0, 0.0, 0.0},
	{ 0.0, 0.0, 0.0, 0.0},
	{ 0.0, 1.0, 0.0, 0.0},
	{ 1.0,-1.0, 0.0, 0.0},
	{ 1.0, 0.0, 0.0, 0.0},
	{ 1.0, 1.0, 0.0, 0.0}};
#endif

// Shadow mapping pixel shader
float4 ShadowPS (in float4 inPos: POSITION,
					in float3 inNormal: TEXCOORD0,
					in float4 inDepth: TEXCOORD1) : COLOR0
{
// Calculate the diffuse term:

// Calculate the shadow term
#ifdef USE_PCF
	float fShadow = 0.0;
	for (int i=0; i < 9; i++)
	{
		float4 fTap = inDepth + fPCF*fTaps_PCF[i];
		fShadow += fDist(fTap,inDepth.z)/9;
	}
#else
	float fShadow = fDist(inDepth,inDepth.z);
#endif		
	return float4(1,1,1,0) * fShadow;// * fDiffuse;
}

technique techShadow
{
	pass p0
	{
		VertexShader = compile vs_2_0 ShadowVS();
		PixelShader  = compile ps_2_0 ShadowPS();
	}
}


and the new ShadowFilter.fx
Code:
texture TargetMap;
texture shadowT_bmap;

sampler2D smpScreen = sampler_state
{
	Texture = <TargetMap>;
	
	MinFilter	= linear;
	MagFilter	= linear;
	MipFilter	= linear;
	
	AddressU		= Clamp;
	AddressV		= Clamp;
};

sampler2D smpShadow = sampler_state
{
	Texture = <shadowT_bmap>;
	
	MinFilter	= linear;
	MagFilter	= linear;
	MipFilter	= linear;
	
	AddressU		= Clamp;
	AddressV		= Clamp;
};

float4 Shader(float2 tex : TEXCOORD0) : COLOR
{
	float4 Color = tex2D(smpScreen, tex);
	float4 Color2 = tex2D(smpShadow, tex);
	Color2.a = 1.0 -(Color2.r + Color2.b + Color2.g)/3;
	Color = lerp(Color,float4(0,0,0,1),Color2.a);
	
	return Color;
}

technique tech_00
{
	pass pass_00
	{
		VertexShader = null;
		PixelShader = compile ps_2_0 Shader();
	}
}


but i think it only works on graphics cards, which are suporting "render to texture".
Plese don't forget that tha shadow view must have the same position, rotation, aspect,arc then the camera, elseway it wont work...
Have fun with it! wink
EDIT: i've removed some pointless lines maby i've removed somthing non pointless with it.
xxxxxxx

Last edited by xxxxxxx; 05/11/11 19:19.

Es ist immer wieder erstaunlich, dass Leute die riesen Scripte schreiben die einfachsten sachen nicht können zb. mich mit SIEBEN x zu schreiben! tongue

Moderated by  Blink, Hummel, Superku 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1