|  | Blitz3D Intermediates: Blitz-Tokamak Static Meshs
by Bot Builder 
 
This tutorial assumes you have either read the Blitz-Tokamak Basics Tutorial or possess equivalent knowledge.
 
 IntroductionThe purpose of this tutorial is to show how static meshes work in tokamak, and to provide some functions I wrote to make it easier to use.  In the last tutorial, I mentioned animated bodies once or twice. They are animated by the program, and are not affected by physics.  You might think that you will have to tediously construct your level out of animated bodies, but thankfully you can use a handy feature of tokamak called a static mesh.  A static mesh is generally your level, and maybe some other details.  You pass these object to tokamak which involves it in the physics simulation.  This article might look big, but don't worry, most of it is explanation of how my functions work, and if you like you can just skip over that.
 Useful functionsThese are some functions I wrote to make tokamak static meshes easier to create. They allow you to use multiple entities, with multiple surfaces, and hierarchal meshes in your static mesh.These functions are part of a bigger function set found here. Please inform me if you find an error.  If you have questions on usage of these functions, post here.  Note that variable and array initializations must occur before any functions are used.
 
UsageThese commands are pretty easy to use. First, setup the base scene.
 This is just the same stuff we learned last tutorial, with maybe a bit of modification etc. As you might notice, this is part of the "TOKA_TERRAIN.BB" that came with tokamak wrapper v03, even though our version will use my functions rather than being hard-coded.
 Here is the code you need to place somewhere after TOKSIM_CreateSimulator and before the main loop, so basically the loading area:
 That's all! Now the terrain/level is in the physical scene. If you don't have the terrain.3ds mesh, either get your own or Click Here
 Here's a basic rundown on the commands:
 
Call TOK_SetTextureMaterial to set a texture as a material. So, for instance if you had a metal.png texture you wanted to be assigned to material 4, you would call TOK_SetTextureMaterial("metal.png",4). Note that if you want a material to be set, you must call it before using Tok_AddMesh.Tok_AddMesh adds a blitz mesh to the tokamak staticmesh. Set recurse to 1 in order to recurse through a hierarchal mesh. Set mat to a value greater than -1 and texture assotiations are ignored, and the value of mat is used for the whole mesh. A value for mat of -2 and down means that the material will be used if a texture assotiations doesn't exist. The material that will be used in this case is -2-mat. So if mat is -5, and surface 1 doesn't have a material associated texture, the material index will be 3. Set Texture index to set which texture in a multi-textured mesh will determine the material properties.
TOK_SetMesh finalizes mesh creation, sends it over to tokamak, and frees the banks.TOK_ReInitializeBanks re-creates the  banks and sets the vars, so you can say, load a new level up.StripPath$ is a function I got from Peter Scheutz in "B3D text file format". You can use it in main proggie if you like.How it worksThis is just an extra section you can safely skip. It's just an explanation of how my functions work.
 
TOK_SetTextureMaterial - All this does is store the texture name in an array, and the material index for later recovery.TOK_AddMesh - This is the main function, and is complex, so I'm going to do it by sections:
This resizes the banks holding triangle and vertex data according to the size of the mesh. Since multiple meshes could be added, it has to add vertices to the proper spot, and add triangles with proper vertex indices.
 
 This is the start of the loop, and it also checks if the texture of the surface matches one of the ones given by TOK_SetTextureMaterial, and sets the variable mindex to the material ID of the texture.
 
 This stores the number of vertices and triangles in this surface in two variables.  One thing to note is the variable tric, which now equals one after the last vertex added to the static mesh. This will be later added to vertex indices in the mesh to get the global index.
 
 This loops through all the vertices in the mesh and writes their information to the bank. Currently, each vertex is 16 bytes, 4 floats.  The first 3 are the coordinates and the last is currently unused.  Notice that A tformpoint command is used beforehand. This is so that the mesh can be positioned, rotated, and scaled before adding it to the static mesh.  The variable offsetv stores the current byte offset inside the vertex bank.
 
 This loops through the triangles in the mesh, writing them to the triangle bank. Eact triangle is 24 bytes, or 6 integers. The First 3 are the vertice index.  The vertex index reported by blitz is added to tric, as before mentioned to get the global index which is written to the bank.  On the next line, it determines what material to use.  The last 8 bytes are currently unused.  The variable offsett stores the current byte offset inside the triangle bank.
 
 This recurses throughout the entities children calling TOK_AddMesh, if the parameter recurse is set to True.
TOK_SetMesh - This simply passes the banks to tokamak using TOKSIM_SetStaticMesh, and then frees the banks.
TOK_ReInitializeBanks - This sets all the necessary global vars to 0 and recreates the banks.
Final Product:I could probably take out some of the unused Tok_* funtions, but eh.  If you don't have the terrain.3ds mesh, either get your own or Click Here
 Have fun with static meshes!
 For a printable copy of this article, please click HERE.
 
   
 
 |