Thank you for visiting Blazing Games

Revisiting Raycasting

The sewers are rendered using a more advanced version of the raycasting engine that was introduced back in episode 31. A detailed explanation on how raycasting works was made in chapter 31 so I will only briefly cover the theory. The basic idea behind this technique is that you are taking a 2D map and extruding it to simulate a 3D world. For each column of pixels, a ray is launched from the player's current position at the angle that the column would represent based on the direction that the player is facing. The ray travels until it hits a wall on the map. The distance that the ray travels determines how big the slice of wall should be drawn.

The raycaster in chapter 31 used solid colors for drawing the display. This time we are going to be adding texture to the walls. This is done using a technique known as texture-mapping. In 3D graphics, images are often made up of a large numer of polygons. The idea behind texture mapping was instead of using a large number of colored polygons to make more realistic image, an image of a texture could be mapped onto a single polygon. This has the advantages of reducing the amount of polygons needed to be tracked/drawn (speeding up the drawing) and making the image look much more realistic. While this sounds complicated, this implementation is very easy. You are essentially taking an image and distorting it to fit the shape of the polygon it is being mapped onto.

The simplest form of texture mapping is just distorting an image to fit a polygon. This works fine for distant objects, but results in a distorted look when the objects are close to the camera. This is due to the z component not being linear when projected onto a 2D plane. The further away you move along the z axis, the smaller the distance between projected pixels. Simply distorting a rectangle to fit into a polygon is the equivalent of linearly interpolating the Z values but the values aren't linear so the result appears distorted. There are a number of techniques that can be used to reduce or solve this problem, but thanks to the fast that we are using ray casting this issue has already been solved for us.

When you think about it, the 3D view is actually extracted from the 2D, each strip is simply a vertical line of the texture image stretched or shrunk to the height of that strip. The point where the ray intersects the tile determines the column of pixels that will be used to draw the image strip. Finally, the distance to the tile is the z value which we can use for z-buffering but isn't directly needed for the texture mapping as that has been determined by the intersection point.

Previous page
Chapter 45 Page 4

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