View Single Post
Old 29-Mar-2007, 03:37 AM   #1 (permalink)
Iphone
Fixed Error!
 
Iphone's Avatar

Posts: 4,202
Join Date: Mar 2007
Rep Power: 6 Iphone is on a distinguished road

IM:
Default Camera Movement in OPENGL

Below is my code for creation of walls and apply texture to it.All the methods below in the class are used to create the walls in a polygon grid (10 X 10) format.And also create a ambient light for Day and Night effect.Now the only thing i need to do i put in a camera so that the user can move only inside the room.

Sorry for my silly question.As i am not programmer basically and i am working in networking feild.And this is the first time i am using C++ and opengl.


#ifndef WIN32
#define WIN32
#endif
#include
#include
#include
#include
#include
#include
#include
#include


// included to enable using of images
#include "textureclass.cpp"
#include "Camera.h"

Camera view;

// The below 2 vriables are for the rotation
static GLfloat spin = 0.0;
int ifFound=0;

// Variable to enable and disable night mode.
// nightMode - false, enables global ambient
// nightMode - true, disables global ambient
static bool nightMode = false;

GLUquadric* quad;

float xtmp;


// The viewing position of the camera
static GLdouble xPos = 0.0;
static GLdouble yPos = 0.0;
static GLdouble zPos = 5.0;

// Texture Maps are declared
static Texture_Image wall_bmp;
static Texture_Image bathRug_bmp;
static Texture_Image bathDoor_bmp;
static Texture_Image bathwalls_bmp;
static Texture_Image bathceiling_bmp;


void draw_backwall(void)
{
// Store all the Values of the vertices x,y,z and the diffenrce between the
// first and the consecutive polygon.
float x= 0.978024;
float y= 1.00523;
float z = 0;
float diffy = 0.197046;
float diffx = 0.196046;



for(int i=1;i<=10;i++)
{
x= 0.978024;
for(int j=1;j<=10;j++)
{
glBegin (GL_POLYGON);
glVertex3f(x,y,z);
glVertex3f(x,y-diffy,z);
glVertex3f(x-diffx,y-diffy,z);
glVertex3f(x-diffx,y,z);
glEnd();
x=x-diffx;
}
y=y-diffy;
}

}


void draw_ceilingwall(void)
{
// Store all the Values of the vertices x,y,z and the diffenrce between the
// first and the consecutive polygon.
float x= -0.982438;
float y= 1.005;
float z = 0.206816;
float diffz = 0.197046;
float diffx = -0.196046;

glBindTexture( GL_TEXTURE_2D, bathceiling_bmp.Get_Texture() );
float fwidth, fheight;
int width, height;
bathceiling_bmp.Get_Fraction_Used ( &fwidth, &fheight);
glEnable(GL_TEXTURE_2D);

for(int i=1;i<=10;i++)
{
x= -0.982438;
for(int j=1;j<=10;j++)
{

glBegin (GL_POLYGON);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x-diffx,y,z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x-diffx,y,z-diffz);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y,z-diffz);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z);
glEnd();
x=x-diffx;

}
z=z+diffz;
}
glDisable(GL_TEXTURE_2D);
}


void draw_floorwall(void)
{
// Store all the Values of the vertices x,y,z and the diffenrce between the
// first and the consecutive polygon.

float x= -0.786392;
float y= -0.97277;
float z = 1.765194;
float diffz = 0.197046;
float diffx = 0.196046;



for(int i=1;i<=10;i++)
{
glBindTexture( GL_TEXTURE_2D, wall_bmp.Get_Texture() );
float fwidth, fheight;
int width, height;
wall_bmp.Get_Fraction_Used ( &fwidth, &fheight);
glEnable(GL_TEXTURE_2D);
x= -0.786392;
for(int j=1;j<=10;j++)
{

glBegin (GL_POLYGON);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y,z+diffz);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x-diffx,y,z+diffz);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x-diffx,y,z);
glEnd();
x=x+diffx;

}
z=z-diffz;

}
glDisable(GL_TEXTURE_2D);
}



void draw_rightwall(void)
{
// Store all the Values of the vertices x,y,z and the diffenrce between the
// first and the consecutive polygon.

float x= 0.979804;
float y= -0.768184;
float z = 1.76026;
float diffy = 0.197046;
float diffz = -0.19605;

glBindTexture( GL_TEXTURE_2D, bathwalls_bmp.Get_Texture() );
float fwidth, fheight;
int width, height;
bathwalls_bmp.Get_Fraction_Used ( &fwidth, &fheight);
glEnable(GL_TEXTURE_2D);

for(int i=1;i<=10;i++)
{
z= 1.76026;
for(int j=1;j<=10;j++)
{

glBegin (GL_POLYGON);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y-diffy,z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y-diffy,z-diffz);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z-diffz);
glEnd();
z=z+diffz;

}
y=y+diffy;

}
glDisable(GL_TEXTURE_2D);
}

void draw_leftwall(void)
{
// Store all the Values of the vertices x,y,z and the diffenrce between the
// first and the consecutive polygon.

glBindTexture( GL_TEXTURE_2D, bathwalls_bmp.Get_Texture() );
float fwidth, fheight;
int width, height;
bathwalls_bmp.Get_Fraction_Used ( &fwidth, &fheight);
glEnable(GL_TEXTURE_2D);


float x= -0.990528;
float y= 1.00523;
float z = -0.0160207;
float diffy = 0.197046;
float diffz = -0.1960457;

for(int i=1;i<=10;i++)
{
z= -0.0160207;
for(int j=1;j<=10;j++)
{

glBegin (GL_POLYGON);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x,y,z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x,y-diffy,z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x,y-diffy,z-diffz);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x,y,z-diffz);
glEnd();
z=z-diffz;

}
y=y-diffy;
}
glDisable(GL_TEXTURE_2D);
}

