镜面光源在原点(0,0,0)处断开

时间:2015-09-15 23:58:20

标签: directx hlsl specular

我有问题让镜面光照工作。看起来我的应用程序中存在某些我无法跟踪的错误。

光线从正面传来(截图显示相机在-z方向看,左边是-x)。

一个简单的镜面反射输出显示以下失败:

enter image description here

使用的代码:

    float3 n = normalize(input.normal); // world space normal, mul(float4(normal, 0.0), modelMatrix)
    float3 l = normalize(-sDirection); // constant direction (like 0.7, -0.8, -0.7)
    float3 v = normalize(viewPos.xyz - input.vertexWorldSpace.xyz); // viewpos = world space camera position
    float3 LightReflect = normalize(reflect(n,l));
    float SpecularFactor = dot(v, LightReflect);

    color = float4(SpecularFactor, SpecularFactor, SpecularFactor, 1.0);

为了检查可能的变量错误,我检查了input.VertexWorldSpace:

enter image description here

还检查lightdirection和normal,我检查了漫反射术语:

enter image description here

和相机到顶点视图矢量(v): enter image description here

对我来说,所有部件看起来都很好,但镜面反射在原点(0,0,0)处仍然是黑色并垂直于光线方向。

我还制作了一个gif,显示了原点(0,0,0)视图方向向量发生的情况

http://imgur.com/2YlqcGP

另一个显示相机位置的gif以及镜面变黑的位置 http://i.imgur.com/ajUaekA.gifv

我对v使用了错误的计算吗?

1 个答案:

答案 0 :(得分:0)

好的,问题在于我的缓冲区对齐:https://msdn.microsoft.com/en-us/library/windows/desktop/bb509632(v=vs.85).aspx

而不是传递

Matrix
Matrix 
Vec3
Vec3

hlsl想要16字节对齐矢量 所以我不得不使用

Matrix
Vec3
float1 // 16 byte alignment
Vec3
float1 // 16 byte alignment
相关问题