![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The TextureDriftWin::CreateScene() FunctionThe TextureDriftWin::CreateScene() function creates one mesh and a texture. The texture is associated with the mesh, but no texture wrap is supplied. A callback function is used to generate a texture wrap for each screen update. The CreateScene() function appears as Listing 5.5. Listing 5.5 The TextureDriftWin::CreateScene() function. BOOL TextureDriftWin::CreateScene() { //------ MESHBUILDER ------ D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH ); resinfo.lpType="MESH"; LPDIRECT3DRMMESHBUILDER meshbuilder; d3drm->CreateMeshBuilder( &meshbuilder ); meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); meshbuilder->Scale( D3DVALUE(1), D3DVALUE(1), D3DVALUE(.5) ); ScaleMesh( meshbuilder, D3DVALUE(35) ); meshbuilder->SetPerspective( TRUE ); //------- TEXTURE ------ HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE( IDR_TEXTURE), "TEXTURE" ); LPDIRECT3DRMTEXTURE texture; d3drm->LoadTextureFromResource( texture_id, &texture ); meshbuilder->SetTexture( texture ); texture->Release(); texture=0; //-------- MESH -------- LPDIRECT3DRMMESH mesh; meshbuilder->CreateMesh( &mesh ); meshbuilder->Release(); meshbuilder=0; //-------- MESH FRAME -------- LPDIRECT3DRMFRAME meshframe; d3drm->CreateFrame( scene, &meshframe ); meshframe->SetOrientation( scene, D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1), D3DVALUE(0), D3DVALUE(1), D3DVALUE(0)); meshframe->AddVisual( mesh ); meshframe->AddMoveCallback( MoveTexture, NULL ); meshframe->Release(); meshframe=0; mesh->Release(); mesh=0; //-------- LIGHTS ---------- LPDIRECT3DRMLIGHT light; d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT, D3DVALUE(1),D3DVALUE(1), D3DVALUE(1), &light ); scene->AddLight( light ); light->Release(); light=0; //-------- VIEWPORT ---------- d3drm->CreateFrame( scene, &camera ); camera->SetPosition( scene, D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(-50.0)); d3drm->CreateViewport( device, camera, 0, 0, device->GetWidth(), device->GetHeight(), &viewport ); return TRUE; } The CreateScene() function performs six steps:
First, the Direct3DRMMeshBuilder interface is used to load a mesh from the demos resources: D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH ); resinfo.lpType="MESH"; LPDIRECT3DRMMESHBUILDER meshbuilder; d3drm->CreateMeshBuilder( &meshbuilder ); meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); meshbuilder->Scale( D3DVALUE(1), D3DVALUE(1), D3DVALUE(.5) ); ScaleMesh( meshbuilder, D3DVALUE(35) ); meshbuilder->SetPerspective( TRUE ); Notice that after the mesh is loaded, we are using the Direct3DRMMeshBuilder Scale() function to reduce the Z dimension of the mesh. Using the value 0.5 for the Z argument of the Scale() function cuts the size of the mesh along the Z axis in half. After the Scale() function is called, the ScaleMesh() function is used to specify that the mesh should be scaled so that its longest dimension is equal to 35. The SetPerspective() function is called to enable perspective correction. Next, the texture is created: HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE(IDR_TEXTURE), "TEXTURE" ); LPDIRECT3DRMTEXTURE texture; d3drm->LoadTextureFromResource( texture_id, &texture ); meshbuilder->SetTexture( texture ); texture->Release(); texture=0; The new texture is associated with the previously created meshbuilder with the SetTexture() function, but no texture wrap is created. Next, an instance of the Direct3DRMMesh interface is created using the existing meshbuilder: LPDIRECT3DRMMESH mesh; meshbuilder->CreateMesh( &mesh ); meshbuilder->Release(); meshbuilder=0; After the CreateMesh() function is called, the meshbuilder is no longer needed, so it is released. Next, a new frame is created, and the mesh is attached: LPDIRECT3DRMFRAME meshframe; d3drm->CreateFrame( scene, &meshframe ); meshframe->SetOrientation( scene, D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1), D3DVALUE(0), D3DVALUE(1), D3DVALUE(0)); meshframe->AddVisual( mesh ); meshframe->AddMoveCallback( MoveTexture, NULL ); meshframe->Release(); meshframe=0; mesh->Release(); mesh=0; The new frame is oriented so that the mesh will appear at a 45 degree angle to the viewport, and the AddVisual() function is used to attach the mesh to the frame. The MoveTexture() callback function that will animate the texture is installed with the AddMoveCallback() function. Notice that the mesh pointer is released. We released the meshbuilder and texture pointers earlier in the function, so this means that we no longer have pointers to any graphical objects. This would make it difficult to apply new texture wrap settings if there wasnt a way to retrieve a frames visual objects. Well see how this is done when we look at the MoveTexture() callback function. Steps 5 and 6 create a light source and a viewport for the scene.
|
![]() |
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. |