如何一次性删除所有制作的纹理?

发布于 2024-12-01 08:17:07 字数 1009 浏览 0 评论 0原文

如何删除我制作的所有纹理?假设我加载一些纹理:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-08 08:17:07

如果将所有纹理标识符放入一个数组中,则可以使用 在一次调用中将它们全部删除glDeleteTextures (就像您可以使用 glGenTextures)。

GLuint textures[3];
glGenTextures(3, textures);

/* ... */

glDeleteTextures(3, textures);

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 using glGenTextures).

GLuint textures[3];
glGenTextures(3, textures);

/* ... */

glDeleteTextures(3, textures);
ぃ弥猫深巷。 2024-12-08 08:17:07

也许不完全符合您的意图,但 RAII 将是一个明智的选择:

class Texture
{
public:
    Texture( const std::string& name, int width, int height ) :
        m_id( LoadTexture(name.c_str(),width,height) )
    {
    }

    ~Texture()
    {
        if(m_id)
            glDeleteTextures(1,&m_id);
        m_id = 0;
    }

    GLuint id() const
    {
        return m_id;
    }

private:
    Texture( const Texture& );
    Texture& operator=( const Texture& );

    GLuint m_id;
};

用法:

Texture tx_wall("tex_wall.raw",512,512);
Texture tx_floor("tex_floor.raw",512,512);
Texture tx_tiles("tex_tiles.raw",512,512);

并且:

glBindTexture(GL_TEXTURE_2D,tx_wall.id());
  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.id());
  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();

好处是您可以仅在需要时保留纹理,代码是异常安全的,并且纹理释放是自动的。缺点是,如果没有某种引用计数,您就无法复制纹理对象(为此,我建议使用 boost::shared_ptr 而不是滚动自己的对象,因为多线程环境中的引用计数很棘手)。后者就是私有复制构造函数和赋值运算符的原因。

Perhaps not exactly what you were intending, but RAII would be a sensible option:

class Texture
{
public:
    Texture( const std::string& name, int width, int height ) :
        m_id( LoadTexture(name.c_str(),width,height) )
    {
    }

    ~Texture()
    {
        if(m_id)
            glDeleteTextures(1,&m_id);
        m_id = 0;
    }

    GLuint id() const
    {
        return m_id;
    }

private:
    Texture( const Texture& );
    Texture& operator=( const Texture& );

    GLuint m_id;
};

Usage:

Texture tx_wall("tex_wall.raw",512,512);
Texture tx_floor("tex_floor.raw",512,512);
Texture tx_tiles("tex_tiles.raw",512,512);

and:

glBindTexture(GL_TEXTURE_2D,tx_wall.id());
  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.id());
  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();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文