如何一次性删除所有制作的纹理?
如何删除我制作的所有纹理?假设我加载一些纹理:
GLuint tx_wall,tx_floor,tx_tiles;
tx_wall=LoadTexture("tex_wall.raw",512,512),
tx_floor=LoadTexture("tex_floor.raw",512,512),
tx_tiles=LoadTexture("tex_tiles.raw",512,512);
然后使用它们:
glBindTexture(GL_TEXTURE_2D,tx_wall);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(0, 50, 0);
glTexCoord2f(1, 1); glVertex3f(0, 0, 14);
glTexCoord2f(0, 1); glVertex3f(0, 50, 14);
glEnd();
glBindTexture(GL_TEXTURE_2D,tx_floor);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(50, 50, 0);
glTexCoord2f(1, 1); glVertex3f(50, 50, 0);
glTexCoord2f(0, 1); glVertex3f(0, 0, 0);
glEnd();
(and so on)
当游戏结束时,删除它们:
glDeleteTextures(1,&tx_wall);
glDeleteTextures(1,&tx_floor);
glDeleteTextures(1,&tx_tiles);
一切正常,但如果我有 10 或 20 个纹理,我将如何终止它们而不获取它们的名字?
How can I delete all the textures I've made? Suppose I load a few textures:
GLuint tx_wall,tx_floor,tx_tiles;
tx_wall=LoadTexture("tex_wall.raw",512,512),
tx_floor=LoadTexture("tex_floor.raw",512,512),
tx_tiles=LoadTexture("tex_tiles.raw",512,512);
then use them:
glBindTexture(GL_TEXTURE_2D,tx_wall);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(0, 50, 0);
glTexCoord2f(1, 1); glVertex3f(0, 0, 14);
glTexCoord2f(0, 1); glVertex3f(0, 50, 14);
glEnd();
glBindTexture(GL_TEXTURE_2D,tx_floor);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(1, 0); glVertex3f(50, 50, 0);
glTexCoord2f(1, 1); glVertex3f(50, 50, 0);
glTexCoord2f(0, 1); glVertex3f(0, 0, 0);
glEnd();
(and so on)
and when the game ends, delete them:
glDeleteTextures(1,&tx_wall);
glDeleteTextures(1,&tx_floor);
glDeleteTextures(1,&tx_tiles);
All works fine but if i have 10 or 20 textures than how will i terminate them all without taking their names?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果将所有纹理标识符放入一个数组中,则可以使用
在一次调用中将它们全部删除glDeleteTextures
(就像您可以使用glGenTextures
)。If you put all texture identifiers in an array, you can delete them all in one call using
glDeleteTextures
(just like you can generate them all in one call usingglGenTextures
).也许不完全符合您的意图,但 RAII 将是一个明智的选择:
用法:
并且:
好处是您可以仅在需要时保留纹理,代码是异常安全的,并且纹理释放是自动的。缺点是,如果没有某种引用计数,您就无法复制纹理对象(为此,我建议使用 boost::shared_ptr 而不是滚动自己的对象,因为多线程环境中的引用计数很棘手)。后者就是私有复制构造函数和赋值运算符的原因。
Perhaps not exactly what you were intending, but RAII would be a sensible option:
Usage:
and:
The benefit is that you can retain textures only for as long as you need them, the code is exception safe and texture release is automatic. The downside is that you can't copy Texture objects around without some kind of reference counting (for which I would recommend using boost::shared_ptr rather than rolling your own, because reference counting in multi-threaded environments is tricky). The latter is the reason for the private copy constructor and assignment operator.