Thank you for visiting Blazing Games

Implementing Texture Mapping

As was explained in the previous section, a textured strip is simply a vertical line of the texture image that is stretched to the height of the strip. The routine is designed to handle both stretching and shrinking. Streching columns have a special case of a strip being taller than the screen. To handle this, there are three variables used to determine what part of the strip I'd drawn. The offset is how far into the texture to start drawing. The strt is the first y coordinate to draw a pixel on. The end is the last y coordinate in which a pixel is drawn.

The vertical line of the texture to draw is given as a fraction. This has the advantage of allowing any width of texture be used. The texelsize is the portion of a texture pixel a displayed pixel is worth. As with the width, this technique allows for any height of texture be used. For this implementation there is no pixel blending being done so for strips with a texelsize greater than 1 pixels will be skipped while a texelsize size of less than one will result in pixels being repeated. This was done for speed reasons.

The main loop then simply copies pixels from the texture onto the screen. Here is the function.

/**
* This draws a strip.
*
* @param x column to draw strip into
* @param height height of the strip
* @param strip the strip to draw (fraction is used to determine strip)
*/
public function paintStrip(x:int, h:int, texture:BitmapData, strip:Number):void
{
var strt:int, end:int, offset:int;

if (h < _displayHeight) {
offset = (_displayHeight - h) / 2;
strt = 0;
end = h;
} else {
offset = 0;
strt = (h - _displayHeight) / 2;
end = strt + _displayHeight;
}

var stripPart:Number = strip - (Math.floor(strip));
var stripX:int = texture.width * stripPart;
var colorByte:uint = uint(255 * stripPart);
var colour:uint = 0xff000000 | (colorByte << 16) | (colorByte << 8) | colorByte;
var texelSize:Number = texture.height / h;
for (var cntr:int = strt; cntr < end; ++cntr)
{
_bitmapData.setPixel32(x,offset, //colour);
texture.getPixel32(stripX, cntr * texelSize));
++offset;
}
}

This is actually a fairly good technique for texturing strips. If you compare this episode with episode 31, you can clearly see how the exta element of texture mapping really improves the visuals.

Previous page
Chapter 45 Page 5

About - Privacy Policy - Contact - Links - FAQ
Copyright © 2012 Blazing Games Inc. All Rights Reserved