我有一个GLTexture类,它用以下方法从文件中初始化纹理:
void GLTexture::LoadBMP(char *name)
{
// Create a place to store the texture
AUX_RGBImageRec *TextureImage[1];
// Set the pointer to NULL
memset(TextureImage,0,sizeof(void *)*1);
// Load the bitmap and assign our pointer to it
TextureImage[0] = auxDIBImageLoad(name);
// Just in case we want to use the width and height later
width = TextureImage[0]->sizeX;
height = TextureImage[0]->sizeY;
// Generate the OpenGL texture id
glGenTextures(1, &texture[0]);
// Bind this texture to its id
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Use mipmapping filter
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// Generate the mipmaps
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
// Cleanup
if (TextureImage[0])
{
if (TextureImage[0]->data)
free(TextureImage[0]->data);
free(TextureImage[0]);
}
}它似乎确实处理了数据。然后通过调用此函数在程序中使用纹理
void GLTexture::Use()
{
glEnable(GL_TEXTURE_2D); // Enable texture mapping
glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind the texture as the current one
}由于纹理必须以某种方式驻留在内存中才能被绑定,我想知道在退出时是否会有额外的清理任务?或者加载方法中的那个是否足够...
发布于 2011-12-02 07:59:32
是啊,你应该用glDeleteTextures。您可能可以在退出时不使用它,因为您的整个上下文将被销毁,但不管怎样,最好还是这样做。
发布于 2011-12-02 08:00:28
你应该使用
glDeleteTextures
发布于 2011-12-02 08:03:35
您在示例代码中看到的free是释放从文件加载的数据所需的调用。
调用gluBuild2DMipmaps时,OpenGL 会从传递给它的源复制数据,但纹理的OpenGL副本(实际纹理)仍驻留在内存中(这就是为什么可以在free调用之后使用它)。
使用完纹理后,应调用glDeleteTextures以释放由OpenGL分配的内存。
https://stackoverflow.com/questions/8350077
复制相似问题