OpenGL 聚光灯与 Cg

发布于 2024-10-09 00:53:02 字数 985 浏览 3 评论 0原文

我正在尝试用 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 ,聚光灯根本不起作用。

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

我对此的主要参考是第 5 章(灯光),来自 Cg 教程

I'm trying to accomplish a spotlight effect with Cg.
I've already managed to do normal ambient and diffuse lighting.

I understand the basic functionality of a spotlight (position, direction, cutoff angle), yet handling these in Cg still eludes me.

This is how I compute the spotlight parameters:

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 is the pixel the spotlight is pointed to ( ex: (0, 0, 0) )

lightPosition is the position of the light

P is the point i am trying to light. IN.position comes from the vertex shader, and it's already multiplied with modelViewProj.

angle is the cosine of the angle between the direction of the light and the direction from the light, to the point I'm trying to light.

The problem is that changing the direction of the light does not affect the spotlight whatsoever.It will always be centered in 0,0,0.
If I change lightPosition, the spotlight changes, but it still starts from 0,0,0 and expands oppsite the position of the light

Another thing is that when I compute the direction vector, I use lightPosition, instead of lightP.If I use lightP, the spotlight won't work at all.

Also the spotlight only lights on half of the scene.

My main reference for this was Chapter 5(Lighting), from The Cg Tutorial.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-10-16 00:53:02

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

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

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

Here's my take on this, I think your vectors are pointing in the wrong directions (when you do the subtractions):

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

A bit late, but I hope that helps :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文