Spinning cube
In the previous tutorial you’ve seen how you can render a blank screen to your form. This is not like a game or 3D application yet, since we didn’t make any 3D things yet. In this tutorial we’ll make a cube that will be spinning around.

What do we need to get a spinning cube?
First of all, you’ll need to know something about the structure of Truevision3D (i’ll call it TV from now on).
- The first thing you’ll need in TV is a scene. The scene of TV is called TVScene. A scene is basically what you would call a world in the real world. It contains all different kind of things just like the real world does. In a scene, there are a couple things that are important. First there are meshes. A mesh is a static object in the real world. For example a table or a chair. They are movable, but they won’t deform/animate (except in a fairy-tale). There are a lot of other things in the scene, but I won’t go into depth about them just now.
- The second thing you’ll need is a mesh. I’ve explained what they are and will go into depth later in this tutorial. The mesh object of TV is called TVMesh.
- Furthermore we’ll need a camera. How else would we be able to see things in our 3D world without a camera? The camera object of TV is the TVCamera.
- Last but not least we need a variable to track how much time has passed since the last frame got drawn. We want to make a spinning cube, but we don’t want it to spin at a different speed on every computer. The loop we’re using will cause the cube to spin really fast on fast computers, and slow on slow computers if we add an angle to it every frame, since fast computers will have a lot of frames per second. That’s why we add the time that passed since the last frame, multiplied by a constant value. So
How do we setup the engine and other things?
You’ve seen the initialisation of the engine itself in the previous tutorial, so i won’t go over that again. The new objects do need a bit of explanation though.
This is the instantiation of the scene object. We don’t need to initialize anything in the scene, because all the settings of the scene are good by default.
This is the instantiation of the camera object. Later in the code you’ll see a line
This line is making the camera object move 20 units back on the Z-axis. This means it’s getting 20 units backwards, because the axis system in TV is probably different than what you would expect. The xz-plane is the ground plane, in contrary to some modeling programs, where xy is the ground. So in TV the y axis is the upward axis, remember that!
This is the instantiation of the cube/mesh object. This seems a bit weird, since we never made a TVMesh object at all did we? Well yea, we did. Or actually the scene object made a TVMesh for us when we called the CreateMeshBuilder method. This method creates a TVMesh that we can use. The method has an optional string parameter, which defines the name of the mesh so we can search it later on. It is really important to make a mesh object in this way. The scene needs to know which meshes there are in it, that’s why you should always create the mesh with the scene.CreateMeshBuilder() command. So TVMesh temp = new TVMesh() won’t work, it will make your program crash, and we don’t want that, do we?
Now for the more important part, we want to see a 3D cube! We can make one by caling
This will make a box which is 5 units by 5 units by 5 units big, at position (0,0,0). The first 3 parameters are the size parameters and the 4th parameter is a pretty difficult one to explain. If you make that parameter true, then the box will be consisting of 6 groups. Now, what is a group? A group is basicly a set of polygons that have the same group number. If you split the box in 6 groups, you’ll have the 6 faces split in 6 groups. That means you can apply a different texture to every face (we’ll get to that in the next tutorial).
How do we render this?
The rendering of the cube is not that hard. It’s basically 1 line of code more than the previous tutorial. It’s this line:
This is really easy, but the technical background is a bit bigger than with the last tutorial. To simplify it a bit, the engine looks which points of the cube are on the camera, what distance they are, what color they are. Then it draws the area on the already black background (it’s black because we called TV.Clear(False)). With the TV.RenderToScreen() command, we put the screen with the cube painted on it on the form.
There is something i didn’t explain yet. The cube isn’t spinning. To do that, we made the UpdateScene() method. This method rotates the cube. We are rotating the cube by calling RotateX, RotateY and RotateZ. Earlier i told you we wanted to do this: angle = angle + timeelapsed*constant. In TV, this is really easy. The Rotate X, Y and Z have 2 parameters, the first one is the angle, the second one is a boolean that makes the rotation relative to the previous rotation. That means that if we rotate the cube 45° and we rotate it 45° again, it’ll be rotated 90° if the boolean is true, but it will be rotated 45° when the boolean is false. That’s all that’s needed right now to spin the cube, just 3 lines:
Cube.RotateY(0.003f * fTime, true);
Cube.RotateZ(0.00003f * fTime, true);
Things you can add/change yourself
- Of course a cube is pretty boring. It’s easy to understand though. The TVMesh object also has a way of creating a cone. I think that’s a bit more interesting. Try making it have a radius of 2, a height of 5, precision of 12 and make it capped. It should say where to put what value in visual studio, but in case you use something else, the way i said it is the way in which you are supposed to put the parameters.
- Try messing with the rotation constants (0.0003f etc). You’ll notice the difference in rotation. You can also try to figure out how the axis are. I already explained how they are positioned, but it’s always good to find out yourself. Keep in mind that Rotate X, Y or Z will make the mesh rotate around that axis, not in the axis direction.
- Try adding another cube. It’s not too hard to do, and it’s fun! Make it smaller or bigger to your liking. Also, don’t forget to change the position of the cube. It’s on 0,0,0 on default, so you’ll need to change that. To do that, call Cube2.SetPosition(2,0,0) for example. Don’t forget to render the mesh inside the render loop ofcourse, because if you don’t do that, it won’t show up at all.
- Try moving the camera a bit (just a couple units will be enough to tell which axis are where, and we still want to see the cube to have some reference).