I wonder how OpenGL get to know that id variable of a OGL object doesn’t exist anymore… Because I noticed it does it. I have Texture class which is just a wrapper for loading an image with SOIL and id handler. Its object is member of another class which draws it etc, doesn’t really matter. But the matter is: when I push drawable object (which uses Texture) to std::vector by push_back, OpenGL doesn’t draw texture (it’s just black). But why? ID is copied with Texture object and glDeleteTextures wasn’t called, so I think I don’t understand how it works. The variable which is given to glGen… functions isn’t just a ID number, is it something more (I’m a little confused, I don’t understand why copying breaks it) ? If I push it to vector by emplace_back (no copying), it is drawn. Below is a code of Texture class and drawing code:
class Texture
{
private:
typedef unsigned char uchar;
typedef glm::tvec2<GLint> vec2i;
public:
Texture();
explicit Texture(std::string _filePath);
public:
static std::tuple<uchar*, glm::vec2> loadImage(std::string _filePath);
GLuint getId() { return m_texId; }
vec2i getSize() { return m_size; }
private:
GLuint m_texId;
vec2i m_size;
};
Texture::Texture(std::string _filePath)
{
glGenTextures(1, &m_texId);
glBindTexture(GL_TEXTURE_2D, m_texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
uchar* imageData;
std::tie(imageData, m_size) = loadImage(_filePath);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_size.x, m_size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(imageData);
glBindTexture(GL_TEXTURE_2D, 0);
}
std::tuple<unsigned char*, glm::vec2> Texture::loadImage(std::string _filePath)
{
vec2i size;
unsigned char* image = SOIL_load_image(_filePath.c_str(), &size.x, &size.y, 0, SOIL_LOAD_RGB);
return std::make_tuple(image, size);
}
/* Drawing code - Sprite class */
void Sprite::draw()
{
m_shader.use(); //opengl shader wrapper
glActiveTexture(GL_TEXTURE0);
//Sprite has a member pointer to Texture object
glBindTexture(GL_TEXTURE_2D, m_ptexture->getId());
glUniform1i(glGetUniformLocation(m_shader.getId(), "texSampler"), 0);
//here some glUniforms...
glBindVertexArray(m_vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glBindVertexArray(0);
glUseProgram(0);
}