GLS  1.0.0
GL Stuff - A library aimed at reducing the boilerplate OpenGL code you always have to write.
texture< Target > Class Template Reference

Class encapsulating an OpenGL texture object. More...

#include <texture.hpp>

Public Member Functions

 texture ()
 Default constructor. More...
 
GLuint name () const
 Retrieve the OpenGL name of this texture. More...
 
GLsizei width () const
 Retrieve the width of the level 0 image of the allocated texture storage. More...
 
GLsizei height () const
 Retrieve the height of the level 0 image of the allocated texture storage. More...
 
GLsizei depth () const
 Retrieve the depth of the level 0 image of the allocated texture storage. More...
 
void bind ()
 Bind this texture to its target. More...
 
void parameter (GLenum pname, GLfloat param)
 Set a texture parameter. More...
 
void parameter (GLenum pname, GLint param)
 Set a texture parameter. More...
 
void parameter (GLenum pname, const GLfloat *param)
 Set a texture parameter. More...
 
void parameter (GLenum pname, const GLint *param)
 Set a texture parameter. More...
 
template<typename T >
void image_1d (GLint level, GLint internal_format, T the_width, GLenum format, GLenum type, const GLvoid *data)
 Allocate storage for a 1D image and upload texture data. More...
 
template<typename T , typename U >
void image_2d (GLint level, GLint internal_format, T the_width, U the_height, GLenum format, GLenum type, const GLvoid *data)
 Allocate storage for a 2D image and upload texture data. More...
 
template<typename T , typename U , typename V >
void image_3d (GLint level, GLint internal_format, T the_width, U the_height, V the_depth, GLenum format, GLenum type, const GLvoid *data)
 Allocate storage for a 3D image and upload texture data. More...
 
template<typename T , typename U >
void sub_image_1d (GLint level, T xoffset, U the_width, GLenum format, GLenum type, const GLvoid *data)
 Update a section of a 1D image within the texture. More...
 
template<typename T , typename U , typename V , typename W >
void sub_image_2d (GLint level, T xoffset, U yoffset, V the_width, W the_height, GLenum format, GLenum type, const GLvoid *data)
 Update a section of a 2D image within the texture. More...
 
template<typename T , typename U , typename V , typename W , typename X , typename Y >
void sub_image_3d (GLint level, T xoffset, U yoffset, V zoffset, W the_width, X the_height, Y the_depth, GLenum format, GLenum type, const GLvoid *data)
 Update a section of a 3D image within the texture. More...
 
void get_image (GLint level, GLint format, GLenum type, GLvoid *img)
 Get image data from this texture. More...
 
void generate_mipmap ()
 Generate mipmaps for this texture. More...
 

Static Public Member Functions

static void unbind ()
 Unbind the current texture from the target. More...
 

Detailed Description

template<GLenum Target = GL_TEXTURE_2D>
class gls::texture< Target >

Class encapsulating an OpenGL texture object.

A gls::texture is an object that encapsulates an OpenGL texture object.

Template Parameters
TargetThe target this buffer object will be bound to when calling bind()

Like all objects in GLS, the underlying name is generated at object construction and deleted at destruction. This can be retrieved with name().

Texture objects store arbitrary image data within server (GPU) memory. This data can then be used by shader samplers to e.g. provide texturing to rendered primitives.

Texture objects can contain multiple images. Each image is simply a block of data with an internal format. Typically, when uploading texture data to apply to a primitive, one uploads it as the level 0 image of the texture. In order to reduce aliasing effects that might occur when sampling from them, multiple smaller versions of the level 0 image can be created. This is what is known as a mipmap chain. OpenGL can automatically generate the mipmap chain for you from your level 0 data when you call the generate_mipmap() method. The algorithm used is implementation specific. Alternatively, you can manually upload each level yourself if you wish to.

In addition to the data, each texture object has certain parameters associated with it. These can control a multitude of different things. Parameters that are automatically set for you on creation of a gls::texture are GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER. They are both set to GL_LINEAR by default, meaning OpenGL uses bilinear filtering when sampling from the texture. If you generate mipmaps, those parameters should be set to take them into account. Other common parameters include those that control how the texture is tiled or clamped when the sampler is passed certain texture coordinates in the shader.

To use a gls::texture, simply call bind(). It will bind the gls::texture to its target binding and replace any previously bound to that target. To clear the binding to that target, call unbind(). In newer OpenGL, textures are bound to texture units which are in turn used by the samplers within a shader to access texel data within them. The number of available texture units depends on the hardware.

Example usage:

auto texture_data = std::vector<GLubyte>{ 255, 0, 0, 255, 0, 255, 0, 255, 0, 0, 255, 255 };
texture.image_2d( 0, GL_RGBA, 3, 1, GL_RGBA, GL_UNSIGNED_BYTE, texture_data.data() );
texture.generate_mipmap();
// Disable filtering
texture.parameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
texture.parameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glActiveTexture( GL_TEXTURE0 );
texture.bind();
glActiveTexture( GL_TEXTURE0 + 1 );
... bind some other texture ...
... render some stuff using the textures ...
glActiveTexture( GL_TEXTURE0 );
texture.unbind();
glActiveTexture( GL_TEXTURE0 + 1 );
...

