Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Creating Textures in OpenGL 4.x

This topic is 1367 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Coming from OpenGL ES 2.0, this was the typical way to create a texture:

GLuint texID = 0; // texture handle
uint8_t pixels[4] = { 0xFF, 0x00, 0xFF, 0x00 }; // 2x2 checkerboard texture

glActiveTexture(GL_TEXTURE0); // assuming you haven't already set this
glGenTextures(1, &texID); // get a free texture handle (aka, "name")
glBindTexture(GL_TEXTURE_2D, texID); // generate free texture name, and bind it
glTexImage(GL_TEXTURE_2D, 0, GL_ALPHA, 2, 2, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels); // upload texture data to OpenGL's memory

It looks like OpenGL 4 still supports this way of doing things, but I've noticed that glTexStorage2D() is now used in the OpenGL book I've been reading. It's been stated in my book that this allows for generating mutable textures (format, dimensions, etc). It's a little simpler:

GLuint texID = 0;
uint8_t pixels[4] = { 0x00, 0xFF, 0x00, 0xFF };

glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, &texID);
glTexStorage2D(GL_TEXTURE_2D, 4, GL_R8, 2, 2); // allocate storage for an 8-bit, 2x2 texture with 4 mipmap levels
glTexSubImage(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RED, GL_UNSIGNED_BYTE, pixels); // allocate texture data for the entire area of the texture for the first mipmap level

From what I've gathered, this is done for textures that are considered immutable in terms of changing its target, format, dimensions, etc. The data can change, however. As for generating 4 mipmaps... would I have to call glTexSubImage(), and provide 1/4-sized images for each of my mipmaps? It makes sense. I've seen somewhere that I only have to provide the initial mipmap, and I can call glGenerateMipmaps(), or for compatibility, render to FBOs to make this happen. Is this correct?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!