[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ] [ Search: ]

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 Frame ();
  ...

Then, edit ‘simple.cpp’ and make the following changes at the end of our SetupModules() function:

 
bool Simple::SetupModules ()
{
  ...
  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));
  
  printer.AttachNew (new FramePrinter (GetObjectRegistry ()));

  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::Frame ()
{
  rm->RenderView (view);
}

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

The FramePrinter instance we created earlier will take care of updating the display.

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 ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated using texi2html 1.76.