![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
Like the target mesh, the missile mesh is stored in the programs resources. After the mesh is loaded, the Direct3DRMMeshBuilder SetColorRGB() function is used to give the mesh a light blue color. The SetQuality() function is used to indicate to Direct3D that the mesh should be rendered with flat shading. This setting can be changed from the demos Render menu. Lastly, the ScaleMesh() function is used to scale the mesh to seven units. Step 5 uses a nested loop to create 15 frames and attach the missile mesh to each. The code also installs a callback function for each frame: 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; } } The meshframe pointer is used to initialize each frame. New frames are positioned according to the current iteration of the two loops. The Direct3DRMFrame AddVisual() function is used to attach the missile mesh to each frame. The AddMoveCallback() function is used to install the OrientFrame() callback function. Notice that the targetframe pointer is used as the second AddMoveCallback() argument. This gives the callback function access to the frame that will be followed by the missile. The meshframe pointer is released after the AddMoveCallback() function is called. Steps 6 and 7 create light sources and a viewport. Well forgo discussion of these steps because light sources and viewports are discussed in Chapters 6 and 9 respectively. The TargetWin::MoveTarget() FunctionThe MoveTarget() callback function updates the animation sequence once per system update. The function looks like this: void TargetWin::MoveTarget(LPDIRECT3DRMFRAME, void* p, D3DVALUE) { LPDIRECT3DRMANIMATION animation=(LPDIRECT3DRMANIMATION)p; static D3DVALUE time; time+=D3DVALUE(.5); animation->SetTime( time ); } First, the function prepares a pointer to the Direct3DRMAnimation instance that controls the targets movement. A static counter variable is used to track the current position in the animation sequence. The variable is incremented and then used as an argument to the Direct3DRMAnimation SetTime() function. The TargetWin::OrientFrame() FunctionThe OrientFrame() callback function is used to modify the missile frames according to the movement of the target. This is accomplished with the Direct3DRMFrame LookAt() function. The LookAt() function orients one frame to look at another. The OrientFrame() function is defined this way: void TargetWin::OrientFrame(LPDIRECT3DRMFRAME frame, void* p, D3DVALUE) { LPDIRECT3DRMFRAME targetframe=(LPDIRECT3DRMFRAME)p; LPDIRECT3DRMFRAME scene; frame->GetScene( &scene ); frame->LookAt( targetframe, scene, (D3DRMFRAMECONSTRAINT)0 ); } First, a pointer to the target frame is prepared. The pointer is initialized with the user-determined second parameter. Next, the scenes root frame is retrieved. The root frame is required because the LookAt() function, like many of the Direct3DRMFrame member functions, requires that a reference frame be supplied (actually, you can use zero if you want to use the scenes root frame as a reference frame). Finally, the LookAt() function is called. The first LookAt() argument is a pointer to the frame that is to be followed. The second argument is the reference frame. The third argument can be used to indicate that the frames axes be constrained. We want our missile frames to enjoy full freedom of movement, so we use zero rather than supply any constraint flags. ConclusionThis chapter introduces animation topics that Direct3D supports. The demos use Direct3Ds animation support in as simple a way as possible (without being too boring). For real-life applications, however, this may be simplistic. A key-framing sequence, for example, is not usually arrived at by trial and error. More often, an animation tool is used to develop the sequence. The animation data is then exported from the animation tool and used from within an application to create the actual animation sequence. This chapter is not intended as a discussion of how to design an animation sequence. Instead, the discussion concentrates on how to use the Direct3D API to implement an animation sequence once it has been designed. In Chapter 8, well take a closer look at meshes. In particular, we will study sub-mesh animation. That is, rather than animating a meshs position, well be animating a meshs vertex positions. This technique is useful for a number of applications, including morphing.
|
![]() |
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. |