Be a Supporter!

C++: 3D Camera

  • 4,060 Views
  • 22 Replies
New Topic Respond to this Topic
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
C++: 3D Camera 2006-03-11 15:48:42 Reply

c++ main

This is for c++ with openGL.

Thanks to Delta for a lot of help with the vector math stuff.

So what is a camera? In a 3D game, you'll notice that a camera usually follows the character around, and you can often use buttons to rotate that camera around. A camera is one of the most basic elements of a 3D game, and yet one of the most important. Seems easy to implement? HECK NO! A really basic camera might be easy to make, but when you start to want to move it around and rotate up and down instead of just left and right, things get messy. But before we get there, here's an example.
Left, Right, Up, Down, Space
You might need SDL.DLL

Well, it looks kinda simple, right? not when you look at the code. First thing you'll need is a vector class. This is the bulky part of the code, but i'll comment it down nicely for you

class Vector{

public:

float x, y, z;
//simple enough, these 3 values represent the vector <x,y,z>
public:

Vector(float xi, float yi, float zi):x(xi), y(yi), z(zi){}

Vector():x(0),y(0),z(1){}
//Just some constructors, self-explanatory
///////

Vector dot(Vector b){

return Vector(x*b.x, y*b.y, z*b.z);

}
//dot product. Not used in my camera class but it's in here cause i plan to add more to this class
Vector cross(Vector b){

return Vector(y*b.z-z*b.y, z*b.x-x*b.z, x*b.y-y*b.x);

}
//cross product. Important in finding a vector perpendicular to 2 others The next method is the important one
void rotate(float a, Vector b){
//this is for rotation about another vector. a = angle. b = the vector to rotate about
a *= 3.1415/180;
//convert angle to radians
float s = sin(a);

float c = cos(a);

float t = 1-cos(a);
//3 trig functions to simplify the code a bit so i dont have to type too much stuff and so it only needs to call each trig function once
double tX = x*(t*b.x*b.x+c) + y*(t*b.x*b.y-s*b.z) + z*(t*b.x*b.z+s*b.y);

double tY = x*(t*b.x*b.y+s*b.z) + y*(t*b.y*b.y+c) + z*(t*b.y*b.z-s*b.x);

double tZ = x*(t*b.x*b.z-s*b.y) + y*(t*b.y*b.z+s*b.x) + z*(t*b.z*b.z+c);
//this multiplies the current matrix be a rotation matrix (thanks to delta again for the rotation matrix). It stores the values in 3 functions (which really should be a new vector, but whatever it's simpler this way)
x = tX;

y = tY;

z = tZ;
//store the temporary values into this one's new values

}

};

Well that's the math part. If you really want to understand the math (way too much to explain and stuff i dont quite know exactly why, just google "rotation matrices" and "how to multiply matrices" (treat the vector like a 1x3 matrix)

Now for the camera part.

enum direction{
UP,
DOWN,
LEFT,
RIGHT,
FORWARD,
BACKWARD
};
//if you dont know what an enum is look it up
class Camera{
private:
Vector eye;
Vector up;
float x, y, z;
//all values needed for gluLookAt, the ones to pay attention to are the 2 vectors. x,y,z are just the position of the camera, but the vectors are where the meat is at. eye is the vector that specifys what direction the camera is facing. Up is a vector perpendicular to eye, that specifys the top of the camera, by altering it you could like spin the screen or something for an effect if you want.
float dist;
//just how far the visible range is. If it's 500, objects more than 500 pixels away wont be rendered on the screen
public:
Camera():x(0.00),y(0.0),z(0.0),dist(1000){
eye = *(new Vector(0.0, 0.0, -1.0));
up = *(new Vector(0.0, 1.0, 0.0));
}
//bleh constructor
void update(){
gluLookAt(x , y , z,
eye.x*dist, eye.y*dist, eye.z*dist,
up.x, up.y, up.z);
}
//gluLookAt. Puts the camera at x,y,z and makes it face eye and the tiop is up. Simple enough.
void rotate(direction dir, float deg){
//this is a function to rotate the camera using the vector's ROTATE method
if(dir==UP){
Vector temp = eye.cross(up);
//the temporary vector is perpendicular to both up and eye.
eye.rotate(deg, temp);
up.rotate(deg, temp);
//then we rotate up and eye
}
if(dir==DOWN){
Vector temp = eye.cross(up);
eye.rotate(-deg, temp);
up.rotate(-deg, temp);
}
//same here
if(dir==LEFT){
Vector temp = up;
//temporary vector is the same as up
eye.rotate(deg, temp);
up.rotate(deg, temp);
//we really dont need to rotate up, just eye. But i'm too lazy to delete it
}
if(dir==RIGHT){
Vector temp = up;
eye.rotate(-deg, temp);
up.rotate(-deg, temp);
}
//same here
}
void move(direction dir, float dis){
//this moves the camera forward or backward depending on it's current rotation. since the EYE vector has a length of 1 even after you rotate it, we just have to move X Y and Z by the eye vector * the speed. If you wanted, you could stick code for LEFT and RIGHT and move it based on the cross between up and eye too. Really simple if you think about it. UP and DOWN could be based off of the up vector even! The possibilities are endless
if(dir==FORWARD){
x += eye.x*dis;
y += eye.y*dis;
z += eye.z*dis;
}
if(dir==BACKWARD){
x -= eye.x*dis;
y -= eye.y*dis;
z -= eye.z*dis;
}
}
};

