![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The FireflyWin::CreateScene() FunctionThe Firefly demo constructs its scene with the CreateScene() function. The function is responsible for creating two meshes: one for the chalice and one for the firefly. One point light and one viewport is created. The light source and the spherical mesh are animated using dummy frames. Refer to the Decal demo in Chapter 5 for a discussion of dummy frames. The Firefly demo CreateScene() function appears in Listing 6.2. Listing 6.2 The FireflyWin::CreateScene() function. BOOL FireflyWin::CreateScene() { //-------- CHALICE MESH -------- D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_CHALICEMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &chalicebuilder ); chalicebuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); ScaleMesh( chalicebuilder, D3DVALUE(25) ); //------- CHALICE FRAME ------ LPDIRECT3DRMFRAME chaliceframe; d3drm->CreateFrame( scene, &chaliceframe ); chaliceframe->AddVisual( chalicebuilder ); chaliceframe->Release(); chaliceframe=0; //-------- POINT LIGHT -------- LPDIRECT3DRMLIGHT pointlight; d3drm->CreateLightRGB( D3DRMLIGHT_POINT, D3DVALUE(1.0),D3DVALUE(1.0), D3DVALUE(1.0), &pointlight ); //-------- FLY MESH ------ resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH ); resinfo.lpType="MESH"; LPDIRECT3DRMMESHBUILDER flybuilder; d3drm->CreateMeshBuilder( &flybuilder ); flybuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); flybuilder->SetQuality( D3DRMRENDER_WIREFRAME ); ScaleMesh( flybuilder, D3DVALUE(0.3) ); //-------- LIGHT FRAMES -------- LPDIRECT3DRMFRAME dummyframe; d3drm->CreateFrame( scene, &dummyframe ); dummyframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(0), D3DVALUE(.08) ); LPDIRECT3DRMFRAME lightframe; d3drm->CreateFrame( dummyframe, &lightframe ); lightframe->SetPosition( dummyframe, D3DVAL(15), D3DVAL(6), D3DVAL(0) ); lightframe->AddLight( pointlight ); lightframe->AddVisual( flybuilder ); flybuilder->Release(); flybuilder=0; lightframe->Release(); lightframe=0; dummyframe->Release(); dummyframe=0; pointlight->Release(); pointlight=0; //-------- VIEWPORT ------------ d3drm->CreateFrame( scene, &camera ); camera->SetPosition( scene, D3DVALUE(0.0), D3DVALUE(20.0), D3DVALUE(-50.0) ); camera->SetOrientation( scene, D3DVALUE(0), D3DVALUE(-20), D3DVALUE(50), D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) ); d3drm->CreateViewport( device, camera, 0, 0, device->GetWidth(), device->GetHeight(), &viewport ); return TRUE; } The CreateScene() function performs six steps:
The first step is the creation of the chalice mesh: D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_CHALICEMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &chalicebuilder ); chalicebuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); ScaleMesh( chalicebuilder, D3DVALUE(25) ); The Direct3DRMMeshBuilder interface is used to load the mesh from the programs resources. Then, the ScaleMesh() function is used to resize the mesh. The new mesh is then attached to a frame called chaliceframe: LPDIRECT3DRMFRAME chaliceframe; d3drm->CreateFrame( scene, &chaliceframe ); chaliceframe->AddVisual( chalicebuilder ); chaliceframe->Release(); chaliceframe=0; The chalice mesh is attached to the new frame with the AddVisual() function. The frame is not repositioned, so the mesh will appear at the origin. Notice that the chalicebuilder pointer is not declared in the CreateScene() function. The chalicebuilder pointer is a FireflyWin data member. This is done so that the rendering method can be changed from the Render menu. Notice, also, that the chalicebuilder pointer is not released in the CreateScene() function. If we did release the chalicebuilder pointer, we would have no way to change the render method during the programs execution. The next step is the creation of the point light. First, the Direct3DRM CreateLightRGB() function is used to create the light source: LPDIRECT3DRMLIGHT pointlight; d3drm->CreateLightRGB( D3DRMLIGHT_POINT, D3DVALUE(1.0), D3DVALUE(1.0), D3DVALUE(1.0), &pointlight ); The D3DRMLIGHT_POINT constant is used to indicate the type of light source that we are creating. The following three numeric arguments indicate that we are creating a light source that will emit white light. The final argument is the address of the pointer that is to point to the new Direct3DRMLight interface. Next, the mesh that represents the firefly is created: resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH ); resinfo.lpType="MESH"; LPDIRECT3DRMMESHBUILDER flybuilder; d3drm->CreateMeshBuilder( &flybuilder ); flybuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); flybuilder->SetQuality( D3DRMRENDER_WIREFRAME ); ScaleMesh( flybuilder, D3DVALUE(0.3) ); There is no actual firefly mesh, a spherical mesh is used to represent the firefly. The sphere mesh resource identifier is used to prepare the resinfo structure. The meshbuilder is created with the Direct3DRM CreateMeshBuilder() function, and the mesh is loaded with the Direct3DRMMeshBuilder Load() function. The SetQuality() function is then used to specify the wireframe rendering method. This is done so that the mesh will appear distinctly in the scene. Remember that the wireframe rendering method uses the mesh color to draw the mesh and ignores any light sources in the scene. Using the wireframe method causes the mesh to appear in the exact color of the mesh and not a calculated color. Finally, the meshbuilder is scaled with the ScaleMesh() function.
|
![]() |
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. |