-
Notifications
You must be signed in to change notification settings - Fork 45
Textures
This article describes how images can be loaded from files and subsequently into textures for drawing. It will also show how to use multiple textures in a single drawing operation.
OOGL contains an Image
container class that can load and save BMP, TGA, JPEG and PNG images. This can be done by specifying a path to a supported image file to the constructor.
GL::Image image( "/path/to/image.png" );
It is also possible to create an image yourself from a byte array or with SetPixel
calls.
Just like any other OpenGL object, textures have been wrapped into a class. A Texture
can be created from scratch, but the constructor can also take a previously loaded or created Image
instance.
GL::Texture tex( image );
There is an optional second parameter that can be used to specify the internal storage format of the texture, which defaults to RGBA.
To use a texture, first bind it to a texture unit by calling BindTexture
.
gl.BindTexture( tex, 0 );
It can then be sampled with a sampler2D
in a shader with the texture unit assigned to it.
Using multiple textures is as simple as using multiple texture units and multiple samplers bound to those units.
An example:
GL::Image im1( "/path/to/image1.png" );
GL::Image im2( "/path/to/image2.png" );
GL::Texture tex1( im1 );
GL::Texture tex2( im2 );
gl.BindTexture( tex1, 0 );
gl.BindTexture( tex2, 1 );
program.SetUniform( program.GetUniform( "t1" ), 0 );
program.SetUniform( program.GetUniform( "t2" ), 1 );
in vec2 Coords;
out vec4 outColor;
uniform sampler2D t1;
uniform sampler2D t2;
void main() {
outColor = mix( texture2D( t1, Coords ), texture2D( t2, Coords ), 0.5 );
}
This example will blend two texture images. Note that it was previously not necessary to set the sampler unit because the value of the uniform defaults to 0
already.