PDA

View Full Version : How to change a 2D maze to 3D?



ShaolinMist
12-08-2011, 06:29 PM
Hi I'm new to opengl programming and I have a hard time. I want to change my 2d
maze into a 3D maze like a first person shooter but when I try to create the walls, nothing shows! I'm sure I have to change something here...Here's my code:

void drawWalls() {

for(int i = 0; i < map_width; i++) {
for(int j = 0; j < map_length; j++) {
if(mappings[i][j] == 1) {
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex3f(i * BLOCK_SIZE, j * BLOCK_SIZE, .1);
glVertex3f(i * BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, .1);
glVertex3f(i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, .1);
glVertex3f(i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE, .1);
glEnd();

}
}
}
}

Here's the rest of the code:
#include <OPENGL/glu.h>
#include <OPENGL/gl.h>
#include <GLUT/glut.h>
#include <math.h>

#include <stdio.h>
#include <stdlib.h>

#define FOVY 60
#define NEAR 1
#define FAR 10000
#define STEP 2
#define ROTATION 2
#define WINDOW_HEIGHT 600
#define WINDOW_WIDTH 600
#define DEG2RAD 3.1415926535f / 180 // degree to rad
#define BLOCK_SIZE 20


void init(); // initializing
void displayHandler(); // display handler
void reshapeHandler(int, int); // reahpe handler
void keyboardHandler(unsigned char, int, int); // keyboard handler
void mouseHandler(int, int, int, int); // mouse handler
void specialHandler(int, int, int); // special handler(arrow key, function keys)

void drawPerson();
void drawGround();
void drawWalls();
void cube(GLfloat, GLfloat, GLfloat);
void drawMiniMap();
bool checkWall(GLfloat, GLfloat);
void debug();
void setOrth(); // set the mode as ortho
void setPers(); // set the mode as perspective

//*** data of person
struct Person {
GLfloat x;
GLfloat y;
GLfloat z;
GLfloat direction;
};

Person person;
bool b3rdParty;
int** mappings;
int map_length;
int map_width;
int ground_length;
int ground_width;
int viewPortHeight;
int viewPortWidth;
GLfloat boxHeight = 10.00;


int main(int argc, char *argv[]) {

glutInit(&amp;argc, argv); // initialize glut and gl

// double buffer, RGB mode, and depth mode
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );

glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); // set the window size
glutInitWindowPosition(0, 0); // set the start point of window
glutCreateWindow("CSC 630 - Final Project"); // create window

glutDisplayFunc(displayHandler); // set display callback
glutReshapeFunc(reshapeHandler); // set reshape callback
glutKeyboardFunc(keyboardHandler); // set keyboard callback
glutMouseFunc(mouseHandler); // set mouse callback
glutSpecialFunc(specialHandler); // specialHandler function handles special event

init(); // call init for initializing
glutMainLoop(); // running in a loop

return 0; // finish successfully
}

void init() {


glClearColor(1, 1, 1, 1); // set backgoround color as white
glShadeModel(GL_SMOOTH); // smooth mode
glEnable(GL_DEPTH_TEST); // enable depth mode

person.x = 5;
person.y = 5;
person.z = 10;
person.direction = 180;

b3rdParty = false;

viewPortHeight = WINDOW_HEIGHT;
viewPortWidth = WINDOW_WIDTH;

FILE* ifp;

ifp = fopen("stage1.txt", "r");


fscanf(ifp, "%d %d ", &amp;map_length, &amp;map_width);

mappings = (int**)malloc(sizeof(int*) * map_width);

for(int i = 0; i < map_width; i++) {
mappings[i] = (int*)malloc(sizeof(int) * map_length);
}

for(int i = 0; i < map_width; i++) {
for(int j = 0; j < map_length; j++) {
fscanf(ifp, "%d ", &amp;mappings[i][j]);

// starting point of the user
if(mappings[i][j] == 10) {
person.x = i * BLOCK_SIZE + (BLOCK_SIZE / 2);
person.y = j * BLOCK_SIZE + (BLOCK_SIZE / 2);
printf("Start here: %d %d\n", i, j);
}
}
}

ground_length = map_length * BLOCK_SIZE;
ground_width = map_width * BLOCK_SIZE;

printf("Start!\n");

fclose(ifp);
}

