A Tokamak Display class using OpenGL
What is this class for?
Tokamak offers the user primitive geometries like boxes or spheres to approximate the "real" bodies. In a game, you won't display these primitives. Instead, you render complex objects made of meshes.
But during development you might get problems - the physics seem to behave strange. So wouldn't it be helpful to see exactly what Tokamak "sees" - the primitve geometries?
The class which I want to introduce here realizes this: It takes the tokamak objects and displays them. This is also very helpful when you start developing with Tokamak: You do not have to care about displaying the objects, just write some physics code. The class does the rest for you!

Please note that this class does not display 100% of the "Tokamak world" - for example the rigid particles are not yet supported. But for most purposes the class should work well - and is extensible for what you need. If you make any changes, please e-mail me!
Getting started
If you have not already downloaded the source code, please do this now.
Extract the ZIP file to a folder. If you are using Windows, you can start the EXE-file to see what happens:

There are several bodies (slightly blue) falling onto a little floor object (a flat box, slightly red) - calculated by Tokamak.

If you want to compile the Visual Studio 7 project, you need two libraries: If you do not have Visual Studio 7, you can either download the free "Express" edition or build the files in your favorite IDE, that should not be too hard!
The components of the project
So you have seen the program running and have compiled it succesfully - now let's look at its parts. If you are not familiar with tokamak, please read Adam Dawes' tutorials on Tokamak at http://www.adamdawes.com, then proceed.
He has done a very good job on explaining Tokamak from the ground on.
The files (and the corresponding header files) are required for the camera, which is used in all my tokamak tutorials - see my OpenGL page for further information.

I have written the files as a little contribution to the tokamak community. The TokSim.cpp file contains the OpenGL display class, the TokSpring.cpp a spring class. The first class will be explained later in the document, the second class in my third tutorial.

These 5 files mentioned above are the same in all my tutorials. Also, the main file SimpleSample.cpp (see below) is very similar in all tutorials. In order to create your own Tokamak simulation, you only need a class, inherited from CTokSim, which creates the simulation. In this tutorial this is done in SimpleTokSim.cpp.
Here is a more detailed explanation of these files:
The display results
You may have wondered, why the little bodies falling down are displayed slightly blue and the floor is slightly red, although you have not provided any display information.
The reason is simple: The class CTokSim displays rigid bodies slightly blue, animated bodies slightly red. This is helpful to get a faster overview about the scene. You cannot change these settings (without editing the CTokSim.cpp file). This class is not thought to produce good-looking results! It only displays the Tokamak simulation!
How does CTokSim work?
As explained above CTokSim is a base class for Tokamak simulations. It helps to seperate the simulation from the displaying code.
My target was, that the user (you or me) does not have to care about displaying in any way. This is the reason why I decided to put the Tokamak simulation and pointers to all rigid and animated bodies as protected member variables into the base class.
So the Display() function of CTokSim has access to the bodies and you do not have to add each body to the class.

If you look at the public member functions of CTokSim, you can see The constructor only initializes some basic values but not the simulation itsself. The destructor destroys the simulation, if it has been created before.
The Init() method initializes the tokamak simulator with numbers for the rigid and animated body counts. This method must be seen as a little helper function. You might do this as well on your own, but the code is often the same, so I added this method.
After initializing the tokamak simulation, you can add bodies. Again, I have created some little helping functions for this. The code is what you would expect it to be. As an example - here is the code of CreateRigidBody():
	neRigidBody * CTokSim::CreateRigidBody()
	{
	         if (m_RigidCount>= MAX_RIGID_COUNT)
	                 return NULL;

	         neRigidBody * rigid = m_Sim->CreateRigidBody();
	         m_Rigid[m_RigidCount++] = rigid;
	         return rigid;
	}

The heart of CTokSim is its Display() function. It works in four steps:
I have decided to use a very simple method to draw the mesh: Just render it solid and as wireframe, both without lighting. Lighting would mean that you have to calculate normal vectors for each vertex of the mesh which means some effort. Because I didn't find a method to query the mesh from Tokamak, I have added the method "SetTerrainMesh(...)". You have to use this method if you want CTokSim to display the mesh. It copies the vertices and indices to an OpenGL vertex array.
To display the rigid and the animated bodies is more or less the same: Displaying boxes and spheres is quite simple if you query their size. The tokamak cylinders, which are capsules, can be displayed by drawing a "real" cylinder and adding two half spheres on the top and on the bottom. I draw simply complete spheres as the other half disappears within the cylinder.

The detail level of the spheres and cylinder is defined in line 11 of TokSim.cpp. The higher this value is set, the more polygons are used to render the spheres and cylinders. I have chosen quite a low level - which is faster and helps you to see, if the bodies are rotating (you cannot see this at a lit, one-colored, "perfect" sphere!).

Springs are displayed as simple lines between their connection points. The color is dependent from their length. More about springs in the Spring tutorial.

**************************************************************************

>>> continue to the joint tutorial

**************************************************************************

Any comments? Conact me!

philipp.crocoll@web.de
www.codecolony.de