(Ok, this is for C++ 3D OpenGL programmers =) )
Rotating the Camera
-------------------
Well, I've decided to share some knowledge to anyone who know C++ and is having trouble with their 3D camera or anything, here's just my own techniques and theory.
There are a few things you need to know, I use OpenGL which has a 4x4 matrix it works on. So the Rotation Matrices are like this:
PITCH (x-axis)
-----
| 1 0 0 0|
| 0 cos(theta) -sin(theta) 0|
| 0 sin(theta) cos(theta) 0|
| 0 0 0 1|
YAW (y-axis)
---
| cos(theta) 0 sin(theta) 0|
| 0 1 0 0|
|-sin(theta) 0 cos(theta) 0|
| 0 0 0 1|
ROLL (z-axis)
----
| cos(theta) -sin(theta) 0 0|
| sin(theta) cos(theta) 0 0|
| 0 0 1 0|
| 0 0 0 1|
-----------------------------------
Well, those are the basic rotation methods for a 3D camera, hehe, some real Quake 3 knowledge right there. Now, to make things work you'll need some definitions.
----------------------------------------
#define PI (3.14159265359)
#define DEG2RAD(a) (PI/180*(a))
#define RAD2DEG(a) (180/PI*(a))
typedef float scalar_t // your scalar
----------------------------------------
________________________________________
Now, for the 3D camera movement code :-D|
-----------------------------------------
void CCameraCV::Animate(scalar_t deltaTime)
{
// You can do some calculations for the pitch,yaw, and roll
float cosYaw = (scalar_t)cos(DEG2RAD(yaw));
float sinYaw = (scalar_t)sin(DEG2RAD(yaw));
float sinPitch = (scalar_t)sin(DEG2RAD(pitch));
// speed on the z axis
float speed = velocity.z * deltaTime;
// friction
if (velocity.Length() > 0.0)
acceleration = -velocity * 1.5f;
velocity += acceleration*deltaTime;
// Calc the 3D Camera position
position.x += float(cos(DEG2RAD(yaw + 90.0)))*strafeSpeed;
position.z += float(sin(DEG2RAD(yaw + 90.0)))*strafeSpeed;
position.x += float(cosYaw)*speed;
position.z += float(sinYaw)*speed;
// Calc the 3D Camera rotation
lookAt.x = float(position.x + cosYaw);
lookAt.y = float(position.y + sinPitch);
lookAt.z = float(position.z + sinYaw);
gluLookAt(position.x, position.y, position.z,
lookAt.x, lookAt.y, lookAt.z,
0.0, 1.0, 0.0);
}
DONE! That's it! This code is good enough for Quake 4! Now that wasn't so hard huh. Hope this gives light to any C++ 3D programmer out there.
ID:193436
Jan 13 2002, 11:34 am
|
|
Jan 13 2002, 12:14 pm
|
|
Can you point me in the direction of very good C++ tutorials?
|
In response to Gojira
|
|
I think one of my 3D game programming books that uses DirectX has the same pitch, yaw, and roll matrices.
I can't wait to get my OpenGL book. I ordered this one a week ago. Which one(s) do you have? |
In response to Cinnom
|
|
Yea, I have that programming book. But Here's where I learned most of OpenGL: http://nehe.gamedev.net ;) I also journey to http://gamedev.net to find Math help on rotations and stuff. Maybe I'll figure out how they did Max Payne rotating effects. I'm sure pitch, yaw, and roll are in every 3D programming book tho, it's just what rotating on those axis are called.
---------------------------------------------------------- ...........|../z(roll). ...........|./......... ...........|/.......... ...____|____x(pitch) ........../|........... ........./.|........... ......../..|........... ......./...|y(yaw). Yea, that looks like a grid right :) (My ASCII ART) |
In response to Goten84
|
|
Thanks. I wasn't expecting free stuff.
|
In response to Cinnom
|
|
hehe np :)
|
happen to know of any engines(controls or libraries) of Open GL for Visual Basic?
I am a heavy Visual Basic programmer, and i would love to learn open gl or direct x, either one. FIREking |
In response to FIREking
|
|
On the Nehe site they have ported most of the OpenGL tutorials to Visual Basics. http://nehe.gamedev.net =)
|
In response to Goten84
|
|
Do you think it will be worth learning DirectX if I'm also going to be learning OpenGL? I heard (from you, before) that OpenGL is easier than DirectX. Does OpenGL offer most of the things that DirectX offers, or is it graphics only?
I'll probably learn both regardless of what you say, but I just wanted to know your opinion. Thanks. |
In response to Cinnom
|
|
Cinnom wrote:
Do you think it will be worth learning DirectX if I'm also going to be learning OpenGL? I heard (from you, before) that OpenGL is easier than DirectX. Does OpenGL offer most of the things that DirectX offers, or is it graphics only? Actually, as far as my knowledge goes, OpenGL is more powerful, while DirectX is easier and is still supported by its owners. nVidia is the classic case of a monopoly -- I wonder why Microsoft is getting busted down instead of them... |
In response to Spuzzum
|
|
Spuzzum wrote:
nVidia is the classic case of a monopoly -- I wonder why Microsoft is getting busted down instead of them... Could you give me an explanation on that? I love hearing business stories... |
In response to Spuzzum
|
|
Yea, with Nvidia taking over OpenGL is finally getting light, (sorry Microsoft =D). I personally think OpenGL is easier tho. I've tried DirectX but didn't like the API. It's really up to you if you want to learn both tho. Here is a list of games that have been made with OpenGL ;) http://www.opengl.org/users/apps_hardware/applications/ games.html
BTW, Microsoft won't include OpenGL dlls and support in their OS because they know it would KILL DirectX!!! That's my belief anyways. |
In response to Nadrew
|
|
But there is nothing along the great simpleness of the ZBT
|
In response to Goten84
|
|
Goten84 wrote:
Yea, with Nvidia taking over OpenGL is finally getting light, (sorry Microsoft =D). I personally think OpenGL is easier tho. I've tried DirectX but didn't like the API. It's really up to you if you want to learn both tho. Here is a list of games that have been made with OpenGL ;) http://www.opengl.org/users/apps_hardware/applications/ games.html That list is an indicator that, at this point anyway, OpenGL is losing. Pretty thin list... |
In response to Deadron
|
|
most of those games are pretty recent, like Baulders Gate 2, Diablo 2 and Tribes 2. Plus, games made with direct X and converted to another OS with OpenGL aren't listed on there, these are purely OpenGL games. So I'd say the list is pretty big. Plus with DirectX updating every 2 months I'd say it's tiresome for some programmers to try to update their knowledge whereas OpenGL hasn't had to update in years and still keeps up in high-quality 3Dgfx. Also, Sony is going to make PS2 OpenGL compatiable soon, not DirectX which is probably why Mircrosoft is making the XBox directX-based only, to keep it alive. And to my knowledge the new Quake 4 game is being made with OpenGL 1.2, but when 2.0 comes out it's gonna kicka$$ in graphics.
Although both APIs are really good. http://www.flipcode.com/cgi-bin/ msg.cgi?showThread=01-14-2002&forum=iotd&id=-1 here's a game that uses both OpenGL for the GFX and DirectX for sound and input (which a lot of people like myself do :-D) |