OpenGL 聚光灯与 Cg
我正在尝试用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我对此的看法,我认为你的向量指向错误的方向(当你做减法时):
有点晚了,但我希望有所帮助:)
Here's my take on this, I think your vectors are pointing in the wrong directions (when you do the subtractions):
A bit late, but I hope that helps :)