void displayHandler() {

// clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity(); // load identity

if(b3rdParty) {
gluLookAt(
person.x, person.y, person.z, // eye view
person.x + FAR * sin(DEG2RAD * person.direction),
person.y + FAR * cos(DEG2RAD * person.direction),
person.z, // center view
0, 0, 1); // up direction
} else {
gluLookAt(
person.x, person.y, person.z, // eye view
person.x + FAR * sin(DEG2RAD * person.direction),
person.y + FAR * cos(DEG2RAD * person.direction),
person.z, // center view
0, 0, 1); // up direction
}

drawGround();
drawWalls();
drawPerson();


//*** Show mini map
setOrth(); // chage the drawing mode as ortho

glPushMatrix(); // push current setting
glLoadIdentity(); // load identity

drawMiniMap();

// pop previous setting
glPopMatrix();

// change the drawing mode as perspective
setPers();
//*** End of minimap

glFlush(); // draw
glutSwapBuffers(); // change buffer
}

void reshapeHandler(int width, int height) {

// reset the size of viewport
glViewport(0, 0, width, height);

viewPortHeight = height;
viewPortWidth = width;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// perspective mode. andgle, ratio, near, far
gluPerspective(FOVY, (GLfloat)width/height, NEAR, FAR);

glMatrixMode(GL_MODELVIEW);
}

//************************************************** ******************
// keyboard callback
//************************************************** ******************
void keyboardHandler(unsigned char c, int x, int y) {

switch(c) {
// q or Q pushed?
case 'q':
case 'Q':
// program terminate
exit(0);
}
}

//************************************************** ************************
// special callback
//************************************************** ************************
void specialHandler(int key, int x, int y) {

int tempX = person.x;
int tempY = person.y;

switch(key) {
// left arrow?
case GLUT_KEY_LEFT:
person.direction -= ROTATION;

break;
// right arrow?
case GLUT_KEY_RIGHT:
person.direction += ROTATION;

break;
// up arrow?
case GLUT_KEY_UP:
// forward the person
tempX = person.x + STEP * sin(DEG2RAD * person.direction);
tempY = person.y + STEP * cos(DEG2RAD * person.direction);
//person.x += STEP * sin(DEG2RAD * person.direction);
//person.y += STEP * cos(DEG2RAD * person.direction);

break;
// down arrow?
case GLUT_KEY_DOWN:
// backward the person
tempX = person.x - STEP * sin(DEG2RAD * person.direction);
tempY = person.y - STEP * cos(DEG2RAD * person.direction);
// person.x -= STEP * sin(DEG2RAD * person.direction);
// person.y -= STEP * cos(DEG2RAD * person.direction);

break;
}

//*** change the direction correspondingly
if(person.direction > 180)
person.direction -= 360;
else if(person.direction <= -180)
person.direction += 360;

//*** blocking by wall (x)
if(checkWall(tempX, person.y)) {
person.x = tempX;

//*** check bound (x)
if(person.x < 0)
person.x = 0;
else if(person.x > ground_width)
person.x = ground_width;
}

//*** blocking by wall (y)
if(checkWall(person.x, tempY)) {
person.y = tempY;

if(person.y < 0)
person.y = 0;
else if(person.y > ground_length)
person.y = ground_length;
}

//debug();

glutPostRedisplay();
}

void mouseHandler(int, int, int, int) {

}

void drawGround() {

glBegin(GL_POLYGON);
// change ambient for ground
glColor3f(0.5, 0.5, 0.5);

glVertex3f(0, 0, 0);
glVertex3f(0, ground_width, 0);
glVertex3f(ground_length, ground_width, 0);
glVertex3f(ground_length, 0, 0);
glEnd();
}

