PDA

View Full Version : Do you know why this code is slow?



Ace_Man
11-03-2000, 10:39 PM
Sorry about all this code. I can't really do nice worlds in 3D if I can't even get 4 cylinders and a box to run at a decent speed.
I guess I'm too new to OpenGl to know which parts of this code are eating my frame rate....so I put it all here...Sorry...
It's all here so you can load it up. It may be easier to see what I mean this way. Just stand beside one of the posts in my scene and try to rotate the world..It all breaks up and goes slow...

It only slows like this if I maximize my window..If the window is small..it runs fine...I need it to run fast in fullscreen
Thank you all so much


#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <windows.h>
#include <math.h>

//Globals
//Position of camera
float x = 0;
float y = 0;
float z = 0;
float walkBob = 0;
float lookAround = 0;
float walkSpeed = 0.2;
//For rotation/movement
float angle = 0;
float rotate = 0;
float triRot = 0; //Triangle


GLuint texture[5]; //Hold five textures (unsigned int)
//Five image records for storing bitmap data
AUX_RGBImageRec *tex[5];

//Quadrics
GLUquadricObj *cylinder = gluNewQuadric();

//All input handled here
void Input() {
if(GetAsyncKeyState(VK_UP)) {
x += sin(angle * (3.14 / 180)) * walkSpeed;
z += cos(angle * (3.14 / 180)) * walkSpeed;
walkBob += 10;
//For bounce/Add later
y += sin(360 - walkBob * (3.14 /180)) / 50;
}
if(GetAsyncKeyState(VK_DOWN)) {
x -= sin(angle * (3.14 / 180)) * walkSpeed;
z -= cos(angle * (3.14 / 180)) * walkSpeed;
walkBob -= 10;
//For bounce/add later
y -= sin(360 - walkBob * (3.14 /180)) / 50;
}
//Increase angle of rotation
if(GetAsyncKeyState(VK_LEFT)) { angle += 0.5;}
//Decrease angle of rotation
if(GetAsyncKeyState(VK_RIGHT)) { angle -= 0.5;}
if(GetAsyncKeyState(VK_HOME)) { lookAround += 1;}
if(GetAsyncKeyState(VK_END)) { lookAround -= 1;}
}

//Hit escape to cancel program
void KeyboardHandler(unsigned char key, int x, int y) {
switch(key) {
case GLUT_KEY_F1:
break;
case GLUT_KEY_F2:

break;
case 27:
exit(0);
break;
default:
break;
}
glutPostRedisplay();
}


void createTexture(int index, char *path) {
tex[index] = auxDIBImageLoad(path);
glGenTextures(1, &texture[index]);
glBindTexture(GL_TEXTURE_2D, texture[index]);

//Specify the quality of the texture at different distances
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//Create the texture (WHERE TO GET IT)
glTexImage2D(GL_TEXTURE_2D,
0, /*Image level (left 0)*/
3, /*Number of data components (R,G,B)*/
tex[index]->sizeX, /*Width of bitmap*/
tex[index]->sizeY, /*Height of bitmap*/
0, /*Border (left at 0)*/
GL_RGB, /*Color mode*/
GL_UNSIGNED_BYTE, /*What data is made of*/
tex[index]->data); /*where the data is*/
//Free memory
if(tex[index]) {
if(tex[index]->data) {
free(tex[index]->data);
}
free(tex[index]);
}
}

void loadTextures() {
createTexture(1, "C:/pix/sand.bmp");
createTexture(3, "C:/pix/grass1.bmp");
}

