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


The ZoomWin::CreateScene() Function

The Zoom demo’s scene is constructed by the CreateScene() function. The function appears in Listing 9.1.

Listing 9.1 The ZoomWin::CreateScene() function.

BOOL ZoomWin::CreateScene()
{
    // ------- MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_Z_MESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    meshbuilder->SetColorRGB(D3DVALUE(.67),
            D3DVALUE(.82), D3DVALUE(.94) );
    ScaleMesh( meshbuilder, D3DVALUE(25) );

    // ------- MATERIAL ----------
    LPDIRECT3DRMMATERIAL material;
    d3drm->CreateMaterial( D3DVALUE(10), &material );
    meshbuilder->SetMaterial( material );
    material->Release();
    material=0;

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

    // ------- ANIMATION --------
    d3drm->CreateAnimation( &animation );
    animation->SetOptions(  D3DRMANIMATION_SPLINEPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION );
    animation->AddPositionKey( D3DVALUE(0),
            D3DVALUE(.4), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(24),
            D3DVALUE(5), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(49),
            D3DVALUE(1), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(74),
            D3DVALUE(5), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(99),
            D3DVALUE(.4), D3DVALUE(0), D3DVALUE(0) );

    d3drm->CreateFrame( scene, &zoomframe );
    animation->SetFrame( zoomframe );

    // --------- LIGHT --------
    LPDIRECT3DRMLIGHT dlight;
    d3drm->CreateLightRGB( D3DRMLIGHT_DIRECTIONAL,
            D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
            &dlight );

    LPDIRECT3DRMLIGHT alight;
    d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
            D3DVALUE(.40), D3DVALUE(.40), D3DVALUE(.40),
            &alight );
    LPDIRECT3DRMFRAME lightframe;
    d3drm->CreateFrame( scene, &lightframe );
    lightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );

    lightframe->AddLight( dlight );
    lightframe->AddLight( alight );

    dlight->Release();
    dlight=0;
    alight->Release();
    alight=0;
    lightframe->Release();
    lightframe=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 );
    camera->AddMoveCallback( AdjustField, NULL );
    return TRUE;
}

The CreateScene() function performs six steps:

1.  Creates and loads a mesh
2.  Creates and applies a material to the mesh
3.  Creates a frame for the mesh
4.  Constructs an animation sequence that will be used to control the viewport’s field-of-view
5.  Creates two light sources
6.  Creates a viewport and installs a callback function

The first step uses the Direct3DRMMeshBuilder interface to load a mesh from a file. Let’s take a closer look:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_Z_MESH );
resinfo.lpType="MESH";
meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL );
meshbuilder->SetColorRGB(D3DVALUE(.67), D3DVALUE(.82), D3DVALUE(.94) );
ScaleMesh( meshbuilder, D3DVALUE(25) );

A D3DRMLOADRESOURCE is used to identify the resource entry that contains the mesh. The meshbuilder data member is initialized with the Direct3DRM CreateMeshBuilder() function. The Load() function is then used to load the mesh. The color of the mesh is assigned with the SetColorRGB() function. Finally, the mesh is scaled to 25 units with the ScaleMesh() function.

Next, a material is applied to the mesh:

LPDIRECT3DRMMATERIAL material;
d3drm->CreateMaterial( D3DVALUE(10), &material );
meshbuilder->SetMaterial( material );
material->Release();
material=0;

Materials allow us to fine-tune the mesh’s appearance by changing the behavior and color of specular highlights. In this case, we are using a material to give the mesh a shiny appearance.


TIP:  Testing material settings
Xpose is an elaborate object viewer that was created using the techniques in this book. Both it and the code to create it are on the book’s CD-ROM. The fastest way to learn about material settings and how they affect a mesh’s appearance is to use Xpose to experiment. The Xpose Material Settings dialog allows you to change material settings and see the results immediately.

The Direct3DRM CreateMaterial() function is used to create the material. The new material is applied to the mesh with the Direct3DRMMeshBuilder SetMaterial() member function. The local material pointer is then released.

Step 3 is the creation of a frame for the mesh:

LPDIRECT3DRMFRAME meshframe;
d3drm->CreateFrame( scene, &meshframe );
meshframe->SetRotation( scene,
        D3DVALUE(1), D3DVALUE(.4), D3DVALUE(0),
        D3DVALUE(.1) );
meshframe->AddVisual( meshbuilder );
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.