![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The WrapsWin::LoadMeshes() FunctionThe LoadMeshes() function creates three meshes and attaches each mesh to a frame. The frames are positioned apart from each other and given identical rotation attributes. The LoadMeshes() function appears as Listing 5.2. Listing 5.2 The WrapsWin::LoadMeshes() function. BOOL WrapsWin::LoadMeshes() { HRESULT r; const D3DVALUE meshscale=D3DVALUE(13); const D3DVALUE meshspacing=D3DVALUE(15); D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_BOXMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &box ); r=box->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL); if (r!=D3DRM_OK) { TRACE("failed to load internal box mesh\n"); return FALSE; } box->SetPerspective(TRUE); ScaleMesh( box, meshscale-D3DVALUE(2) ); LPDIRECT3DRMFRAME boxframe; d3drm->CreateFrame( scene, &boxframe ); boxframe->SetPosition(scene, -meshspacing, D3DVALUE(0), D3DVALUE(0) ); boxframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(1), D3DVALUE(.1) ); boxframe->AddVisual( box ); resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_CYLMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &cyl ); cyl->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); if (r!=D3DRM_OK) { TRACE("failed to load internal cylinder mesh\n"); return FALSE; } cyl->SetPerspective( TRUE ); ScaleMesh( cyl, meshscale ); LPDIRECT3DRMFRAME cylframe; d3drm->CreateFrame( scene, &cylframe ); cylframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(1), D3DVALUE(.1) ); cylframe->AddVisual( cyl ); resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &sphere ); sphere->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL); if (r!=D3DRM_OK) { TRACE("failed to load internal sphere mesh\n"); return FALSE; } sphere->SetPerspective( TRUE ); ScaleMesh( sphere, meshscale ); LPDIRECT3DRMFRAME sphereframe; d3drm->CreateFrame( scene, &sphereframe ); sphereframe->SetPosition( scene, D3DVALUE(meshspacing), D3DVALUE(0), D3DVALUE(0) ); sphereframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(1), D3DVALUE(.1) ); sphereframe->AddVisual( sphere ); return TRUE; } The function declares some constant values that are used in the code to position and scale the meshes, and then initializes a pointer to the Direct3DRMMeshBuilder interface. The cube mesh is loaded from the programs resources with the Load() function. Next, the SetPerspective() function is used to enable perspective correction. If you have installed the demos on your computer, you are encouraged to comment out this function call and recompile the Wraps demo. Perspective correction is especially discernible in the case of the cube mesh because of the large faces. The mesh is then scaled with the ScaleMesh() function. We discussed the ScaleMesh() function in Chapter 4. ScaleMesh() is provided by the RMWin class as a convenient way to scale a mesh to a specific size. The constant used to determine the cubes size is adjusted a bit because cube shaped meshes tend to appear larger than other meshes (because they allow maximum volume in minimum space). After the mesh has been scaled, the LoadMeshes() function creates a frame and positions it according to the meshspacing constant. The cube mesh appears to the left of the other meshes, so it is given a negative X axis value. The frame is then given a rotation attribute with the SetRotation() function. Finally, the mesh is attached to the frame with the Frame AddVisual() function. The WrapsWin::LoadWrapsTexture() FunctionThe LoadWrapsTexture() function loads the texture shown in Figure 5.6 and associates it with the three meshes loaded previously by the LoadMeshes() function. The LoadWrapsTexture() function appears below: BOOL WrapsWin::LoadWrapsTexture() { HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE( IDR_JADETEXTURE), "TEXTURE" ); LPDIRECT3DRMTEXTURE texture; d3drm->LoadTextureFromResource( texture_id, &texture ); box->SetTexture( texture ); cyl->SetTexture( texture ); sphere->SetTexture( texture ); texture->Release(); texture=0; return TRUE; } The LoadWrapsTexture() function uses the Win32 FindResource() function to initialize an instance of the HRSRC structure. The HRSRC structure identifies the texture that we are attempting to load and is used as an argument to the LoadTextureFromResource() function. The new texture is then associated with the three meshes with the Direct3DRMMeshBuilder SetTexture() member function. The pointer to the texture is then released and TRUE is returned to indicate success. The WrapsWin::OnWrapsReset() FunctionRecall that the Wraps demo CreateScene() function called three functions before creating a light source and a viewport: LoadMeshes(), LoadWrapsTexture(), and OnWrapsReset(). The OnWrapsReset() function assigns data members in the WrapsWin class so that a flat texture wrap is applied to the cube mesh, a cylindrical wrap is assigned to the cylinder mesh, and a spherical wrap is assigned to the sphere mesh. The OnWrapsReset() function looks like this: void WrapsWin::OnWrapsReset() { boxwraptype=D3DRMWRAP_FLAT; cylwraptype=D3DRMWRAP_CYLINDER; spherewraptype=D3DRMWRAP_SPHERE; ApplyWraps(); } The boxwraptype, cylwraptype, and spherewraptype variables are WrapsWin data members that are of the Direct3D D3DRMWRAPTYPE type. The OnWrapReset() function assigns each of these data members using constants that are also supplied by Direct3D. Well talk about the ApplyWraps() function next.
|
![]() |
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. |