Creating first scene

So you are up and running? Fine. As you will soon see creating your first scene will be fairly easy task. It is all due to nGENE architecture (about which you will learn throughout the tutorial) and simple framework provided with aim to simplify demos and prototypes creation.

Ok, create new class in VS:

#include "FrameworkWin32.h"
 
using namespace nGENE::Application;
 
class MyFirstApp: public FrameworkWin32
{
public:
    MyFirstApp() {}
    ~MyFirstApp() {}
 
    void createApplication();
};

As you see our application's class derives from FrameworkWin32. This parent class is framework mentioned a while back. It provides quite a few useful methods but for now we are using only createApplication() which is responsible for creating our app and letting us add some content.

Now lets move to the definition of MyFirstApp class.

#include "MyFirstApp.h"
#include "nGENE.h"
 
void MyFirstApp::createApplication()
{
    FrameworkWin32::createApplication();
 
    SceneManager* sm = Engine::getSingleton().getSceneManager(0);
    Renderer::getSingleton().setClearColour(0, 40, 160);
 
    Camera* cam;
    cam = sm->createCamera(L"CameraFirstPerson", L"Camera");
    cam->setPosition(Vector3(0.0f, 2.0f, -5.0f));
    sm->getRootNode()->addChild(L"Camera", cam);
    Engine::getSingleton().setActiveCamera(cam);
 
    PrefabBox* box = sm->createBox(1.0f, 2.0f, 1.0f);
    box->setPosition(0.0f, 3.0f, 7.0f);
    Surface* surf = box->getSurface(L"Base");
    Material* mat = MaterialManager::getSingleton().getLibrary(L"default")->getMaterial(L"crate");
    surf->setMaterial(mat);
    sm->getRootNode()->addChild(L"Box", box);
 
    Engine::getSingleton().getRenderer().setLightEnabled(false);
}
 
int main()
{
    MyFirstApp app;
 
    app.createApplication();
    app.run();
 
    app.shutdown();
 
    return 0;
}

Not a lot of code really. Now lets look into the details to understand what's going on here.

As we have overriden virtual method createApplication() we have to first call its super class version. Unless we do that, we will need to initialize whole engine by hand what is not that easy at the beginning.

Then we obtain default Scene Manager by invoking:

SceneManager* sm = Engine::getSingleton().getSceneManager(0);

What is Scene Manager? If you are familiar with other engines think of it as of scene graph. If not - it's a container for all of the objects of the 3D world. For now it has to suffice. We will focus on Scene Managers in later parts of the tutorial.

This line in turn:

Renderer::getSingleton().setClearColour(0, 40, 160);

sets background colour to blue (Red = 0, Green = 40, Blue = 160).

After creating Scene Manager we can start creating objects of our world. nGENE is built around concept of Nodes. Instead of having one parameterized class it contains several different ones for different purposes, eg.:

  • Camera which is the viewer
  • NodeTerrain which is used for landscapes rendering
  • NodePrefab for simple objects like box or sphere
  • NodeParticleSystem for particle systems

In our case we are using only Camera and PrefabBox which derives from NodePrefab and is used for creating boxes. Ok, so lets move on. We create camera like that:

Camera* cam;
cam = sm->createCamera(L"CameraFirstPerson", L"Camera");
cam->setPosition(Vector3(0.0f, 2.0f, -5.0f));
sm->getRootNode()->addChild(L"Camera", cam);
Engine::getSingleton().setActiveCamera(cam);

All nodes in nGENE are created using this sm->createSomething(parameters) manner. At least the default ones. In this case we pass "CameraFirstPerson" and "Camera" as the arguments. The first specifies the type of the camera, for now its FPP (but there are other as well), the latter the name of the node. All nodes have to have unique names or otherwise their identification will be impossible. Next we set camera position in 3D space: x = 0, y =2, z = -5. Note that nGENE uses Left Handed coordinate system so positive "Zs" are in "front". After that we add this new node to the scene (otherwise it will be not accessible as creation method only allocated memory and did initialization). Finally we set this camera as the active one. What for? It is possible in nGENE to have multiple cameras and switch between them constantly. It might be useful when simulating monitoring system in a game. You can have dozens of cameras in the level and then switch between them in the security room.

Next we are creating a box:

PrefabBox* box = sm->createBox(1.0f, 2.0f, 1.0f);
box->setPosition(0.0f, 3.0f, 7.0f);
Surface* surf = box->getSurface(L"Base");
Material* mat = MaterialManager::getSingleton().getLibrary(L"default")->getMaterial(L"crate");
surf->setMaterial(mat);
sm->getRootNode()->addChild(L"Box", box);

First two lines should be easy now, so we'll skip them. Then we obtain Surface from the newly created box. All renderable objects consist of Surfaces - a Surface is just a set of vertices and indices sharing some common attributes, like Material. To make surface visible we have to assign to it a Material (Texture + Shaders). For that we get one named "crate" from the default material library provided with nGENE and then assign it. Finally we add the box to the scene.

There is one last line:

Engine::getSingleton().getRenderer().setLightEnabled(false);

Using this call we turn lightning off as we added no lights to the scene and otherwise everything would be black. That's all about createApplication() method. In main() we just create our application, run it and when we're done - close it.

Ok lets now compile it and see what we get:

Tut_1_2_1.jpg

Not much but it's only the beginning!

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License