Crystal Space: 4.2.1.4 The Camera

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### 4.2.1.4 The Camera

In Crystal Space there is an interface called ``iView'` which encapsulates both ``iCamera'` and ``iClipper2D'` instances. In principle you can use those classes directly but using ``iView'` is easier. Now, edit ``simple.h'` to make use of ``iView'`:

    ...
    class Simple
    {
    private:
      ...
      csRef<iView> view;
      ...
      void ProcessFrame ();
      void FinishFrame ();
      ...

Then, edit ``simple.cpp'` and make the following changes at the end of our `Application()` function:

    bool Simple::Application ()
    {
      ...
      view.AttachNew(new csView (engine, g3d));
      iGraphics2D* g2d = g3d->GetDriver2D ();
      view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());
      ...
      view->GetCamera ()->SetSector (room);
      view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, -3));

      return true;
    }

So, first we create a view for our world and a particular 3D renderer. The view has a current sector which is passed to the camera and is set by `SetSector()`. The camera also has a position in that sector which you can set by first getting the camera with `GetCamera()` and then setting the position (which is a ``csVector3'`) with `SetPosition()`. The view also holds a clipping region which corresponds to the area on the window that is going to be used for drawing the world. Crystal Space supports convex polygons as viewing areas, but in this case we use a simple rectangle the same size as the window. We set this viewing rectangle with `SetRectangle()`.

The call to create a new view is a bit special. See the discussion on smart pointers for a detailed discussion (see section Correctly Using Smart Pointers).

Now, this still isn't enough. We have a camera but the camera is not used. We have to write code that actually draws the screen. We will do this in the functions `ProcessFrame()` and `FinishFrame()`. Note that Crystal Space is event driven so the actual drawing needs to be triggered by the event handler. Add the following code somewhere in the source file:

    void Simple::ProcessFrame ()
    {
      // Tell 3D driver we're going to display 3D things.
      if (!g3d->BeginDraw(
        engine->GetBeginDrawFlags() | CSDRAW_3DGRAPHICS))
        return;

      // Tell the camera to render into the frame buffer.
      view->Draw ();
    }

    void Simple::FinishFrame ()
    {
      g3d->FinishDraw ();
      g3d->Print (0);
    }

Drawing the screen is done in two steps. First there is the part that is done in `ProcessFrame()`. Here, we will actually fill the display. In this case we let the engine do most of that work by calling `view->Draw()`. But, in principle, you can do any kind of drawing here.

In `ProcessFrame()` we first have to indicate to the 3D rasterizer that we want to start drawing 3D graphics. This call makes sure that the needed buffers are set up and performs all necessary initialization. The engine often needs extra settings for this as well so you must call `engine->GetBeginDrawFlags()` to get these flags and bitwise-or them with the ones that you want.

The second part is in `FinishFrame()` where we actually dump the frame to the screen. The reason this is split is that other components (plugins) in Crystal Space may choose to listen to events and draw additional things on top of the 3D view rendered in `ProcessFrame()`. When a frame needs to be rendered, the Crystal Space framework will send four messages:

- ``csevPreProcess'` is sent first. This allows plugins to preprocess things before drawing can happen.
- ``csevProcess'` follows. In this pass the application will render.
- ``csevPostProcess'` is after that. This is the pass that can be used by external components to render on top of the view rendered by the application.
- ``csevFinalProcess'` is last. In this pass the application will display the frame on screen.

Compile and run this example. For the first time you should see something: A solid wall. Congratulations, you have created your first almost useful Crystal Space application.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ]

This document was generated using texi2html 1.76.