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 SpotlightWin::CreateScene() Function

The Spotlight demo’s CreateScene() function is responsible for creating the meshes, the spotlight, and installing a callback function that animates the spotlight. The SpotlightWin::CreateScene() function appears in Listing 6.5.

Listing 6.5 The SpotlightWin::CreateScene() function.

BOOL SpotlightWin::CreateScene()
{
    //----- MESHES --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
    resinfo.lpType="MESH";
    LPDIRECT3DRMMESHBUILDER builder;
    d3drm->CreateMeshBuilder( &builder );
    builder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    builder->SetColorRGB(D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.0) );
    builder->CreateMesh( &mesh1 );
    builder->SetColorRGB(D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0) );
    builder->CreateMesh( &mesh2 );
    builder->SetColorRGB(D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0) );
    builder->CreateMesh( &mesh3 );
    builder->Release();
    builder=0;

    //----- MESH FRAMES --------
    LPDIRECT3DRMFRAME frame1;
    d3drm->CreateFrame( scene, &frame1 );
    frame1->SetPosition( scene,
            D3DVALUE(-2), D3DVALUE(0), D3DVALUE(0) );
    frame1->AddVisual( mesh1 );
    frame1->Release();
    frame1=0;

    LPDIRECT3DRMFRAME frame2;
    d3drm->CreateFrame( scene, &frame2 );
    frame2->SetPosition(scene,
            D3DVALUE(2), D3DVALUE(0), D3DVALUE(0) );
    frame2->AddVisual( mesh2 );
    frame2->Release();
    frame2=0;

    LPDIRECT3DRMFRAME frame3;
    d3drm->CreateFrame( scene, &frame3 );
    frame3->SetPosition(scene,
            D3DVALUE(0), D3DVALUE(0), D3DVALUE(2) );
    frame3->AddVisual( mesh3 );
    frame3->Release();
    frame3=0;

    //------- SPOTLIGHT ------
    d3drm->CreateLightRGB( D3DRMLIGHT_SPOT,
            D3DVALUE(0.8), D3DVALUE(0.8), D3DVALUE(0.8),
            &spotlight );
    OnBeamNormal();

    //------ SPOTLIGHT FRAME --------
    LPDIRECT3DRMFRAME lightframe;
    d3drm->CreateFrame( scene, &lightframe );
    lightframe->SetPosition( scene,
            D3DVALUE(0), D3DVALUE(10), D3DVALUE(-10) );
    lightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
    lightframe->AddLight( spotlight );
    lightframe->AddMoveCallback( MoveLight, NULL );
    lightframe->Release();
    lightframe=0;

    //------ CAMERA ------
    d3drm->CreateFrame( scene, &camera );
    camera->SetPosition( scene,
            D3DVALUE(0), D3DVALUE(6), D3DVALUE(-6) );
    camera->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1.1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
    d3drm->CreateViewport( device, camera, 0, 0,
            device->GetWidth(), device->GetHeight(),
            &viewport );
    return TRUE;
}

The CreateScene() function performs five steps:

1.  Uses the Direct3DRMMeshBuilder interface to load a spherical mesh and creates three pointers to the Direct3DRMMesh interface
2.  Creates and positions a frame for each of the three meshes
3.  Creates and configures a spotlight
4.  Creates a frame for the spotlight
5.  Creates and configures a viewport

Let’s look at the first step:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
resinfo.lpType="MESH";
LPDIRECT3DRMMESHBUILDER builder;
d3drm->CreateMeshBuilder( &builder );
builder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
        NULL, NULL );
builder->SetColorRGB(D3DVALUE(1.0), D3DVALUE(0.0), D3DVALUE(0.0) );
builder->CreateMesh( &mesh1 );
builder->SetColorRGB(D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0) );
builder->CreateMesh( &mesh2 );
builder->SetColorRGB(D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0) );
builder->CreateMesh( &mesh3 );
builder->Release();
builder=0;

First, an instance of the D3DRMLOADRESOURCE structure is declared and initialized. The resinfo structure identifies the spherical mesh that we will be using.

A pointer to the Direct3DRMMeshBuilder interface called builder is then declared. The Direct3DRM CreateMeshBuilder() function is used to initialize the builder pointer. The Load() function is used to load the mesh. The address of the previously prepared resinfo structure is used as the first argument to the Load() function.

Next, the three pointers to the Direct3DRMMesh interface are initialized. Each is initialized with the Direct3DRMMeshBuilder CreateMesh() function. The meshbuilder is assigned a different color setting before each invocation of CreateMesh(). This creates three meshes, each represented by the Direct3DRMMesh interface and each a different color. Once the third mesh has been created, the builder pointer is released.

The second step that the CreateScene() function performs is the creation and positioning of three frames, one for each mesh:

LPDIRECT3DRMFRAME frame1;
d3drm->CreateFrame( scene, &frame1 );
frame1->SetPosition(scene,
        D3DVALUE(-2), D3DVALUE(0), D3DVALUE(0) );
frame1->AddVisual( mesh1 );
frame1->Release();
frame1=0;
LPDIRECT3DRMFRAME frame2;
d3drm->CreateFrame( scene, &frame2 );
frame2->SetPosition(scene,
        D3DVALUE(2), D3DVALUE(0), D3DVALUE(0) );
frame2->AddVisual( mesh2 );
frame2->Release();
frame2=0;

LPDIRECT3DRMFRAME frame3;
d3drm->CreateFrame( scene, &frame3 );
frame3->SetPosition(scene,
        D3DVALUE(0), D3DVALUE(0), D3DVALUE(2) );
frame3->AddVisual( mesh3 );
frame3->Release();
frame3=0;

Each frame is created with the Direct3DRM CreateFrame() function and then positioned in a different location. The third frame is placed behind the first two. Each of the previously created meshes is attached to one of the frames with the AddVisual() function. Each frame pointer is released once a mesh has been attached.

Step 3 is the creation of the spotlight. The code looks like this:

d3drm->CreateLightRGB( D3DRMLIGHT_SPOT,
        D3DVALUE(0.8), D3DVALUE(0.8), D3DVALUE(0.8),
        &spotlight );
OnBeamNormal();


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.