KGLLib
Public Member Functions | Protected Member Functions | Protected Attributes
KGLLib::Texture Class Reference

2D texture class More...

#include <texture.h>

Inheritance diagram for KGLLib::Texture:
KGLLib::TextureBase

List of all members.

Public Member Functions

 Texture (const QImage &img, GLenum filter=GL_LINEAR_MIPMAP_LINEAR)
 Texture (const QPixmap &pix, GLenum filter=GL_LINEAR_MIPMAP_LINEAR)
 Texture (const QString &filename, GLenum filter=GL_LINEAR_MIPMAP_LINEAR)
 Texture (int width, int height, GLint internalformat=GL_RGBA, GLint format=0)
int width () const
int height () const
QSize size () const
virtual void setWrapMode (GLenum mode)
virtual GLenum glTarget () const
void render (const QRectF &rect) const
QImage convertToGLFormat (const QImage &img) const

Protected Member Functions

bool init (int width, int height, GLint internalformat=GL_RGBA, GLint format=0)
bool init (const QImage &img, GLenum filter)
bool init (const QString &filename, GLenum filter)

Protected Attributes

int mWidth
int mHeight

Detailed Description

2D texture class

Creation

There are several ways how a Texture can be created.

You can convert QImage or QPixmap into a texture. QImage is more efficient here because QPixmap will be first converted into QImage and then into texture.

You can also specify a filename where the texture image should be loaded from. The image file has to be supported by Qt.

Finally, you can create an empty texture, specifying only its size (width and height). In that case you should usually upload the texture contents yourself.

If the creation fails for some reason, the resulting texture cannot be used and isValid() will return false.

Basic usage

To use a texture, first you need to enable texturing and bind the texture. Both of those operations can be achieved by calling enable(). This enables texturing with specific texture type (in this case, 2d textures) and also binds this texture, making it ready to use. Once you are done using the texture, you can call disable() to unbind it and disable texturing.

 // Load texture from image file
 Texture* tex = new Texture("logo.jpg");

 // Enable texturing with this texture
 tex->enable();
 // Render textured geometry
 glBegin(GL_TRIANGLES);
 ...
 glEnd();
 // Disable texturing
 tex->disable();
See also:
TextureBase, Mesh

Constructor & Destructor Documentation

KGLLib::Texture::Texture ( const QImage &  img,
GLenum  filter = GL_LINEAR_MIPMAP_LINEAR 
) [explicit]

Creates texture from given QImage. If the image is null image, then the resulting texture will be invalid.

Mipmaps are created automatically unless filter is GL_NEAREST or GL_LINEAR.

KGLLib::Texture::Texture ( const QPixmap &  pix,
GLenum  filter = GL_LINEAR_MIPMAP_LINEAR 
) [explicit]

Creates texture from QPixmap. If the pixmap is null pixmap, then the resulting texture will be invalid.

Mipmaps are created automatically unless filter is GL_NEAREST or GL_LINEAR.

KGLLib::Texture::Texture ( const QString &  filename,
GLenum  filter = GL_LINEAR_MIPMAP_LINEAR 
) [explicit]

Creates texture from image loaded from the given file. If the image cannot be loaded, then the resulting texture will be invalid.

Mipmaps are created automatically unless filter is GL_NEAREST or GL_LINEAR.

KGLLib::Texture::Texture ( int  width,
int  height,
GLint  internalformat = GL_RGBA,
GLint  format = 0 
)

Creates texture with the given size. The texture contents will be undefined until they're specified by user.

internalformat and format parameters describe format of the texture. If format is 0 (default value), then it will be computed automatically based on the value of internalformat.


Member Function Documentation

virtual GLenum KGLLib::Texture::glTarget ( ) const [inline, virtual]

TODO: maybe rename to glType()

Returns:
type of this texture (e.g. GL_TEXTURE_2D)

Implements KGLLib::TextureBase.

int KGLLib::Texture::height ( ) const [inline]

Return height of this texture.

See also:
width(), size()
void KGLLib::Texture::setWrapMode ( GLenum  mode) [virtual]

Sets wrap mode for both horizontal as well as vertical coordinates of this texture.

See also:
Texture::setWrapMode(GLenum coordinate, GLenum mode)

Implements KGLLib::TextureBase.

QSize KGLLib::Texture::size ( ) const [inline]

Return size (width and height) of this texture.

See also:
width(), height()
int KGLLib::Texture::width ( ) const [inline]

Return width of this texture.

See also:
height(), size()

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