我發現了幾個奇怪HLSL錯誤 - 或Pix的是告訴無義:HLSL意想不到ACOS導致
我有2個正交向量:A = {0.0F,-1.0F,0.0F}和B {0.0F, 0.0f,1.0f}
如果我使用HLSL點函數,輸出是(-0.0f),這很有意義,但現在該輸出的acos是-0.0000675917(這就是Pix說的 - 這是什麼着色器輸出)這不是我所期望的;即使我自己計算點積(A.x * B.x + A.y * B.y +等),結果仍然是0.0f,但是我的結果的最終結果並不爲零。
我確實需要acos的結果儘可能精確,因爲我想根據三角法線和給定矢量之間的角度爲我的頂點着色。
float4 PS_MyPS(VS_OUTPUT input) : COLOR
{
float Light = saturate(dot(input.Normal, g_LightDir)) + saturate(dot(-input.Normal, g_LightDir)); // compute the lighting
if (dot(input.Vector, CameraDirection) < 0) // if the angle between the normal and the camera direction is greater than 90 degrees
{
input.Vector = -input.Vector; // use a mirrored normal
}
float angle = acos(0.0f) - acos(dot(input.Vector, Vector));
float4 Color;
if (angle > Angles.x) // Set the color according to the Angle
{
Color = Color1;
}
else if (angle > Angles.y)
{
Color = Color2;
}
else if (angle >= -abs(Angles.y))
{
Color = Color3;
}
else if (angle >= Angles.z)
{
Color = Color4;
}
else
{
Color = Color5;
}
return Light * Color;
}
它適用於0.01度以上的角度,但對於較小的值會給出錯誤的結果。
我發現的其他錯誤是:hlsl中的「length」函數在Pix中向量(0,-0,-0,0)返回1,並且該向量上的HLSL函數「any」返回true好。這意味着-0.0f!= 0.0f。
有沒有其他人遇到這些,也許有我的問題的解決方法? 我在Intel HD Graphics 4600和Nvidia卡上測試了它,結果相同。
提供一個[較短,自成體系,正確的示例]這個問題(http://sscce.org)。 – Oswald
如果對A = {0.0f,-1.0f,0.0f}和B {0.0f,0.0f,1.0f}使用HLSL點函數,並且輸出爲-0.0f(根據您的問題,這是有意義的) ,爲什麼你將錯誤歸因於HLSL而不是'acos'函數? – Oswald
你是什麼意思*似乎有長度1 *?你是如何測試它的? – Oswald