First Person Games - Camera help!

Started by
11 comments, last by YodaTheCoder 22 years, 6 months ago
gluLookAt isnt an OpenGL function, rather its a utility function that generally makes it easier to move the world and make it look directly at a specific location.

For a 1st person shooter, you dont want to have to calculate the specific location that the camera is to look at when you move around. gluLookAt internally calls glTranslatef and glRotatef, and needs to be in the same area of the code anyway.
-----------------------"When I have a problem on an Nvidia, I assume that it is my fault. With anyone else's drivers, I assume it is their fault" - John Carmack
Advertisement
Okay, want to bore yourselves in my pathetic code? Here it is (compiled perfectly with MSVC6.0) NOTE: There are some includes and variables that are declared and not used yet; theyre for later.

#include
#include
#include
#include
#include
#include "main.h"

bool forward; //Movement bool''s
bool backward;
bool turnleft;
bool turnright;
bool strafeleft;
bool straferight;

float FBpos=0.0; //front back position
float LRpos=0.0; //left right position

//Function Declarations
void keysDown(unsigned char key, int x, int y);
void keysUp(unsigned char key, int x, int y);
void renderScene(void);
int moveFB(int direction);
int moveLR(float angle);


int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitWindowPosition(100,100);
glutInitWindowSize(550,550);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutCreateWindow("<-Hello world!->");
glutFullScreen();
glutReshapeFunc(changeSize);
glutDisplayFunc(renderScene);
glutKeyboardUpFunc(keysUp);
glutKeyboardFunc(keysDown);
glutIdleFunc(renderScene);

glutMainLoop();
return 0;
}

//Keyboard - Keydown Function
void keysDown(unsigned char key, int x, int y){
switch(key){
case ''w'': forward=1; break;
case ''s'': backward=1; break;
case ''a'': turnleft=1; break;
case ''d'': turnright=1; break;


case 27: exit(1); break;
default: break;
}
cout<}

//Keyboard - Keyup Function
void keysUp(unsigned char key, int x, int y){
switch(key){
case ''w'': forward=0; break;
case ''s'': backward=0; break;
case ''a'': turnleft=0; break;
case ''d'': turnright=0; break;

default: break;
}
cout<}

//Terrain Function
void renderScene(void){
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);

if(forward){
FBpos+=0.1;
moveFB(1);
}
if(backward){
FBpos-=0.1;
moveFB(-1);
}
if(turnleft){
LRpos-=0.01;
moveLR(LRpos);
}
if(turnright){
LRpos+=0.01;
moveLR(LRpos);
}

//DRAW THE FLOOR
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(GREEN);
glVertex3f( 2.5f,-0.5f,-2.5f);
glColor3f(RED);
glVertex3f( 2.5f,-0.5f, 7.5f);
glColor3f(BLUE);
glVertex3f(-2.5f,-0.5f, 7.5f);
glColor3f(WHITE);
glVertex3f(-2.5f,-0.5f,-2.5f);
glEnd();
glPopMatrix();

//DRAW THE CEILING
glPushMatrix();
glBegin(GL_POLYGON);
glColor3f(BLUE);
glVertex3f( 20.5f, 5.5f,-20.5f);
glColor3f(BLUE);
glVertex3f( 20.5f, 5.5f, 70.5f);
glColor3f(TEAL);
glVertex3f(-20.5f, 5.5f, 70.5f);
glColor3f(TEAL);
glVertex3f(-20.5f, 5.5f,-20.5f);
glEnd();
glPopMatrix();


glutSwapBuffers();
}


NOW IN another file, main.h, I have the following:

#include
#include

#define WHITE 1.0f, 1.0f, 1.0f
#define TEAL 0.0f, 1.0f, 1.0f
#define BLUE 0.0f, 0.0f, 1.0f
#define RED 1.0f, 0.0f, 0.0f
#define GREEN 0.0f, 1.0f, 0.0f
#define BLACK 0.0f, 0.0f, 0.0f

static float angle=0.0,ratio;
static float x=0.0f,y=0.0f,z=0.0f;
static float lx=0.0f,ly=0.0f,lz=-1.0f;

int moveFB(int direction){ //''1'' if forward ''-1'' if backwards
x += direction*(lx)*0.1;
z += direction*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0,1,0);
return 0;
}
int moveLR(float angle){
lx = sin(angle);
lz = -cos(angle);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0,1,0);
return 0;
}


void changeSize(int w, int h) {

if(h == 0)
h = 1;

float ratio = 1.0* w / h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glViewport(0, 0, w, h);

gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
}



Look newbie-ish? Ya, thought so... Compile/run it, it looks okay! (now all I need to learn is texture mapping).. which im on right now =)

~Jesse
By the way, the movement controls are


w = forward
s = bakcwards
a = turn left
d = turn right



Ill eventually use the arrow keys for strafing..

~Jesse

This topic is closed to new replies.

Advertisement