void drawWalls() {

for(int i = 0; i < map_width; i++) {
for(int j = 0; j < map_length; j++) {
if(mappings[i][j] == 1) {
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
glVertex3f(i * BLOCK_SIZE, j * BLOCK_SIZE, .1);
glVertex3f(i * BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, .1);
glVertex3f(i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, .1);
glVertex3f(i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE, .1);
glEnd();

}
}
}
}

void drawPerson() {

}



void drawMiniMap() {

GLfloat nStartX = viewPortWidth * 0.68;
GLfloat nStartY = viewPortHeight * 0.02;
GLfloat nEndX = viewPortWidth * 0.98;
GLfloat nEndY = viewPortHeight * 0.32;

GLfloat miniBlockWidth = (nEndX - nStartX) / map_width;
GLfloat miniBlockHeight = (nEndY - nStartY) / map_length;

GLint userMappedX = person.x / BLOCK_SIZE;
GLint userMappedY = person.y / BLOCK_SIZE;

//*** black background
/*glBegin(GL_POLYGON);
glColor3f(0, 0, 0);
glVertex2f(nStartX, nStartY);
glVertex2f(nStartX, nEndY);
glVertex2f(nEndX, nEndY);
glVertex2f(nEndX, nStartY);
glEnd();*/

//*** draw current user's position
glBegin(GL_POLYGON);
glColor3f(1, 1, 0);
glVertex2f(nStartX + userMappedX * miniBlockWidth, nStartY + userMappedY * miniBlockHeight);
glVertex2f(nStartX + userMappedX * miniBlockWidth, nStartY + userMappedY * miniBlockHeight + miniBlockHeight);
glVertex2f(nStartX + userMappedX * miniBlockWidth + miniBlockWidth, nStartY + userMappedY * miniBlockHeight + miniBlockHeight);
glVertex2f(nStartX + userMappedX * miniBlockWidth + miniBlockWidth, nStartY + userMappedY * miniBlockHeight);
glEnd();

//*** draw walls and ways
for(int i = 0; i < map_width; i++) {
for(int j = 0; j < map_length; j++) {
if(mappings[i][j] == 1)
glColor3f(0, 0, 1);
else if(mappings[i][j] == 20)
glColor3f(1, 0, 0);
else
glColor3f(0.5, 0.5, 0.5);

glBegin(GL_POLYGON);
glVertex2f(nStartX + i * miniBlockWidth, nStartY + j * miniBlockHeight);
glVertex2f(nStartX + i * miniBlockWidth, nStartY + j * miniBlockHeight + miniBlockHeight);
glVertex2f(nStartX + i * miniBlockWidth + miniBlockWidth, nStartY + j * miniBlockHeight + miniBlockHeight);
glVertex2f(nStartX + i * miniBlockWidth + miniBlockWidth, nStartY + j * miniBlockHeight);
glEnd();
}
}
}

bool checkWall(GLfloat originalX, GLfloat originalY) {

int mappedX = originalX / BLOCK_SIZE;
int mappedY = originalY / BLOCK_SIZE;

if(mappings[mappedX][mappedY] == 1)
return false;
else
return true;
}

void setOrth() {

// switch to projection mode
glMatrixMode(GL_PROJECTION);
// save previous matrix which contains the
//settings for the perspective projection
glPushMatrix();
// reset matrix
glLoadIdentity();
// set a 2D orthographic projection
gluOrtho2D(0, viewPortWidth, 0, viewPortHeight);
// invert the y axis, down is positive
glScalef(1, -1, 1);
// mover the origin from the bottom left corner
// to the upper left corner
glTranslatef(0, -viewPortHeight, 0);
glMatrixMode(GL_MODELVIEW);
}

void setPers() {
// set the current matrix to GL_PROJECTION
glMatrixMode(GL_PROJECTION);
// restore previous settings
glPopMatrix();
// get back to GL_MODELVIEW matrix
glMatrixMode(GL_MODELVIEW);
}

void debug() {

printf("x: %f, y: %f, z: %f, direction: %f\n",
person.x, person.y, person.z, person.direction);
}