Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

OpenGL : error with invalid format but NULL data

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading