![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The MorphWin::LoadMeshes() FunctionThe LoadMeshes() function is responsible for loading meshes from the supplied file. In addition to making sure that each mesh has the same vertex count, the LoadMeshes() function must extract and store vertex data. It is for this reason that the function is rather long (and ugly). The LoadMeshes() function appears as Listing 8.3. Listing 8.3 The MorphWin::LoadMeshes() function. BOOL MorphWin::LoadMeshes( const CString& filename ) { for (DWORD i=0;i<nummorphtargets;i++) delete [] morphmeshdata[i]; nummorphtargets=0; if (morphmesh) { morphmesh->Release(); morphmesh=0; } BOOL load_ok=TRUE; for (i=0;i<MAXMORPHTARGETS && load_ok;i++) { CString msg; HRESULT r; LPDIRECT3DRMMESHBUILDER builder; d3drm->CreateMeshBuilder( &builder ); r=builder->Load( (void*)(LPCTSTR)filename, (void*)morphmeshname[i], D3DRMLOAD_FROMFILE | D3DRMLOAD_BYNAME, NULL, NULL ); load_ok=r==D3DRM_OK; if (r==D3DRMERR_FILENOTFOUND) { TRACE("file not found\n"); CString msg=filename + ": file not found"; AfxMessageBox( msg ); morphing=FALSE; } if (!load_ok) goto nomoremeshes; D3DVALUE scale; if (i==0) scale=ScaleMesh( builder, D3DVALUE(25) ); else builder->Scale( scale, scale, scale ); builder->SetQuality( D3DRMRENDER_FLAT ); LPDIRECT3DRMMESH mesh; builder->CreateMesh( &mesh ); unsigned vcount, fcount; DWORD ds; mesh->GetGroup( 0, &vcount, &fcount, 0, &ds, 0); if (i==0) nummorphvertices=vcount; else if(vcount!=nummorphvertices && load_ok) { TRACE("invalid vertex count\n"); AfxMessageBox("Invalid vertex count"); morphing=FALSE; return FALSE; } morphmeshdata[i]=new D3DRMVERTEX[nummorphvertices]; r=mesh->GetVertices( 0, 0, nummorphvertices, morphmeshdata[i] ); if (r!=D3DRM_OK) TRACE("mesh->GetVertices() failed\n"); msg.Format("Mesh %d - %d vertices, %d faces", i+1, vcount, fcount ); SetWindowText( msg ); if (i==0) morphmesh=mesh; else { mesh->Release(); mesh=0; } nomoremeshes: builder->Release(); builder=0; } nummorphtargets=i-1; morphing=TRUE; return TRUE; } This function is called whenever a new morph sequence is loaded, so morph sequences may already be loaded when LoadMeshes() is called. The first thing that the function does is release any previously allocated resources. The function uses a loop to attempt the loading of 26 targets. The loop stops if a given mesh is not found or if meshes with different vertex counts are discovered. When a mesh is loaded, its vertex data is extracted with the Direct3DRMMesh GetVertices() function. After each mesh is loaded, a new message is displayed on the window title bar. The nummorphvertices and nummorphtargets data members are both initialized by the LoadMeshes() function. The MorphWin::CreateAnimations() FunctionThe CreateAnimations() function is called only if the LoadMeshes() function is successful. CreateAnimations() is responsible for the initialization of the Direct3DRMAnimation objects that will perform the morphing calculation for both vertex locations and vertex normals. The function looks like this: BOOL MorphWin::CreateAnimations() { static int vertexcount; ReleaseAnimations( vertexcount ); vertexcount=nummorphvertices; posanimation=new LPDIRECT3DRMANIMATION[nummorphvertices]; posframe=new LPDIRECT3DRMFRAME[nummorphvertices]; normanimation=new LPDIRECT3DRMANIMATION[nummorphvertices]; normframe=new LPDIRECT3DRMFRAME[nummorphvertices]; for (DWORD vert=0; vert<nummorphvertices; vert++) { d3drm->CreateAnimation( &posanimation[vert] ); posanimation[vert]->SetOptions( D3DRMANIMATION_LINEARPOSITION | D3DRMANIMATION_POSITION ); d3drm->CreateFrame( scene, &posframe[vert] ); posanimation[vert]->SetFrame( posframe[vert] ); d3drm->CreateAnimation( &normanimation[vert] ); normanimation[vert]->SetOptions( D3DRMANIMATION_LINEARPOSITION | D3DRMANIMATION_POSITION ); d3drm->CreateFrame( scene, &normframe[vert] ); normanimation[vert]->SetFrame( normframe[vert] ); } return TRUE; } The function first releases any previously created resources with the ReleaseAnimations() function. The number of vertices is stored in the static vertexcount variable for future calls to the ReleaseAnimations() function. Next, the posanimation, posframe, normanimation, and normframe arrays are initialized. A loop is used to create two instances of the Direct3DRMAnimation object for each vertex. The animation objects in the posanimation array will be used to calculate vertex positions, and the animation objects in the normanimation array will be used to calculate vertex normals. The MorphWin::PrepareMorphVertices() FunctionThe PrepareMorphVertices() function allocates an array that will be used to store calculated vertex data: BOOL MorphWin::PrepareMorphVertices() { if (morphvertex) { delete [] morphvertex; morphvertex=0; } morphvertex=new D3DRMVERTEX[nummorphvertices]; return TRUE; } As with the previous two functions, if existing resources are detected, they are released before any new resources are allocated. The MorphWin::GetNumMorphTargets() FunctionThe GetNumMorphTargets() function is declared in the MorphWin class definition: DWORD GetNumMorphTargets() { return nummorphtargets; } The function simply returns the value stored in the nummorphtargets data member.
|
![]() |
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. |