Ok I fixed it. IT denotes Inverse so the full set of matricies is:
1. World
2. View
3. Projection
4. WorldIT
5. ViewIT
6. ProjectionIT (I am guessing at this one)
None of these should need to be set explicitly for the effect. To get the transpose of any of the above you need to set the matrix (or the inverse matrix) parameter and use "SET EFFECT TRANSPOSE" which will transpose all of the matricies explicitly sent to the effect.
So the working shader code is:
string description = "Simple Test Shader For DarkBasic";
//------------------------------------
float4x4 worldViewProj : WorldViewProjection;
float4x4 world : World;
float4x4 worldInverseTranspose : WorldInverseTranspose;
float4x4 viewInverse : ViewIT;
float4 lightDir : Direction
<
string Object = "DirectionalLight";
string Space = "World";
> = {1.0f, -1.0f, 1.0f, 0.0f};
float4 lightColor : Diffuse
<
string UIName = "Diffuse Light Color";
string Object = "DirectionalLight";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float4 lightAmbient : Ambient
<
string UIWidget = "Ambient Light Color";
string Space = "material";
> = {0.0f, 0.0f, 0.0f, 1.0f};
float4 materialDiffuse : Diffuse
<
string UIWidget = "Surface Color";
string Space = "material";
> = {0.0f, 1.0f, 1.0f, 1.0f};
float4 materialSpecular : Specular
<
string UIWidget = "Surface Specular";
string Space = "material";
> = {1.0f, 1.0f, 1.0f, 1.0f};
float shininess : SpecularPower
<
string UIWidget = "slider";
float UIMin = 1.0;
float UIMax = 128.0;
float UIStep = 1.0;
string UIName = "specular power";
> = 30.0;
//------------------------------------
struct vertexInput {
float3 position : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 hPosition : POSITION;
float4 diffAmbColor : COLOR0;
float4 specCol : COLOR1;
};
//------------------------------------
vertexOutput VS_TransformAndTexture(vertexInput IN)
{
vertexOutput OUT;
OUT.hPosition = mul( float4(IN.position.xyz , 1.0) , worldViewProj);
float3 worldEyePos = viewInverse[3].xyz;
float3 worldVertPos = mul(IN.position, world).xyz;
float4 N = mul(IN.normal, worldInverseTranspose);
float3 E = normalize(worldEyePos - worldVertPos);
float3 L = normalize( -lightDir.xyz);
float3 H = normalize(E + L);
float diff = max(0 , dot(N,L));
float spec = pow( max(0 , dot(N,H) ) , shininess );
if( diff <= 0 )
{
spec = 0;
}
float4 ambColor = materialDiffuse * lightAmbient;
float4 diffColor = materialDiffuse * diff * lightColor ;
OUT.diffAmbColor = diffColor + ambColor;
float4 specColor = materialSpecular * lightColor * spec;
OUT.specCol = specColor;
return OUT;
}
//-----------------------------------
float4 PS_Textured( vertexOutput IN): COLOR
{
return IN.diffAmbColor + IN.specCol;
}
//-----------------------------------
technique simple
{
pass p0
{
VertexShader = compile vs_1_1 VS_TransformAndTexture();
PixelShader = compile ps_1_1 PS_Textured();
}
}
The only changes being setting the viewInverse constant to use the semantic ViewIT.
And the fixed DB code is:
sync on : sync rate 60
#constant LIGHT_DIR 1
#constant LIGHT_COL 2
#constant LIGHT_AMB 3
#constant MAT_DIFF 4
#constant MAT_SPEC 5
#constant MATRIX_WORLD_INV_TRANS 1
make object sphere 1, 1, 50, 50
convert object fvf 1, FVF_XYZ || FVF_NORMAL
load effect "simple.fx", 1, 0
perform checklist for effect values 1
perform checklist for effect errors 1
r = make vector4(DIFFUSE_VECTOR)
set vector4 DIFFUSE_VECTOR, 1.0, 1.0, 1.0, 1.0
r = make vector4(LIGHT_DIR)
set vector4 LIGHT_DIR, 1.0, -1.0, 0.0, 0.0
r = make vector4(LIGHT_COL)
set vector4 LIGHT_COL, 1.0, 1.0, 1.0, 1.0
r = make vector4(LIGHT_AMB)
set vector4 LIGHT_AMB, 0.0, 0.0, 0.0, 1.0
r = make vector4(MAT_DIFF)
set vector4 MAT_DIFF, 1.0, 1.0, 0.0, 1.0
r = make vector4(MAT_SPEC)
set vector4 MAT_SPEC, 1.0, 1.0, 1.0, 1.0
r = make matrix4(MATRIX_WORLD_INV_TRANS)
world matrix4 MATRIX_WORLD_INV_TRANS
r = inverse matrix4(MATRIX_WORLD_INV_TRANS, MATRIX_WORLD_INV_TRANS)
set effect transpose 1, 1
rem set effect constant matrix 1, "worldViewProj", MATRIX_WORLD_VIEW_PROJ
rem set effect constant matrix 1, "world", MATRIX_WORLD
rem set effect constant matrix 1, "viewInverse", MATRIX_VIEW_INV
set effect constant matrix 1, "worldInverseTranspose", MATRIX_WORLD_INV_TRANS
set effect constant vector 1, "lightDir", LIGHT_DIR
set effect constant vector 1, "lightColor", LIGHT_COL
set effect constant vector 1, "lightAmbient", LIGHT_AMB
set effect constant vector 1, "materialDiffuse", MAT_DIFF
set effect constant vector 1, "materialSpecular", MAT_SPEC
set effect constant float 1, "shininess", 30.0
set object effect 1, 1
do
set cursor 0, 0
printchecklist()
sync
loop
function printCheckList()
count = checklist quantity()
for i = 1 to count
print checklist string$(i)
next i
endfunction
Phew!
Neophyte:
I know what the identity matrix is, but the fx browser seems to be setting all matrix parameters to it, that shouldn't work - having the world, view and projection matricies as the identity will not transform anything! Of course I could be reading the code wrong (not really studied it).
The constant lightDir is being set (although it's not doing much right now, but that's a much simpler problem to solve).
Thanks for the help guys.
Darren