GLSL Phong Light,相机问题
我刚刚开始编写一个 phong 着色器
vert:
varying vec3 normal, eyeVec;
#define MAX_LIGHTS 8
#define NUM_LIGHTS 3
varying vec3 lightDir[MAX_LIGHTS];
void main() {
gl_Position = ftransform();
normal = gl_NormalMatrix * gl_Normal;
vec4 vVertex = gl_ModelViewMatrix * gl_Vertex;
eyeVec = -vVertex.xyz;
int i;
for (i=0; i<NUM_LIGHTS; ++i){
lightDir[i] = vec3(gl_LightSource[i].position.xyz - vVertex.xyz);
}
}
,我知道我需要使用 uniform
获取相机位置,但是如何以及在哪里放置这个值?
附:我正在使用opengl 2.0
I Just started write a phong shader
vert:
varying vec3 normal, eyeVec;
#define MAX_LIGHTS 8
#define NUM_LIGHTS 3
varying vec3 lightDir[MAX_LIGHTS];
void main() {
gl_Position = ftransform();
normal = gl_NormalMatrix * gl_Normal;
vec4 vVertex = gl_ModelViewMatrix * gl_Vertex;
eyeVec = -vVertex.xyz;
int i;
for (i=0; i<NUM_LIGHTS; ++i){
lightDir[i] = vec3(gl_LightSource[i].position.xyz - vVertex.xyz);
}
}
I know that I need to get the camera position with uniform
, but how, and where put this value?
ps. I'm using opengl 2.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要传递相机位置,因为 OpenGL 中没有相机。
照明计算在眼睛/世界空间中执行,即在与模型视图矩阵相乘之后,该矩阵还执行“相机定位”。所以实际上你已经做好了正确的事情。使用
ftransform()
效率有点低,因为你只做了一半的事情(gl_ModelviewMatrix * gl_Vertex
,你可以通过添加将其变成 ftransform gl_Position = gl_ProjectionMatrix * eyeVec
)因此,如果您的灯光在“相机”变换时似乎在移动,则说明您没有正确变换灯光的位置。要么预先计算变换后的灯光位置,要么也在着色器中变换它们。这更多的是一个选择的约定的问题,如果使用着色器则更少的限制。
You don't need to pass the camera position, because, well, there is no camera in OpenGL.
Lighting calculations are performed in eye/world space, i.e. after the multiplication with the modelview matrix, which also performs the "camera positioning". So actually you already got the right things in place. using
ftransform()
is a little inefficient, as you're doing half of what it does again (gl_ModelviewMatrix * gl_Vertex
, you can make this into ftransform by addinggl_Position = gl_ProjectionMatrix * eyeVec
)So if your lights seem to move when your "camera" transforms, you're not transforming the light's positions properly. Either precompute the transformed light positions, or transform them in the shader as well. It's more a matter of choosen convention, less laid out constraint if using shaders.