Trees


If Joyce Kilmer had been a game developer, he might have written, "I think that I shall never see, a model so annoying as a tree." Or something like that. But he didn't, so that's too bad. Really talented game developer poets are rare.

Nonetheless, computer model trees really are annoying. There is this conundrum: If you can interact reasonably well with a model tree, then it looks awful. If the tree looks really good, then interacting with it is awful.

The problem is twofold. We see them everywhere, in most parts of the world, so they provide a great deal of the background to our daily lives. This means that in virtual worlds, if there are no trees in the background, we just know something's wrong even if we can't quite put our finger on the problem. They are ubiquitous. And that means we also have a highly developed subconscious sense of what they should look like, when we aren'tum actually looking at them. With me so far? Okay, that's problem number one.

The other problem is that they are so dad-blamed complex! Even a sapling has lots of little branches and twigs and leaves and buds and stuff. If you have a polygon budget (and if you are making games, you have a polygon budget!), then these suckers will dry up that account faster than a barking moonbat can change its mind.

So, on the one hand, to have convincing trees that satiate the subconscious gamer's mind, we need to be attentive to details. And on the other hand, those very details can drag our frame rates lower than a snake's belly in a wheel rut.

To interact well with a tree means several things: When you approach it, circle it, look up into its branches and leaves, you see things properly in three-dimensional perspective.You can collide with the thicker parts like the trunk and the big lower branches, but if you fell onto a tree from above, you would likely fall a long way down through the airy upper structures before you stopped.

But unless you want to put 30,000 polygon trees into your game world, you'll have to compromise on all those fiddly details. There are ways to strip off a few thousand polygons here and there, but long before you get anywhere reasonable, your compromises start making the tree much less treelike. So then you have to pass a few edicts like this:

From this point hence, this tree, and all other trees like this tree that grace our fair land, may only be viewed from certain angles—all of the aforementioned angles being from a level on the ground to a level not exceeding the height that one man can jump.

But then you start to drain the flexibility out of your game world. What if you are standing on the back of a truck? Or on a nearby hill? Or flying overhead in an ornithopter? Well, you can't do any of those things if you really want to save the trees!

And don't even talk to me about having forests of these things in a game. Though there are ways to make trees look absolutely stunning from a distance, and only take two polygons to accomplish the task! Using a technique called billboarding we can create trees that look great from any angle as long we are at least moderately far from the tree—say a couple dozen meters or more. But up close they are nothing but flat planes that turn to always face you. You can't look up into them from below and search for robin's nests. You can't climb them. And you certainly can't fall into them from above! I mean, what fun is that?

So why all the blather, you ask? Well, I'll tell you. We're going to look at modeling some game-friendly trees in this section, and I want you to enter into this prepared, understanding why I'm going to show you two different ways to model a tree. There are other ways, but the two represent the opposite extremes. First, we'll create a "normal" low-polygon solid tree with a collision mesh. One that you could potentially climb using appropriate program code. One that you could actually get beneath and peer up into. It won't look all that great, but it will look like a tree. After that, we'll create a billboard tree that can be used to make vast forests of trees that will actually look like forests.

The Solid Tree

The solid tree is constructed of 3D object primitives, mostly cylinders that join end to end and taper. The one we'll make won't have any leaves—it's a generic big backyard tree in the winter.

I should warn you now that we aren't going to build a megapolygon old oak tree or anything like that here. Instead, we're going to do just enough so you'll have a good idea where you can go with the model and what's involved with this approach.

Having said all that, go ahead and create a new empty document in MilkShape, and let's get crackin'.

  1. Select the Cylinder tool and set it to 4 stacks and 12 slices in the parameter boxes.

  2. Click your cursor in the Side view, and drag down and to the right to create a cylinder like the one shown in Figure 16.12.

    click to expand
    Figure 16.12: Four-stack cylinder.

  3. Still in the Side view, select the vertices in the second row from the bottom, and then use the Move tool to shift them to one side.

  4. Switch to the Top view and do the same thing, moving the vertices slightly away from being aligned with the center of the cylinder.

  5. Repeat steps 3 and 4 with the next two rows of vertices going up, one row at a time, using Figure 16.13 as a guide.

    click to expand
    Figure 16.13: Crooked cylinder.

  6. Do an incremental scaling working from the second row of vertices going up, so that you get a tapered trunk, like that shown in Figure 16.14.

    click to expand
    Figure 16.14: Crooked cylinder becomes a tree trunk.

  7. Use the Duplicate function to make a copy of the trunk.

    Tip

    If you've forgotten how to duplicate an object, I'll go over it quickly here.

    First make sure that the object to be copied has been selected in face mode. Then choose Edit, Duplicate Selection. Make sure you only do that once, and don't click your mouse in the window.

    It will look like nothing happened, but a copy was made in place. Select the Move tool and then drag the selected object to a clear area. There you go— you will have the copy, and the original object will have been left behind.

  8. Move the copy to one side. Then scale it and rotate it so that you get something that looks like Figure 16.15.

    click to expand
    Figure 16.15: Branching out.

  9. Drag the branch over to the trunk and place it with the larger end inside the bounds of the trunk.

  10. Make more copies of the branch, scaling, rotating, and tweaking them as desired, until you get something like the model shown in Figure 16.16.

    click to expand
    Figure 16.16: Adding more branches.

  11. On the Materials tab, create a new material, using C:\3DGPAi1\RESOURCES\CH16\bark.p ng as the bitmap.

  12. Name the material "bark".

  13. Select the trunk using the Groups tab, and assign the bark material to it.

  14. Assign the bark material to each of the branches. Do not select them all at once and assign the material—instead, do them one at a time.

  15. Make sure that the 3D view has been set to texture mode. You should see the textured tree there, like that in Figure 16.17.

    click to expand
    Figure 16.17: The textured tree.

    Okay, we'll stop there. Of course, we could go on and on, making it more detailed, and that's certainly something I encourage you to do. It's just pointlessly repetitive to do it right now. Let's move along and add a collision mesh.

  16. Create a box and position it as shown in Figure 16.18.

    click to expand
    Figure 16.18: The tree collision mesh.

  17. Rename the box, calling it "Collision".

  18. Save your work.

  19. After saving your work, choose File, Export, Torque Game Engine DTS.

  20. You want to take all the defaults (Collision Mesh should set to Bounding Box).

  21. Export the box to C:\3DGPAi1\ fps\data\shapes\organic\tree1.dts. Click Yes if you get an alert asking if you want to replace an existing file with that name.

