there's a problem with my texture mapping, when I execute the code, it run smoothly for 5-8 sec, then the framerate drop drastically, when I monitor the task manager, it seem the program consuming almost 95% of my physical memory, anybody know how to solve this problem?? I'm using visual studio c++ 2010
here's the code
GLuint LoadtankTexture(const char * pic, int width, int height){
GLuint tankTexture;
BYTE * data;
FILE * picfile;
picfile = fopen(pic, "rb");
data = (BYTE *)malloc(width * height * 3);
fread(data, width * height, 3, picfile);
fclose(picfile);
glGenTextures(1, &tankTexture);
glBindTexture(GL_TEXTURE_2D, tankTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
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_MIN_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
free(data);
return tankTexture;}
here's another code
void Tank::drawSelf(){
glPushMatrix();
glTranslatef(this->posX, 0.0f, this->posZ);
glRotatef(this->rotation, 0.0f, 1.0f, 0.0f);
static float w, h, d, d2;
w = this->width; //0.5
h = this->height; //0.5
d = this->depth; //0.7
d2 = this->depth / 1.4f; //0.5
glColor3f(0.0f,1.0f, 0.3f);
glEnable(GL_TEXTURE_2D);
GLuint tankTex;
tankTex = LoadtankTexture("tankbmp.bmp", 500, 500);
glBindTexture(GL_TEXTURE_2D, tankTex);
glBegin(GL_QUADS);
//Front
glNormal3f(0.0f, h, -d);
glTexCoord3f(-w, h, -d); glVertex3f(-w, h, -d);
glTexCoord3f(w, h, -d); glVertex3f(w, h, -d);
glTexCoord3f(w, 0.0, -d2); glVertex3f(w, 0.0, -d2);
glTexCoord3f(-w, 0.0, -d2); glVertex3f(-w, 0.0, -d2);
//Back
glNormal3f(0.0f, -0.5f, 0.7f);
glTexCoord3f(-w, h, d2); glVertex3f(-w, h, d2);
glTexCoord3f(w, h, d2); glVertex3f(w, h, d2);
glTexCoord3f(w, 0.0, d); glVertex3f(w, 0.0, d);
glTexCoord3f(-w, 0.0, d); glVertex3f(-w, 0.0, d);
//Left
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord3f(-w, h, -d); glVertex3f(-w, h, -d);
glTexCoord3f(-w, 0.0, -d2); glVertex3f(-w, 0.0, -d2);
glTexCoord3f(-w, 0.0, d); glVertex3f(-w, 0.0, d);
glTexCoord3f(-w, h, d2); glVertex3f(-w, h, d2);
//Right
glNormal3f(1.0f, 0.0f, 0.0f);
glTexCoord3f(w, h, -d); glVertex3f(w, h, -d);
glTexCoord3f(w, 0.0, -d2); glVertex3f(w, 0.0, -d2);
glTexCoord3f(w, 0.0, d); glVertex3f(w, 0.0, d);
glTexCoord3f(w, h, d2); glVertex3f(w, h, d2);
//Bottom
glNormal3f(0.0f, -1.0f, 0.0f);
glTexCoord3f(-w, 0.0, -d2); glVertex3f(-w, 0.0, -d2);
glTexCoord3f(-w, 0.0, d); glVertex3f(-w, 0.0, d);
glTexCoord3f(w, 0.0, d); glVertex3f(w, 0.0, d);
glTexCoord3f(w, 0.0, -d2); glVertex3f(w, 0.0, -d2);
//Top
glNormal3f(0.0f, 1.0f, 0.0f);
glTexCoord3f(-w, h, -d); glVertex3f(-w, h, -d);
glTexCoord3f(-w, h, d2); glVertex3f(-w, h, d2);
glTexCoord3f(w, h, d2); glVertex3f(w, h, d2);
glTexCoord3f(w, h, -d); glVertex3f(w, h, -d);
glEnd();
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslatef(0.0f, h*1.3f, 0.0f);
glRotatef(this->turretRotation, 0.0f, 1.0f, 0.0f);
glTranslatef(0.0f, 0.0f, this->recoilDistance*0.5);
makeRectangularPrism(w*(3.0f/5.0f), 0.0f, -d/2, -w*(3.0f/5.0f), h/2, d/2);
glTranslatef(0.0f, 0.025f, -0.6f);
glTranslatef(0.0f, 0.0f, -0.2f + this->recoilDistance*0.8f);
makeRectangularPrism(0.05f, -0.05f, -1.0f, -0.05f, 0.05f, 0.0f);
glPopMatrix();
if(this->hasShieldLeft()){
glColor4f(0.1f, 0.1f, 1.0f, this->shieldOpacity);
glutSolidSphere(this->width*3, 20, 20);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
glPopMatrix(); }
You are calling LoadtankTexture() every time drawSelf() is called.
LoadtankTexture() creates a new texture when it is called and fills it with the texture data.
Calling LoadtankTexture() regularly is creating many copies of the texture in memory.
You only need to create a texture once (unless you actively delete it or you have suffered context loss) and should therefore move the call to LoadtankTexture from drawSelf to your initialization code and keep the returned int somewhere (e.g. make tankTex a member variable).
You should continue to call glEnable(GL_TEXTURE_2D) and glBindTexture(GL_TEXTURE_2D, tankTex) from drawSelf as you are doing at the moment.
If this does not fix the problem then it is likely you have another memory leek elsewhere in your code.
The slowdown after a few seconds is almost certainly caused by the computer running out of physical memory forcing the OS to start swapping.