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 MoleculeWin Class

The Molecule demo provides its functionality with the MoleculeWin class:

class MoleculeWin : public RMWin
{
public:
    MoleculeWin();
    BOOL CreateScene();
protected:
    //{{AFX_MSG(MoleculeWin)
    afx_msg void OnDepth1();
    afx_msg void OnDepth2();
    afx_msg void OnDepth3();
    afx_msg void OnDepth4();
    afx_msg void OnDepth5();
    afx_msg void OnDepth6();
    afx_msg void OnChildren1();
    afx_msg void OnChildren2();
    afx_msg void OnChildren3();
    afx_msg void OnChildren4();
    afx_msg void OnUpdateChildren1(CCmdUI* pCmdUI);
    afx_msg void OnUpdateChildren2(CCmdUI* pCmdUI);
    afx_msg void OnUpdateChildren3(CCmdUI* pCmdUI);
    afx_msg void OnUpdateChildren4(CCmdUI* pCmdUI);
    afx_msg void OnUpdateDepth1(CCmdUI* pCmdUI);
    afx_msg void OnUpdateDepth2(CCmdUI* pCmdUI);
    afx_msg void OnUpdateDepth3(CCmdUI* pCmdUI);
    afx_msg void OnUpdateDepth4(CCmdUI* pCmdUI);
    afx_msg void OnUpdateDepth5(CCmdUI* pCmdUI);
    afx_msg void OnUpdateDepth6(CCmdUI* pCmdUI);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    BOOL CreateHierarchy();
    BOOL CreateChildren(LPDIRECT3DRMFRAME frame, int depth);
private:
    LPDIRECT3DRMMESH mesh[MAXDEPTH];
    int curdepth;
    int numchildren;
    int framecount;
};

The class declares two public functions: a constructor and the CreateScene() function. The constructor is used to initialize the class’s data members. The CreateScene() function constructs the demo’s initial scene. We’ll look at CreateScene() soon.

Twenty protected message-handling functions have been installed using ClassWizard. These functions provide functionality for the demo’s Depth and Children menus.

Two private member functions are declared: CreateHierarchy() and CreateChildren(). The CreateHierarchy() function is responsible for the construction of frame hierarchies given the demo’s current settings. This function is called at program startup and whenever one of the hierarchy settings is changed.

The CreateHierarchy() function uses the CreateChildren() function to create hierarchies. The CreateChildren() function is a recursive function that adds child frames to an existing frame.

Finally, three data members are declared:

LPDIRECT3DRMMESH mesh[MAXDEPTH];
int curdepth;
int numchildren;

The mesh data member is an array of pointers to the Direct3DRMMesh interface. We’ll use this array to store a mesh for each frame depth. The demo creates six meshes—one for each possible depth. Multiple instances of each mesh are displayed when a mesh is attached to multiple frames.

The curdepth and numchildren data members store the demo’s current hierarchy settings. These data members are modified by the Depth and Children menu command handler functions, and are used by the CreateHierarchy() and CreateChildren() functions. These data members are initialized by the MoleculeWin constructor as shown:

MoleculeWin::MoleculeWin()
{
    curdepth=4;
    numchildren=2;
}

The MoleculeWin::CreateScene() Function

The Molecule demo’s initial scene is constructed by the CreateScene() function. The function appears in Listing 7.1.

Listing 7.1 The MoleculeWin::CreateScene() function.

BOOL MoleculeWin::CreateScene()
{
    // ------- SRAND --------
    srand((unsigned)time(NULL));

    // ------- MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
    resinfo.lpType="MESH";
    LPDIRECT3DRMMESHBUILDER meshbuilder;
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );

    for (int i=0;i<MAXDEPTH;i++)
    {
        ScaleMesh( meshbuilder, D3DVALUE(MAXDEPTH-i) );
        D3DCOLOR clr=meshcolor[i];
        D3DVALUE r=D3DRMColorGetRed( clr );
        D3DVALUE g=D3DRMColorGetGreen( clr );
        D3DVALUE b=D3DRMColorGetBlue( clr );
        meshbuilder->SetColorRGB( r, g, b );
        LPDIRECT3DRMMESH m;
        meshbuilder->CreateMesh( &m );
        mesh[i]=m;
    }

    meshbuilder->Release();
    meshbuilder=0;

    // -------- FRAME HIERARCHY ------
    CreateHierarchy();

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

    LPDIRECT3DRMFRAME dlightframe;
    d3drm->CreateFrame( scene, &dlightframe );
    dlightframe->AddLight( dlight );
    dlightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0));
    dlight->Release();
    dlight=0;
    dlightframe->Release();
    dlightframe=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 five steps:

1.  Initializes a random number generator
2.  Creates six meshes
3.  Creates the frame hierarchy, complete with attached meshes
4.  Creates a light source
5.  Creates a viewport

First, the srand() function is called using the return value from the time() function as an argument. This is done to initialize the random number generator. The Molecule demo uses the rand() function to add random characteristics to the frame hierarchies. Because we are initializing the random generator with a value that changes each time the demo is executed, the frame hierarchy attributes (such as rotation axes and velocities) differ with each invocation of the demo.

Step 2 is the creation of six meshes. First, a spherical mesh is loaded:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
resinfo.lpType="MESH";
LPDIRECT3DRMMESHBUILDER meshbuilder;
d3drm->CreateMeshBuilder( &meshbuilder );
meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
        NULL, NULL );


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.