//Draws the world in which to move
void draw(void) {
//Clear window with current color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Input(); //Check for user input

/************************************************** ******************/
//Move the scene
glLoadIdentity(); //Reset the scene
rotate = 360 - angle; //Determine wrap around angle
glRotatef(360 - lookAround, 1,0,0); //Rotate up and down
glRotatef(rotate,0,1,0); //Rotate side to side
glTranslatef(x,y,z); //Move to correct position
//End Move
/************************************************** ******************/
//Enable texture mapping
glEnable(GL_TEXTURE_2D);
glColor3f(1,1,1);

//FLOOR
glBindTexture(GL_TEXTURE_2D, texture[1]);
glBegin(GL_QUADS);
glTexCoord2f(20,0); glVertex3f(-150, -2, 150);
glTexCoord2f(0,0); glVertex3f(150, -2, 150);
glTexCoord2f(20,20); glVertex3f(150, -2, -150);
glTexCoord2f(0,20); glVertex3f(-150, -2, -150);
glEnd();
//End Floor

//BUILDING
glBindTexture(GL_TEXTURE_2D, texture[3]);
//Post1
glPushMatrix();
glTranslatef(15,-2,0);
glRotatef(-90,1,0,0);
gluQuadricTexture(cylinder, GL_TRUE);
gluCylinder(cylinder, 0.5,0.5,7,5,1);
glPopMatrix();
//End Post1

//Post2
glPushMatrix();
glTranslatef(7.5,-2,0);
glRotatef(-90,1,0,0);
gluQuadricTexture(cylinder, GL_TRUE);
gluCylinder(cylinder, 0.5,0.5,7,5,1);
glPopMatrix();
//End Post2

//Post3
glPushMatrix();
glTranslatef(15,-2,10);
glRotatef(-90,1,0,0);
gluQuadricTexture(cylinder, GL_TRUE);
gluCylinder(cylinder, 0.5,0.5,7,5,1);
glPopMatrix();
//End Post3

//Post4
glPushMatrix();
glTranslatef(7.5,-2,10);
glRotatef(-90,1,0,0);
gluQuadricTexture(cylinder, GL_TRUE);
gluCylinder(cylinder, 0.5,0.5,7,5,1);
glPopMatrix();
//End Post4

//Roof bottom
glBegin(GL_QUADS);
//Lower left
glTexCoord2f(0.0,0.0); glVertex3f(6,5,-1);
//Lower right
glTexCoord2f(2.0,0.0); glVertex3f(16,5,-1);
//Upper right
glTexCoord2f(2.0,2.0); glVertex3f(16,5,11);
//Upper left
glTexCoord2f(0.0,2.0); glVertex3f(6,5,11);
glEnd();
//Roof top
glBegin(GL_QUADS);
//Lower left
glTexCoord2f(0.0,0.0); glVertex3f(6,5.5,-1);
//Lower right
glTexCoord2f(2.0,0.0); glVertex3f(16,5.5,-1);
//Upper right
glTexCoord2f(2.0,2.0); glVertex3f(16,5.5,11);
//Upper left
glTexCoord2f(0.0,2.0); glVertex3f(6,5.5,11);
glEnd();

//Left wall roof
glBegin(GL_QUADS);
//Lower left
glTexCoord2f(0.0,0.0); glVertex3f(6,5,-1);
//Lower right
glTexCoord2f(2.0,0.0); glVertex3f(6,5.5,-1);
//Upper right
glTexCoord2f(2.0,2.0); glVertex3f(6, 5.5,11);
//Upper left
glTexCoord2f(0.0,2.0); glVertex3f(6, 5, 11);
glEnd();

//right wall roof
glBegin(GL_QUADS);
//Lower left
glTexCoord2f(0.0,0.0); glVertex3f(16,5,-1);
//Lower right
glTexCoord2f(2.0,0.0); glVertex3f(16,5.5,-1);
//Upper right
glTexCoord2f(2.0,2.0); glVertex3f(16, 5.5,11);
//Upper left
glTexCoord2f(0.0,2.0); glVertex3f(16, 5, 11);
glEnd();

//back wall roof
glBegin(GL_QUADS);
//Lower left
glTexCoord2f(0.0,0.0); glVertex3f(6,5,-1);
//Lower right
glTexCoord2f(2.0,0.0); glVertex3f(16,5,-1);
//Upper right
glTexCoord2f(2.0,2.0); glVertex3f(16, 5.5,-1);
//Upper left
glTexCoord2f(0.0,2.0); glVertex3f(6, 5.5, -1);
glEnd();

//front wall roof
glBegin(GL_QUADS);
//Lower left
glTexCoord2f(0.0,0.0); glVertex3f(6,5, 11);
//Lower right
glTexCoord2f(2.0,0.0); glVertex3f(16,5, 11);
//Upper right
glTexCoord2f(2.0,2.0); glVertex3f(16, 5.5, 11);
//Upper left
glTexCoord2f(0.0,2.0); glVertex3f(6, 5.5, 11);
glEnd();
//Disable when not used
glDisable(GL_TEXTURE_2D);
//Empty buffer (double buffering)
glutSwapBuffers();
//Draw again
glutPostRedisplay();
}

//Change the window size
void ChangeSize(GLsizei w, GLsizei h) {
//Prevent divide by zero
if (h==0) { h = 1;}

//Affects camera
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

//Set viewport
glViewport(0,0,w,h);

//Feild of view, ratio, start distance, end distance
gluPerspective(45, w / h, 1, 1000);
//Affects models
glMatrixMode(GL_MODELVIEW);
}

//All setup code
void Setup(void) {
loadTextures();
//Great perspective calculations
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//Enable Z-Buffering
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);

//Clear the screen to black
glClearColor(0,0,0,1);
}


//Set up the window/Initialize/Define outines
void main(void) {
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(1024,768);
glutCreateWindow("AceMan");
glutDisplayFunc(draw);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(KeyboardHandler);
Setup();
glutMainLoop();
}

BK
11-05-2000, 03:10 AM
How big is your desktop resolution? Its possible that when you maximise the window there is not enough video memory to support it so the driver is forced to use software mode.

For full screen you would normally change the resolution to something resonable.

Ace_Man
11-05-2000, 10:20 AM
That was it..Thanks!

Succinct
11-05-2000, 10:20 AM
What's "resonable" on current hardware?
I was hoping it was around 1024x768x32bpp, but i also know it doesn't get much bigger.

BK
11-05-2000, 04:39 PM
Reasonable in that sentence means anything the works properly http://www.opengl.org/discussion_boards/ubb/smile.gif