Skip to main content
gamedev.net
🔒 Locked

get view ray

Started by Eitsch 6 replies 1k views
Original Post
Eitsch
Eitsch
i want to get the view ray of the current camera. i use gluLookAt followed by several glTranslates and glRotates. i now want to get my current "view ray". i thought on something like the vector from eye into eye-look-direction. pos = eyePos + eyeDir*t ---- as line equation. i am just unable to figure out the coefficients. i know modelview matrix but i just cant figure it out.... :-(( in the end i need it for intersection test to get screen coordinates to world coordinates.
ToohrVyk
ToohrVyk
If you want to find the position of the cursor in world space, you might as well use glUnProject, which would save you a lot of effort.

Also, to find the camera position and camera forward vector, simply transform the (0,0,0,1) and (0,0,1,0) vectors by the inverse of the view matrix.
Eitsch
Eitsch
very good. thanks.

but....
since i only have a 3x3 matrix class and xyz vector i have problems implementing it.
in the past i treated this problem somehow like this:

transformed = 3x3SubMatrix*VertexToTransform + TranslationsOfViewMatrix;
TranslationsOfViewMatrix = vec(modelview[12],modelview[13],modelview[14]);

how do i have to handle this multiplication of 0,0,0,1 with the matrix. it seems that i cant split it up. i have the RotationMatrix (the 3x3 submatrix) inverted - but how to move on?

i appreciate not to modify my very old messy math stuff....any ideas?

god....what would i give for a math libary like these in the high level shading languages........
Eitsch
Eitsch
grrr.

i am hacking around in my invert method.
i know my structure is ugly but it is very old:
class awx_matrix
{
float a[4][4];
}
so a[0][1] is row 1 and line 0.

my current code:

awx_matrix tmp;float d = determinant();if (closeEnough(d, 0.0f)){tmp.loadIdentity();}else{d = 1.0f / d;tmp.a[0][0] = d * (a[1][1] * a[2][2] - a[1][2] * a[2][1]);tmp.a[0][1] = d * (a[0][2] * a[2][1] - a[0][1] * a[2][2]);tmp.a[0][2] = d * (a[0][1] * a[1][2] - a[0][2] * a[1][1]);tmp.a[1][0] = d * (a[1][2] * a[2][0] - a[1][0] * a[2][2]);tmp.a[1][1] = d * (a[0][0] * a[2][2] - a[0][2] * a[2][0]);tmp.a[1][2] = d * (a[0][2] * a[1][0] - a[0][0] * a[1][2]);tmp.a[2][0] = d * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);tmp.a[2][1] = d * (a[0][1] * a[2][0] - a[0][0] * a[2][1]);tmp.a[2][2] = d * (a[0][0] * a[1][1] - a[0][1] * a[1][0]);tmp.a[0][3] = -((a[0][3] * tmp.a[0][0]) + (a[1][3] * tmp.a[1][0]) + (a[2][3] * tmp.a[2][0]));tmp.a[1][3] = -((a[0][3] * tmp.a[0][1]) + (a[1][3] * tmp.a[1][1]) + (a[2][3] * tmp.a[2][1]));tmp.a[2][3] = -((a[0][3] * tmp.a[0][2]) + (a[1][3] * tmp.a[1][2]) + (a[2][3] * tmp.a[2][2]));}return tmp; // This should be the inverted of a[][]


as everybody might expect it is not working. tmp.a[0][3] scaled by 0.5 would be the correct result.

any ideas? thanks and sorry for my dumb approach

[Edited by - Eitsch on December 26, 2006 11:15:41 AM]
ToohrVyk
ToohrVyk
1° Your inversion method is slow and overkill for orthogonal matrices (where a transpose suffices).

2° Transforming (0,0,0,1) yields the translation vector, so if you already have it around, just keep it.

3° (0,0,1,0) basically ignores the translation, so you can transform (0,0,1) by the inverse rotation instead.
Eitsch
Eitsch
1°: orthogonal means: unscaled matrix?

maybe you didnt understand me. i dont have the translation vector itself...only the modelview.
lets say it looks like this:

the correct inverted one (taken from internet inverse matrix calculator)


1) get camera position:
if i understood it correct i only have to take the last row of the inverted view matrix. (so camera is on: 5.61/2.4/-4.69)
it looks lie this:

the white lines are many wireframe cubes from camera position interpolated to world origin to visualize the result.
even if it should go to lower left corner (strange) it is not really correct. gluUnProject works correct but i want understand my problem here

i don
t understand why this could be correct.
in my opinion it should look like this:
0,0,0 = viewMatrix * x + translation
- translation = viewMatrix*x
so: viewMatrixInverted * (-translation) = cameraposition
but it doesnt work....
... i am very confused now.

2) inverted - transposed.

if i only transpose the view matrix the last row gets 0,0,0,1. camera positon 0,0,0 can`t be correct.

ToohrVyk
ToohrVyk
Quote:
Original post by Eitsch
1°: orthogonal means: unscaled matrix?


Nope. Orthogonal means that it contains only rotations. If it's a homogenous matrix, then it may also contain a translation component. For a homogenous 4x4 orthogonal matrix without scaling (as would result from LookAt + Translation + Rotation), the inverse can be computed as:

[ A B C D ]     [ A E I -D ][ E F G H ]  => [ B F J -H ][ I J K L ]     [ C G K -L ][ 0 0 0 1 ]     [ 0 0 0  1 ]


Then, the position of the camera would be (-D,-H,-L) and the direction of the view ray would be (I,J,K), assuming that the left matrix is the view matrix.

Eitsch
Eitsch
ok everything clear. thanks!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.