Testing the Solid Tree

The solid tree has a collision mesh— you can't go through it. You also can't climb it as is (you could if you wrote the appropriate script code). Anyway, to test out our solid tree, do the following:

  1. Browse to C:\3DGPAi1 and click the Run fps Demo shortcut.

  2. Click Start Mission.

  3. In the Launch dialog box, make sure that the Multiplayer Mission box is cleared.

  4. Select Water World from the mission list.

  5. Click Launch.

  6. After the game loads, look around for your tree poking up from a hill behind that block where you found the Health Kit.

  7. Run over to the tree and admire your handiwork.

  8. Try to run through the tree. If you hurt your head, you know where the first-aid kit is!

The Billboard Tree

The billboard tree is the Ferrari of game trees. It looks good and is so low on polygons that if you remove one polygon, it vanishes! But it is specialized and you can't do much with it.

Create a new empty document in MilkShape.

  1. Select the Vertex tool and place three vertices in an equilateral triangle formation.

  2. Use the Face tool and create a face, as shown in Figure 16.19.

    click to expand
    Figure 16.19: The tree triangle.

  3. On the Materials tab, create a new material, using C:\3DGPAi1\RESOURCES\CH16\spruce.jpg as the bitmap.

    Note

    There is a quirk in MilkShape when using bitmaps that have alpha channels in them. Because of this, when we make a model that uses a texture with an alpha channel, like the billboard tree, we need to make another version of the texture without the alpha channel, but with the same name (not including extension). So although in the game we will use the spruce.png texture file, we need to use spruce.jpg in the MilkShape model material.

  4. Name the material "spruce: Trans". The ": Trans" part tells the exporter that the alpha channel for this material should be set to translucent mode.

  5. With the new material selected, move the right-hand slider to the middle position. This will tell the exporter to enable alpha transparency for this material.

  6. Select the triangle using the Groups tab, and assign the spruce material to it.

  7. Make sure that the 3D view has been set to Texture mode. You should see the textured tree there, as shown in Figure 16.20.

    click to expand
    Figure 16.20: The textured tree.

  8. Rename the triangle in the Groups tab as "firstface: BillboardZ". This identifies the polygon as a billboard-z type.

  9. Create a special material. This is a material that tells the exporter how to do certain things. The directive is embedded in the name—in this case call the material "opt:size=100". There is nothing else needed for the material. This directive tells the exporter to scale the object up by 100 times.

    Note

    A billboard is a single polygon object with a texture that always faces toward the player in the game. Some kinds of billboards only face the player horizontally. If you get above them, they don't face up. These are called billboard-z.

  10. Save your work.

  11. After saving your work, choose File, Export, Torque Game Engine DTS.

  12. You want to take all the defaults (Collision Mesh should be set to None).

  13. Export the box to C:\3DGPAi1\fps\data\shapes\organic\tree2.dts. Click Yes if you get an alert asking if you want to replace an existing file with that name.

Testing the Billboard Tree

The billboard tree has no collision mesh—you can't bump the tree but instead you just go right through it. You also can't climb it, though you probably wouldn't want to.

  1. Browse to C:\3DGPAi1 and click the Run fps Demo shortcut.

  2. Click Start Mission.

  3. In the Launch dialog box, make sure that the Multiplayer Mission box is cleared.

  4. Select Water World from the mission list.

  5. Click Launch.

  6. After the game loads, your tree is actually a group of trees poking up from a hill behind that block where you found the Health Kit.

  7. Run over to the trees and take a look.

  8. Try to run through any of the trees. If you hurt your head, go back and check your model! You may have inadvertently left a collision mesh in it.

As you can see, the billboard tree is kind to your video card and will keep your frame rates right up there. In fact, you can build forests of these things and only have as many polygons as one of the solid trees!




3D Game Programming All in One
3D Game Programming All in One (Course Technology PTR Game Development Series)
ISBN: 159200136X
EAN: 2147483647
Year: 2006
Pages: 197

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net