void draw_mat(void)
{

glBindTexture( GL_TEXTURE_2D, bathRug_bmp.Get_Texture() );
float fwidth, fheight;
int width, height;
bathRug_bmp.Get_Fraction_Used ( &fwidth, &fheight);
glEnable(GL_TEXTURE_2D);
glBegin (GL_POLYGON);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.41,-0.96,0.05);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.95,-0.96,0.05);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.95,-0.96,0.38);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.41,-0.96,0.38);
glEnd();
glDisable(GL_TEXTURE_2D);
}

void draw_door(void)
{

glBindTexture( GL_TEXTURE_2D, bathDoor_bmp.Get_Texture() );
float fwidth, fheight;
int width, height;
bathDoor_bmp.Get_Fraction_Used ( &fwidth, &fheight);
glEnable(GL_TEXTURE_2D);
glBegin (GL_POLYGON);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.25,0.86,0.01);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.95,0.86,0.01);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0.95,-0.96,0.01);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.25,-0.96,0.01);
glEnd();
glDisable(GL_TEXTURE_2D);
}

// method to setup the lighting
void set_light ( void)
{


// controlling the ambient light
GLfloat ambientVal;
if(!nightMode)
ambientVal = 2.0;
else
ambientVal = 0.2;

GLfloat lmodel_ambient[] = {ambientVal,ambientVal,ambientVal, 1.0 };
glClearColor(0.0,0.0,0.0,1.0);
glShadeModel(GL_SMOOTH);

// enable ambient light
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

glLightModeli ( GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE );
glLightModeli ( GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE );

// enable all the lighting
glEnable ( GL_LIGHTING ); // ambient
glEnable (GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); // to enable proper transparency and color after the lighting
}

void control(unsigned char key, int x, int y){
Camera temp(view);
Point a;

//Point blah = temp.getLooking();
switch(key){
//Strafe Left
case '7':
temp.strafe(1);
a.copy(temp.getLooking());

break;
//Strafe Right
case '9':
temp.strafe(-1);
a.copy(temp.getLooking());

break;
//Look up
case '8':
temp.pitch(1);
a.copy(temp.getLooking());
break;
//Look down
case '5':
temp.pitch(-1);
a.copy(temp.getLooking());
break;
//Turn left
case '4':
temp.turn(1);
a.copy(temp.getLooking());
break;
//Turn right
case '6':
temp.turn(-1);
a.copy(temp.getLooking());
break;
//Move foreward
case '0':
temp.walk(1);
a.copy(temp.getLooking());
break;
//Move backward
case '1':
temp.walk(-1);
a.copy(temp.getLooking());
break;
case 'q':
temp.altitude(1);
break;
case 'a':
temp.altitude(-1);
break;
default:
break;
}

bool x1 = false;
temp.atLoc.setY(Collision::checkScene(temp.getAt()));
view.upd(temp,a,x1);
view.setStatic();
glutPostRedisplay();
}



void init(void)
{
//glClearColor (.5,1,1,1); // Black is the Background
glShadeModel (GL_SMOOTH); // The Shade Model is smooth , to create the reality.

glClearColor (1,1,1, 1.0);
glClearDepth (1.0);

// set all the textures
wall_bmp.Make_Std_Texture("wall_back.bmp",true,GL_BLEND);
bathRug_bmp.Make_Std_Texture("bath_rug.bmp",true,GL_DECAL);
bathDoor_bmp.Make_Std_Texture("bath_door.bmp",true,GL_DECAL);
bathwalls_bmp.Make_Std_Texture("bath_walls.bmp",true,GL_DECAL);
bathceiling_bmp.Make_Std_Texture("bath_walls.bmp",true,GL_DECAL);

glShadeModel (GL_FLAT);
quad = gluNewQuadric();
glShadeModel (GL_SMOOTH);
}


void display(void)
{

set_light(); // Call the light
// clear colour and depth buffer(window)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();


// current position of viewer x,y,z
gluLookAt ( xPos, yPos, zPos, 0., 0., 0., 0., 1.0, 0. );


// Enabling depth testing
glEnable (GL_DEPTH_TEST);


glScalef(1.55,1.55,1.55);
// Draw the Back Wall
draw_backwall();

// Draw the roof of the room.
draw_ceilingwall();

// Draw the floor of the wall
draw_floorwall();

// Draw the right Wall
draw_rightwall();


// Draw the left Wall
draw_leftwall();

// Draw the mat at the entrance
draw_mat();

// Draw the door the bathrroom
draw_door();

glutSwapBuffers();

}

void reshape(int width, int height)
{


// Viewport is set to the same size of the window.
glViewport (0, 0, (GLsizei) width, (GLsizei) height);

// Sets the Matrix mode to Projection
glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Program uses a perspective projection
// Parameters: field of view (in angle degrees), aspect ratio, distance to near clipping plane,
// distance to far cutoff plane

gluPerspective(75.0, 1.0, 0.5, 1500.0);

// Sets the Matrix mode to Modelview
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}



void keyboard(unsigned char key, int x, int y)
{
switch (key) {

case 'o':
nightMode = false;
break;

case 'O':
nightMode = true;
break;

default:
return;
break;
}
glutPostRedisplay ();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); //set up window (double buffering, colour setup)
glutInitWindowSize (700, 700); // Set initial window size to 700 x 700
glutInitWindowPosition (10, 10); // Set initial window position of project
glutCreateWindow (argv[0]); // Create the application window

init();

glutDisplayFunc(display);

glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}
Iphone is offline   Reply With Quote