Sometimes we need to initialize a texture without data. So I want to write an utility function, something like that:
GLuint create_texture(GLenum internal_format, int width, int height)
{
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGB, GL_FLOAT, NULL);
return id;
}
I want to be able to initialize with any format I want (GL_RGB
, GL_RGBA
, etc...).
However, this will crash if the format
argument doesn't match the internal_format
argument even if there is no actual data. Is it possible to avoid this error without using an external table that will match the format
for the given internal_format
(which is possible by the help of the this page, at Errors paragraph)? However, this is a bit tedious, so I was wondering if there is an alternative.
We can't use format=internal_format
because some values are not compatible, like GL_RGBA16F
.
CodePudding user response:
If you have access to OpenGL 4.2, the glTexStorage
method does exactly what you need. It allocates the storage for a texture but does not initialize it with any data.