<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html401/loose.dtd"> <html> <!-- Created by texi2html 1.76 --> <!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others. Maintained by: Many creative people <dev@texi2html.cvshome.org> Send bugs and suggestions to <users@texi2html.cvshome.org> --> <head> <title>Crystal Space 1.2.1: 4.2.1.4 The Camera</title> <meta name="description" content="Crystal Space 1.2.1: 4.2.1.4 The Camera"> <meta name="keywords" content="Crystal Space 1.2.1: 4.2.1.4 The Camera"> <meta name="resource-type" content="document"> <meta name="distribution" content="global"> <meta name="Generator" content="texi2html 1.76"> <meta http-equiv="Content-Type" content="text/html; charset=us-ascii"> <style type="text/css"> <!-- a.summary-letter {text-decoration: none} pre.display {font-family: serif} pre.format {font-family: serif} pre.menu-comment {font-family: serif} pre.menu-preformatted {font-family: serif} pre.smalldisplay {font-family: serif; font-size: smaller} pre.smallexample {font-size: smaller} pre.smallformat {font-family: serif; font-size: smaller} pre.smalllisp {font-size: smaller} span.sansserif {font-family:sans-serif; font-weight:normal;} ul.toc {list-style: none} --> </style> </head> <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000"> <a name="Simple-Camera"></a> <a name="0"></a> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="Simple-World.html#0" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="Simple-Locomotion.html#0" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="Using-Crystal-Space.html#0" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="Tutorial-Simple.html#0" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Working-with-Engine-Content.html#0" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="index.html#SEC_Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="cs_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="cs_Index.html#0" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="cs_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <hr size="1"> <h4 class="subsubsection"> 4.2.1.4 The Camera </h4> <p>In Crystal Space there is an interface called <samp>‘iView’</samp> which encapsulates both <samp>‘iCamera’</samp> and <samp>‘iClipper2D’</samp> instances. In principle you can use those classes directly but using <samp>‘iView’</samp> is easier. Now, edit <tt>‘simple.h’</tt> to make use of <samp>‘iView’</samp>: </p> <table><tr><td> </td><td><pre class="example">... class Simple { private: ... csRef<iView> view; ... void ProcessFrame (); void FinishFrame (); ... </pre></td></tr></table> <p>Then, edit <tt>‘simple.cpp’</tt> and make the following changes at the end of our <code>Application()</code> function: </p> <table><tr><td> </td><td><pre class="example">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; } </pre></td></tr></table> <p>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 <code>SetSector()</code>. The camera also has a position in that sector which you can set by first getting the camera with <code>GetCamera()</code> and then setting the position (which is a <samp>‘csVector3’</samp>) with <code>SetPosition()</code>. 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 <code>SetRectangle()</code>. </p> <p>The call to create a new view is a bit special. See the discussion on smart pointers for a detailed discussion (see section <a href="Smart-Pointers.html#0">Correctly Using Smart Pointers</a>). </p> <p>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 <code>ProcessFrame()</code> and <code>FinishFrame()</code>. 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: </p> <table><tr><td> </td><td><pre class="example">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); } </pre></td></tr></table> <p>Drawing the screen is done in two steps. First there is the part that is done in <code>ProcessFrame()</code>. Here, we will actually fill the display. In this case we let the engine do most of that work by calling <code>view->Draw()</code>. But, in principle, you can do any kind of drawing here. </p> <p>In <code>ProcessFrame()</code> 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 <em>must</em> call <code>engine->GetBeginDrawFlags()</code> to get these flags and bitwise-or them with the ones that you want. </p> <p>The second part is in <code>FinishFrame()</code> 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 <code>ProcessFrame()</code>. When a frame needs to be rendered, the Crystal Space framework will send four messages: </p> <ul> <li> <samp>‘csevPreProcess’</samp> is sent first. This allows plugins to preprocess things before drawing can happen. </li><li> <samp>‘csevProcess’</samp> follows. In this pass the application will render. </li><li> <samp>‘csevPostProcess’</samp> 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. </li><li> <samp>‘csevFinalProcess’</samp> is last. In this pass the application will display the frame on screen. </li></ul> <p>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. </p> <hr size="1"> <table cellpadding="1" cellspacing="1" border="0"> <tr><td valign="middle" align="left">[<a href="Simple-World.html#0" title="Previous section in reading order"> < </a>]</td> <td valign="middle" align="left">[<a href="Simple-Locomotion.html#0" title="Next section in reading order"> > </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="Using-Crystal-Space.html#0" title="Beginning of this chapter or previous chapter"> << </a>]</td> <td valign="middle" align="left">[<a href="Tutorial-Simple.html#0" title="Up section"> Up </a>]</td> <td valign="middle" align="left">[<a href="Working-with-Engine-Content.html#0" title="Next chapter"> >> </a>]</td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left"> </td> <td valign="middle" align="left">[<a href="index.html#SEC_Top" title="Cover (top) of document">Top</a>]</td> <td valign="middle" align="left">[<a href="cs_toc.html#SEC_Contents" title="Table of contents">Contents</a>]</td> <td valign="middle" align="left">[<a href="cs_Index.html#0" title="Index">Index</a>]</td> <td valign="middle" align="left">[<a href="cs_abt.html#SEC_About" title="About (help)"> ? </a>]</td> </tr></table> <p> <font size="-1"> This document was generated using <a href="http://texi2html.cvshome.org/"><em>texi2html 1.76</em></a>. </font> <br> </p> </body> </html>