【发布时间】:2018-10-29 19:34:39
【问题描述】:
我的纹理有一个小问题。
我想要那个:
- m_texture 的纹理 ID 为 #5
- 应该不删除 ID 为 #5 的纹理。
问题:此时纹理 #5 被删除。
纹理在析构函数中自行删除。
inline void SetTexture(Texture texture) //texture -> #5, m_texture -> #7
{
m_texture = texture;
//m_texture -> #5
//texture -> #5
// But #5 is deleted, and I don't want that
}
[...]
private:
Texture m_texture;
编辑:
这行得通:
Texture texture(0, 102, 153);
rect.SetTexture(texture);
但这不是:
rect.SetTexture(Texture(0, 102, 153));
编辑 2: 我认为这个问题将被关闭,因为它有很多代码。 [对不起]
纹理标题:
class Texture
{
public:
Texture(const std::string& fileName);
Texture(int red, int green, int blue);
void Bind();
virtual ~Texture();
protected:
private:
void Init(unsigned char* imageData, int width, int height);
GLuint m_texture;
};
纹理类:
Texture::Texture(const std::string& fileName)
{
int width, height, numComponents;
unsigned char* imageData = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);
if (imageData == NULL)
std::cerr << "Texture loading failed for texture: " << fileName << std::endl;
Init(imageData, width, height);
stbi_image_free(imageData);
}
Texture::Texture(int red, int green, int blue)
{
unsigned char imageData[] = {
static_cast<char>(red),
static_cast<char>(green),
static_cast<char>(blue)
};
Init(imageData, 1, 1);
}
void Texture::Init(unsigned char* imageData, int width, int height) {
glGenTextures(1, &m_texture);
glBindTexture(GL_TEXTURE_2D, m_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
}
Texture::~Texture()
{
glDeleteTextures(1, &m_texture);
}
void Texture::Bind()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_texture);
}
【问题讨论】:
-
作为一个关于你的问题的可能提示:你关注the rules of three, five or zero吗?
-
@FreddyC。请自行研究。使用搜索引擎!
-
@FreddyC。我对
shared_ptr什么都没说,我说的是移动语义。 “它不起作用”只是意味着你做错了。你需要一个移动构造函数。至于投票:它们并不是为了帮助您,而是表明您的问题对其他人有多大用处。 -
好吧,不知道你是如何定义
Texture的,我们能提供什么帮助?如此写。你的问题没用,只值得投反对票……
标签: c++ destructor