Jump to content
  • Advertisement
Sign in to follow this  

camera direction on GLSL

This topic is 2832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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 = gl_Position.xyz;

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!