# 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:

- Colour - The colour a triangle is rendered in. (see BrushColor)
- Texture - From 0 to 8 texture maps that are used to render a triangle. (see BrushTexture)
- Alpha - How transparent a triangle is. Alpha can range from 0 (completely transparent) to 1 (completely opaque). (see BrushAlpha)
- Shininess - How 'shiny' a triangle is. This value can range from 0 (not shiny) to 1 (really shiny!). (see BrushShininess)
- Blend - The blend mode used to render a triangle. Blend mode describes how a triangle is combined with what's already on the screen. (see BrushBlend)
- FX - Optional special effects for rendering. (see BrushFX)

To create a brush, you use the CreateBrush command:

| ;create a brush. brush=CreateBrush() |

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:

- A mesh is made up of any number of surfaces.
- Each surface in a mesh contains any number of vertices and triangles.
- All triangles in a surface are rendered using a common brush.

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:

- VertexCoords - Changes the coordinates of a vertex.
- VertexColor - Changes the colour of a vertex.
- VertexTexCoords - Changes the texture mapping coordinates of a vertex.
- VertexNormal - Changes the normal of a vertex.

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