# OpenGL Camera

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 11-30-2003
frenchfry164
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.
• 11-30-2003
glUser3f
check out the OpenGL camera tutorial at GameTutorials.com.
• 11-30-2003
Silvercord
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.
• 12-03-2003
frenchfry164
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.
• 12-03-2003
neandrake
What bugs does the gametuts one have?
• 12-05-2003
Silvercord
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.
• 12-05-2003
frenchfry164
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.
• 12-06-2003
Silvercord
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

Quote:

float fast_sqrt(float f)
{
float result;
_asm
{
mov eax, f
sub eax, 0x3f800000
sar eax, 1
mov result, eax
}
return result;
}
i just typed that from a book, I don't know what the sar instruction does.
• 12-06-2003
JaWiB
Quote:

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"
• 12-06-2003
Silvercord
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
• 12-06-2003
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/
• 12-06-2003
Silvercord
thanks
• 12-07-2003
glUser3f
Quote:

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.
• 12-07-2003
frenchfry164
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.
• 12-08-2003
Silvercord
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 :)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last