Blitz3D Newbies: The Power of The Mesh
by Storm

Introduction:

Meshes aren't all that hard to understand. The only problem is, the Blitz help files don't fully explain it all at once. You're left in the dark about how to set it up, and what to use. That's the purpose of this tutorial: to fully explain how to use meshes.

Before I get started, I'm assuming you already know most things about Blitz3D. Things like how to create a camera, how to create and move primitives, how to texture entities, and what parent entities and handles are. I'm not going to discuss any of that in here.

Creating Meshes and Surfaces

To create a mesh, you must use the command CreateMesh. This command has an optional parent parameter, but no required parameters. It returns the mesh handle, which you'll need in order to modify it. Here's an example:


Now, you've got a mesh. But it's totally blank. If you were to put your mesh on your screen, nothing would show up. Now you have to modify your new mesh.

The next step is to add a new surface to your mesh. This requires the command AddSurface. This, of course, creates a new surface to your mesh. The only parameter for this command is the mesh it belongs to (which is required). Here's the command.


Meshes can have unlimited surfaces. But it must have at least one surface, so you can place vertices and triangles on it. We'll explain the purposes of the surface when we start texturing it.

Working with Vertexes and Triangles

Now, with our surface, we can begin our real work. Creating meshes involves using vertices (plural for vertex), and then forming triangles out of those. Only the triangles will be visible. Let's start with an example.


The parameters of this command are as follows: the surface of the vertex, the x point in 3-space, the y point in 3-space, and the z point in 3-space. Optional parameters are the texture x position, texture y position, and a non-used parameter is at the end.

This will add a vertex to the surface I've created. The vertex will be located at point 0, 0, 0 in 3-space. This is very important, because it determines what my shape will look like when finished. Spotone holds the handle of this new vertex, which we'll need when we declare the triangle.

As you probably know, it takes 3 points to create a triangle. In order to create a triangle, we must declare at least 3 vertices to use. Let's do that now.


Now I have three vertices in which to build a triangle. To create a triangle, we must use the command AddTriangle. This command is what actually creates the visible part of the mesh. It works like this:


The MySurface is, of course, the surface that you're adding a triangle to. The SpotOne, SpotTwo, and SpotThr are the three vertices that are used to create our triangle.

Updating Normals

There is one more command we need for our mesh. We use the command UpdateNormals for this. This command updates all the vertices of your mesh to allow for proper lighting. It is used like this:


Handling the Meshes

Now your mesh can be handled like any other entity.

Now we have something visible on our screen. It should be white. We'll worry about coloring this later.

Notice that if you view the triangle from the back side, you won't see it. If you were to create a sphere or a box, or any 3D object, and then put the camera inside it, you wouldn't see it either. All objects are only visible on one side. For meshes, the side that your triangle is visible on depends entirely on the AddTriangle command. More specifically, it depends on what order you list the vertices that make the triangle. Vertices that are in order of a clock-wise fashion will show up on the screen. If my mesh example is viewed from the front side, the three vertices will be shown in a clockwise manner, i.e. SpotOne SpotTwo SpotThr, and will be visible. If I were to view it from the back side, the vertices will be in a counter-clockwise order, and therefore will not show up.

To get this triangle to show up on the back side instead, we can do one of several things. We could rotate the object using the RotateEntity command. This works well for one triangle, but meshes can hold many triangles. If only one of these triangles were reversed, rotating the entity around would do no good. We also have the option of using a command called FlipMesh. This command automatically flips all the vertices in a mesh. This command works as follows:


However, while this works for an object with a single triangle, if we have more than one, it will do us no good, for then all the triangles except the one that's reversed will be facing the wrong way.

The only choice we have left is to adjust the order we gave to the AddTriangle command. We will simply have to re-order the vertices of the triangle so that they're in a clock-wise fashion when we want to see it. I find it easiest to swap the second and third two for this.

Now, suppose I wanted to have both a back side AND a front side to my mesh. To do this, all I have to do is to create a new triangle. I will change my AddTriangle command to this:


The first triangle I add is just like the earlier example. In the second one, the second and third vertices are reversed. So this rotates in a counter-clockwise fashion when viewed from the front, but in a clock-wise fashion from the rear (which is what we want).

Working with Color

Now, I'm sure you'd like to create more than white objects on the screen. You can easily color this object using the EntityColor command. Or, you can color each surface labeled by first creating a brush, and then coloring the surface. To create a brush, we can use the CreateBrush command, like this:


This will create a brush, which can be used to paint a surface. The 3 parameters determine what color the brush is, in this case, yellow. Notice that I didn't specify a surface to create a brush. We can create a brush, and apply it to as many surfaces as we want.

Now we just need to apply it to a surface. For that we need the command PaintSurface, which works like this:


This simply paints the specified surface with the specified brush.

Now, I'm sure you don't want to paint your objects with just colors of the rainbow. You want to put some real textures on there. For that, we need to use the command LoadBrush. It works a lot like the createbrush command; like this:


This will load a bitmap called brush01.bmp to be used as a texture. The command will return it's handle, which is placed in MyBrush for this example.

Now, if you paint your surface with this brush, you might get an odd color, you might not see anything at all. In any case, the surface you're trying to paint isn't the way you want it to be.

The problem is that blitz doesn't know how to lay the texture out on your mesh. The only way we can do this is to use the optional parameters in the AddVertex command, the ones dealing with the texture x and y. What we have to do is manipulate these x and y values so that the texture lays as smoothly over the mesh as possible.

Here's how we gotta do that. The texture has x and y positions on it, with the point 0, 0 at the bottom left. The top right point of the texture is point 1, 1, with every point in between these to extremes. So, the center point of the texture is at 0.5, 0.5. Now, you can go above 1, or below 0, which would cause the texture to tile in that direction. You can also reverse the direction of which way the texture moves across the points, which will flip the texture over, if we want to.

Keeping that in mind, we now must determine where on each vertex the texture crosses at. Going back to our triangle example, our vertices will now look like this:


This will allow the texture we loaded to be placed properly on the surface.

Conclusion

There are a couple of things left to remember. The more round you make a single surface, the harder it is to texture it properly. There are a few work-arounds to this. First, you can simply create a new surface. Another option is to create more points to use, so that you can divide your surface up even more. You place the points on top of each other in 3-space, and then make the triangles go to different ones.

As for actually making the models, it's a lot of work. Don't plan on making high-poly models with the mesh commands. It is possible, but you'll have to use loops of some sort, or predetemined data, or copy previous models. I have a little advice on making models, but not much. First, name your vertex points in relation to where it is on the model. It's a lot of writing, but it will save time in the long run, for you can then get a general idea of where the point is located. Second, you're going to have to frequently visualize your model in 3D. Get used to it, and try to get better at it. Third, look at your model frequently, so that you know what's going on, and what you need to do.

So that's all you need to know to build models using the mesh commands. Good luck.

-Storm


For a printable copy of this article, please click HERE.


This site is Copyright© 2000-2004, BlitzCoder. All rights reserved.