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


More Animation

As I mentioned at the beginning of this chapter, animation is a large topic. We’ll close this chapter by presenting one more demo. The Target demo, unlike the other demos in this book, is not designed to showcase any one particular technique or functionality. The Target demo uses three forms of animation.

The Target Demo

The Target demo creates a scene in which a bay of rockets or missiles follow a target. The target’s movement is dictated by an animation sequence, much in the same way that the rocket mesh in the Rocket demo is animated. Each missile is animated. The missiles follow the target’s movement throughout the animation sequence. The Target demo also uses an animated camera. The camera orbits the scene, always looking toward the missiles. The Target demo appears in Figure 7.5.


Figure 7.5  The Target demo.

The Target demo demonstrates the following techniques:

  Using the Direct3DRMFrame LookAt() function to adjust frame orientation
  Using the Direct3DRMAnimation interface to create and execute an animation sequence
  Animating a camera with a dummy frame
  Using mesh instances

We’ll talk about each of these techniques as we talk about the Target demo code.

The TargetWin Class

The Target demo provides its functionality with the TargetWin class:

class TargetWin : public RMWin
{
public:
    TargetWin();
    BOOL CreateScene();
protected:
    //{{AFX_MSG(TargetWin)
    afx_msg void OnRenderWireframe();
    afx_msg void OnRenderFlat();
    afx_msg void OnRenderGouraud();
    afx_msg void OnUpdateRenderFlat(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderGouraud(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderWireframe(CCmdUI* pCmdUI);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    static void OrientFrame(LPDIRECT3DRMFRAME frame, void*, D3DVALUE);
    static void MoveTarget(LPDIRECT3DRMFRAME frame, void*, D3DVALUE);
private:
    LPDIRECT3DRMMESHBUILDER meshbuilder;
};

Two public member functions are declared: a constructor and CreateScene(). The constructor initializes the class’s single data member. The CreateScene() function constructs the demo’s scene.

Six protected member functions are declared. These functions provide support for the demo’s Render menu.

Two private callback functions are declared: OrientFrame() and MoveTarget(). The OrientFrame() function is used to position each missile to point toward the target. The MoveTarget() function is used to updated the target’s position.

Finally, a single private data member is declared. The meshbuilder pointer is used to load the mesh that represents the demo’s missiles. This data member is used both by the CreateScene() function and the Render menu functions.

The TargetWin::CreateScene() Function

The Target demo CreateScene() function appears in Listing 7.3.

Listing 7.3 The TargetWin::CreateScene() function.

BOOL TargetWin::CreateScene()
{
    // ------- TARGET MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
    resinfo.lpType="MESH";
    LPDIRECT3DRMMESHBUILDER targetbuilder;
    d3drm->CreateMeshBuilder( &targetbuilder );
    targetbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    ScaleMesh( targetbuilder, D3DVALUE(.75) );

    // --------- TARGET ANIMATION ----------
    LPDIRECT3DRMANIMATION animation;
    d3drm->CreateAnimation( &animation );
    animation->SetOptions(  D3DRMANIMATION_SPLINEPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION );
    animation->AddPositionKey( D3DVALUE(0),
            D3DVALUE(-20), D3DVALUE(0), D3DVALUE(-20) );
    animation->AddPositionKey( D3DVALUE(12),
            D3DVALUE(0), D3DVALUE(15), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(24),
            D3DVALUE(20), D3DVALUE(0), D3DVALUE(-20) );
    animation->AddPositionKey( D3DVALUE(35),
            D3DVALUE(0), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(49),
            D3DVALUE(20), D3DVALUE(0), D3DVALUE(20) );
    animation->AddPositionKey( D3DVALUE(65),
            D3DVALUE(0), D3DVALUE(15), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(74),
            D3DVALUE(-20), D3DVALUE(0), D3DVALUE(20) );
    animation->AddPositionKey( D3DVALUE(85),
            D3DVALUE(0), D3DVALUE(0), D3DVALUE(0) );
    animation->AddPositionKey( D3DVALUE(99),
            D3DVALUE(-20), D3DVALUE(0), D3DVALUE(-20) );

    // ---------- TARGET FRAME --------
    LPDIRECT3DRMFRAME targetframe;
    d3drm->CreateFrame( scene, &targetframe );
    animation->SetFrame( targetframe );
    targetframe->AddVisual( targetbuilder );
    targetframe->AddMoveCallback( MoveTarget, animation );

    targetbuilder->Release();
    targetbuilder=0;

    // ------- MISSILE MESH --------
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_MISSLEMESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    meshbuilder->SetColorRGB(
            D3DVALUE(.67), D3DVALUE(.82), D3DVALUE(.94) );
    meshbuilder->SetQuality( D3DRMRENDER_FLAT );
    ScaleMesh( meshbuilder, D3DVALUE(7) );

    // ------- MISSILE FRAMES ------
    for (int i=0;i<5;i++)
    {
        for (int j=0;j<3;j++)
        {
            LPDIRECT3DRMFRAME meshframe;
            d3drm->CreateFrame( scene, &meshframe );

            meshframe->SetPosition( scene,
                    D3DVALUE((i-2)*8),
                    D3DVALUE(-12),
                    D3DVALUE((j-1)*8) );
            meshframe->AddVisual( meshbuilder );
            meshframe->AddMoveCallback( OrientFrame, targetframe );

            meshframe->Release();
            meshframe=0;
        }
    }

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

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

    //------ CAMERA----------
    LPDIRECT3DRMFRAME cameradummy;
    d3drm->CreateFrame( scene, &cameradummy );
    cameradummy->SetRotation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(.01) );
    d3drm->CreateFrame( cameradummy, &camera );
    camera->SetPosition( scene,
            D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50));
    d3drm->CreateViewport( device, camera, 0, 0,
            device->GetWidth(), device->GetHeight(),
            &viewport);

    return TRUE;
}


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.