C++ directx11点光源问题。从原点转换的对象未正确照亮
正如标题中所述,我的问题是我的点光源似乎没有考虑任何对象的当前位置。它似乎照亮了对象,就好像它仍在围绕原点绘制一样 例如: 我正在使用的当前场景包含两个长方体,第一个长方体尚未平移,因此仍然位于原点周围,我已增加了该长方体的z轴比例,将其拉伸,灯光似乎与对象的新大小配合良好。 第二个长方体已沿z轴向后平移,并已将x轴和y轴放大以创建一种墙。此长方体上看不到任何灯光,直到灯光本身离开长方体,此时表面上会出现一个亮点,就像灯光从长方体上的该点发出一样 当灯光到达长方体曲面应保持在原点的位置时,会发生这种情况 我希望上面的示例能够解释我遇到的问题,这可能是由于我将矩阵传递给着色器,但我无法找到哪里出了问题 更新矩阵和渲染代码C++ directx11点光源问题。从原点转换的对象未正确照亮,c++,directx,hlsl,directx-11,lighting,C++,Directx,Hlsl,Directx 11,Lighting,正如标题中所述,我的问题是我的点光源似乎没有考虑任何对象的当前位置。它似乎照亮了对象,就好像它仍在围绕原点绘制一样 例如: 我正在使用的当前场景包含两个长方体,第一个长方体尚未平移,因此仍然位于原点周围,我已增加了该长方体的z轴比例,将其拉伸,灯光似乎与对象的新大小配合良好。 第二个长方体已沿z轴向后平移,并已将x轴和y轴放大以创建一种墙。此长方体上看不到任何灯光,直到灯光本身离开长方体,此时表面上会出现一个亮点,就像灯光从长方体上的该点发出一样 当灯光到达长方体曲面应保持在原点的位置时,会发生
//Temporary Matricies
XMMATRIX worldMatrix;
XMMATRIX modelMatrix;
worldMatrix = worldMx;
//Rotate the model Matrix
XMFLOAT3 TempRot = m_GeometryList[ID].GetRotation();
worldMatrix = XMMatrixRotationRollPitchYaw(TempRot.x, TempRot.y, TempRot.z);
//Scale the model matrix
XMFLOAT3 TempScale = m_GeometryList[ID].GetScale();
worldMatrix *=XMMatrixScaling(TempScale.x, TempScale.y, TempScale.z);
//translate into world space
XMFLOAT3 TempPos = m_GeometryList[ID].GetPosition();
worldMatrix *= XMMatrixTranslation(TempPos.x, TempPos.y, TempPos.z);
//Create the model matrix
modelMatrix = XMMatrixIdentity();
modelMatrix = worldMatrix * viewMx * projMx;
// Update the constant buffer
_WorldViewProjMx->SetMatrix((float*)&modelMatrix);
// Update the World Matrix
//worldMatrix = XMMatrixTranspose(worldMatrix);
_WorldMx->SetMatrix((float*)&worldMatrix);
////(Earlier in the code)
_WorldViewProjMx = _fX->GetVariableByName("worldViewProj")->AsMatrix();
_WorldMx = _fX->GetVariableByName("worldMatrix")->AsMatrix();
/////
着色器
// Shaders
struct VertexIn {
float3 pos : POSITION;
float4 color : COLOR;
float2 TexCoord : TEXCOORD;
float3 Normal : NORMAL;
};
struct VertexOut {
float4 posH : SV_POSITION;
float4 color : COLOR;
float2 TexCoord : TEXCOORD0;
float3 Normal : NORMAL;
float3 ViewDirection : TEXCOORD1;
float4 posW : POSITION;
};
VertexOut RenderSceneVS(VertexIn vin) {
VertexOut vout;
vout.posH = mul(float4(vin.pos, 1.0f), worldViewProj);
vout.Normal = mul(vin.Normal, worldMatrix);//worldInverseTranspose);
vout.color = vin.color;
vout.TexCoord = vin.TexCoord;
// needed for point lights
vout.posW = mul(vin.pos, worldMatrix);
// needed for specular lighting
vout.ViewDirection = cameraPosition.xyz - vout.posW.xyz;
vout.ViewDirection = normalize(vout.ViewDirection);
return vout;
}
float4 RenderPointDiffusePS(VertexOut pin) : SV_TARGET0
{
float4 colour = Texture.Sample(Sampler, pin.TexCoord);
//Create the vector between light position and pixels position
float3 lightToPixelVec = light.pos - pin.posW;
//Find the distance between the light pos and pixel pos
float d = length(lightToPixelVec);
//Create the ambient light
float3 finalAmbient = colour * light.ambient;
//If pixel is too far, return pixel color with ambient light
if( d > light.range )
{
return float4(finalAmbient, colour.a);
}
//Turn lightToPixelVec into a unit length vector describing
//the pixels direction from the lights position
lightToPixelVec /= d;
//Calculate how much light the pixel gets by the angle
//in which the light strikes the pixels surface
float howMuchLight = dot(lightToPixelVec, pin.Normal);
float3 tempColor = float3(0.0f, 0.0f, 0.0f);
//If light is striking the front side of the pixel
if( howMuchLight > 0.0f )
{
//Add light to the finalColor of the pixel
tempColor += howMuchLight * colour * light.diffuse;
//Calculate Light's Falloff factor
tempColor /= light.att[0] + (light.att[1] * d) + (light.att[2] * (d*d));
}
float3 finalColor = saturate(tempColor + finalAmbient);
return float4(finalColor, colour.a);
}
欢迎提供任何帮助/建议,我不知道是什么导致了这个问题。
如果你需要更多的信息,请告诉我
提前感谢您的帮助。此行:
vout.posW=mul(vin.pos,worldMatrix)代码>
应该是这样的:vout.posW=mul(float4(vin.pos,1),worldMatrix)代码>
因为现在你没有把平移应用到你的posW向量上