【问题标题】:OpenGL spotlight with Cg带 Cg 的 OpenGL 聚光灯
【发布时间】:2012-10-01 17:53:30
【问题描述】:

我正在尝试使用 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);

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

lightPosition是灯光的位置

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

角度是光的方向和从光的方向到我要照亮的点之间的夹角的余弦。

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

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

此外,聚光灯只照亮一半的场景。

我对此的主要参考是第 5 章(照明),来自 The Cg Tutorial

【问题讨论】:

    标签: opengl lighting cg


    【解决方案1】:

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

    // 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);
    

    有点晚了,但我希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-29
      • 1970-01-01
      相关资源
      最近更新 更多