| Lesson #5
Textures and printing on screen
In this lesson you will discover the use of textures
Although things you'v learnt in previous lessons provided some general info on TBGL, for higher quality rendering you need at least knowledge of applying of textures and work with light.
The first mentioned is topic of this tutorial. To attract you little more here are two images. There are displayed two same models, the first is just colored and the second textured. It's obvious that for the most of the people the textured model looks more eye candy.
Before I start with textures, I'd like to explain one simple thing - the way you can draw some text on screen in TBGL. It is just little bit related to the textures, but we will need it in future lessons.
The simpliest way to plot some text on the screen is using bitmap font. Luckily, one example bitmap font is budled with TBGL, under name TBGL_FONT.BMP. It's normal bitmap image so you can edit it Paintbrush for example.
To use the font in program you must do 3 simple things.
First, you need to load the bitmap font. This is done this way ( make sure the copy of TBGL_FONT.BMP is in the same directory as the script ):
You MUST call it AFTER creation of TBGL window, or it will have null effect. You can use just one bitmap font at time.
The second step is to prepair screen to draw. This is done by reseting matrix after displaying whole scene.
To reset matrix just type:
This asures that rendered characters will be always positioned at desired locations.
After calling this you just enters commands to display text:
For example this command will draw text "Hello graphics" positioned at first column and third row.
The position 1, 1 means upper left corner of the TBGL window. Whole TBGL screen can be filled with 26x27 characters.
So here ends my little off topic :) and begins fun with the textures.
Let me tell you some introduction to the topic of textures. Textures are nothing more that image files, in TBGL it must be ( at least for version 0.1.6 ) simple BMP file.
But caution ! There are some special rules which I strongly recommend to follow, if you don't get into troubles.
The first recommendation/restrinction is, that the bit depth of BMP file must be 8 bit ( 256 color ) and higher. The 16 colors textures or even black & white bitmaps are not supported !
Although 256 textures can seriously decrease size of the application on the hard drive, they usually doesn't look very good. But for some cases they are good enough.
Second rule is to keep the dimensions of texture as square with sides in power of two. I mean for example 64x64, 128x128 and so on.
I recommend to not go over 512x512, because some older 3D cards simply doesn't support this sizes or they just has little memory.
But if you are sure the code will run only on speedy beasts like Radeons or GeForces, you could use theoretically textures as big as 2048x2048.
The last thing you should now is, that there are 3 levels of texture handling in TBGL.
With the lowest, expressed by equate %TBGL_TEX_NEAREST you will experience faster rendering on some really really old cards, but it will look simply awful. Do you remember old DOOM 1 game ?
It was graphical revolution in its times, but if you watch the nasty sqaure pixeled texture mapping now... Sadly it's not very cool.
Better results will be obtained with %TBGL_TEX_LINEAR. This style will perform kind of smoothing the texture. Although the texture is for example in low resolution, you will not see those terrible big square areas of stretched pixels, it will look blury.
Although you could think this is nice, there are some problems too. The textures loaded with this style will look very good, if the texture object will be close to the camera. But with increasing distance from camera the texture will look worse and worse. Also rotated polygons doesn't look yummy.
The last texture style in TBGL is %TBGL_TEX_MIPMAP. It is the best in terms of quality of display, but it can be very memory consuming. Why ? The mipmapping effect is way to avoid strange look with growing distance. It is very reliable effect, but as I said, very memory consuming. It is based on internal creation of low resolution versions of the original texture.
If the textured object is close to the camera, the texture is rendered at full resolution. As the texture is getting far from the eye of camera, the resolution is dynamically decreasing to avoid the pixel scramble problem.
See this image to compare the quality:
I'v talked enough about theory. Now lets practise. The loading of the texture is performed by this command:
This must be called after creation of TBGL window, because tbgl_CreateWindow allocates buffers for textures for you.
The "TextureIdentifier" is number in range 1 - 255, which will later identify the texture. The "Quality" is one of the equates mentioned before, so %TBGL_TEX_NEAREST, %TBGL_TEX_LINEAR or %TBGL_TEX_MIPMAP.
To turn use of textures on or off, you must use simple flag statement
If the flag is number zero, it will disable use of textures, number one will enable it.
This flag turns on use of textures generally, not for some specific.
To tell TBGL to use special texture, you should use
But that's not all. The applying of textures to polygons is not automatic, you must assign "texture coordinate" to each vertex.
What is texture coordinate ? It is like a position in bitmap raster.
The coordinate 0,0 means lower left corner and the 1,1 means upper right one. So if you want to cover square with already loaded texture with TextureIdentifier for example 7, you will do it this way:
With tbgl_TexCoord2D you can play with texture mapping a lot, by changing the texture coordinates you can map the image upside down or make the texture dynamicaly moving ...
The last but very important thing is, that if you specify texture coordinate major than 1 it will perform "texture repeating". This nice feature allows you to save some memory, because instead of super big texture with brick wall 10x10 meters you can load just one and repeat it ten times !
Hope you enjoyed this tutorial ( first one with explicative images in text :) ), and I hope you will have fun exploring the texturing capatibilities in source code examples !