Results 1 to 2 of 2

Thread: 3d maze is moving too slow!

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    15

    3d maze is moving too slow!

    hi,
    After adding texture to the walls in my 3d maze, the game has slowed down considerably. What can I do to fix this?

    Code :
    /*** 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", &n,&m);
        fscanf(fp,"%d\n", &r);
     
        printf("Image size:%d x %d, # of color of image: %d\n", 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);
     
    }
    Code :
    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 ", &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);
     
     
    }
    Code :
    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);
     
    }
    Code :
    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\n",
               person.x, person.y, person.z, person.direction);
    }
    What in the.....????!!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Apr 2007
    Posts
    268

    Re: 3d maze is moving too slow!

    glTexImage2D() only needs to be called once. (I mean not even once per frame, only once in the whole application). It triggers a memory transfer from the RAM to the VRAM.

    You are calling it each time you draw a polygon in polygon(). Don't do that.

Similar Threads

  1. openGL 2D maze
    By C4X4SIX in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-27-2012, 03:26 PM
  2. Code for 2d Maze
    By anilmadan in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 04-04-2011, 04:42 PM
  3. 3D Maze
    By fleurmtl in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-07-2009, 12:47 PM
  4. creating a maze
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 01-21-2006, 07:01 AM
  5. 2D Maze Code
    By anilmadan in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 01-30-2001, 11:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean