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

The SpaceStation demo is one of the simplest demos on the CD-ROM. The demo uses one mesh and one light source, and uses no callbacks or textures. The SpaceStation scene is constructed with the SpaceStationWin:: CreateScene() function, as shown in Listing 6.3.

Listing 6.3 The SpaceStationWin::CreateScene() function.

BOOL SpaceStationWin::CreateScene()
{
    // ------- MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_STATIONMESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    ScaleMesh( meshbuilder, D3DVALUE(32) );

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

    // --------- LIGHT AND FRAME --------
    LPDIRECT3DRMLIGHT light;
    d3drm->CreateLightRGB( D3DRMLIGHT_DIRECTIONAL,
            D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
            &light );

    LPDIRECT3DRMFRAME lightframe;
    d3drm->CreateFrame( scene, &lightframe );
    lightframe->AddLight( light );
    lightframe->SetOrientation( scene,
            D3DVALUE(-1), D3DVALUE(0), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
    light->Release();
    light=0;
    lightframe->Release();
    lightframe=0;

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

    return TRUE;
}

The CreateScene() function performs four steps:

1.  Creates the space station mesh
2.  Creates a frame for the space station mesh
3.  Creates the directional light source and frame
4.  Creates a viewport

The code that performs the first step looks like this:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_STATIONMESH );
resinfo.lpType="MESH";
d3drm->CreateMeshBuilder( &meshbuilder );
meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
        NULL, NULL );
ScaleMesh( meshbuilder, D3DVALUE(32) );

First, an instance of the D3DRMLOADRESOURCE structure is declared and used to store the location of the space station mesh in the program’s resources. Then, the Direct3DRM CreateMeshBuilder() function is used to initialize the meshbuilder pointer. The meshbuilder pointer is a data member of the SpaceStationWin class, so it isn’t declared in the CreateScene() function. Next, the Direct3DRMMeshBuilder Load() function is used to load the space station mesh. No error checking is provided here because the mesh is part of the program’s EXE file. Once the program has been tested (as this demo has), the Load() function’s success is virtually guaranteed. Once the Load() function returns, the ScaleMesh() function is used to specify an ideal size for the mesh.

The next step (Step 2) is the creation of a frame for the space station mesh:

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

Once the frame has been created, the space station meshbuilder (meshframe) is attached to the new frame with the Direct3DRMFrame AddVisual() function. The frame is then given a rotation attribute with the SetRotation() function. The arguments used with SetRotation() will cause the mesh to rotate around the Y axis by .05 radians per screen update. Finally, the meshframe pointer is released and assigned to zero.

Next, CreateScene() creates and configures the directional light source, and attaches it to a frame:

LPDIRECT3DRMLIGHT light;
d3drm->CreateLightRGB( D3DRMLIGHT_DIRECTIONAL,
        D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
        &light );

LPDIRECT3DRMFRAME lightframe;
d3drm->CreateFrame( scene, &lightframe );
lightframe->AddLight( light );
lightframe->SetOrientation( scene,
        D3DVALUE(-1), D3DVALUE(0), D3DVALUE(1),
        D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
light->Release();
light=0;
lightframe->Release();
lightframe=0;

A pointer to the Direct3DRMLight interface called light is declared and then initialized with the Direct3DRM CreateLightRGB() function. The D3DRMLIGHT_DIRECTIONAL constant is used to indicate the desired type of light source. The following three arguments indicate a white light color, and the final argument is the address of the light pointer.

Next, a frame for the light source is created. The lightframe pointer is initialized with the CreateFrame() function, and the light is attached to the new frame with the Direct3DRMFrame AddLight() function.

The SetOrientation() function is used to orient the light source. Notice that the forward vector (the vector defined by the first three numeric arguments) defines a vector that points away from the origin along both the X and Z axes. The negative X axis value indicates that the light will travel from left to right (from the viewer’s point of view), and the positive Z axis value indicates that the light will travel away from the viewer. Figure 6.6 illustrates the light’s direction in relation to the space station mesh (as viewed from above).


Figure 6.6  The SpaceStation scene, as seen from above.

After the light has been oriented, both the light and the lightframe pointers are released.


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.