Thread: OpenGL Camera

  1. #1

    OpenGL Camera

    Okay I'm having problems implementing a camera in OpenGL. The translations are easy as crap, I just can't get rotation to work.

    I could get it to work if I specified everything in global coordinates, but I want to define my meshes in local coordinates and then translate them to where they need to be.

    I have heard of quaternions before when people ask similiar questions, would they help? The only thing I know about them is they have something to do with the complex number system.

  2. #2
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    check out the OpenGL camera tutorial at GameTutorials.com.

  3. #3
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I highly suggest you don't look at gametuts, because it is full of bugs and highly confusing. I suggest you check out my rotation about an arbitrary axis equations tutorial posted on this site. It explains the calculations used in gametut's code for rotation a vector about an arbitrary axis I also suggest you look at my quaternion code which implements rotations using quaternions. There's also another method, sphereical coordinates, which is actually easier but won't advance your 3D math knowledge unless you derive them (which is basically from the X and Y axis rotation equations I think, but I get something slightly different when I multiply the X and Y axis rotation equations together).

    I'm including my Vector.cpp file which encloses the RotateVector function, and I've include my code for my FPSCam::CheckMouse() functions (two versions) and the cpp code for my quaternion class.

  4. #4
    I still don't understand cameras. Every "tutorial" I find are just source code downloads and they put comments in to show you how to do it. I HATE THOSE SO BADLY.

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    What bugs does the gametuts one have?
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    frency fry the only thing you really need to learn is how to rotate vectors in 3d space, the rest you can throw together yourself.

    gametut's camera doesn't convert the mousemovement to real radians for one thing, and it's extremely easy to move the mouse fast and have gimbal lock. my camera doesn't ever get gimbal lock, either with quaternions or the regular vector rotatione equations.

  7. #7
    I understand that, I just couldn't figure out how to get a camera system working. I went to www.lighthouse3d.com and looked at their tutorial, and I know how to do it now. I just gotta make some sine and cosine lookup tables now.

  8. #8
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    just call sine and cosine...don't optimize before you've got a finished module...you can call sine and cosine 1200 times a frame before you'd notice a substantial difference, depending on your cpu.

    EDIT:
    also, I found a fast_sqrt method which is done in assembly from one of my books, with a 5% margin of error. you can use this when you don't absolutely need a vector that's has a length of exactly 1, but fairly close

    float fast_sqrt(float f)
    {
    float result;
    _asm
    {
    mov eax, f
    sub eax, 0x3f800000
    sar eax, 1
    add eax, 0x3f800000
    mov result, eax
    }
    return result;
    }
    i just typed that from a book, I don't know what the sar instruction does.
    Last edited by Silvercord; 12-06-2003 at 01:50 PM.

  9. #9
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    SAR Shift Arithmetic Right - This is a new instruction that does not shift
    the sign bit (i.e. the msb) of its operand. The other bits are shifted
    as normal except that the new bits that enter from the left are copies
    of the sign bit (that is, if the sign bit is 1, the new bits are also 1).
    Thus, if a byte is shifted with this instruction, only the lower 7 bits
    are shifted. As for the other shifts, the last bit shifted out is stored in
    the carry flag.
    Code:
     mov ax, 0C123H
     sal ax, 1 ; ax = 8246H, CF = 1
     sal ax, 1 ; ax = 048CH, CF = 1
     sar ax, 2 ; ax = 0123H, CF = 0
    From "PC Assembly Language"
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  10. #10
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    ohh, so like in a floating point it modifies only the mantissa by a binary shift but doesn't change the sign bit? i.e the same as the '>>' in C++ (sar's instruction is probably what is written to the object file then when the C++ compiler encounters it)

    is that a book? I have an assembly book aimed for Linux and DOS

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It's a *.pdf book...Seems pretty good, but learning asm is slow going

    Shouldn't be too hard to find on google...Maybe I'll post the link

    Edit:
    http://www.drpaulcarter.com/pcasm/
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    thanks

  13. #13
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Originally posted by JaWiB
    It's a *.pdf book...Seems pretty good, but learning asm is slow going

    Shouldn't be too hard to find on google...Maybe I'll post the link

    Edit:
    http://www.drpaulcarter.com/pcasm/
    thx for the link JaWiB, gave me something to read for the nexrt few days.

  14. #14
    thx for the link dude. That's awesome. Oh, and the fast_sqrt is going to helpful as well. I thought sine and cosine were much slower than that.

  15. #15
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    im not sure, but ive figured out a way to write sine and cosine approximations using the fast_sqrt

    EDIT:
    This is what I had in mind, for a simple function, for an angle between 0 and 90 degrees

    cos(theta)^2 + sin(theta)^2 = 1
    so therefore (90-theta) / 90 = cos(theta)^2
    so therefore you could say the cosine is:
    fast_sqrt((90-theta)/90)

    obviously this isn't totally accurate, even if you used the real sqrt method. here's a table of sqrt((90-theta)/90) vs the real cosine of theta, from 0 to 90 degrees in steps of 10 degrees, my method on the left and the real cosine on the right

    Code:
    0 degrees
    mine = 1, real = 1
    
    .94281, .98481
    .88192, .93969
    .8165, .86603
    .74536, .76604
    .66667, .64279
    .57735, .5
    .4714, .34202
    .33333, .17365
    0
    Sometime it's pretty close, sometimes the difference is too big. If you did that with the fast_sqrt method you could call this function probably 100,000 times, vs 2,000 times per frame, and not notice a difference, plus you could find a function to model the difference between the real and accepted and apply that to the approximation to get an even closer value to the real thing, but ultimately it's just better to call sine and cosine and just code smart instead of inventing hacks like this before you get a real problem
    Last edited by Silvercord; 12-08-2003 at 08:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. OpenGL camera errors..can't find 'em!?
    By psychopath in forum Game Programming
    Replies: 12
    Last Post: 04-22-2004, 06:17 PM
  5. Camera rotation/movement in 3D world
    By tegwin in forum Game Programming
    Replies: 11
    Last Post: 01-24-2003, 01:43 PM