Constructor & Destructor Documentation

texture ( )

Default constructor.

The constructor, after a name has been generated for the object, sets the GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER texture parameters to GL_LINEAR.

texture ( int  )
protected

Constructor that doesn't set texture parameters.

Currently only used internally by gls::buffertexture.

Member Function Documentation

GLuint name ( ) const

Retrieve the OpenGL name of this texture.

Returns
OpenGL name of this texture
GLsizei width ( ) const

Retrieve the width of the level 0 image of the allocated texture storage.

Returns
Width of the level 0 image of the allocated texture storage
GLsizei height ( ) const

Retrieve the height of the level 0 image of the allocated texture storage.

Returns
Height of the level 0 image of the allocated texture storage
GLsizei depth ( ) const

Retrieve the depth of the level 0 image of the allocated texture storage.

Returns
Depth of the level 0 image of the allocated texture storage
void bind ( )

Bind this texture to its target.

Bind this texture to the target specified by the Target template parameter. This replaces any previous binding on that target.

static void unbind ( )
static

Unbind the current texture from the target.

Unbind the texture currently bound to the target specified by the Target template parameter.

void parameter ( GLenum  pname,
GLfloat  param 
)

Set a texture parameter.

Set the given parameter to the given value

Parameters
pnameName of the parameter to set
paramValue to set the parameter to
void parameter ( GLenum  pname,
GLint  param 
)

Set a texture parameter.

Set the given parameter to the given value

Parameters
pnameName of the parameter to set
paramValue to set the parameter to
void parameter ( GLenum  pname,
const GLfloat *  param 
)

Set a texture parameter.

Set the given parameter to the given value

Parameters
pnameName of the parameter to set
paramPointer to the value to set the parameter to
void parameter ( GLenum  pname,
const GLint *  param 
)

Set a texture parameter.

Set the given parameter to the given value

Parameters
pnameName of the parameter to set
paramPointer to the value to set the parameter to
void image_1d ( GLint  level,
GLint  internal_format,
the_width,
GLenum  format,
GLenum  type,
const GLvoid *  data 
)

Allocate storage for a 1D image and upload texture data.

This marks any previously allocated image storage for deletion by the GL.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
internal_formatInternal format of the texture
the_widthWidth of the texture image
formatFormat of the data passed to this method
typeType of the data passed to this method
dataPointer to memory containing the data to upload
void image_2d ( GLint  level,
GLint  internal_format,
the_width,
the_height,
GLenum  format,
GLenum  type,
const GLvoid *  data 
)

Allocate storage for a 2D image and upload texture data.

This marks any previously allocated image storage for deletion by the GL.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
internal_formatInternal format of the texture
the_widthWidth of the texture image
the_heightHeight of the texture image
formatFormat of the data passed to this method
typeType of the data passed to this method
dataPointer to memory containing the data to upload
void image_3d ( GLint  level,
GLint  internal_format,
the_width,
the_height,
the_depth,
GLenum  format,
GLenum  type,
const GLvoid *  data 
)

Allocate storage for a 3D image and upload texture data.

This marks any previously allocated image storage for deletion by the GL.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
internal_formatInternal format of the texture
the_widthWidth of the texture image
the_heightHeight of the texture image
the_depthDepth of the texture image
formatFormat of the data passed to this method
typeType of the data passed to this method
dataPointer to memory containing the data to upload
void sub_image_1d ( GLint  level,
xoffset,
the_width,
GLenum  format,
GLenum  type,
const GLvoid *  data 
)

Update a section of a 1D image within the texture.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
xoffsetx-offset of the region to update
the_widthWidth of the region to update
formatFormat of the data passed to this method
typeType of the data passed to this method
dataPointer to memory containing the data to upload
void sub_image_2d ( GLint  level,
xoffset,
yoffset,
the_width,
the_height,
GLenum  format,
GLenum  type,
const GLvoid *  data 
)

Update a section of a 2D image within the texture.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
xoffsetx-offset of the region to update
yoffsety-offset of the region to update
the_widthWidth of the region to update
the_heightHeight of the region to update
formatFormat of the data passed to this method
typeType of the data passed to this method
dataPointer to memory containing the data to upload
void sub_image_3d ( GLint  level,
xoffset,
yoffset,
zoffset,
the_width,
the_height,
the_depth,
GLenum  format,
GLenum  type,
const GLvoid *  data 
)

Update a section of a 3D image within the texture.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
xoffsetx-offset of the region to update
yoffsety-offset of the region to update
zoffsetz-offset of the region to update
the_widthWidth of the region to update
the_heightHeight of the region to update
the_depthDepth of the region to update
formatFormat of the data passed to this method
typeType of the data passed to this method
dataPointer to memory containing the data to upload
void get_image ( GLint  level,
GLint  format,
GLenum  type,
GLvoid *  img 
)

Get image data from this texture.

Parameters
levelThe level of detail number, 0 is the base image, level n is the n-th mipmap
formatFormat of the returned data
typeType of the returned data
imgPointer to memory to write the image data to
void generate_mipmap ( )

Generate mipmaps for this texture.