![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The JadeWin::CreateScene() FunctionThe Jade demo CreateScene() function is shown in Listing 5.1. Listing 5.1 The JadeWin::CreateScene() function. BOOL JadeWin::CreateScene() { HRESULT r; //------- BACKGROUND -------- scene->SetSceneBackgroundRGB( D3DVALUE(.2), D3DVALUE(.2), D3DVALUE(.2) ); //-------- MESH -------- D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &meshbuilder ); meshbuilder->SetPerspective( TRUE ); r=meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL ); if (r!=D3DRM_OK) { TRACE("meshbuilder->Load() failed\n"); return FALSE; } ScaleMesh( meshbuilder, D3DVALUE(35) ); //-------- TEXTURE -------- LPDIRECT3DRMTEXTURE texture; HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE(IDR_JADETEXTURE), "TEXTURE" ); r = d3drm->LoadTextureFromResource( texture_id, &texture ); if (r!=D3DRM_OK) { TRACE("d3drm->LoadTextureFromResource() failed\n"); return FALSE; } meshbuilder->SetTexture( texture ); texture->Release(); texture=0; //-------- WRAP -------- D3DRMBOX box; meshbuilder->GetBox( &box ); D3DVALUE w=box.max.x-box.min.x; D3DVALUE h=box.max.y-box.min.y; LPDIRECT3DRMWRAP wrap; d3drm->CreateWrap(D3DRMWRAP_FLAT, scene, D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0), // wrap origin D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0), // z axis of wrap D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0), // y axis of wrap D3DVALUE(0.5), D3DVALUE(0.5), // texture origin D3DDivide(1,w), D3DDivide(1,h), // texture scale &wrap); wrap->Apply( meshbuilder ); wrap->Release(); wrap=0; //------- MESH FRAME ---------- LPDIRECT3DRMFRAME meshframe; d3drm->CreateFrame( scene, &meshframe ); meshframe->AddVisual( meshbuilder ); meshframe->AddMoveCallback( MoveFrame, NULL ); meshframe->Release(); //---------- LIGHT ----------- LPDIRECT3DRMLIGHT light; d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT, D3DVALUE(1),D3DVALUE(1), D3DVALUE(1), &light ); scene->AddLight( light ); light->Release(); light=0; //---------- CAMERA ----------- 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 seven steps:
Lets examine this function one step at a time. The first thing that CreateScene() does is change the scenes background color using the SetSceneBackgroundRGB() function: scene->SetSceneBackgroundRGB( D3DVALUE(.2), D3DVALUE(.2), D3DVALUE(.2) ); The SetSceneBackgroundRGB() function takes three arguments that describe the red, green, and blue components of the new background color. In this case, we are using a dark gray. Next, the Direct3DRMMeshBuilder interface is used to load and configure the mesh: D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &meshbuilder ); meshbuilder->SetPerspective( TRUE ); r=meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, NULL, NULL); if (r!=D3DRM_OK) { TRACE("meshbuilder->Load() failed\n"); return FALSE; } ScaleMesh( meshbuilder, D3DVALUE(35) ); In order to load meshes from resources, a D3DRMLOADRESOURCE structure must be prepared. The D3DRMLOADRESOURCE structure contains three fields: hModule, lpName, and lpType. The hModule field identifies the module that contains the resource. This is useful if resources must be loaded from executables other than the calling executable, but for our purposes, we can use NULL to indicate that the resources are located in the calling program. The lpName field is used to store a value that identifies the resource that we are seeking. The lpType field is used to indicate what type of resource is to be located.
Once the D3DRMLOADRESOURCE structure has been prepared, the meshbuilder pointer is initialized with the Direct3DRM CreateMeshBuilder() function. Perspective correction is enabled for the new mesh with the SetPerspective() function. Perspective correction will prevent the texture that we apply to the mesh from drifting during animation (comment this line out and compile the demo to see this effect). Next, the Load() member function is called using a pointer to the D3DRMLOADRESOURCE structure as the first argument. The third Load() argument (the D3DRMLOAD_FROMRESOURCE flag) indicates to Direct3D that we are loading a file from a resource and not from disk. The Load() return value is then checked for success. The CreateScene() function returns FALSE if the Load() function fails.
|
![]() |
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. |