开发者

GLSL Phong Light, Camera Problem

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


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 adding gl_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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