我知道这个问题已经被问了很多,但我仍然无法做到正确。
所以,在我的顶点着色器中,我需要获得光线的位置和眼睛的位置。这是我的代码:
void main(void)
{
vec3 p = vec3 ( gl_ModelViewMatrix * gl_Vertex );
l = normalize (vec3 (gl_LightSource[0].position));
v = normalize ( vec3 ( eyePos ) - p );
n = normalize ( gl_NormalMatrix * gl_Normal );
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
结果,光随着相机的移动而移动。我到底需要在这里做什么?可能不使用非 opengl 矩阵。
是否有具有适当相机控制的 glsl 照明示例?
* 更新 *
感谢 Kos,我解决了“灯光随相机移动”的问题,但这是另一个问题:
我有
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
ctLight -> enable();
glPushMatrix();
glTranslatef(0, 0, 0);
glRotatef(angle, 0, 1, 0);
glutSolidTeapot(2.0);
glPopMatrix();
glPushMatrix();
glTranslatef(0, 0, 8);
glRotatef(angle, 0, 1, 0);
glutSolidTeapot(2.0);
glPopMatrix();
ctLight -> disable();
glDisable(GL_LIGHT0);
现在第二个茶壶的灯光与 (0, 0, 0) 中的一个相同。我该如何解决?
顶点着色器代码以防万一:
void main(void)
{
vec3 p = vec3 ( gl_ModelViewMatrix * gl_Vertex ); // transformed point to world space
l = normalize ( vec3 (gl_LightSource[0].position) );
v = normalize ( - p );
h = normalize ( l + v );
n = normalize ( gl_NormalMatrix * gl_Normal ); // transformed n
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}