Setting Up a Simple Camera

The setup of a viewpoint requires a few steps, once it has been created:

  1. Setup the viewpoint axis system: this includes the viewpoint sight, top, right vectors and the viewpoint’s eye position.

  2. Setup the viewpoint projection type we’re going to use. For basic viewpoints, this is RED::VPT_PERSPECTIVE and RED::VPT_PARALLEL (we ignore custom camera projection models in this simple task);

  3. Setup a near / far clip distances. This is a mandatory step to make sure that our viewing frustum is well defined.

And that’s it! The corresponding code is detailed below:

// create and access our viewpoint's interface:
RED::Object* viewpoint = RED::Factory::CreateInstance( CID_REDViewpoint );
if( !viewpoint )
    RC_TEST( RED_ALLOC_FAILURE );

RED::IViewpoint* iviewpoint = viewpoint->As< RED::IViewpoint >();

// 1. Setup our viewpoint axis system: Define where we want to look at. This is 'target', and the direction
//    from which we look at our target. This is 'sight'. We look at our target from a 'distance':
RED::Vector3 target( 1000.0, 300.0, 0.0 );
RED::Vector3 sight( -1.0, -1.0, -1.0 );
sight.Normalize();
double distance = 500.0;

RED::Vector3 eye, top, right;

// Calculate the eye position:
eye = target - sight * distance;

// Keep the head up (along the z axis), and compute the missing axis. We must ensure that our axis system
// forms an orthographic basis (e.g. each axis is perpendicular to others):
top = RED::Vector3( 0.0, 0.0, 1.0 );
right = sight.Cross2( top );
top = right.Cross2( sight );

RC_TEST( iviewpoint->SetEye( eye, iresmgr->GetState() ) );
RC_TEST( iviewpoint->SetViewingAxis( sight, top, right, iresmgr->GetState() ) );

// 2. Let's define a perspective projection for instance, assuming we're displaying into a 1920 x 1080 pixels window:
RC_TEST( iviewpoint->SetFrustumPerspective( RED_PI / 4.0f, 1080.0 / 1920.0, iresmgr->GetState() ) );

// 3. And finally set the near / far clip distances to complete our camera definition:
RC_TEST( iviewpoint->SetNearFar( 1.0, 10000.0, iresmgr->GetState() ) );