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


A new frame is created with the Direct3DRM CreateFrame() function. The previously created rocket mesh is attached to the frame using the Direct3DRMFrame AddVisual() function.

The UpdateScene() callback function is installed with the AddMoveCallback() function. The callback will be used to update the animation sequence during the demo’s operation. Notice that the animation pointer is used as the second argument. Any value used as the second AddMoveCallback() argument is passed to the callback function each time the callback function is invoked. Sending a pointer to the animation object will allow our callback function to control the animation sequence (remember, callback functions are static, so they do not enjoy automatic access to data members).

Next, the Direct3DRMAnimation SetFrame() function is used to attach the frame to the Direct3DRMAnimation object that was initialized in Step 2. Now the frame’s location, rotation, and scale will be controlled by the animation object.

In steps 4 and 5, CreateScene() creates light sources and a viewport. We’ll skip discussion of these steps so that we can study the UpdateScene() callback function.

The RocketWin::UpdateScene() Function

The UpdateScene() callback function is responsible for updating the animation sequence. The function is defined like this:

void RocketWin::UpdateScene(LPDIRECT3DRMFRAME, void* p, D3DVALUE)
{
    LPDIRECT3DRMANIMATION animation=(LPDIRECT3DRMANIMATION)p;
    static D3DVALUE time;
    time+=speed;
    animation->SetTime( time );
}

First, the function initializes a pointer to the Direct3DRMAnimation interface. Recall that the animation pointer was passed to the AddMoveCallback() function. This means that Direct3D is passing the pointer’s value to our callback function, so the UpdateScene() function’s second parameter has the same value as the pointer. Before we can use it, however, we must cast it to the correct type. The local animation pointer is initialized using the function’s second parameter and a type cast.

Next, a static counter variable, time, is incremented. The increment value is stored in the static speed variable. As you’ll see later, the speed value can be changed from the demo’s Speed menu. This allows the speed of the animation sequence to be changed easily.

The Direct3DRMAnimation SetTime() function is used to install a new time index. The time index controls the current state of the animation sequence. This function call causes the animation object to calculate a new position and rotation based on the new time index. The frame attached to the animation sequence is then repositioned according to the results. Because our rocket mesh is attached to the frame, the SetTime() function call repositions the rocket mesh.

The RocketWin Animation Functions

The Rocket demo’s Animation menu supports two commands: Linear and Spline. Each of these commands is implemented with two functions. The four functions that implement the Animation menu commands look like this:

void RocketWin::OnAnimationLinear()
{
    animation->SetOptions( D3DRMANIMATION_LINEARPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION |
            D3DRMANIMATION_SCALEANDROTATION );
}

void RocketWin::OnAnimationSpline()
{
    animation->SetOptions( D3DRMANIMATION_SPLINEPOSITION |
            D3DRMANIMATION_CLOSED |
            D3DRMANIMATION_POSITION |
            D3DRMANIMATION_SCALEANDROTATION );
}

void RocketWin::OnUpdateAnimationLinear(CCmdUI* pCmdUI)
{
    D3DRMANIMATIONOPTIONS options;
    options = animation->GetOptions();
    pCmdUI->SetCheck( options & D3DRMANIMATION_LINEARPOSITION );
}

void RocketWin::OnUpdateAnimationSpline(CCmdUI* pCmdUI)
{
    D3DRMANIMATIONOPTIONS options;
    options = animation->GetOptions();
    pCmdUI->SetCheck( options & D3DRMANIMATION_SPLINEPOSITION );
}

The first two functions, OnAnimationLinear() and OnAnimationSpline(), use the Direct3DRMAnimation SetOptions() function to specify a set of flags. The flags used by the two functions differ by one. The OnAnimationLinear() function uses the D3DRMANIMATION_LINEARPOSITION flag, and the OnAnimationSpline() function uses the D3DRMANIMATION_SPLINEPOSITION flag.

The D3DRMANIMATION_CLOSED flag indicates a closed animation sequence (as opposed to an open animation sequence). A closed animation sequence means we can use a continuously increasing time index value with the SetTime() function. As the time index increases, the animation sequence is executed repeatedly.

The D3DRMANIMATION_POSITION flag indicates that we are interested in the animation’s positional output. The D3DRMANIMATION_SCALEANDROTATION flag indicates that we want scale and rotational output as well.

The OnUpdateAnimationLinear() and OnUpdateAnimationSpline() functions use the Direct3DRMAnimation GetOptions() function to retrieve the animation’s current settings. The GetOptions() return value is used to check if the particular animation mode is in effect. If so, the menu check-mark is enabled.

The RocketWin Speed Functions

The Rocket demo provides a Speed menu that allows three speed settings: Fast, Medium, and Slow. The two functions that implement the Speed|Fast menu command look like this:

void RocketWin::OnSpeedFast()
{
    speed=fastspeed;
}

void RocketWin::OnUpdateSpeedFast(CCmdUI* pCmdUI)
{
    pCmdUI->SetCheck( speed==fastspeed);
}

These functions simply use the fastspeed constant to assign and inspect the speed data member. The Speed menu functions use these constants to control the animation sequence speed:

const D3DVALUE fastspeed=D3DVALUE(0.026);
const D3DVALUE mediumspeed=D3DVALUE(0.013);
const D3DVALUE slowspeed=D3DVALUE(0.007);

The larger the increment, the faster the animation sequence appears. The slowspeed constant indicates a very small value, resulting in slow, smooth animation.


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.