【发布时间】:2011-01-02 05:34:34
【问题描述】:
我想加载纹理,然后让它们被多个对象使用。这行得通吗?
class Sprite
{
GLuint* mTextures; // do I need this to also be a reference?
Sprite( GLuint* textures ) // do I need this to also be a reference?
{
mTextures = textures;
}
void Draw( textureNumber )
{
glBindTexture( GL_TEXTURE_2D, mTextures[ textureNumber ] );
// drawing code
}
};
// normally these variables would be inputed, but I did this for simplicity.
const int NUMBER_OF_TEXTURES = 40;
const int WHICH_TEXTURE = 10;
void main()
{
std::vector<GLuint> the_textures;
the_textures.resize( NUMBER_OF_TEXTURES );
glGenTextures( NUMBER_OF_TEXTURES, &the_textures[0] );
// texture loading code
Sprite the_sprite( &the_textures[0] );
the_sprite.Draw( WHICH_TEXTURE );
}
我有什么不同的方法可以做到这一点,即使它会起作用?
谢谢。
【问题讨论】:
-
只是一个注释。如果您将某些东西定义(而不是声明)作为参考,这通常意味着您的设计有问题。
-
@Falmarri,你是什么意思?您是否建议将类字段作为引用或将局部变量作为引用是错误的?
-
@Kos:我说的是定义一个类字段作为参考。
-
听起来像是实现任何单个 UML 关联端点的自然方式,已知该端点在对象的整个生命周期内都是不变的。也可以使用 const 指针。
标签: c++ pointers opengl reference pass-by-reference