Special

Clearance Sale!

We've been publishing for over five years now and it's time to clear out our inventory of back issues, so we're slashing prices!

RBD Magazines

Check out this amazing clearance sale of all our past issues. Missing some issues? This is a great time to complete your RBD collection. Save up to 40% off the regular price of our printed back issue packages. These prices are only good until the end of the year May 2008 and supplies are limited, so place your order today.

Article Preview


Buy Now

Print:
PDF:

The Topographic Apprentice

The Hills Are Alive...

Generating Terrain

Issue: 4.3 (January/February 2006)
Author: Joe Nastasi
Article Description: No description available.
Article Length (in bytes): 8,248
Starting Page Number: 32
RBD Number: 4315
Resource File(s):

Download Icon 4315.sit Updated: Monday, March 20, 2006 at 10:39 AM

Related Link(s): None
Known Limitations: None

Excerpt of article text...

After a bit of stagnation, the REALbasic 3D API got a huge upgrade for REALbasic 2005 Release 3. Much of Quesa's functionality that previously could only be accessed via declares or wrapper classes are now part of the REALbasic API. One of the biggest improvements is the ability to create 3D models under program control. We are not talking about hacking 3DMF files here, but creating and modifying trimeshes using the new Trimesh class. What we are going to discuss in this issue is a fairly standard technique called terrain mapping. The basic idea is that a grayscale image is used to generate a landscape. In the simplest form, each pixel maps to a vertex, mapping out the terrain's depth and breadth. The height of each vertex is generated from each pixel in the image. In other words, the intensity of the pixel drives the Y component of the vertex's position vector. The map is typically called a height map. Why do it this way? After all, we could build an editor that allows you to generate a flat terrain and then pull vertexes up or down manually. In fact, most terrain editors do allow you to edit a terrain in this manner. But for the initial generation of the terrain, it is a tedious method. It's much easier, especially for an artist/level designer, to use all the tools a commercial image editor offers to draw a 2D representation of the terrain. Designer's brains quickly assimilate the concept that pure black is "sea level" and the closer to white you paint, the higher the ground rises at that point. The amount of contrast determines if a terrain is gently sloped or rough and jagged. Of course, even with a great image editor, there will be a point in the terrain design process where more precision is needed and an actual 3D image of the terrain is required for proper feedback. That's when a terrain editing program can import the height map and manipulate the trimesh that it generates. Building Terrain Dimensions Our first job is to extract the terrain's depth, breadth, and height from the grayscale image. Note that it IS possible to use a color image, but it will be converted into grayscale by this process and will not be as intuitive to the designer. We want to loop through every pixel and create a data point from each. If the image used is a high resolution image however, it would make the trimesh larger than it might need to be. That means we will want to provide a means of adjusting the resolution of the terrain model. More on that later. Listing 1 shows the BuildMapArray method which simply creates a two-dimensional array of integers in a global property, ImageData. We are using RGBSurface to access the individual pixels, and since we can only access the pixels as color values (either RGB, HSV or CMY), we convert to grayscale by adding the RGB values and dividing by 3. This leaves us with just one 0 to 255 value that we will use to represent height. Listing 1: BuildMapArray MethodPrivate Sub BuildMapArray(image As Picture) // build an array of height data Dim surf As RGBSurface Dim x, y, g As Integer // size array based on image size Redim ImageData(self.imageholder.Width,Self.Imageholder.Height) // parse image and calculate a height value surf = image.RGBSurface For y = image.height - 1 DownTo 0 For x = image.width - 1 DownTo 0 // convert color to gray scale and save g = (surf.Pixel(x,y).Red + surf.Pixel(x,y).Green + surf.Pixel(x,y).Blue) / 3 ImageData(x,y) = g Next Next End Sub

...End of Excerpt. Please purchase the magazine to read the full article.

Article copyrighted by REALbasic Developer magazine. All rights reserved.


 


|

 


Weblog Commenting and Trackback by HaloScan.com