Creating a 3D scene

Now it’s time to go 3D! We start by creating a simple 3D game level in i.e. 3ds max. If you don’t have 3ds max, any other 3D software that supports to export to the COLLADA (.dae) format will work. If you don’t want to make your own level, you can download the data files used in this example from the link at the bottom of this page.

Creating a simple game level in 3ds max

In this example we’ve created a level with both diffuse map and an ambient lightmap for each object and exported it as “level.dae” into the data-folder of our project together with all the required textures.

Loading the 3D scene

Loading the 3D scene is pretty straight foreward using the RMS. (If you don’t understand this syntax, please refer to the previous part of this tutorial).

RScene level("level.dae{Path=data}");

Loading a shader

This step is optional. Shaders are small programs that run on the GPU and produce the vertices used in geometry and the pixel colors outputed when rendering an image. We won’t cover this in detail here. To make sure the lightmaps are displayed correctly, we have written a lightmap shader and called it shader.cg (You’ll find it together with the source code downloadable from the bottom of the page). We’ll load it now, and remember later to set it as the program used by SimpleRenderer later. If you don’t specify a shader, SimpleRenderer will generate one for you that only supports diffuse maps.

RProgram shader("data/shader.cg");

SimpleRenderer

SimpleRenderer is a class that handles rendering of RScene-objects. It is a very simple renderer that performs no culling and no post-processing of any kind. It is only recommended for learning and prototyping. It allows you to change shader and control the camera - that’s it. But that’s also all we need for now :) Let’s initialize it:

SimpleRenderer renderer(setup.GetGraphicsDevice());
renderer.SetShader(shader); // Optional, only needed if you want to use a custom shader
renderer.GetCamera()->SetPosition(Math::Vector3(0, 0, 5)); // Set initial camera position

Now we are done loading and setting up. The rest of the code should go inside the main loop.

Drawing the scene

Start by clearing the screen. Then, the actual rendering of the scene is very easy:

renderer.Render(setup.GetState(), level);

And you’re done :D

SimpleRenderer

Challenges

  • Let the player control moving and strafing with the keyboard (Hint 1: See the Utils::Camera class documentation)
  • Let the player control moving the camera with the mouse (Hint 2: See the Mouse::Lock() function)

Source code

Complete source code and data files for this tutorial can be found at http://svn2.assembla.com/svn/fwg/trunk/tutorials/simple/part5/.




FireStats icon Powered by FireStats