Introduction to Meshes

Meshes are the entities you will most frequently be working with - unless you're writing something weird (please do...)!

Meshes are made up of vertices and triangles, and can be either loaded from files (perhaps the result of a 3d modeler program), or built 'by hand'. (assembled from within a B3d program in realtime)

 Loading Meshes 

There are 2 commands provided for loading meshes from files - 'LoadMesh' and 'LoadAnimMesh', both of which load a '.X' or '.3DS' file and return an entity. So what's the difference ?

Models stored in files are usually made up of several 'parts'. In the case of a character model, these parts may represent arms, legs etc.

In addition, files also contain animation information with the 'LoadAnimMesh' command. The entity returned will actually be the parent entity of the whole bunch of child 'parts'.

Therefore, 'LoadAnimMesh' really loads in several meshes !

If you don't need to animate a model, you can use 'LoadMesh' instead. This will 'collapse' all the parts in a mesh and return a single, combined mesh. The collapsed mesh will look the same as a mesh loaded with LoadAnimMesh, only you wont be able to animate it, and it wont have any child meshes.

Why bother with LoadMesh at all ? SPEED! - It's faster for B3D to deal with a single mesh than with multiple meshes, so if you're not planning to animate or doing anything tricky with a mesh, use LoadMesh.

 Creating Meshes 

Before looking closely at creating meshes, you'll need to know about 'Brushes' in Blitz3D. A brush is a collection of properties used when rendering triangles. These properties are:

To create a brush, you use the CreateBrush command:

    ;create a brush.


Once we have a brush, we can set its properties:

    ;a red brush.
    BrushColor brush,255,255,0

  a shiny red brush. 
   BrushShininess brush,1


So what has all this got to do with meshes ?

Well, when I said meshes are made up of vertices and triangles, I sort of lied !. Meshes are actually made of 'surfaces', and surfaces are made up of vertices and triangles !.

When a surface is created, you provide it with a brush that controls how all the triangles in the surface are rendered.

So, a quick overview:

Lets dive on in and create a simple mesh:

    brush=CreateBrush()                           ; create a brush
    BrushColor brush,255,0,0                  ; a red brush

    mesh=CreateMesh()                             ; create a mesh
      surf=CreateSurface( mesh,brush)    ; create a (red) surface

    AddVertex surf,-1,1,0                            ; Now, we add 4 vertices...
    AddVertex surf,1,1,0 
    AddVertex surf,1-,1,0 
    AddVertex surf,-1,-1,0

    AddTriangle surf,0,1,2                          ; and 2 triangles...
    AddTriangle surf,0,2,3

    UpdateNormals mesh 


This code will create a simple red square mesh entity.

So what's with the weird 'UpdateNormals' command at the end ?. Well, in order for a mesh to be correctly lit, its 'vertex normals' must be calculated. Without going into the gory details, the UpdateNormals command will do this for you. If you are aware of how normals work, you can actually set your own vertex normals using the VertexNormal command. If not, just remember to stick an 'UpdateNormals' command at the end of any mesh modifications you do, or else your meshes will not be correctly lit.

Note that you can create any number of surfaces you want. So, the same mesh can contain many differently coloured/textured/whatever triangles !.

Why bother with surfaces at all ?, Why not just create a bunch of separate meshes, each with its own rendering properties and skip all this surface nonsense ?. Well, again it comes down to SPEED !. It's faster for Blitz3D to handle multiple surfaces - which can NOT be moved or rotated as entities can - than it would be to handle multiple meshes.

 Modifying Meshes 

Once you've created a mesh, there are various commands available for modifying the mesh in realtime. This can be used to create a range of impressive special effects such as waving flags, rippling water and so on.

These commands are:

For more info on how to use these commands, take a look at the Vertex tutorial further on down the line.

By Paul Gerfen (GamecodingUK)
Copyright 2001 Mark Sibly/Guildhall Leisure Ltd