[solved]HLSL problem

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
r-type
Posts: 41
Joined: Sun Feb 12, 2006 1:54 am

[solved]HLSL problem

Post by r-type »

Hihi,

This piece is driving me nuts! it's simple and im prolly overlooking something utterly trivial but after hours of staring i cant seem to find it.

The deal, simple lighting using HLSL lit function.

The problem, when I transform the object the lighting isnt right(as though the object still is at (0,0,0). I am multiplying the vertex positions by the WVP matrix. Any ideas ? many thanks in advance!

Code: Select all


float4x4 mWorldViewProj ; 
float4 fLightLocA; 
float4 fLightLocB;
float4 fLightLocC;
float3 mCamPos; 

struct transfer
{   	
     float4 tex0		: TEXCOORD0; 
     float3  lightingA		: TEXCOORD1;    
     float4  position		: POSITION; 
};

transfer vertexMain(	
			float4 position	: POSITION ,  
			float3 normal	: NORMAL ,  
			
			float4 tex0	: TEXCOORD0				
			
			)
{
	transfer OUT;

	float4 pos = mul( position,mWorldViewProj);
	
	float3 pos2cam    = normalize( mCamPos - pos);
	
	float3 pos2lighta = normalize( fLightLocA - pos);
	
	
	float3 halfwaya = (pos2lighta + pos2cam) / 2.0;
		
	OUT.lightingA = lit(dot(pos2lighta,normal), dot(halfwaya,normal), 2);
			
	OUT.position = pos;
	OUT.tex0 = tex0;     


	return OUT;
}



float4 pixelMain(transfer IN ) : COLOR0
{
	
	float4 color = IN.lightingA.z;
   	
  	return color;
}

Feeding in the WVP matrix like so:

Code: Select all

core::matrix4 worldViewProj;		
	worldViewProj  = driver->getTransform(video::ETS_PROJECTION);
	worldViewProj *= driver->getTransform(video::ETS_VIEW);
	worldViewProj *= driver->getTransform(video::ETS_WORLD);
				
	services->setVertexShaderConstant("mWorldViewProj", reinterpret_cast<f32*>(&worldViewProj.M[0]), 16);
edit: Im multiplying bot normal and position with the worldmatrix, last parameter for the lit function must range from 0 to 1.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

This doesn't solve your problem, but the reinterpret cast should not be necessary. You should be able to write...

Code: Select all

services->setVertexShaderConstant("mWorldViewProj", worldViewProj.M, 16);
Also, shouldn't the matrices be multiplied in the correct order...

Code: Select all

core::matrix4 worldViewProj;        
worldViewProj  = driver->getTransform(video::ETS_WORLD); 
worldViewProj *= driver->getTransform(video::ETS_VIEW); 
worldViewProj *= driver->getTransform(video::ETS_PROJECTION); 
Travis
Post Reply