Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Cutting Edge Direct 3D Programming
(Publisher: The Coriolis Group)
Author(s): Stan Trujillo
ISBN: 1576100502
Publication Date: 11/01/96

Bookmark It

Search this book:
 
Previous Table of Contents Next


Constructing Scenes

At this point, we’ve initialized the standard Direct3D interfaces. The steps we’ve discussed are performed by RMApp and RMWin. The next step is to create a scene.

The CreateScene() Function

When we were looking at the CreateDevice() function, we learned that one of the last things that CreateDevice() does is call the CreateScene() function. The CreateScene() function is declared as a pure virtual member function in the RMWin class. This means that classes derived from RMWin must provide a version of CreateScene(). The CreateScene() member is responsible for the creation of any meshes, light sources, and frame hierarchies that the application will display.

Before we present the CreateScene() function, we should mention that the SampleWin class inherits several important data members from the RMWin class. The CreateScene() function has access to these data members because they are declared as protected data members. These data members are:

  d3drm: This is the pointer to the Direct3DRM interface that was created in the RMWin::OnCreate() function. We will use this pointer to create meshbuilders, lights, frames and other Direct3D objects.
  device: This is a pointer to the Direct3DRMDevice interface. It can be used to specify device settings, such as the maximum rendering quality. The device pointer is also used for creating a viewport.
  scene: The scene data member is a pointer to the Direct3DRMFrame interface and serves as the root frame for our scene. The objects we create will be attached to the scene frame.
  camera: The camera data member is also a pointer to the Direct3DRMFrame interface. Unlike the d3drm, device, and scene data members, camera is not initialized by the RMWin class. It is up to us to create the camera frame. The orientation and location that we give to the camera frame will determine the orientation and location from which the scene is viewed.
  viewport: The viewport data member is a pointer to the Direct3DRMViewport interface. This pointer, like the camera pointer, is uninitialized. We will initialize it using the Direct3DRM CreateViewport() function.

Listing 4.3 is the CreateScene() function from our sample application.

Listing 4.3 The SampleWin::CreateScene() function.

BOOL SampleWin::CreateScene()
{
    HRESULT r;
    // ------MESH--------
    d3drm->CreateMeshBuilder( &meshbuilder );
    r=meshbuilder->Load( meshname, NULL, D3DRMLOAD_FROMFILE,
            NULL, NULL );
    if (r!=D3DRM_OK)
    {
        CString msg;
        msg.Format( "Failed to load file '%s'\n", meshname );
        AfxMessageBox( msg );
        return FALSE;
    }
    ScaleMesh( meshbuilder, D3DVALUE(25) );

    LPDIRECT3DRMFRAME meshframe;
    d3drm->CreateFrame( scene, &meshframe );
    meshframe->AddVisual( meshbuilder );
    meshframe->SetRotation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(.1) );
    meshframe->Release();
    meshframe=0;

    // --------SPOT LIGHT--------
    LPDIRECT3DRMLIGHT slight;
    d3drm->CreateLightRGB( D3DRMLIGHT_SPOT,
            D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
            &slight);

    LPDIRECT3DRMFRAME slightframe;
    d3drm->CreateFrame( scene, &slightframe );
    slightframe->AddLight( slight );
    slightframe->SetPosition ( scene,
            D3DVALUE(0),D3DVALUE(20),D3DVALUE(-20) );
    slightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-20), D3DVALUE(20),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0));
    slightframe->AddMoveCallback( MoveLight, NULL );
    slight->Release();
    slight=0;
    slightframe->Release();
    slightframe=0;

    //------ CAMERA----------
    d3drm->CreateFrame( scene, &camera );
    camera->SetPosition( scene, D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50));
    d3drm->CreateViewport( device, camera, 0, 0,
            device->GetWidth(), device->GetHeight(),
            &viewport);

    return TRUE;
}

The CreateScene() function shown in Listing 4.3 performs three steps:

1.  Creates and configures a mesh
2.  Creates and configures a spotlight
3.  Creates and configures a viewport

Creating A Mesh

The first step requires that a pointer to the Direct3DRMMeshBuilder interface is created. The Direct3DRMMeshBuilder Load() member function is used to load a mesh from a file. If the Load() function fails (if the file doesn’t exist or if it is not a valid file), a message box is displayed and the function returns FALSE, signaling the application to terminate. Let’s take another look at this code:

d3drm->CreateMeshBuilder( &meshbuilder );
r=meshbuilder->Load( meshname, NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
if (r!=D3DRM_OK)
{
    CString msg;
    msg.Format( "Failed to load file '%s'\n", meshname );
    AfxMessageBox( msg );
    return FALSE;
}
ScaleMesh( meshbuilder, D3DVALUE(25) );

If the mesh loads successfully, the meshbuilder is scaled with the ScaleMesh() function. ScaleMesh() is a convenience function provided by the RMWin class. We use it here to scale the mesh to insure that it appears properly, regardless of the mesh’s original dimensions. We’ll look at the ScaleMesh() function later in this chapter.

Next, the meshbuilder is added to a frame. This portion of code appears as follows:

LPDIRECT3DRMFRAME meshframe;
d3drm->CreateFrame( scene, &meshframe );
meshframe->AddVisual( meshbuilder );
meshframe->SetRotation( scene,
        D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
        D3DVALUE(0.1) );
meshframe->Release();
meshframe=0;


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.