CUGL
Cornell University Game Library
Public Types | Public Member Functions | Static Public Member Functions | List of all members
cugl::Texture Class Reference

#include <CUTexture.h>

Inheritance diagram for cugl::Texture:

Public Types

enum  PixelFormat : GLenum {
  PixelFormat::RGBA = GL_RGBA, PixelFormat::RGB = GL_RGBA, PixelFormat::RED = GL_RED, PixelFormat::ALPHA = GL_ALPHA,
  PixelFormat::UNDEFINED = GL_RG
}
 

Public Member Functions

 Texture ()
 
 ~Texture ()
 
void dispose ()
 
bool init (int width, int height, PixelFormat format=PixelFormat::RGBA)
 
bool initWithData (const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA)
 
bool initWithFile (const std::string &filename)
 
const Textureoperator= (const void *data)
 
const Textureset (const void *data)
 
bool isReady () const
 
bool isActive () const
 
bool hasMipMaps () const
 
void buildMipMaps ()
 
GLuint getBuffer ()
 
void setName (std::string name)
 
const std::string & getName () const
 
unsigned int getWidth () const
 
unsigned int getHeight () const
 
Size getSize ()
 
PixelFormat getFormat () const
 
GLuint getMinFilter () const
 
GLuint getMagFilter () const
 
void setMinFilter (GLuint minFilter)
 
void setMagFilter (GLuint magFilter)
 
GLuint getWrapS () const
 
GLuint getWrapT () const
 
void setWrapS (GLuint wrap)
 
void setWrapT (GLuint wrap)
 
const std::shared_ptr< Texture > & getParent () const
 
std::shared_ptr< TexturegetParent ()
 
std::shared_ptr< TexturegetSubTexture (GLfloat minS, GLfloat maxS, GLfloat minT, GLfloat maxT)
 
bool isSubTexture () const
 
GLfloat getMinS () const
 
GLfloat getMinT () const
 
GLfloat getMaxS () const
 
GLfloat getMaxT () const
 
void bind ()
 
void unbind ()
 
std::string toString (bool verbose=false) const
 
 operator std::string () const
 

Static Public Member Functions

static std::shared_ptr< Texturealloc (int width, int height, PixelFormat format=PixelFormat::RGBA)
 
static std::shared_ptr< TextureallocWithData (const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA)
 
static std::shared_ptr< TextureallocWithFile (const std::string &filename)
 

Detailed Description

This is a class representing an OpenGL texture.

We enforce that all textures must be a power-of-two along each dimension (though they need not be square). This is still required by some mobile devices and so it is easiest to require it across the board.

This class also provides support for texture atlases. Any non-repeating texture can produce a subtexture. A subtexture wraps the same texture data (and so does not require a context switch in the rendering pipeline), but has different start and end boundaries, as defined by minS, maxS, minT and maxT. See getSubtexture() for more information.

Member Enumeration Documentation

enum cugl::Texture::PixelFormat : GLenum
strong

This enum lists the possible texture pixel formats.

Because of cross-platform issues (we must support both OpenGL and OpenGLES), our textures only support a small subset of formats. No other formats are supported.

Enumerator
RGBA 

The default type; RGB with alpha transparency

RGB 

RGB with no alpha (all blending assumes alpha is 1.0)

RED 

A single color channel of red (all blending assumes alpha is 1.0)

ALPHA 

An alpha-only channel

UNDEFINED 

The image is not yet defined

Constructor & Destructor Documentation

cugl::Texture::Texture ( )

Creates a new empty texture with no size.

This method performs no allocations. You must call init to generate a proper OpenGL texture.

cugl::Texture::~Texture ( )
inline

Deletes this texture, disposing all resources

Member Function Documentation

static std::shared_ptr<Texture> cugl::Texture::alloc ( int  width,
int  height,
PixelFormat  format = PixelFormat::RGBA 
)
inlinestatic

Returns a new empty texture with the given dimensions.

When initialization is done, the texture is no longer bound. However, any other texture that was bound during initialization is also no longer bound.

You must use the set() method to load data into the texture.

Parameters
widthThe texture width in pixels
heightThe texture height in pixels
formatThe texture data format
Returns
a new empty texture with the given dimensions.
static std::shared_ptr<Texture> cugl::Texture::allocWithData ( const void *  data,
int  width,
int  height,
PixelFormat  format = PixelFormat::RGBA 
)
inlinestatic

Returns a new texture with the given data.

When initialization is done, the texture is no longer bound. However, any other texture that was bound during initialization is also no longer bound.

The data format must match the one given.

Parameters
dataThe texture data (size width*height*format)
widthThe texture width in pixels
heightThe texture height in pixels
formatThe texture data format
Returns
a new texture with the given data.
static std::shared_ptr<Texture> cugl::Texture::allocWithFile ( const std::string &  filename)
inlinestatic

