Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Keeping track of camera position

  1. #11

    Re: Keeping track of camera position

    Regarding your jupiter problem, u will be using something like

    glPushMatrix()
    glTranslatef(coords of moon);
    // draw moon
    glPopMatrix();

    This means that u will probably be having the coords of the moon and Jupiter in some variables. Use these variables to position and orient the camera.

    I'll try to code this myself and let u know if I meet with any success.

    CZEP, since I haven't yet worked on the polarview system, I can't offer any help. My apologies for deviating from ur question.
    Regards.

    Abhijeet.


    [This message has been edited by abhijeet_maharana (edited 07-09-2003).]

  2. #12
    Junior Member Regular Contributor
    Join Date
    Feb 2003
    Location
    Westerville, Oh, USA
    Posts
    201

    Re: Keeping track of camera position

    Abhijeet - Thanks, but I think I got it. I compute the transformation matrix of each object in the system (jupiter and the moons) and before drawing, I use Jupiter's position as my "look", and (Jupiter + moon) position as my "eye" and call gluLookAt(). I get the relative positions of the objects from elements 12, 13, and 14 in their respective transformation matrices.

    Thanks to all who have helped on this thread and in others. I've been fiddling with this bug for over a week, but the good thing that came out of it is that I really understand transformations a lot better now.

  3. #13
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    765

    Re: Keeping track of camera position

    Starman -- it sounds like you've already sorted out your camera function, but I thought I'd tell you about this method so you can keep it in mind for future projects.

    I can see what effect you're after and why you're analysing the modelview matrix to feed into gluLookAt. I agree that in this context it IS a good idea. (My discussion earlier was based on an assumption that you're moving the camera with gluLookAt + translation + rotation and then trying to reverse engineer the movemements rather than precompute them and pass through to gluLookAt).

    Anyway, the idea I wanted to tell you about was... arguably a "more elegant" way of getting back appropriate values. This is the way I'd do it if I was setting up the system, anyway.

    Suppose you set up your geometry like this:

    Code :
    glLoadIdentity();
    renderSun();
    glRotatef(earthorbitrotation, 0.0, 1.0, 0.0);
    glTranslatef(earthorbitdistance, 0.0, 0.0);
    renderEarth();
    glRotatef(moonorbitrotation, 0.0, 1.0, 0.0);
    glTranslatef(moonorbitdistance, 0.0, 0.0);
    renderMoon();
    where renderSun() et al all draw an insanely big sphere around the origin of the local coordinate system. You can see how the transformation stack so that the moon orbits the earth and the earth orbits the sun.

    Now, if i wanted to set up the camera system to be at the moon looking at the sun, then one way---which you've sorted out---is to grab the opengl transformation matrix and extract the world... uh, *universe* position of the moon from the fourth column. But if you think about what you're REALLY doing is working out how the origin of the moon's local coordinate system is being mapped by the matrix stack, right? So, what you're effectively finding is

    Code :
    m11 m12 m13 m14   0
    m21 m22 m23 m24   *   0
    m31 m32 m33 m34       0
    m41 m42 m43 m44       1
    where mij is the modelview matrix when we're rendering the moon. The location turns out to be the fourth column since the first three columns are obliterated by the mul by 0. The reason why I'm pointing THIS out is because you can then think about DIFFERENT transformations. Suppose you want the view of a person standing at so many degrees latitude and so many degrees longitude on the moon's surface looking at the sun.. what do you do then? you can't simply rip the fourth column out of the matrix because we're no longer talking about the origin of the moon.

    The answer is we can compute a new point in the moon's coordinate system and multiply that the matrix and plug the answer into gluLookat. I can't remember which way longitude/latitude work out, but suppose the first is rotate about y and the second is rotate about x (for argument's sake), then you could do this:

    Code :
    pos=M * Ry * Rx * T * [ 0 0 0 1]'
    where M is the moon modelview matrix from above, Ry is the rotate about y degrees longitude matrix, Rx is the rotate about x degrees latitude matrix, T is the translation matrix with the vector <0, 0, sealevel> (to shift the observer to some point on the moons sealevel (if it had a sea)) and 0 is the origin. Actualy, we could just axe the T and the 0, 0, 0, 1 and replace it by 0, 0, sealevel, 1... but i'm confident you can see that.

    Oh, and something else I didn't mention... all this matrix multiplication is why people like keeping track of their own modelview matricies in s/w so you can precompute this kind of stuff without using opengl and reading it back from the pipe (which can be slow). So... that multiplication above would be written in my program by just

    Point3D pos=M*Geometry::RotateY(y)*Geometry::RotateX(x)*Ve ctor3D(0.0, 0.0, z);
    where M is built in a similar way but for the moon transform.

    Does that make sense?

    cheers
    John

  4. #14
    Junior Member Regular Contributor
    Join Date
    Feb 2003
    Location
    Westerville, Oh, USA
    Posts
    201

    Re: Keeping track of camera position

    John - I think we're on the same page. I'm not calling glGetFloatv() anymore to get the current GL modelview matrix - I'm getting the position vector out of Jupiter's and the moon's local object matrix, which I build manually inside the objects.

    look = Jupiter's position
    eye = Jupiter's position + moon position

    As far as viewing from a point other than the origin of the moon, I plan to implement something similar next. I want the camera to orbit the moon itself. For this, I was going to implement a point (or object) that orbits the moon in the same way the moon orbits Jupiter, and use its position as my camera position.

    Isn't that kind of what your proposing? I do get what you're saying about transforming a point rather than just plucking the values out of the matrix. Yours is definitely the more elegant and intuitive approach.

    [This message has been edited by starman (edited 07-09-2003).]

  5. #15
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    765

    Re: Keeping track of camera position

    I'm getting the position vector out of Jupiter's and the moon's local object matrix, which I build manually inside the objects.
    excellent, excellent. soon world domination will be ours!
    :-)

    <stuff from above> ... Isn't that kind of what your proposing?
    why yes, yes it is. In that case you'd shift the point higher than sealevel in the same process as above.

    I do get what you're saying about transforming a point rather than just plucking the values out of the matrix. Yours is definitely the more elegant and intuitive approach.
    cheers! :-)

    good luck. may the force be with you!

    John

  6. #16
    Junior Member Regular Contributor
    Join Date
    Feb 2003
    Location
    Westerville, Oh, USA
    Posts
    201

    Re: Keeping track of camera position

    Side question, John: Is there any merit in taking this one step further? i.e., instead of building my own matrices (which entails a few manual matrix multiplications if the object has multiple rotations/translations) and then calling glMultMatrix to put each object in the MV matrix, why not just manually multiply the whole scene and then load the modelview matrix once?

  7. #17
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    765

    Re: Keeping track of camera position

    Hello,

    no, that would really soak your performance with zero gain. I take it you're suggesting computing the modelview matrix for the moon and then iterating over all verticies and multiplying them by the mv matrix and transferring the result to opengl? You wouldn't get any advantage--what would you do with the data that you've stored?--and you'd lose any advantages of building display lists. You'd also trade efficient matrix multiplication in your graphics hardware for CPU matrix multiplication.

    cheers,
    John

  8. #18
    Advanced Member Frequent Contributor
    Join Date
    Apr 2000
    Location
    Adelaide, South Australia, Australia
    Posts
    765

    Re: Keeping track of camera position

    oh, and another thing---just because you're computing your OWN modelview matrix to compute the observer position doens't necessarily mean you should throw away opengl's matrix building operations with glLoadMatrix. You'd really only see performance gains with a very complicated matrix stack.

    Furthermore, you probably don't want to compute the modelview matrix for all geometry since you probablu won't use the results, so you'd still be better off buidling matricies with opengl rather than computing them yourself and uploading them.

    cheers
    John

  9. #19
    Junior Member Regular Contributor
    Join Date
    Feb 2003
    Location
    Westerville, Oh, USA
    Posts
    201

    Re: Keeping track of camera position

    Thanks for all the help, John. Your contributions to this forum are valued. Take care.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •