Skip to main content
gamedev.net
🔒 Locked

Camera class question

Started by mits Jan 30, 2004 at 10:34 AM 5 replies 1.1k views
Original Post
mits
mits
I dont want to use gluLookAt() and I have implemented a simple camera class. I have three vectors for my camera, m_pos , m_view (which is not the point I look at, but the direction of viewing) and m_up. When I initialize the class I declare : m_pos = {0.0f,0.0f,0.0f}; m_view = {0.0f,0.0f,-1.0f}; // Look to negative z axis direction m_up = {0.0f,1.0f,0.0f}; // Up is the positive y axis and then I use cross product of m_view and m_up (m_view x m_up)to calculate the m_right vector. I normalize it after that. In my camera render function (i.e. similar to gluLookAt()) I do these: void MyCamera::Render() { float mat[16]; float tx = - Dot(m_right,m_pos); float ty = - Dot(m_up,m_pos); float tz = - Dot(m_view,m_pos); mat[0] = m_right.x; mat[4] = m_right.y; mat[8] = m_right.z; mat[1] = m_up.x; mat[5] = m_up.y; mat[9] = m_up.z; mat[2] = m_view.x; mat[6] = m_view.y; mat[10] = m_view.z; mat[3] = mat[7] = mat[11] = 0.0f; mat[12] = tx; mat[13] = ty; mat[14] = tz; mat[15] = 1.0f; glMultMatrixf(mat); } and I use this function in my DrawGL() function exactly after my glLoadIdentity() function. I then draw my other objects usinf glTranslate() and glRotate(). But nothing is shown on screen. What it might be wrong? I know that the camera matrix is the inverse of the camera''s world matrix (if i treat it as an object)so if camera''s world matrix is T * R , the inverse will be inverse(R) * inverse(T) which (I think) is the matrix I construct. So what''s wrong? Thanks in advance.
Trienco
Trienco
the "inversion" looks ok so far. did you check if your right vector is really right and not left? also, if you call loadidentity before that anyway you could use loadmatrix. another thing is to make sure your 3 vectors are orthonormal (after a few rotations rounding errors will make sure they arent anymore).

is it working if you dont touch the camera after initializing it (ie, when it should produce the identity) and if not: is simply setting the identity working?
f@dzhttp://festini.device-zero.de
mits
mits
It isn''t working even if I don''t touch the matrix after I have initialized it. The right vector is indeed the right vector. I don''t know what''s wron.Thanks for the answer.
Captian Goatse
Captian Goatse
Why not use gluLookAt? Eventually you''ll end up developing your own version of it anyway. I suggest you go to mesa3d and look what gluLookAt does and then copy it, and then split the code so that you can do other things such as rotate ect. If you search, trienco already posted a cool camera class. Its cool because its pretty cool. you can find it from flipcode also.

Vector+degree>plain degree
Trienco
Trienco
one thing that might help to find the error:
set the view with your camera, get the matrix from opengl.
set the view with glulookat, get the matrix. compare and see where and why they dont match. without touching it the inverse should be the identity (surprise), so you might not even need glulookat to compare.

arg.. im an idiot. with the inversed z axis you need to invert these components too, so it should work with:

float tz = Dot(m_view,m_pos);
[edit] *cough* with a 50/50 chance there was no way in hell i'd pick the right one[/edit]
mat[2] = -m_view.x;
mat[6] = -m_view.y;
mat[10] = -m_view.z;

(things like that annoyed me enough to change the projection matrix and make z point into the screen.. and you can finally handle it like any other axis)

[edited by - Trienco on January 31, 2004 7:07:57 AM]
f@dzhttp://festini.device-zero.de
mits
mits
I am an idiot too because I didn''t think it.It worked just by negating tz and m_view.Thanks Trienco.
teamonkey
teamonkey
quote:
Original post by Captian Goatse
Why not use gluLookAt? Eventually you''ll end up developing your own version of it anyway.


What he''s already got is almost exactly what gluLookAt() does :D

Here''s the maths: http://pyopengl.sourceforge.net/documentation/manual/gluLookAt.3G.html
[teamonkey] [blog] [tinyminions]

Topic Locked

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

Sign in to reply to this topic.