How can i calculate where does my entire screen look at, in 3d vector ? I mean, what is my camera's direction in glsl ? I will try to code an outline shader so i must dot product terrain normals with my camera's view
How can i calculate where does my entire screen look at, in 3d vector ? I mean, what is my camera's direction in glsl ? I will try to code an outline shader so i must dot product terrain normals with my camera's view
If your camera is uses Euler angles, try this:
Code :Vec3f RotationToVector(float xRotRads, float yRotRads) { Vec3f dir; float cosY = cosf(yRotRads); dir.x = sinf(xRotRads) * cosY; dir.y = -sinf(yRotRads); dir.z = cosf(xRotRads) * cosY; return dir; }
It may be pointing in the opposite direction. If that happens, just flip it.
thanks but isn't there any way to calculate it in vertex shader instead of passing variables ?
Yes, you can just transform the point (1.0, 0.0, 0.0) by the modelview matrix, but why would you want to do that?
You would needlessly recalculate it for every vertex! That would be expensive!
umm then passing variables -i mean computing in cpu instead of gpu- is the best way for now ?
Yes, such precomputed things should always be passed as uniforms.
i think i must find another way to do an outline effect.. i had found another way but i couldn't make stencil buffer work with VBOs..
You can extract view direction from modelview matrix
(no need to calculate anything in shader, just take apropriate column), look here:
http://www.songho.ca/opengl/gl_transform.html#modelview
Last edited by kowal; 06-25-2012 at 03:33 AM. Reason: typos
Why don't you simply maintain a forward vector with your camera implementation? You can use the basis vectors of the cam coordinate system for ton of things. Since the basis needs only be recomputed once per frame (in most cases at least) you have a marginal performance impact. Furthermore you can simply pass the camera position to a shader directly with glUniform4fv().
what i can't understand is gl_position doesn't work correctly, or as i wanted it to work. I am passing camera position to glsl and substract it from gl_position, normalize and dot product with terrain normals, i also tried backwards but everywhere looks black.