So there you have it. The almighty camera. Not so bad once you examine it in detail. But those matrices are annoying.

So, um END OF POST
------------------------------------------
--------------->8----------

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to C++: 3D Camera 2006-03-11 16:32:23 Reply

I downloaded and it didnt work


- Matt, Rustyarcade.com

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: 3D Camera 2006-03-11 16:32:49 Reply

At 3/11/06 04:32 PM, Ninja-chicken wrote: I downloaded and it didnt work

download the dll too

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to C++: 3D Camera 2006-03-11 16:45:27 Reply

At 3/11/06 04:32 PM, Glaiel_Gamer wrote:
At 3/11/06 04:32 PM, Ninja-chicken wrote: I downloaded and it didnt work
download the dll too

yea yea I did I dunno what went wrong I get an error message (I cant be assed to write it here)


- Matt, Rustyarcade.com

0x41
0x41
  • Member since: Dec. 30, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to C++: 3D Camera 2006-03-11 17:02:58 Reply

Works for me, well done.

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to C++: 3D Camera 2006-03-11 17:34:14 Reply

At 3/11/06 05:02 PM, 0x41 wrote: Works for me, well done.

hmm very weird


- Matt, Rustyarcade.com

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++: 3D Camera 2006-03-11 17:36:15 Reply

I'm guessing this is what NC got too:

This application has failed to start because MSVCP71D.dll was not found. Re-installing the application may fix this problem.


Sup, bitches :)

BBS Signature
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to C++: 3D Camera 2006-03-11 17:39:59 Reply

yup thats the one thanks liam


- Matt, Rustyarcade.com

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++: 3D Camera 2006-03-11 18:00:10 Reply

Download the .dll here

It's pretty cool, I keep getting an error when compiling (I'm using Dev) though..

[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
[Project1.exe] Error 1

:'(

My only linker is -lopengl32, is there anything else I need?


Sup, bitches :)

BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: 3D Camera 2006-03-11 18:15:32 Reply

At 3/11/06 06:00 PM, -liam- wrote: Download the .dll here

It's pretty cool, I keep getting an error when compiling (I'm using Dev) though..

