hi,
After adding texture to the walls in my 3d maze, the game has slowed down considerably. What can I do to fix this?
/*** This is where the image is read into array and image is filled ****/
void readPPM()
{
int n,m;
char dummy[200];
FILE *fp;
int r;
fp = fopen( "brick_texture.tga", "r");
fgets(dummy, 200, fp);
fgets(dummy, 200, fp);
fgets(dummy, 200, fp);
fgets(dummy, 200, fp);
fgets(dummy, 200, fp);
fscanf(fp,"%d %d
", &n,&m);
fscanf(fp,"%d
", &r);
printf("Image size:%d x %d, # of color of image: %d
", n, m, r);
image = (GLubyte*)malloc(3 * sizeof(GLubyte) * n * m);
fread(image, 1,3 * n * m, fp);
fclose(fp); //dont forget to close file!!
free(image);
}
int main(int argc, char* argv[]) {
glutInit(&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() {
FILE *ifp;
readPPM();
glClearColor(1, 1, 1, 1); // set backgoround color as white
glShadeModel(GL_SMOOTH); // smooth mode
glEnable(GL_DEPTH_TEST); // enable depth mode
glEnable(GL_NORMALIZE);
person.x = 5;
person.y = 5;
person.z = 10;
person.direction = 180;
b3rdParty = false;
viewPortHeight = WINDOW_HEIGHT;
viewPortWidth = WINDOW_WIDTH;
ifp = fopen( "stage1.txt", "r");
fscanf(ifp, "%d %d ", &map_length, &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 ", &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
", i, j);
}
}
}
ground_length = map_length * BLOCK_SIZE;
ground_width = map_width * BLOCK_SIZE;
printf("Start!
");
fclose(ifp);
}
void drawGround() {
glBegin(GL_POLYGON);
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(int i, int j) {
GLfloat v[][3] = {{i * BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, 0},
{i * BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, WALL_HEIGHT},
{i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, WALL_HEIGHT},
{i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE + BLOCK_SIZE, 0},
{i * BLOCK_SIZE, j * BLOCK_SIZE, 0},
{i * BLOCK_SIZE, j * BLOCK_SIZE, WALL_HEIGHT},
{i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE, WALL_HEIGHT},
{i * BLOCK_SIZE + BLOCK_SIZE, j * BLOCK_SIZE, 0}};
polygon(v[0], v[3], v[2], v[1]);
polygon(v[3], v[7], v[6], v[2]);
polygon(v[0], v[4], v[5], v[1]);
polygon(v[4], v[7], v[6], v[5]);
}
void polygon(GLfloat* a, GLfloat* b, GLfloat* c, GLfloat* d)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE,
(GLvoid *)image);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glEnable(GL_TEXTURE_2D);
glBegin(GL_POLYGON);
glColor3f(0, 0, 1);
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord2d(0.0,0.0);
glVertex3fv(a);
glTexCoord2d(1.0,0.0);
glVertex3fv(b);
glTexCoord2d(1.0,1.0);
glVertex3fv(c);
glTexCoord2d(0.0,1.0);
glVertex3fv(d);
glEnd();
glDisable(GL_TEXTURE_2D);
}
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, nEndY - userMappedY * miniBlockHeight);
glVertex2f(nStartX + userMappedX * miniBlockWidth, nEndY - userMappedY * miniBlockHeight - miniBlockHeight);
glVertex2f(nStartX + userMappedX * miniBlockWidth + miniBlockWidth, nEndY - userMappedY * miniBlockHeight - miniBlockHeight);
glVertex2f(nStartX + userMappedX * miniBlockWidth + miniBlockWidth, nEndY - 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, nEndY - j * miniBlockHeight);
glVertex2f(nStartX + i * miniBlockWidth, nEndY - j * miniBlockHeight - miniBlockHeight);
glVertex2f(nStartX + i * miniBlockWidth + miniBlockWidth, nEndY - j * miniBlockHeight - miniBlockHeight);
glVertex2f(nStartX + i * miniBlockWidth + miniBlockWidth, nEndY - 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
",
person.x, person.y, person.z, person.direction);
}