the worl matrix and loca object matrix help

Started by
0 comments, last by dpadam450 12 years, 4 months ago
i am using directx11 , and i create light source ( parallel light source )and scene ( loaded from obj file ) , and every thing look correct but when i rotate some object in the scene , my light rotate too with the object , and i don't wont to rotate the light and keep it fixed .

i try to fix the problem by do something like this , but i fail :




hlsl code


cbuffer LIGHT
{
float4 light_color ;
float3 Direction ;
float3 Position ;
float3 attribute ;
float Power ;
float range ;
float spotpower ;


};

cbuffer CAMERA
{
float4x4 view ;
float4x4 world ;
float4x4 proj ;
};

cbuffer local
{
float3 eye;
float4x4 localworld ;

};
Texture2D texture_obj ;
SamplerState Texture_sampler ;
/* diffuse texture */
float4 Get_Texture (float2 uv )

{
return texture_obj.Sample (Texture_sampler , uv );

}

/* light calc */
float4 Parrallel (float3 eye ,float3 Position ,float3 Normal , MTRL mtrl )
{

float3 lightvec = normalize(-Direction) ;

float4 LitColor = float4(0.0f , 0.0f ,0.0f ,0.0f );

float diff_factor = dot(lightvec , Normal );
float4 diff = light_color * mtrl.Diffuse_Mtrl ;

if (diff_factor > 0.0f )
{
float4 amb = light_color * mtrl.Ambient_Mtrl ;

float3 view = normalize(eye - Position) ;


float3 rf = normalize(reflect (Direction , Normal )) ;



float4 Spec_Fac = pow ( max ( dot (rf , view ) , 0.0f ) , max (1.0f ,Power ));
float4 Spec = light_color * mtrl.Specular_Mtrl ;

LitColor += ( diff *diff_factor) + (Spec_Fac*Spec) + (amb*diff_factor) ;
}
return LitColor ;

}

struct VS_INPUT
{
float4 Pos :POSITION ;
float3 Normal : NORMAL0 ;
float2 UV : TEXCOORD0 ;
};


struct VS_OUT
{
float4 Pos :SV_Position ;
float4 Posw : POSITION ;
float4 Normal : TEXCOORD1 ;
float2 UV : TEXCOORD0 ;


};



VS_OUT VS (VS_INPUT input)
{
VS_OUT v ;





v.Pos = mul ( input.Pos , localworld );
v.Pos = mul (v.Pos , world );
v.Pos = mul (v.Pos , view );
v.Pos = mul (v.Pos , proj );


v.Posw = mul ( input.Pos, localworld );

v.UV = input.UV ;

v.Normal = mul (float4(input.Normal , 0.0f) ,world );

return v ;
}


float4 PS (VS_OUT ps):SV_Target
{

MTRL y ;
y.Diffuse_Mtrl = Get_Texture (ps.UV);
y.Ambient_Mtrl = y.Diffuse_Mtrl /8 ;
y.Specular_Mtrl = float4 ( 0.5f , 0.5f , 0.5f , 0.0f );
float4 licolor = Parrallel ( eye , (float3)ps.Posw , (float3)ps.Normal ,y ) ;


return licolor ;
}



technique11 tech2
{
pass P0
{
SetVertexShader( CompileShader( vs_5_0, VS() ) );
SetPixelShader( CompileShader( ps_5_0, PS() ) );
}

Advertisement
Your light and normal have to be in the same space. So if your normal gets applied by the camera matrix, you need to multiply the light by the camera matrix so that they are both relative to the viewer.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

This topic is closed to new replies.

Advertisement