CUGL
Cornell University Game Library
|
#include <CUTexture.h>
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 Texture & | operator= (const void *data) |
const Texture & | set (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< Texture > | getParent () |
std::shared_ptr< Texture > | getSubTexture (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< Texture > | alloc (int width, int height, PixelFormat format=PixelFormat::RGBA) |
static std::shared_ptr< Texture > | allocWithData (const void *data, int width, int height, PixelFormat format=PixelFormat::RGBA) |
static std::shared_ptr< Texture > | allocWithFile (const std::string &filename) |
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.
|
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.
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.
|
inline |
Deletes this texture, disposing all resources
|
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.
width | The texture width in pixels |
height | The texture height in pixels |
format | The texture data format |
|
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.
data | The texture data (size width*height*format) |
width | The texture width in pixels |
height | The texture height in pixels |
format | The texture data format |
|
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).
filename | The file supporting the texture file. |
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.
|
inline |
Returns the OpenGL buffer for this texture.
This method will return 0 if the texture is not initialized.
|
inline |
Returns the data format of this texture.
The data format determines what type of data can be assigned to this texture.
|
inline |
Returns the height of this texture in pixels.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
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.
|
inline |
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.
|
inline |
Returns the width of this texture in pixels.
|
inline |
Returns the horizontal wrap of this texture.
The default is GL_CLAMP_TO_EDGE.
|
inline |
Returns the vertical wrap of this texture.
The default is GL_CLAMP_TO_EDGE.
|
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)
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.
width | The texture width in pixels |
height | The texture height in pixels |
format | The texture data format |
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.
data | The texture data (size width*height*format) |
width | The texture width in pixels |
height | The texture height in pixels |
format | The texture data format |
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).
filename | The file supporting the texture file. |
|
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.
|
inline |
Returns true if this shader has been compiled and is ready for use.
|
inline |
Returns true if this texture is a subtexture.
This is the same as checking if the parent is not nullptr.
|
inline |
Cast from Texture to a string.
|
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.
data | The buffer to read into the texture |
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.
data | The buffer to read into the texture |
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.
magFilter | The 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.
minFilter | The min filter of this texture. |
|
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.
name | The 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.
verbose | Whether to include class information |
void cugl::Texture::unbind | ( | ) |
Uninds this texture, making it no longer active.
Once unbound, OpenGL calls will no longer use this texture.