Returns a new texture with the data from the given file.

When initialization is done, the texture is no longer bound. However, any other texture that was bound during initialization is also no longer bound.

This method can load any file format supported by SDL_Image. This includes (but is not limited to) PNG, JPEG, GIF, TIFF, BMP and PCX.

The texture will be stored in RGBA format, even if it is a file format that does not support transparency (e.g. JPEG).

Parameters
filenameThe file supporting the texture file.
Returns
a new texture with the given data
void cugl::Texture::bind ( )

Binds this texture, making it active.

Once bound, any OpenGL calls will then use this texture.

void cugl::Texture::buildMipMaps ( )

Builds mipmaps for the current texture.

This method will fail if this texture is a subtexture. Only the parent texture can have mipmaps. In addition, mipmaps can only be built if the texture size is a power of two.

void cugl::Texture::dispose ( )

Deletes the OpenGL texture and resets all attributes.

You must reinitialize the texture to use it.

GLuint cugl::Texture::getBuffer ( )
inline

Returns the OpenGL buffer for this texture.

This method will return 0 if the texture is not initialized.

Returns
the OpenGL buffer for this texture.
PixelFormat cugl::Texture::getFormat ( ) const
inline

Returns the data format of this texture.

The data format determines what type of data can be assigned to this texture.

Returns
the data format of this texture.
unsigned int cugl::Texture::getHeight ( ) const
inline

Returns the height of this texture in pixels.

Returns
the height of this texture in pixels.
GLuint cugl::Texture::getMagFilter ( ) const
inline

Returns the mag filter of this texture.

The mag filter is the algorithm hint that OpenGL uses to make an image larger. The default is GL_LINEAR.

Returns
the mag filter of this texture.
GLfloat cugl::Texture::getMaxS ( ) const
inline

Returns the maximum S texture coordinate for this texture.

When rescaling texture coordinates for a subtexture, this value is used in place of 0.

Returns
the maximum S texture coordinate for this texture.
GLfloat cugl::Texture::getMaxT ( ) const
inline

Returns the maximum T texture coordinate for this texture.

When rescaling texture coordinates for a subtexture, this value is used in place of 0.

Returns
the maximum T texture coordinate for this texture.
GLuint cugl::Texture::getMinFilter ( ) const
inline

Returns the min filter of this texture.

The min filter is the algorithm hint that OpenGL uses to make an image smaller. The default is GL_NEAREST.

Returns
the min filter of this texture.
GLfloat cugl::Texture::getMinS ( ) const
inline

Returns the minimum S texture coordinate for this texture.

When rescaling texture coordinates for a subtexture, this value is used in place of 0.

Returns
the minimum S texture coordinate for this texture.
GLfloat cugl::Texture::getMinT ( ) const
inline

Returns the minimum T texture coordinate for this texture.

When rescaling texture coordinates for a subtexture, this value is used in place of 0.

Returns
the minimum T texture coordinate for this texture.
const std::string& cugl::Texture::getName ( ) const
inline

Returns the name of this texture.

A name is a user-defined way of identifying a texture. Subtextures are permitted to have different names than their parents.

Returns
the name of this texture.
const std::shared_ptr<Texture>& cugl::Texture::getParent ( ) const
inline

Returns the parent texture of this subtexture.

This method will return nullptr is this is not a subtexture.

Returns the parent texture of this subtexture.

std::shared_ptr<Texture> cugl::Texture::getParent ( )
inline

Returns the parent texture of this subtexture.

This method will return nullptr is this is not a subtexture.

Returns the parent texture of this subtexture.

Size cugl::Texture::getSize ( )
inline

Returns the size of this texture in pixels.

Returns
the size of this texture in pixels.
std::shared_ptr<Texture> cugl::Texture::getSubTexture ( GLfloat  minS,
GLfloat  maxS,
GLfloat  minT,
GLfloat  maxT 
)

Returns a subtexture with the given dimensions.

The values must be 0 <= minS <= maxS <= 1 and 0 <= minT <= maxT <= 1. They specify the region of the texture to extract the subtexture.

It is the responsibility of the user to rescale the texture coordinates when using subtexture. Otherwise, the OpenGL pipeline will just use the original texture instead. See the method SpriteBatch#prepare() for an example of how to scale texture coordinates.

It is possible to make a subtexture of a subtexture. However, in that case, the minS, maxS, minT and maxT values are all with respect to the original root texture. Furthermore, the parent of the new subtexture will be the original root texture. So no tree of subtextures is more than one level deep.

Returns a subtexture with the given dimensions.

unsigned int cugl::Texture::getWidth ( ) const
inline

Returns the width of this texture in pixels.

Returns
the width of this texture in pixels.
GLuint cugl::Texture::getWrapS ( ) const
inline

Returns the horizontal wrap of this texture.

The default is GL_CLAMP_TO_EDGE.

