C++ C++;:参考和指针问题(关于OpenGL的示例)
我想加载纹理,然后让它们被多个对象使用。这样行吗C++ C++;:参考和指针问题(关于OpenGL的示例),c++,pointers,opengl,reference,pass-by-reference,C++,Pointers,Opengl,Reference,Pass By Reference,我想加载纹理,然后让它们被多个对象使用。这样行吗 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
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 );
}
类精灵
{
GLuint*mTextures;//我需要它也作为参考吗?
Sprite(GLuint*纹理)//我需要它也作为参考吗?
{
纹理=纹理;
}
无效绘制(纹理枚举器)
{
glBindTexture(GL_TEXTURE_2D,mTextures[TextureEnumber]);
//绘图代码
}
};
//通常这些变量会被输入,但我这样做是为了简单。
const int NUMBER_OF_纹理=40;
const int,其中_纹理=10;
void main()
{
std::向量_纹理;
调整大小(纹理的数量);
glGenTextures(纹理的数量和纹理[0]);
//纹理加载代码
精灵精灵精灵(&U纹理[0]);
绘制(哪种纹理);
}
有没有一种不同的方法可以让我做到这一点,即使它能起作用
谢谢。那种特殊情况应该行得通。但是,一旦“_纹理”超出范围,Sprite持有的指针就会失效。即使它是一个参考,那也是一样的。
在这种情况下,我建议您将std::vector放在Sprite类中,并由该类实例拥有和管理。这种特殊情况应该有效。但是,一旦“_纹理”超出范围,Sprite持有的指针就会失效。即使它是一个参考,那也是一样的。
在这种情况下,我建议您将std::vector放在Sprite类中,并由该类实例拥有和管理。我想知道为什么Draw不能只引用它正在绘制的对象
class Sprite
{
public:
Sprite()
{
}
void Draw( GLuint & texture )
{
glBindTexture( GL_TEXTURE_2D, texture );
// drawing code
}
};
- Sprite是一种以特定方式绘制图形的类型
- GLuint是一种被绘制的类型
顺便说一下,main应该返回int而不是void。我想知道为什么Draw不能只引用它正在绘制的对象
class Sprite
{
public:
Sprite()
{
}
void Draw( GLuint & texture )
{
glBindTexture( GL_TEXTURE_2D, texture );
// drawing code
}
};
- Sprite是一种以特定方式绘制图形的类型
- GLuint是一种被绘制的类型
顺便说一句,main应该返回int而不是void。既然纹理id不变,那么在
Sprite
中使用GLuint
值,而不是存储数组ptr并选择要绘制的Sprite如何
看起来更简单,不必担心范围问题
除非您需要在应用程序退出之前调用glDeleteTextures,否则我建议您创建一个TextureMgr类或一些可以一次性解决此问题的东西。;) 既然纹理id不变,那么在
Sprite
中使用GLuint
值,而不是存储数组ptr并选择要绘制的Sprite如何
看起来更简单,不必担心范围问题
除非您需要在应用程序退出之前调用glDeleteTextures,否则我建议您创建一个TextureMgr类或一些可以一次性解决此问题的东西。;)
static std::vector load_once_textures();
std::vector<GLuint> const& get_textures()
{
static std::vector<GLuint> const the_textures = load_once_textures();
return the_textures;
}
std::vector load_once_textures()
{
// loading
}
static std::vector load_once_textures();
std::vector const&get_纹理()
{
静态std::vector const the_textures=load_once_textures();
返回_纹理;
}
std::向量加载一次纹理()
{
//装载
}
纹理
std::vector<GLuint> const& get_textures();
std::vector const&get_textures();
这是一种简单且足够安全的方法,因为纹理将加载一次,并且加载不会出现静态初始化顺序模糊的问题
static std::vector load_once_textures();
std::vector<GLuint> const& get_textures()
{
static std::vector<GLuint> const the_textures = load_once_textures();
return the_textures;
}
std::vector load_once_textures()
{
// loading
}
static std::vector load_once_textures();
std::vector const&get_纹理()
{
静态std::vector const the_textures=load_once_textures();
返回_纹理;
}
std::向量加载一次纹理()
{
//装载
}
纹理
std::vector<GLuint> const& get_textures();
std::vector const&get_textures();
这是一种简单且足够安全的方法,因为纹理将加载一次,并且加载不会出现静态初始化顺序不明确的问题只是一个注释。如果您正在定义(vs声明)