Sign in to follow this  

camera direction on GLSL

Recommended Posts


as i notice that fttransform() method get the vertex position based on world position. is that right? so if i want to get camera direction (normalized) can i just call

vec3 camDirection = normalized(v);

or are there other way?

thanks in advance

Share this post

Link to post
Share on other sites
Your question doesn't make any sense.

ftransform() is the equivalent of

outVertex = ProjectionMatrix * ModelViewMatrix * inVertex

What is "v" in your equation? What does it have to do with ftransform?

Share this post

Link to post
Share on other sites
ah ok i miss a line why copying it. the previous line i define v as:

vec3 v = ftransform();

and my OnReshape function, if its important, looks like this

void onReshape(int w, int h) {
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLdouble) width/(GLdouble) height, 1.0, 100.0);
glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix);

glGetDoublev(GL_MODELVIEW_MATRIX, lookAtMatrix);

can i just simply do normalize(v) to get the camera direction from vertex? i want to use this as specular calculation, and calculate this direction value to find the half vector.


ok here is what i wanna do: i want to get the camera position relative to the vertex position, using spherical coordinate system. here is the code that i put on the vertex shader:

void main()
vec3 spherical;

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();

vec3 v =;

vec3 cameraPos;
cameraPos.x = cameraPosX;
cameraPos.y = cameraPosY;
cameraPos.z = cameraPosZ;

// ftransform get the position
//vec3 cameraDir = normalize(cameraPos - v);
vec3 ecPos = gl_ModelViewMatrix * gl_Vertex;

//vec3 cameraDir = normalize(cameraPos - ecPos);
vec3 cameraDir = normalize(-1.0 * v);

spherical.x = sqrt((cameraDir.x*cameraDir.x) + (cameraDir.y*cameraDir.y) + (cameraDir.z*cameraDir.z));
spherical.y = acos(cameraDir.y/spherical.x);
spherical.z = atan(cameraDir.z, cameraDir.x);

sphIndexY = spherical.y * 180.0/(3.14159);
sphIndexZ = spherical.z * 180.0/(3.14159);

if (sphIndexY < 0) {
sphIndexY = sphIndexY * -1.0;
if (sphIndexZ < 0) {
sphIndexZ = sphIndexZ * -1.0;

now i have to switch the y and z value since my up is y and the formula around the net is for up=z. but im not getting the right value. did you notice anything wrong on my code?

thanks in advance

[Edited by - svnstrk on October 17, 2010 8:10:05 PM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this