![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
An instance of Direct3DRMMeshBuilder is used to load the mesh from the programs resources. The meshbuilder pointer will be used to access the new mesh. Next, six instances of Direct3DRMMesh are created: for (int i=0;i<MAXDEPTH;i++) { ScaleMesh( meshbuilder, D3DVALUE(MAXDEPTH-i) ); D3DCOLOR clr=meshcolor[i]; D3DVALUE r=D3DRMColorGetRed( clr ); D3DVALUE g=D3DRMColorGetGreen( clr ); D3DVALUE b=D3DRMColorGetBlue( clr ); meshbuilder->SetColorRGB( r, g, b ); meshbuilder->CreateMesh( &mesh[i] ); } A loop is used to create the meshes. The scale and color of the mesh depends on the iteration of the loop. The meshcolor array contains the colors that are to be used for each mesh. Once the color has been assigned, an instance of the Direct3DRMMesh interface is created using the Direct3DRMMeshBuilder CreateMesh() function. Step 3 is the creation of the frame hierarchy: CreateHierarchy(); The CreateHierarchy() function constructs a frame hierarchy based on the curdepth and numchildren data members. These two data members are given default settings by the MoleculeWin constructor but can be modified from the Depth and Children menus. Steps 4 and 5 create a light source and a viewport. Well omit discussion of these steps because light sources are covered in Chapter 6 and the viewport creation step is described in Chapters 4 and 9. The MoleculeWin::CreateHierarchy() FunctionThe CreateHierarchy() function is responsible for creating the frame hierarchy. The function looks like this: BOOL MoleculeWin::CreateHierarchy() { static LPDIRECT3DRMFRAME mainframe; if (mainframe) { scene->DeleteChild( mainframe ); mainframe->Release(); } d3drm->CreateFrame( scene, &mainframe ); for (int i=0;i<numchildren;i++) CreateChildren( mainframe, curdepth ); return TRUE; } The function uses a static frame pointer (mainframe) to access the root frame of the frame hierarchy. This frame should not be confused with the scenes root frame (scene). The scene frame is the root for the entire scene. The mainframe frame is the root of the frame hierarchy. If it has been initialized, the mainframe pointer is removed from the scene with the Direct3DRMFrame DeleteChild() function. This is done to remove previous frame hierarchies from the scene. The mainframe pointer is initialized using the Direct3DRM CreateFrame() function. The scene pointer is used as the first CreateFrame() argument, indicating the new frame is a child of the scene frame. A loop is then used to invoke the CreateChildren() function. The number of loop iterations depends on the numchildren data member. The CreateChildren() function takes two arguments: a pointer to the frame that is to be given child frames, and an integer indicating the desired depth of the frame hierarchy. This integer is important because it determines at which point the recursive CreateChildren() function will cease to invoke itself. The MoleculeWin::CreateChildren() FunctionThe CreateChildren() function assigns rotation attributes, attaches meshes, and creates child frames: BOOL MoleculeWin::CreateChildren(LPDIRECT3DRMFRAME frame, int depth) { LPDIRECT3DRMFRAME parent; frame->GetParent( &parent ); D3DVECTOR vector; D3DRMVectorRandom( &vector ); frame->SetRotation( parent, vector.x, vector.y, vector.z , D3DVALUE(rand()%100)/D3DVALUE(1000)+D3DVALUE(.1) ); frame->AddVisual( mesh[curdepth-depth] ); if (depth>1) { LPDIRECT3DRMFRAME child; d3drm->CreateFrame( frame, &child ); static int count; count++; D3DVALUE trans=distance[curdepth-depth]; D3DVALUE smalltrans=trans/D3DVALUE(2); D3DVALUE xtrans=(count%2) ? trans : -trans; D3DVALUE ytrans=(rand()%2) ? smalltrans : -smalltrans; D3DVALUE ztrans=(rand()%2) ? smalltrans : -smalltrans; child->SetPosition( frame, xtrans, ytrans, ztrans ); for (int i=0;i<numchildren;i++) CreateChildren( child, depth-1 ); } return TRUE; } The CreateChildren() function first assigns a rotation attribute to the supplied frame. In order for such an attribute to be assigned, a pointer to the frames parent frame must be retrieved. The Direct3DRMFrame GetParent() function is used for this purpose. Once the frames parent is known, a rotation attribute is calculated and applied: D3DVECTOR vector; D3DRMVectorRandom( &vector ); frame->SetRotation( parent, vector.x, vector.y, vector.z , D3DVALUE(rand()%100)/D3DVALUE(1000)+D3DVALUE(.1) ); Notice that the parent frame is used as the first argument to the SetRotation() function. First, however, the D3DRMVectorRandom() function is used to calculate a random vector. The vector is used to supply three of the SetRotation() function arguments. The last SetRotation() argument, which indicates the rotation velocity, is calculated with the rand() function. The equation used to calculate the velocity assures a randomly selected, slow-to-medium rotation speed. Next, the appropriate mesh is attached to the frame: frame->AddVisual( mesh[curdepth-depth] ); The mesh is selected based on the depth of the current frame. The remainder of the CreateChildren() function looks like this: if (depth>1) { LPDIRECT3DRMFRAME child; d3drm->CreateFrame( frame, &child ); static int count; count++; D3DVALUE trans=distance[curdepth-depth]; D3DVALUE smalltrans=trans/D3DVALUE(2); D3DVALUE xtrans=(count%2) ? trans : -trans; D3DVALUE ytrans=(rand()%2) ? smalltrans : -smalltrans; D3DVALUE ztrans=(rand()%2) ? smalltrans : -smalltrans; child->SetPosition( frame, xtrans, ytrans, ztrans ); for (int i=0;i<numchildren;i++) CreateChildren( child, depth-1 ); }
|
![]() |
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. |