![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The OrbStar Callback FunctionsThe OrbStar CreateScene() function installs two callback functions: one for the frame to which the star mesh is attached and another for the sphere mesh frame. These functions periodically assign a random rotation attribute to each frame, causing the meshes in the demo to change their rotation speed and direction. The MoveStar() and MoveSphere() callback functions are virtually identical, so only the MoveStar() function is shown. void OrbStarWin::MoveStar(LPDIRECT3DRMFRAME frame, void*, D3DVALUE) { static UINT delay; if (++delay<11) return; delay=0; LPDIRECT3DRMFRAME scene; frame->GetScene( &scene ); D3DVECTOR spinvect; D3DRMVectorRandom( &spinvect ); D3DVALUE spin=D3DDivide(rand()%100+1,200); frame->SetRotation( scene, spinvect.x, spinvect.y, spinvect.z, spin ); } The MoveStar() function uses a static counter variable to regulate the frequency that adjusts the frames rotation attribute. In this case, the counter must reach 11 before an adjustment is made. Once it is determined that a new rotation attribute will be calculated, a pointer to the scenes root frame is retrieved with the Direct3DRMFrame GetScene() function. Remember that callback functions must be declared as static because of the fact that regular member functions require a class pointer in order to be invoked. Static member functions dont have access to their classs data members (unless the data members are also declared static). If the MoveStar() function was non-static, we would be able to use the RMWin::scene data member, just as we did in the CreateScene() function. Luckily, Direct3D passes a pointer to the callbacks frame as the first parameter to the callback function and supplies the GetScene() function. We will need a pointer to the scene frame as the reference pointer to the SetRotation() function later in the function. The MoveStar() function calculates a new rotation attribute randomly. A random vector is retrieved with the D3DRMVectorRandom() function, and a rotation speed is calculated with the rand() function. The frames new rotation attribute is installed with the SetRotation() function. Texture AnimationIn this chapter, youve learned how to apply textures to meshes using texture wrapping methods. Youve learned about decals and about transparency. Now, we will look at texture animation. There are two forms of texture animation. One form animates a single texture by changing the way that the texture is applied. The second form uses multiple textures, applying one at a time to produce a sort of texture movie. Well talk about both forms in this section, starting with animating a single texture. There are a number of ways to animate a single texture. The textures scale, origin, wrap method, and even transparency settings can be adjusted to modify the way that a texture is applied to a mesh. Probably, the most straightforward way is to move a texture across a mesh by changing the texture origin. The TextureDrift DemoThe TextureDrift demo uses a single mesh and a single texture. The texture is applied to the mesh using a slightly different texture origin on each screen update, causing the texture to travel, or drift, across the mesh. The TextureDrift demo is shown in Figure 5.10 (but youll have to run the TextureDrift demo if you want to see any texture animation).
The TextureDrift demo demonstrates the following techniques:
The TextureDrift demo uses a slightly different technique than the other demos that weve looked at. The previous demos used the Direct3DRMMeshBuilder interface to create and display meshes (except for the Decal demo which did not use meshes at all). The TextureDrift demo uses the Direct3DRMMeshBuilder interface for mesh creation but not for mesh display. Instead, the Direct3DRMMeshBuilder interface is used to create an instance of the Direct3DRMMesh interface. This is done for performance reasons. In Chapter 3, we talked about the fact that instances of the Direct3DRMMeshBuilder interface must create internal instances of the Direct3DRMMesh interface whenever a change is made to the mesh. We use the Direct3DRMMesh interface directly in the TextureDrift demo to avoid this extra overhead. This does not mean that we made bad design decisions for the previous demos. The Direct3DRMMeshBuilder interface offers good performance as long as the mesh properties do not change frequently during program execution. We are using the Direct3DRMMesh interface for the TextureDrift demo because the mesh characteristics will be changing for every screen update. The TextureDriftWin ClassThe TextureDrift demo provides its functionality in the TextureDriftWin class. The class is defined as follows: class TextureDriftWin : public RMWin { public: BOOL CreateScene(); static void MoveTexture(LPDIRECT3DRMFRAME frame, void* arg, D3DVALUE delta); protected: //{{AFX_MSG(TextureDriftWin) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; The class declares two functions: CreateScene() and MoveTexture(). The CreateScene() constructs the demos scene, and the MoveTexture() function is a callback function that is used to perform the texture animation. The protected portion of the class is present in case you want to add message handlers with ClassWizard.
|
![]() |
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. |