[英]Do GL_TEXTUREs need to be released/freed?
我有一个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
}
由于纹理必须以某种方式驻留在内存中才能绑定,所以我想知道退出时是否还要执行任何额外的清理任务? 或者加载方法中的一个是否足够...
是的,您应该使用glDeleteTextures
。 您可能可以在退出时不带它而逃脱,因为整个上下文将被破坏,但是无论如何,最好的做法是这样做。
你应该用
您在示例代码中看到的free
是调用,以释放从文件中加载的数据。
调用gluBuild2DMipmaps
,OpenGL从传递给它的源中复制数据,但是纹理(实际纹理)的OpenGL副本仍驻留在内存中(这就是为什么您可以在free
调用后使用它)。
使用完纹理后,应调用glDeleteTextures以释放OpenGL分配的内存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.