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

The SpaceDonut demo’s scene is constructed by the CreateScene() function, as shown in Listing 6.4.

Listing 6.4 The SpaceDonutWin::CreateScene() function.

BOOL SpaceDonutWin::CreateScene()
{
    // ------- DONUT MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_DONUTMESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    meshbuilder->SetPerspective( TRUE );
    meshbuilder->SetColorRGB( D3DVALUE(1), D3DVALUE(1), D3DVALUE(1) );
    ScaleMesh( meshbuilder, D3DVALUE(20) );

    //------ FROSTING TEXTURE --------
    LPDIRECT3DRMTEXTURE texture;
    HRSRC texture_id = FindResource( NULL,
            MAKEINTRESOURCE(IDR_FROSTINGTEXTURE),
            "TEXTURE" );
    d3drm->LoadTextureFromResource( texture_id, &texture );
    meshbuilder->SetTexture( texture );
    texture->Release();
    texture=0;

    //------- WRAP ----------
    D3DRMBOX box;
    meshbuilder->GetBox( &box );
    D3DVALUE w=box.max.x-box.min.x;
    D3DVALUE h=box.max.y-box.min.y;

    LPDIRECT3DRMWRAP wrap;
    d3drm->CreateWrap( D3DRMWRAP_FLAT, scene,
            D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0), // wrap origin
            D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0), // wrap z axis
            D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0), // wrap y axis
            D3DVALUE(0.5), D3DVALUE(0.5),            // texture origin
            D3DDivide(1,w), D3DDivide(1,h),          // texture scale
            &wrap );
    wrap->Apply( meshbuilder );
    wrap->Release();
    wrap=0;

    //------- DONUT FRAMES --------
    LPDIRECT3DRMFRAME leftframe;
    d3drm->CreateFrame( scene, &leftframe );
    leftframe->SetPosition( scene,
            D3DVALUE(-12), D3DVALUE(0), D3DVALUE(0) );
    leftframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(0), D3DVALUE(0), D3DVALUE(1) );
    leftframe->SetRotation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(0.1) );
    leftframe->AddVisual( meshbuilder );
    leftframe->Release();
    leftframe=0;

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

    // --------- PARALLELPOINT LIGHT --------
    LPDIRECT3DRMLIGHT light;
    d3drm->CreateLightRGB( D3DRMLIGHT_PARALLELPOINT,
            D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0),
            &light );

    LPDIRECT3DRMFRAME lightframe;
    d3drm->CreateFrame( scene, &lightframe );
    lightframe->AddLight( light );

    lightframe->Release();
    lightframe=0;
    light->Release();
    light=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 performs these six steps:

1.  Creates the donut mesh
2.  Creates and loads the donut frosting texture
3.  Applies the frosting texture to the donut mesh
4.  Creates two frames and places one on each side of the origin, attaching the donut mesh to each frame
5.  Creates a parallel point light and a frame
6.  Creates a viewport

Let’s examine the code for each of these steps. We’ll start with the first step: creating and loading the donut mesh:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_DONUTMESH );
resinfo.lpType="MESH";
d3drm->CreateMeshBuilder( &meshbuilder );
meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
        NULL, NULL );
meshbuilder->SetPerspective( TRUE );
meshbuilder->SetColorRGB( D3DVALUE(1), D3DVALUE(1), D3DVALUE(1) );
ScaleMesh( meshbuilder, D3DVALUE(20) );

This code is similar to the meshbuilder code in the other demos. The mesh that is to be loaded is identified by the resinfo structure. The IDR_ DONUTMESH constant is the resource identifier for a mesh file that has been imported into the project. The “MESH” string identifies the resource category. The meshbuilder pointer is initialized with the Direct3DRM CreateMeshBuilder() function, then the Load() function is used to load the mesh. Texture correction is enabled with the SetPerspective() function. This is optional and is used to improve the appearance of the mesh once the texture is applied.

Next, the Direct3DRMMeshBuilder SetColorRGB() function is used to set the mesh’s color to white. This is done because the donut mesh used by this demo has multicolored faces. We can set the color of all of the faces in the mesh with the SetColorRGB() function. Finally, the ScaleMesh() function is used to dictate the size of the mesh.

Step 2, creating and loading the donut frosting texture, looks like this:

LPDIRECT3DRMTEXTURE texture;
HRSRC texture_id = FindResource( NULL,
        MAKEINTRESOURCE(IDR_FROSTINGTEXTURE),
        "TEXTURE" );
d3drm->LoadTextureFromResource( texture_id, &texture );
meshbuilder->SetTexture( texture );
texture->Release();
texture=0;

The texture that is to be loaded is identified with the texture_id variable. The FindResource() function is used to specify the resource identifier (IDR_FROSTINGTEXTURE) and the resource type (“TEXTURE”).

The texture pointer is initialized with the LoadTextureFromResource() function. The new texture is associated with the meshbuilder using the SetTexture() function. Finally, the texture pointer is 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.