![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The WrapsWin::ApplyWraps() FunctionThe ApplyWraps() function is responsible for assigning the correct texture wrap type to each mesh depending on the values of the WrapsWin data members. The ApplyWraps() function looks like this: void WrapsWin::ApplyWraps() { if ( boxwraptype==D3DRMWRAP_FLAT ) ApplyFlat( box ); else if ( boxwraptype==D3DRMWRAP_CYLINDER ) ApplyCylinder( box ); else ApplySphere( box ); if ( cylwraptype==D3DRMWRAP_FLAT ) ApplyFlat( cyl ); else if ( cylwraptype==D3DRMWRAP_CYLINDER ) ApplyCylinder( cyl ); else ApplySphere( cyl ); if ( spherewraptype==D3DRMWRAP_FLAT ) ApplyFlat( sphere ); else if ( spherewraptype==D3DRMWRAP_CYLINDER ) ApplyCylinder( sphere ); else ApplySphere( sphere ); } The ApplyWraps() function determines the type of wrap that is to be used for each mesh and calls the appropriate function to perform the actual application of the texture wrap. We wont look at each of these functions because they are all quite similar. The ApplyFlat() function appears below: void WrapsWin::ApplyFlat( LPDIRECT3DRMMESHBUILDER meshbuilder ) { D3DRMBOX box; meshbuilder->GetBox( &box ); D3DVALUE width=box.max.x-box.min.x; D3DVALUE height=box.max.y-box.min.y; LPDIRECT3DRMWRAP wrap; d3drm->CreateWrap( D3DRMWRAP_FLAT, NULL, D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0), // wrap origin D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0), // z axis D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0), // y axis D3DVALUE(0.5), D3DVALUE(0.5), // texture origin D3DDivide(1,width), D3DDivide(1,height), // texture scale &wrap ); wrap->Apply( meshbuilder ); wrap->Release(); wrap=0; } The ApplyFlat() function first calculates the width and height of the mesh that is to be texture mapped. The Direct3DRMMeshBuilder GetBox() function is used to initialize a D3DRMBOX structure with the dimensions of the mesh. Then, an instance of the Direct3DRMWrap interface is created with the Direct3DRM CreateWrap() function. The new wrap is applied to the mesh with the Direct3DRMWrap Apply() function. Finally, the wrap pointer is released. There are three more WrapsWin member functions: OnWrapsFlat(), OnWrapsCylinder(), and OnWrapsSphere(). These functions serve as event handlers for the Wraps menu and are similar to the OnWrapsReset() function, except that they each assign the same type of texture wrap to the three meshes. The OnWrapsFlat() function is shown below: void WrapsWin::OnWrapsFlat() { boxwraptype=D3DRMWRAP_FLAT; cylwraptype=D3DRMWRAP_FLAT; spherewraptype=D3DRMWRAP_FLAT; ApplyWraps(); } DecalsThe demos weve looked at so far apply textures to the Direct3DRMMeshBuilder interface, and the meshbuilder is then attached to a frame with the Direct3DRMFrame AddVisual() function. In this section, we introduce and discuss decals. A decal is a texture that is added directly to a scene. Decals appear in the scene just like they do in a paint program; they cannot be wrapped around objects in the scene. Decals can be moved and scaled, but they cannot be rotated; decals always face the camera. This fact limits the usefulness of decals, but they are still useful for adding 2D elements to a scene. Explosions, for instance, are often represented using decals because representing an explosion with faces and meshes is too demanding for most applications.
The Decal DemoDirect3Ds decal support is showcased in the Decal demo. The Decal demo animates two decals by moving them around the screen. The Decal demo is shown in Figure 5.7.
The Decal demo demonstrates the following techniques:
Before we look at the code for the Decal demo, we need to talk about dummy frames. A dummy frame is a frame used solely to animate other frames. Dummy frames are not attached to any visual objects such as meshes and decals. They are used as parents for frames that are attached to visual objects. The Decal demo animates two decals in an orbit around the origin. This is accomplished by placing two dummy frames at the origin and assigning a different rotation attribute to each frame. The decals are attached to two separate (non-dummy) frames. Each of the non-dummy frames is a child of one of the dummy frames and is positioned away from the origin. Because child frames imitate the motions of parent frames, the non-dummy frames travel in an orbit around their parent frames. The DecalWin ClassThe Decal demo provides its functionality in the DecalWin class: class DecalWin : public RMWin { public: BOOL CreateScene(); protected: //{{AFX_MSG(DecalWin) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; The DecalWin class is simple. It declares only one function: CreateScene(). The protected portion of the class contains ClassWizard code that is necessary if message handlers are to be added later.
|
![]() |
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. |