【发布时间】:2014-05-16 00:40:56
【问题描述】:
正如标题中所述,我的问题是我的点光源似乎没有考虑任何对象的当前位置。它似乎照亮了对象,就好像它仍在原点周围绘制一样。
示例: 我正在使用的当前场景包含两个框,第一个还没有被平移,所以仍然围绕原点,我增加了这个框的 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);
}
欢迎任何帮助/建议,我无法理解导致此问题的原因。 如果您需要更多信息,请告诉我。
提前感谢您的帮助。
【问题讨论】:
标签: c++ directx hlsl directx-11 lighting