-2

我制作了一个用 OpenGL 编写的程序,当我运行它时,程序会填满所有的 ram,然后在大约 3300/4000 Mb 的 ram 处关闭。这是我的程序:

#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include <gl/GLU.h>
#include <gl/glaux.h>


using namespace std;


GLuint texture;

AUX_RGBImageRec* LoadImage (char* file) {

return auxDIBImageLoad (file);
}

int LoadTexture (char* file) {
AUX_RGBImageRec* Textureimage;

Textureimage = LoadImage (file);
glGenTextures (1, &texture);
glBindTexture (GL_TEXTURE_2D, texture);
glTexImage2D (GL_TEXTURE_2D,
0,
GL_RGB,
Textureimage->sizeX,
Textureimage->sizeY,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
Textureimage->data
);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
delete Textureimage;
return 0;
}


void exitkey (unsigned char key, int x, int y) {
switch (key) {
case 27:
exit (0);
}
}


void Render3d () {
glEnable (GL_DEPTH_TEST);

glEnable (GL_NORMALIZE);

glEnable (GL_LIGHTING);

glEnable (GL_LIGHT0);

glEnable (GL_LIGHT1);

glEnable (GL_TEXTURE_2D);

}


void incaseofresize (int w, int h) {
glViewport (0, 0, w, h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (90.0, (double)w / (double)h, 0.7, 300.0);
}


double theangle = 30.0;

void draw () {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -6.0);
glPushMatrix ();

glRotatef (theangle, 0.0, 1.0, 0.0); 

GLfloat ambientlightcolor [] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light0color [] = { 0.5, 0.5, 0.0, 1.0 };
GLfloat light1color [] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat light0position [] = { 2.0, 0.0, 3.0, 1.0 };
GLfloat light1position [] = { -2.0, 0.0, 3.0, 1.0 };

glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambientlightcolor);
glLightfv (GL_LIGHT0, GL_DIFFUSE, light0color);
glLightfv (GL_LIGHT0, GL_POSITION, light0position);
glLightfv (GL_LIGHT1, GL_DIFFUSE, light1color);
glLightfv (GL_LIGHT1, GL_POSITION, light1position);


LoadTexture ("me.bmp");


glBegin (GL_QUADS);
//Drawing shape
glColor3f (1.0, 1.0, 1.0);
//Left side
glNormal3f (0.0, 0.0, 1.0);
glTexCoord2f (0.0, 0.0);
glVertex3f (-1.5, -1.0, -1.5);
glTexCoord2f (1.0, 0.0);
glVertex3f (0.0, -1.0, 1.5);
glTexCoord2f (1.0, 1.0);
glVertex3f (0.0, 1.0, 1.5);
glTexCoord2f (0.0, 1.0);
glVertex3f (-1.5, 1.0, -1.5);

glEnd ();


LoadTexture ("mom.bmp");


glBegin(GL_QUADS);

//Right side
glNormal3f (0.0, 1.0, 1.0);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, -1.0, 1.5);
glTexCoord2f (1.0, 0.0);
glVertex3f (1.5, -1.0, -1.5);
glTexCoord2f (1.0, 1.0);
glVertex3f (1.5, 1.0, -1.5);
glTexCoord2f (0.0, 1.0);
glVertex3f (0.0, 1.0, 1.5);

glEnd ();

glBegin (GL_QUADS);

//Back right side
glNormal3f (0.0, 1.0, 0.0);
glVertex3f (1.5, -1.0, -1.5);
glVertex3f (0.0, -1.0, -4.5);
glVertex3f (0.0, 1.0, -4.5);
glVertex3f (1.5, 1.0, -1.5);
glEnd ();

glBegin (GL_QUADS);

glNormal3f (0.0, 1.0, 0.0);
glVertex3f (0.0, -1.0, -4.5);
glVertex3f (-1.5, -1.0, -1.5);
glVertex3f (-1.5, 1.0, -1.5);
glVertex3f (0.0, 1.0, -4.5);

glEnd ();

glutSwapBuffers ();
}

void rotate (int value) {
theangle += 1.5;
if (theangle > 360) {
theangle -=360;
}
glutPostRedisplay ();
glutTimerFunc (50, rotate, 0);
}

int main (int argcpp, char** argv) {
glutInit (&argcpp, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (720, 480);

glutCreateWindow ("OpenGL");
Render3d ();

glutDisplayFunc (draw);
glutKeyboardFunc (exitkey);
glutReshapeFunc (incaseofresize);
glutTimerFunc (50, rotate, 0);

glutMainLoop ();

return 0;

}

我认为它的 LoadTexture 函数会在每个循环中一遍又一遍地重新加载图像,我找不到在每个循环后删除图像数据的方法。

4

1 回答 1

2

每次您的程序迭代显示函数时,它都会将图像加载到新的纹理对象中,而不是释放先前创建的对象。

规范的方法是只加载一次纹理。OpenGL 在所谓的纹理对象中组织纹理,由所谓的名称 ID 标识。LoadTexture 确实返回此 ID。您通过调用使用这样的纹理glBindTexture(GL_TEXTURE_2D, theTextureID);

于 2012-06-01T00:58:43.107 回答