[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
[Project1.exe] Error 1

'(
My only linker is -lopengl32, is there anything else I need?

um....... i dont use a commant prompt compiler (i dont know PC's too well...)

In visual studio .NET though, you have to do something like #pragma comment("gl32.lib", lib) and glu32 or something along those lines and it has to be a windows application (i dont know the details just yet)

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++: 3D Camera 2006-03-11 18:20:16 Reply

At 3/11/06 06:15 PM, Glaiel_Gamer wrote: um....... i dont use a commant prompt compiler (i dont know PC's too well...)

In visual studio .NET though, you have to do something like #pragma comment("gl32.lib", lib) and glu32 or something along those lines and it has to be a windows application (i dont know the details just yet)

I'm not using a command prompt compiler either, Dev-C++ is a free C++/C compiler.. I've included the glu32 library - that doesn't do anything. I don't have a gl32 lib.

The project is a windows app.


Sup, bitches :)

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++: 3D Camera 2006-03-11 18:31:43 Reply

At 3/11/06 06:20 PM, -liam- wrote:

I don't have a gl32 lib.

opengl32.lib


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to C++: 3D Camera 2006-03-11 18:33:13 Reply

oh and one thing glaiel

youre dot product function is wrong

dot product is a scalar

a dot b = a.x*b.x + a.y*b.y + a.z*b.z, a single scalar, not a vector <a.x*b.x, a.y*b.y, a.z*b.z>


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to C++: 3D Camera 2006-03-11 18:51:04 Reply

At 3/11/06 06:00 PM, -liam- wrote: [Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
[Project1.exe] Error 1

It's because I didn't have a main function =\ Nvm, deleted it now and cba.

IE7 is kewel, almost as good as FF :O

Sup, bitches :)

BBS Signature
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: 3D Camera 2006-03-11 19:38:01 Reply

At 3/11/06 06:33 PM, -dELta- wrote: oh and one thing glaiel

youre dot product function is wrong

dot product is a scalar

a dot b = a.x*b.x + a.y*b.y + a.z*b.z, a single scalar, not a vector <a.x*b.x, a.y*b.y, a.z*b.z>

lol thanks.
again....

shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to C++: 3D Camera 2006-03-16 03:17:28 Reply

At 3/11/06 05:36 PM, -liam- wrote: This application has failed to start because MSVCP71D.dll was not found. Re-installing the application may fix this problem.

yeah i get that error to.. =( i Down Loaded that dll from http://www.dll-files.com and i still get the same error =(

i put the dll file in c:/windows/system and c:/windows/system32 i couldnt remember which one i was ment to go in :0

FireBred
FireBred
  • Member since: Feb. 26, 2006
  • Offline.
Forum Stats
Member
Level 05
Blank Slate
Response to C++: 3D Camera 2006-03-16 05:01:53 Reply

At 3/16/06 03:17 AM, shazwoogle wrote:
At 3/11/06 05:36 PM, -liam- wrote: This application has failed to start because MSVCP71D.dll was not found. Re-installing the application may fix this problem.
yeah i get that error to.. =( i Down Loaded that dll from http://www.dll-files.com and i still get the same error =(

I got a similar error, you need both msvcr71d.dll and msvcp71d.dll. download the both it'll work fine

i put the dll file in c:/windows/system and c:/windows/system32 i couldnt remember which one i was ment to go in :0

You can leave them in the same directory as camera.exe and it'll work fine.

Nice camera btw, clever indeed =)

White-Rhyno
White-Rhyno
  • Member since: Apr. 28, 2003
  • Offline.
Forum Stats
Member
Level 38
Blank Slate
Response to C++: 3D Camera 2006-03-16 08:58:40 Reply

At 3/11/06 03:48 PM, Glaiel_Gamer wrote: But those matrices are annoying.

You used Matrices? Where?

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: 3D Camera 2006-03-16 14:40:56 Reply

At 3/16/06 08:58 AM, White_Rhyno wrote:
At 3/11/06 03:48 PM, Glaiel_Gamer wrote: But those matrices are annoying.
You used Matrices? Where?

in the rotate function, i multiplied the vector by a matrix, but i just skipped the part of declaring the matrix to shorten code

Gokenshadow
Gokenshadow
  • Member since: May. 22, 2005
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to C++: 3D Camera 2006-03-16 18:25:00 Reply

Hey pretty cool. Wow


BBS Signature
HotDog-Productions
HotDog-Productions
  • Member since: May. 22, 2006
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to C++: 3D Camera 2006-06-13 16:57:37 Reply

I have a problem. It says there's errors. It says - `sin' undeclared (first use this function) and `cos' undeclared (first use this function)

What's wrong with it?

seel
seel
  • Member since: Jun. 27, 2005
  • Offline.
Forum Stats
Member
Level 21
Musician
Response to C++: 3D Camera 2006-06-13 17:17:11 Reply

At 6/13/06 04:57 PM, HotDog_Productions wrote: I have a problem. It says there's errors. It says - `sin' undeclared (first use this function) and `cos' undeclared (first use this function)

What's wrong with it?

The fact you are using bloodshed dev-c++..

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to C++: 3D Camera 2006-06-13 18:52:55 Reply

At 6/13/06 04:57 PM, HotDog_Productions wrote: I have a problem. It says there's errors. It says - `sin' undeclared (first use this function) and `cos' undeclared (first use this function)

What's wrong with it?

#include <math.h>