|
Fixed Error!
Posts: 4,202
Join Date: Mar 2007
Rep Power: 6
|
Re: Camera Movement in OPENGL
Ok: The camera class:
#include
#include
#include
#include
#include
//#include "Points.h"
//using namespace std;
class Camera{
private:
float currentLook;
float currentPitch;
static const float moveBy = 1.;
static const float lookRadius = .5;
static const float cameraHeight = 1.5;
static const float PI = 3.1416;
static const float yawn = PI / 32.0;
static const float turnVal = PI / 32.0;
static const float pitchMax = PI / 4.0;
static const float pitchMin = -1.0 * PI / 4.0;
public:
Point atLoc;
Point looking;
//Default class constructor sets the camera position at
// 0, cameraHeight, 0
// Sets the looking position to
// 0 PI along the distance lookRadius
Camera()
{
float x, y, z;
currentLook = 0.0;
currentPitch = 0.0;
Point tLoc(0.0, cameraHeight, 0.0);
atLoc.copy(tLoc);
x = atLoc.getX() + cos(currentLook) * lookRadius;
y = atLoc.getY();
z = atLoc.getZ() + sin(currentLook) * lookRadius;
Point tLook(x, y, z);
looking.copy(tLook);
}
//Class constructor takes in tempAt for camera location
// and the looking angle.
// +X Axis is 0 PI
// +Z Axis is 1/2 PI
// -X Axis is PI
// -Z Axis is 3/2 PI
Camera(Point tempAt, float angle)
{
float x, y, z;
currentLook = angle;
currentPitch = 0.0;
atLoc.copy(tempAt);
x = atLoc.getX() + cos(currentLook) * lookRadius;
y = atLoc.getY();
z = atLoc.getZ() + sin(currentLook) * lookRadius;
Point tLook(x, y, z);
looking.copy(tLook);
}
//Copy Constructor
// Takes in a camera and assigns a new camera
// to be an identical copy of the camera passed in
Camera(Camera &a)
{
currentLook = a.currentLook;
currentPitch = a.currentPitch;
atLoc.copy(a.atLoc);
looking.copy(a.looking);
}
//Takes in a direction:
// 1 - Increases the angle (aka CCW)
// -1 - Decreases the angle (aka CW)
// value of change is turn
void turn(int dir){
float x, y, z;
if(dir == 1){
currentLook += turnVal;
}
else{
currentLook -= turnVal;
}
x = atLoc.getX() + sin(currentLook) * lookRadius;
y = looking.getY();
z = atLoc.getZ() + cos(currentLook) * lookRadius;
Point tLook(x, y, z);
looking.copy(tLook);
}
//Takes in a direction:
// 1 - Increases the angle - Y Axis (aka Up)
// -1 - Decreases the angle (aka down)
// value of change is pitch
// Constraints: pitchMin <= currentPitch <= pitchMax
void pitch(int dir){
float x, y, z;
if(dir == 1){
if(currentPitch <= pitchMax){
currentPitch += yawn;
}
}
else{
if(currentPitch >= pitchMin){
currentPitch -= yawn;
}
}
y = atLoc.getY() + sin(currentPitch) * lookRadius;
looking.setY(y);
}
//Sets glu Camera with values stored in this instantiation
void updateCamera()
{
gluLookAt(atLoc.getX(), atLoc.getY(), atLoc.getZ(),
looking.getX(), looking.getY(), looking.getZ(),
0.0, 1.0, 0.0);
if(currentLook >= (2.0 * PI) ){
currentLook -= (2.0 * PI);
}
}
//Takes in a direction:
// 1 is left
// -1 is right
// Moves by moveBy units
void strafe(int dir){
Point strafeDir;
float x, y, z;
float dist;
if(dir == 1){
//Strafe left
x = atLoc.getX() + sin(currentLook + (PI / 2.0) ) * lookRadius;
z = atLoc.getZ() + cos(currentLook + (PI / 2.0) ) * lookRadius;
dist = moveBy;
}
else{
//Strafe right
x = atLoc.getX() + sin(currentLook + (PI / -2.0) ) * lookRadius;
z = atLoc.getZ() + cos(currentLook + (PI / -2.0) ) * lookRadius;
dist = moveBy;
}
Point sDir(x, y, z);
strafeDir.copy(sDir);
atLoc.setX(atLoc.getX() + dist * (strafeDir.getX() - atLoc.getX()) );
atLoc.setZ(atLoc.getZ() + dist * (strafeDir.getZ() - atLoc.getZ()) );
x = atLoc.getX() + sin(currentLook) * lookRadius;
y = looking.getY();
z = atLoc.getZ() + cos(currentLook) * lookRadius;
Point tLook(x, y, z);
looking.copy(tLook);
}
void altitude(int dir){
if(dir == 1){
looking.setY(looking.getY() + 1.0);
atLoc.setY(atLoc.getY() +1.0);
}
else{
looking.setY(looking.getY() - 1.0);
atLoc.setY(atLoc.getY() - 1.0);
}
}
//Takes in a direction:
// 1 is forward
// -1 is reverse
// Moves by moveBy units
void walk(int dir){
float x, y, z;
float dist;
if(dir == 1){
dist = moveBy * 1.0;
}
else{
dist = moveBy * -1.0;
}
atLoc.setX(atLoc.getX() + dist * (looking.getX() - atLoc.getX()) );
atLoc.setZ(atLoc.getZ() + dist * (looking.getZ() - atLoc.getZ()) );
x = atLoc.getX() + sin(currentLook) * lookRadius;
y = looking.getY();
z = atLoc.getZ() + cos(currentLook) * lookRadius;
Point tLook(x, y, z);
looking.copy(tLook);
}
//Accessor for At Location
//Stored in a Point class
Point getAt(){ return atLoc; }
//Accessor for Looking Location
//Stored in a Point class
Point getLooking() { return looking; }
float getLookAngle() {return currentLook; }
void setLook(Point a)
{
looking.copy(a);
}
void setAt(Point a)
{
atLoc.copy(a);
}
};
You DO need my points class which is this:
class Point{
private:
float x;
float y;
float z;
public:
float pt[3];
Point():x(0), y(0), z(0){/*Empty*/};
Point(float xT, float yT, float zT):x(xT), y(yT), z(zT)
{
pt[0] = x;
pt[1] = y;
pt[2] = z;
}
Point(const Point &a){
x = a.x;
y = a.y;
z = a.z;
}
float getX(){return x;};
float getY(){return y;};
float getZ(){return z;};
void setX(float xT){x = xT;};
void setY(float yT){y = yT;};
void setZ(float zT){z = zT;};
void copy(Point a);
};
void Point::copy(Point a)
{
x = a.x;
y = a.y;
z = a.z;
}
Then you need to add this into your keyboard switch case:
//Strafe Left
case '7':
view.strafe(1);
break;
//Strafe Right
case '9':
view.strafe(-1);
break;
//Look up
case '8':
view.pitch(1); break;
//Look down
case '5':
view.pitch(-1);
break;
//Turn left
case '4':
view.turn(1);
break;
//Turn right
case '6':
view.turn(-1);
break;
//Move foreward
case '0':
view.walk(1);
break;
//Move backward
case '1':
view.walk(-1);
break;
case 'q':
view.altitude(1);
break;
case 'a':
view.altitude(-1);
break;
Do away with that control function you copied from my player class, it doesn't work that way. Then instead of your gluLookAt call in your display function you want view.updateCamera();
;-)
That should be everything you need, possibly some minor debugging to do because I dont have time to actually try to compile this and do it for you but that is the general idea. For a networking guy never before using C++ and OpenGL you did pretty good. :-P
If you have any problems ask away
|