我正在使用directx11,我创建光源(平行光源)和场景(从obj文件加载),并且每件事看起来都正确但是当我在场景中旋转一些物体时,我的光也随着物体旋转,而且我不会旋转灯并保持固定。
我尝试通过这样做来解决问题,但我失败了:
XMMATRIX light_rotat = XMMatrixIdentity()
/*render the light*/
XMMATRIX light_rotat = XMMatrixRotationY(timeGetTime()/3500.0f);
/* render scene */
/ * hlsl代码* /
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() ) );
}
}
这是渲染循环
Device->Draw(Color);
t.localworld = ::XMMatrixTranspose ( XMMatrixRotationY ( timeGetTime() /3000.0f ) );
t.eye = XMFLOAT3( 0.0f, 10.0f, -30.0f );
local->UpdateSubresource (My_Buffer , 0 , NULL ,0, 0 , &t );
ID3DX11EffectConstantBuffer *cm = effect->Get_Effect()->GetConstantBufferByName ( "local");
cm->SetConstantBuffer ( Local->Get_Buffer());
effect->Apply(Flags , Context);
mesh->draw();
Device->EndDraw();
任何帮助?
答案 0 :(得分:0)
好的,'localworld'矩阵应该是这里的关键,你需要做的是创建一个旋转矩阵Y,并在你的渲染中将localworld变量写入HLSL之前,将localworld变量与此旋转矩阵相乘环。这应该只旋转你的顶点,你的光应该保持静止。