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

The Jade demo CreateScene() function is shown in Listing 5.1.

Listing 5.1 The JadeWin::CreateScene() function.

BOOL JadeWin::CreateScene()
{
    HRESULT r;
    //------- BACKGROUND --------
    scene->SetSceneBackgroundRGB( D3DVALUE(.2), D3DVALUE(.2),
        D3DVALUE(.2) );

    //-------- MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->SetPerspective( TRUE );
    r=meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL,
        NULL );
    if (r!=D3DRM_OK)
    {
        TRACE("meshbuilder->Load() failed\n");
        return FALSE;
    }
    ScaleMesh( meshbuilder, D3DVALUE(35) );

    //-------- TEXTURE --------
    LPDIRECT3DRMTEXTURE texture;
    HRSRC texture_id = FindResource( NULL,
        MAKEINTRESOURCE(IDR_JADETEXTURE), "TEXTURE" );
    r = d3drm->LoadTextureFromResource( texture_id, &texture );
    if (r!=D3DRM_OK)
    {
        TRACE("d3drm->LoadTextureFromResource() failed\n");
        return FALSE;
    }
    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(0.0), D3DVALUE(1.0),  // z axis of wrap
        D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0),  // y axis of wrap
        D3DVALUE(0.5), D3DVALUE(0.5),                 // texture origin
        D3DDivide(1,w), D3DDivide(1,h),               // texture scale
        &wrap);
    wrap->Apply( meshbuilder );
    wrap->Release();
    wrap=0;

    //------- MESH FRAME ----------
    LPDIRECT3DRMFRAME meshframe;
    d3drm->CreateFrame( scene, &meshframe );
    meshframe->AddVisual( meshbuilder );
    meshframe->AddMoveCallback( MoveFrame, NULL );
    meshframe->Release();

    //---------- LIGHT -----------
    LPDIRECT3DRMLIGHT light;
    d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
            D3DVALUE(1),D3DVALUE(1), D3DVALUE(1),
            &light );
    scene->AddLight( light );
    light->Release();
    light=0;

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

    return TRUE;
}

The CreateScene() function performs seven steps:

1.  The scene’s background color is changed.
2.  The mesh is loaded.
3.  The jade texture is loaded.
4.  A texture wrap is created and applied.
5.  A frame is created for placement of the mesh.
6.  An ambient light source is created.
7.  A viewport is created.

Let’s examine this function one step at a time. The first thing that CreateScene() does is change the scene’s background color using the SetSceneBackgroundRGB() function:

scene->SetSceneBackgroundRGB( D3DVALUE(.2), D3DVALUE(.2), D3DVALUE(.2) );

The SetSceneBackgroundRGB() function takes three arguments that describe the red, green, and blue components of the new background color. In this case, we are using a dark gray.

Next, the Direct3DRMMeshBuilder interface is used to load and configure the mesh:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH );
resinfo.lpType="MESH";
d3drm->CreateMeshBuilder( &meshbuilder );
meshbuilder->SetPerspective( TRUE );
r=meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL);
if (r!=D3DRM_OK)
{
   TRACE("meshbuilder->Load() failed\n");
    return FALSE;
}
ScaleMesh( meshbuilder, D3DVALUE(35) );

In order to load meshes from resources, a D3DRMLOADRESOURCE structure must be prepared. The D3DRMLOADRESOURCE structure contains three fields: hModule, lpName, and lpType. The hModule field identifies the module that contains the resource. This is useful if resources must be loaded from executables other than the calling executable, but for our purposes, we can use NULL to indicate that the resources are located in the calling program. The lpName field is used to store a value that identifies the resource that we are seeking. The lpType field is used to indicate what type of resource is to be located.


TIP:  The MESH resource type
There is nothing magic about the MESH resource type (Visual C++ doesn’t have any knowledge of Direct3D meshes). The demos in this book store meshes in resource sections, labeled MESH, to separate the mesh resources from other resources.

Once the D3DRMLOADRESOURCE structure has been prepared, the meshbuilder pointer is initialized with the Direct3DRM CreateMeshBuilder() function. Perspective correction is enabled for the new mesh with the SetPerspective() function. Perspective correction will prevent the texture that we apply to the mesh from drifting during animation (comment this line out and compile the demo to see this effect).

Next, the Load() member function is called using a pointer to the D3DRMLOADRESOURCE structure as the first argument. The third Load() argument (the D3DRMLOAD_FROMRESOURCE flag) indicates to Direct3D that we are loading a file from a resource and not from disk. The Load() return value is then checked for success. The CreateScene() function returns FALSE if the Load() function fails.


TIP:  Checking return values
Most of the Direct3D functions return an HRESULT value that indicates the function’s status. As a rule, it isn’t necessary to check the return value of every function, but it is a good idea to check functions that perform operations that may fail for practical reasons. For example, functions that load files often fail because the file cannot be located. In this case it is unlikely that the Load() function will fail because the mesh that is being loaded is part of the program’s EXE file.


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.