Direct State AccessGL 4.5
glCreateTextures
Creates textures of a given type immediately — no bind needed.
Part of the family glCreate*
Signature
void glCreateTextures(GLenum target, GLsizei n, GLuint *textures);
How it works
Because the object is created on the spot, its type (target) is given here instead of at the first bind. The texture can then be allocated with glTextureStorage2D and filled with glTextureSubImage2D, all by ID.
Parameters
targetGLenum
Texture type: GL_TEXTURE_2D, GL_TEXTURE_CUBE_MAP…
nGLsizei
How many textures.
texturesGLuint *
Array receiving the IDs.
Tips & pitfalls
Direct State Access (GL 4.5): the object is passed as a parameter, so nothing needs to be bound and no global state is disturbed.
To use it for drawing, bind to a unit with
glBindTextureUnit(unit, tex) — noglActiveTextureneeded.
Example
example.cpp
1GLuint tex;
2glCreateTextures(GL_TEXTURE_2D, 1, &tex);
3glTextureStorage2D(tex, levels, GL_SRGB8_ALPHA8, w, h);
4glTextureSubImage2D(tex, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
5glGenerateTextureMipmap(tex);