I'm currently writing a game using OpenGL and GLSL. For the shader I need the direction from the current camera to the light source. The lightsource is given by lightSource.position as a uniform as well as the ModelviewMatrix (mat4).

The task is

TODO: compute the vectors from the current vertex towards the camera and towards the light source

I need to fill two varyings with those information, but I'm not sure how to compute them. In addition I've the following information:

#version 330
layout(location = 0) in vec3 vertex;
layout(location = 1) in vec3 vertex_normal;

You can't do it with just that information. You need more.

For example, I assume you also actually have a the world, view, and projection transformation matrices available to you. In that case, you can compute the vertex's position in view space by multiplying it by the world matrix and then by the view matrix.

Since you're in view space, the eye is at (0, 0, 0) you can easily know the vector between that and the transformed, light-space vertex.

You can do the same if you have the light position. Bring the light position in to view space, then you can easily determine the vector between that position and the eye (which is, again fixed at the origin).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.