Textures are probably the coolest thing to happen to 3D graphics since polygons!
Textures are special images which are drawn on top of polygons in order to give them added detail.
Textures can be either created 'from scratch' or loaded from an image file.
Just like ordinary Blitz2D images, textures are made up of pixels. These pixels can be accessed using x,y coordinates just like images - only, in a slightly different way.
Instead of x,y values ranging from 0 up to the size of the texture, x,y values for textures range from 0 to 1. This means the texture pixel at location .5,.5 is always in the center of the texture. This is possibly a little confusing at first, but it actually turns out to be very convenient as you don't have to keep track of how big your textures are!
When a texture is created, a 'texture flags' value is provided to indicate what type of texture you're after.
Here are the legal texture flags values:
1: | Texture has color information. |
2: | Texture has alpha information. |
4: | Texture is a masked texture. This means that black pixels are transparent. |
8: | Texture is mipmapped. |
16: | Texture is horizontally clamped. |
32: | Texture is vertically clamped. |
64: | Texture is a spherical environment map. |
Texture flags can be added together to combine the effect of separate flags. For example, a texture flags value of '3' indicates you want a texture with both color PLUS alpha information.
If neither the color, alpha or mask flags are used, then the texture is created in a special format that allows you to draw to it using standard Blitz2D or even Blitz3D commands! A texture created this way will have color information, but may or may not have alpha information (this depends on what graphics card you're using!), so its really safest to assume it has no alpha information.
If the masked flag is used, the color and alpha flags are ignored.
The clamped flags allow you to control whether the texture 'wraps' or not. By default, textures will 'wrap' or 'repeat' if drawn on large triangles. However, you can prevent this by using the clamped flags.
Finally, the spherical environment map flag provides a dead easy way to do cool 'reflective' type effects. Try it!
You can either create a texture and draw to it by hand, or load a texture from an image file.
Loading a texture from a file is easy:
texture=LoadTexture( "mytexture.jpg",3 ) |
Note the texture flags value of '3'. This indicates we want the texture to contain both color and alpha information.
However, the JPG file format does not support alpha information! In this case, Blitz3D will create its own alpha values based on the color values in the texture.
However, both the PNG and TGA file formats do support alpha information, and Blitz3D will use this information if present.
To create a texture by hand, use the CreateTexture command.
;create a 256x256 texture texture=CreateTexture( 256,256,0 ) ;retrieve its width ;retrieve its height |
Why are we retrieving the texture size when we've already told Blitz3D how big we want the texture?
Well, this is because all 3D graphics cards have limits on the size of texture they can handle. Typically, texture width and height should be a power of 2 value (eg: 1,2,4,8,16,32,64,128,256,512,1024...) and in some cases - 3DFX cards being the main culprit here - must be no larger than 256 by 256.
There are also rumours out there that some cards can only handle square textures - although here at Blitz Research we are yet to run into any such beast!
A texture size where width and height are both power of 2 values and <= 256 is pretty much guaranteed to be supported by your card - but it never hurts to check!
In the event that you specify a texture size not supported by your graphics card, Blitz3D will pick the nearest useful size instead.
Also note that we are using a texture flags value of 0. This is because we probably want to draw something to the texture, and to be able to use graphics commands on a texture you should not specify the color, alpha or masked texture flags.
OK, now we've created our texture we should probably draw something on it:
;set drawing buffer to the texture SetBuffer TextureBuffer( texture ) ;set cls color to red ;clear the texture to red ;set drawing color to blue ;draw a blue oval. ;back to the back buffer... |
Finally, once you've finished with a texture, use FreeTexture to release it.
ScaleTexture, RotateTexture and PositionTexture can all be used to animate textures. For example, to cause a texture to scroll, you might use something like...
texture_x#=texture_x#+.1 ;add .1 to texture x position.
PositionTexture texture,texture_x#,0 ;position the texture.
...in your main loop. You can dynamically scale and rotate textures in a similar way.
Blitz3D allows you to draw up to 8 textures to a single triangle, a technique known as
'multitexturing'.
A common use for multitexturing is 'light mapping', a technique where a texture containing lighting information is combined with a texture containing color information to provide lighting effects.
When you use the EntityTexture or BrushTexture commands, an optional 'index' parameter allows you to control which of the 8 textures you are setting.
By default, multiple textures are combined using multiplication which achieves a lighting type effect. However, the
TextureBlend command allows you to perform other operations such as alpha-blend and add on multiple textures.