2

我正在尝试用 Cg 完成聚光灯效果。我已经设法进行正常的环境和漫射照明。

我了解聚光灯的基本功能(位置、方向、截止角),但在 Cg 中处理这些仍然让我难以理解。

这就是我计算聚光灯参数的方式:

float4 dir_aux = mul(ModelViewProj, direction);
float4 lightP_aux = mul(ModelViewProj, lightPosition);

float3 lightP = lightP_aux.xyz;
float3 dir = normalize(dir_aux.xyz);

float3 P = IN.position;
float3 V = normalize(lightP - P);
dir = normalize(lightPosition - dir);

float angle = dot(V, dir);

方向是聚光灯指向的像素(例如: (0, 0, 0) )

lightPosition是灯光的位置

P是我要强调的重点。IN.position 来自顶点着色器,它已经与 modelViewProj 相乘。

角度是光的方向和光的方向之间的角度的余弦,到我试图照亮的点。

问题是改变光的方向不会影响聚光灯。它总是以 0,0,0 为中心。如果我改变 lightPosition,聚光灯会改变,但它仍然从 0,0,0 开始并在灯光的位置对面扩展

另一件事是,当我计算方向向量时,我使用 lightPosition,而不是 lightP。如果我使用 lightP,聚光灯根本不起作用。

聚光灯也只照亮一半的场景。

我的主要参考资料是The Cg Tutorial中的第 5 章(照明)。

4

1 回答 1

3

这是我对此的看法,我认为你的向量指向错误的方向(当你做减法时):

// direction is actually the location of the target
float4 target = direction; // renamed for clarity   

float4 target_aux = mul(ModelViewProj, target);
float4 lightP_aux = mul(ModelViewProj, lightPosition);

float3 lightP = lightP_aux.xyz;
float3 targetXYZ = target.xyz;

// don't normalise this it's a location at this point, NOT a direction vector
//float3 dir = normalize(dir_aux.xyz);

float3 P = IN.position;
// reversed to give vector from light source to IN.Position
float3 V = normalize(P - lightP);

// reversed to give vector from light source to target
float3 dir = normalize(targetXYZ - lightP);

float angle = dot(V, dir);

有点晚了,但我希望这会有所帮助:)

于 2011-01-31T22:35:01.590 回答