Returns
the horizontal wrap of this texture.
GLuint cugl::Texture::getWrapT ( ) const
inline

Returns the vertical wrap of this texture.

The default is GL_CLAMP_TO_EDGE.

Returns
the vertical wrap of this texture.
bool cugl::Texture::hasMipMaps ( ) const
inline

Returns whether this texture has generated mipmaps.

If this texture is a subtexture of texture with mipmaps, this method will also return true (and vice versa)

Returns
whether this texture has generated mipmaps.
bool cugl::Texture::init ( int  width,
int  height,
PixelFormat  format = PixelFormat::RGBA 
)

Initializes an empty texture with the given dimensions.

When initialization is done, the texture is no longer bound. However, any other texture that was bound during initialization is also no longer bound.

You must use the set() method to load data into the texture.

Parameters
widthThe texture width in pixels
heightThe texture height in pixels
formatThe texture data format
Returns
true if initialization was successful.
bool cugl::Texture::initWithData ( const void *  data,
int  width,
int  height,
PixelFormat  format = PixelFormat::RGBA 
)

Initializes an texture with the given data.

When initialization is done, the texture is no longer bound. However, any other texture that was bound during initialization is also no longer bound.

The data format must match the one given.

Parameters
dataThe texture data (size width*height*format)
widthThe texture width in pixels
heightThe texture height in pixels
formatThe texture data format
Returns
true if initialization was successful.
bool cugl::Texture::initWithFile ( const std::string &  filename)

Initializes an texture with the data from the given file.

When initialization is done, the texture is no longer bound. However, any other texture that was bound during initialization is also no longer bound.

This method can load any file format supported by SDL_Image. This includes (but is not limited to) PNG, JPEG, GIF, TIFF, BMP and PCX.

The texture will be stored in RGBA format, even if it is a file format that does not support transparency (e.g. JPEG).

Parameters
filenameThe file supporting the texture file.
Returns
true if initialization was successful.
bool cugl::Texture::isActive ( ) const
inline

Returns whether this texture is actively in use.

If this texture is a subtexture of texture in use, this method will also return true (and vice versa)

If this texture is the parent of a subtexture in use, this method will return true.

Returns
whether this texture is actively in use.
bool cugl::Texture::isReady ( ) const
inline

Returns true if this shader has been compiled and is ready for use.

Returns
true if this shader has been compiled and is ready for use.
bool cugl::Texture::isSubTexture ( ) const
inline

Returns true if this texture is a subtexture.

This is the same as checking if the parent is not nullptr.

Returns
true if this texture is a subtexture.
cugl::Texture::operator std::string ( ) const
inline

Cast from Texture to a string.

const Texture& cugl::Texture::operator= ( const void *  data)
inline

Sets this texture to have the contents of the given buffer.

The buffer must have the correct data format. In addition, the buffer must be size width*height*format.

This method binds the texture if it is not currently active.

Parameters
dataThe buffer to read into the texture
Returns
a reference to this (modified) texture for chaining.
const Texture& cugl::Texture::set ( const void *  data)

Sets this texture to have the contents of the given buffer.

The buffer must have the correct data format. In addition, the buffer must be size width*height*format.

This method binds the texture if it is not currently active.

Parameters
dataThe buffer to read into the texture
Returns
a reference to this (modified) texture for chaining.
void cugl::Texture::setMagFilter ( GLuint  magFilter)

Sets the mag filter of this texture.

The mag filter is the algorithm hint that OpenGL uses to make an image larger. The default is GL_LINEAR.

Parameters
magFilterThe mag filter of this texture.
void cugl::Texture::setMinFilter ( GLuint  minFilter)

Sets the min filter of this texture.

The min filter is the algorithm hint that OpenGL uses to make an image smaller. The default is GL_NEAREST.

Parameters
minFilterThe min filter of this texture.
void cugl::Texture::setName ( std::string  name)
inline

Sets the name of this texture.

A name is a user-defined way of identifying a texture. Subtextures are permitted to have different names than their parents.

Parameters
nameThe name of this texture.
void cugl::Texture::setWrapS ( GLuint  wrap)

Sets the horizontal wrap of this texture.

The default is GL_CLAMP_TO_EDGE.

void cugl::Texture::setWrapT ( GLuint  wrap)

Sets the vertical wrap of this texture.

The default is GL_CLAMP_TO_EDGE.

std::string cugl::Texture::toString ( bool  verbose = false) const

Returns a string representation of this texture for debugging purposes.

If verbose is true, the string will include class information. This allows us to unambiguously identify the class.

Parameters
verboseWhether to include class information
Returns
a string representation of this texture for debugging purposes.
void cugl::Texture::unbind ( )

Uninds this texture, making it no longer active.

Once unbound, OpenGL calls will no longer use this texture.


The documentation for this class was generated from the following file: