Textures

I always try to add a smiley texture to the game to be on the safe side

Texture Name

Use glGenTextures to create texture objects and glDeleteTextures to delete them. Check if a object is a texture with glIsTexture.

void glGenTextures( GLsizei n, GLuint * textures);

GLboolean glIsTexture(GLuint texture);

void glDeleteTextures(GLsizei n, GLuint * textures);

Set the target

To set the type of the texture use glBindTexture. The target specify things such as the texture dimension and if it is an array texture.

void glBindTexture( GLenum target, GLuint texture);

GL_TEXTURE_*D: Set the texture to be a 1,2 or three dimensional texture.

GL_TEXTURE_*D_ARRAY: Set the texture to be a 1 or 2 dimensional array texture.

GL_TEXTURE_RECTANGLE

GL_TEXTURE_BUFFER: Access a buffer as a special form of 1D texture.

GL_TEXTURE_CUBE_MAP

GL_TEXTURE_CUBE_MAP_ARRAY

GL_TEXTURE_2D_MULTISAMPLE

GL_TEXTURE_2D_MULTISAMPLE_ARRAY

Allocating Memory

There are two ways to allocate memory for a texture, glTexStorage*D and glTexImage*D. There is one for each dimension and also specific ones for multisampling textures. glTexStorage creates an immutable storage where the size of the texture can not be changed later. With glTexImage one can use glTexImage later to change the size of the texture. With glTexImage*D one can optionally also upload the data for one mipmap level of the texture.

void glTexStorage1D(GLenum target​, GLsizei levels​, GLenum internalformat​, GLsizei width​);

void glTexStorage2D(GLenum target​, GLsizei levels​, GLenum internalformat​, GLsizei width​, GLsizei height​);

void glTexStorage3D(GLenum target​, GLsizei levels​, GLenum internalformat​, GLsizei width​, GLsizei height​, GLsizei depth​);

void glTexImage1D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLint border,GLenum format,GLenum type,const GLvoid * data);

void glTexImage2D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLint border,GLenum format,GLenum type,const GLvoid * data);

void glTexImage3D(GLenum target,GLint level,GLint internalFormat,GLsizei width,GLsizei height,GLsizei depth,GLint border,GLenum format,GLenum type,const GLvoid * data);

With GL_TEXTURE_BUFFER instead of creating storage use glTexBuffer to set the buffer to use.

void glTexBuffer(GLenum target​, GLenum internalFormat​, GLuint buffer​);

Write

glTexSubImage*D to upload the data for the texture.

void glTexSubImage1D(GLenum target​, GLint level​, GLint xoffset​, GLsizei width​, GLenum format​, GLenum type​, const GLvoid * data​);

void glTexSubImage2D(GLenum target​, GLint level​, GLint xoffset​, GLint yoffset​, GLsizei width​, GLsizei height​, GLenum format​, GLenum type​, const GLvoid * data​);

void glTexSubImage3D(GLenum target​, GLint level​, GLint xoffset​, GLint yoffset​, GLint zoffset​, GLsizei width​, GLsizei height​, GLsizei depth​, GLenum format​, GLenum type​, const GLvoid * data​);

Read

To read from the current read framebuffer to a bound texture use glCopyTexImage*D. glCopyTexSubImage*D can be used to only copy a part of it. glCopyTextureSubImage*D.

Fill

glTexSubImage* to upload the data for the texture.

void glTexSubImage1D(GLenum target​, GLint level​, GLint xoffset​, GLsizei width​, GLenum format​, GLenum type​, const GLvoid * data​);

void glTexSubImage2D(GLenum target​, GLint level​, GLint xoffset​, GLint yoffset​, GLsizei width​, GLsizei height​, GLenum format​, GLenum type​, const GLvoid * data​);

void glTexSubImage3D(GLenum target​, GLint level​, GLint xoffset​, GLint yoffset​, GLint zoffset​, GLsizei width​, GLsizei height​, GLsizei depth​, GLenum format​, GLenum type​, const GLvoid * data​);

Using Textures

To use a texture select the texture unit to use it on with glActivateTexture() and then bind the texture with glBindTexture(). To access a texture in a shader create a uniform variable of the same sampler type as the texture. Ex a sampler2D for a 2D texture. Set the texture unit of the sampler by getting it with glGetUniformLocation() and then glUniformli(). Another option is to assign the texture unit directly in the shader with binding qualifier.

layout (binding = 0) uniform sampler2D diffuse;

layout (binding = 1) uniform sampler2D spec;

To sample use texelFetch() or the texture() function in the shader.

void glActiveTexture​( GLenum texture​ );

Sampler Objects

Parameters like wrapping mode and filter mode for textures are stored in Sampler Objects. After creating a sampler object it is possible to change it's parameters with glSamplerParameter*(). The sampler can then be bound to a texture unit with glBindSampler and it will then modify any texture that is assgined to that texture unit.

void glBindSampler(GLuint unit​, GLuint sampler​);

void glSamplerParameter[if]​( GLuint sampler​​, GLenum pname​, T param​);

void glSamplerParameter[if]v​( GLuint sampler​​, GLenum pname​, T *params​ );

void glSamplerParameterI[i ui]v​( GLuint sampler​​, GLenum pname​, T *params​ );

Each texture also contains it's own default sampler object that is used when no other is assgined to it's texture unit. They default sampler can be modified with the glTexParameter*() functions.

void glTexParameter[if]​( GLenum target​, GLenum pname​, T param​);

void glTexParameter[if]v​( GLenum target​, GLenum pname​, T *params​ );

void glTexParameterI[i ui]v​( GLenum target​, GLenum pname​, T *params​ );

Reference

https://www.khronos.org/opengl/wiki/Array_Texture