wissen.leben | WWU Münster 


tgt textures example

Overview

This tutorial demonstrates texture loading and handling in tgt.

General notes on textures

To learn about textures in OpenGL in general and how to use them, you might want to read the information provided by OpenGL.

Textures in tgt

Textures and their functionalities concerning OpenGL are wrapped by the tgt::Texture class. tgt’s Textures offer a rich support for OpenGL features (i.e. filtering, uploading to graphics card), but it is beyond the scope of this tutorial to explain the texture class in detail (see Doxygen documentation or .h file to get more information).
In tgt, Textures are loaded by the texture manager, which uses the texture readers automatically registered in the tgt initialization phase. If tgt is compiled with DevIL library support, you already have a reader which is capable of loading the most common image formats.

A sample

To work with textures you need the texture.h include. Texture loading is handled by the texture manager. So we’ll add this one as well.

#include "tgt/texturemanager.h"
#include "tgt/texture.h"

Painter

As in every example we keep things easy by using only the painter class itself. Attributes concerning texturing are therefor the texture itself and an array of texture coordinates.

class TexturePainter : public Painter {
private:
    Texture* myTexture_;
    vec2 texKoord_[4];
...

Initialization

The initialization of the painter consits of loading the texture by using the load method of the texture manager singleton. This is only preceeded by adding a path to its the search list.

Further we’ll set the texture coordinates here to use them when painting.

Destruction of textures is handled by the texture manager. But we need to dispose it. This is done in the painter destructor.

TexturePainter::~TexturePainter(){
    TexMgr.dispose( myTexture_ );
}

void TexturePainter::init(){
    ...
    TexMgr.addPath("data/textures");
    myTexture_ = TexMgr.load( "texture16x16.jpg" );
 
    texKoord_[0] = vec2( 0.f, 0.f );
    texKoord_[1] = vec2( 1.f, 0.f );
    texKoord_[2] = vec2( 1.f, 1.f );
    texKoord_[3] = vec2( 0.f, 1.f );
}

Painting

Next comes painting of textures. Textures are always bound to a face of a geometric structure. To map an area of the texture to this face you have to preceed every glVertex command with a glTexCoord wich sets the texture coordinate to use for the following vertex. Thats exactly what we’ll do to paint some rectangles.

Before the actual rendering commands, the texture to be used has to be bound. In tgt this is done by calling the bind() method of the texture. And as you can see below, you can also alter the OpenGL state concerning for example filtering and wrapping by just calling the according functions.

void TexturePainter::paint() {
    if ( myTexture_ ) {
        myTexture_->bind();
        myTexture_->setWrapping(REPEAT);
        myTexture_->setFilter(LINEAR);

        glBegin(GL_QUADS);
            glTexCoord2f(texKoord_[0].x, texKoord_[0].y); glVertex3i( -1,  0, 0 );
            ...
        glEnd();
        ...
    }
}
Texture Example

Texture Example


Impressum | © 2009 Arbeitsgruppe VisCG | Edit this page
Arbeitsgruppe Visualisierung und Computergrafik
Einsteinstraße 62 · 48149 Münster
Tel.: +49 (251) 83-32700 · Fax: +49 (251) 83-33755