I'm learning to use shaders in Dbpro
If tried to use the "Normal mapping.dbs" shader that comes with Dark Shader.
I followed the Dark Shader manual to pass lightning information to the Shader.
The Shader works well in Dark Shader. I exported the object and the textures with Dark Shader.
I tried a lot of things but the objects stays appearing as a black cube.
What am I doing wrong? Can someone help me, please?
I attach the my code.
rem Initialise
sync on
sync rate 0
rem I use these array because the Dark Shader commands lightred(i), lightgreen(i),lightblue(i) don't compile
dim xred(8) as integer
dim xgreen(8) as integer
dim xbleu(8) as integer
rem Load model and shader effect
load object "cube.dbo",1
rem I commented this out because there is a reference to the textures in the shader
rem It does not make a difference if I load the textures myself
remstart
load image "wallc11.jpg",1
load image "wallc11 bump.jpg",2
texture object 1,0,1
texture object 1,1,2
remend
autocam off
load effect "Normal Mapping.dbs",1,1
set object effect 1,1
set effect technique 1,"DBLight8" rem I tried the other techniques
rem for light 0
Color light 0,255,255,255
xred(1) = 255
xgreen(1) = 255
xbleu(1) =255
for i = 1 to 7
make Light i
READ r,g,b
xred(i+1)= r
xgreen(i+1)=g
xbleu(i+1)=b
color light i,r,g,b
read x,y,z
position light i,x,y,z
rem i did this to see where my lights are positioned
make object sphere 10 +i,2
Position object 10+i,x,y,z
rem
read ra
set light range i,ra
SHOW LIGHT i
next i
null= make vector4(1)
set vector4 1,ambientR,ambientG,ambientB,0.0 rem ambientR,... are dark Shader Commands
set effect constant vector 1,"AMBIENTCOLOR",1 rem constant string from shader
rem set effect constant vector 1,"AmbientColor",1 rem constant string from manual
x=camera Position X ()
y=camera Position Y ()
z=camera Position Z ()
set vector4 1,x,y,z,0.0
rem set effect constant vector 1,"CameraPosition",1
rem set effect constant float 1,"TIME",1.0
rem Draw main camera
rem sync mask %001 :
Element = get object effect (1,-1)
for i = 0 to 7
xx= light Position X (i)
yy= light Position Y (i)
zz = light Position Z (i)
rr= light range (i)
set vector4 1,xx,yy,zz,rr
set effect constant vector element "LIGHTPOSITION",i,1 rem constant string from shader
rem set effect constant vector element "LightPos",i,1 rem constant string from manual
rr= xred(i+1)
gg=xgreen(i+1)
bb=xbleu(i+1)
remstart
Normally I would use
rr= lightred(i)
gg=lightgreen(i)
bb=lightbleu(i)
remend
set vector4 1,rr,gg,bb,1.0
set effect constant vector element "LIGHTCOLOR",i,1
rem set effect constant vector element "LightColor",i,1
NEXT
do
sync
loop
null = delete vector4(1)
delete effect pointer
data 255,0,0,0,100,0,500
data 0,255,0,100,0,0,500
data 0,0,255,0,-100,0,500
data 255,255,0,-100,0,0,500
data 0,255,255,0,0,100,500
data 255,0,0,0,0,-100,500
data 255,0,0,0,100,-100,500
the shader code
string Description = "This shader uses preview lights to produce per pixel normal mapped lighting. This shader requires lights.";
string Thumbnail = "Normal Mapping.png";
//--------------
//Untweakables
//--------------
float4x4 WorldViewProj : WorldViewProjection;
float4x4 World : World;
float4x4 WorldT : WorldTranspose;
float4x4 WorldIT : WorldInverseTranspose;
float4 LightPos[8] : LIGHTPOSITION;
float3 LightColor[8] : LIGHTCOLOR;
float3 AmbientColor : AMBIENTCOLOR;
float3 eyepos : CameraPosition;
float time : TIME;
//--------------
//Tweakables
//--------------
float SpecularPower1
<
string UIWidget = "slider";
float UIMax = 30;
float UIMin = 1;
> = 15.000000;
texture BaseTex
<
string ResourceName = "wall11c.jpg";
>;
sampler diffuse_smp = sampler_state
{
Texture = <BaseTex>;
MinFilter = Anisotropic;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
texture NormalMap
<
string ResourceName = "wall11c bump.jpg";
>;
sampler normalmap_smp = sampler_state
{
Texture = <NormalMap>;
MinFilter = Linear;
MagFilter = Linear;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
struct app_in
{
float4 pos : POSITION;
float3 normal : NORMAL0;
float3 tangent : TANGENT0;
float3 binormal : BINORMAL0;
float2 uv : TEXCOORD0;
};
struct vs_out
{
float4 pos : POSITION;
float2 uv : TEXCOORD0;
float3 normal : TEXCOORD1;
float3 tangent : TEXCOORD2;
float3 binormal : TEXCOORD3;
float4 wpos : TEXCOORD4;
};
vs_out VS( app_in IN )
{
vs_out OUT;
float4 pos = mul( IN.pos, WorldViewProj );
OUT.pos = pos;
OUT.wpos = mul( IN.pos, World );
OUT.uv = IN.uv;
float3 normal = normalize(mul(IN.normal.xyz,(float3x3)World ));
float3 tangent = normalize(mul(IN.tangent.xyz,(float3x3)World ));
float3 binormal = normalize(mul(IN.binormal.xyz,(float3x3)World ));
//smooth out the tangents and binormals with the normals
float3 b = normalize(cross( normal,tangent ));
b *= sign(dot(b,binormal));
float3 t = normalize(cross( normal,b ));
t *= sign(dot(t,tangent));
float3 t2 = normalize(cross( normal,binormal ));
t2 *= sign(dot(t2,tangent));
float3 b2 = normalize(cross( normal,t2 ));
b2 *= sign(dot(b2,binormal));
//pass normal, tangent, and binormal to pixel shader
OUT.normal = normal;
OUT.tangent = normalize((t+t2)*0.5);
OUT.binormal = normalize((b+b2)*0.5);
return OUT;
}
float luminance ( float3 rgb )
{
return rgb.r*0.3 + rgb.g*0.59 + rgb.b*0.11;
}
float4 PS( vs_out IN, uniform int numLights ) : COLOR
{
float3 color = AmbientColor;
float3 n = normalize(IN.normal);
float3 t = normalize(IN.tangent);
float3 b = normalize(IN.binormal);
//build transpose matrix
float3x3 TangentSpace = {t,b,n};
TangentSpace = transpose(TangentSpace);
n = normalize(tex2D(normalmap_smp, IN.uv)*2 - 1);
float3 e = normalize(eyepos - IN.wpos);
float eyeDist = length(eyepos - IN.wpos);
e = mul(e,TangentSpace);
float4 texColor = tex2D( diffuse_smp, IN.uv );
//cycle through all lights, number depending on technique chosen
for ( int i = 0; i < numLights; i++ )
{
float range = LightPos[i].w;
if ( range > 0 )
{
float3 l = (LightPos[i].xyz - IN.wpos.xyz);
l = mul(l,TangentSpace);
//calculate attenuation
float dist = length(l);
float att = saturate((range-dist) / range);
//calculate diffuse lighting
l = normalize(l);
float diffuse = saturate(dot(n,l));
//caculate specular lighting
float3 h = normalize(l+e);
float spec = pow( saturate(dot(n,h)), SpecularPower1 )*(1-pow(1-diffuse,10));
//add to final color
color += (diffuse * att * LightColor[i]) + (spec * att * LightColor[i] * smoothstep(0.3,0.6,luminance(texColor.xyz)));
}
}
return float4(color,1.0) * texColor;
}
//choose the technique corresponding to the number of lights in your application
technique PerPixelLighting
{
pass p0
{
VertexShader = compile vs_2_0 VS( );
PixelShader = compile ps_2_0 PS( 0 );
}
}
technique DBLight1
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 1 );
}
}
technique DBLight2
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 2 );
}
}
technique DBLight3
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 3 );
}
}
technique DBLight4
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 4 );
}
}
technique DBLight5
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 5 );
}
}
technique DBLight6
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 6 );
}
}
technique DBLight7
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 7 );
}
}
technique DBLight8
{
pass p0
{
VertexShader = compile vs_1_1 VS( );
PixelShader = compile ps_2_a PS( 8 );
}
}