![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The Cube2Win::CreateScene() FunctionThe Cube2 demo CreateScene() function appears in Listing 8.2. Listing 8.2 The Cube2Win::CreateScene() function. BOOL Cube2Win::CreateScene() { //---------- MESH -------- d3drm->CreateMesh( &mesh ); mesh->AddGroup( 12, 3, 4, vertorder, &group1 ); mesh->AddGroup( 12, 3, 4, vertorder, &group2 ); mesh->SetVertices( group1, 0, 12, vertexlist ); mesh->SetVertices( group2, 0, 12, vertexlist+12 ); mesh->Translate( D3DVALUE(-0.5), D3DVALUE(-0.5), D3DVALUE(-0.5) ); mesh->Scale( D3DVALUE(15), D3DVALUE(15), D3DVALUE(15) ); //--------- TEXTURE ---------- HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE(IDR_WIN95TEXTURE), "TEXTURE" ); LPDIRECT3DRMTEXTURE texture; d3drm->LoadTextureFromResource( texture_id, &texture ); mesh->SetGroupTexture( group1, texture ); mesh->SetGroupMapping( group1, D3DRMMAP_PERSPCORRECT ); mesh->SetGroupTexture( group2, texture ); mesh->SetGroupMapping( group2, D3DRMMAP_PERSPCORRECT ); texture->Release(); texture=0; //---------- FRAME ---------- LPDIRECT3DRMFRAME meshframe; d3drm->CreateFrame( scene, &meshframe ); meshframe->AddVisual( mesh ); meshframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(0), D3DVALUE(-.05) ); static CallbackData cbdata; cbdata.mesh=mesh; cbdata.group1=group1; cbdata.group2=group2; meshframe->AddMoveCallback( UpdateCube, &cbdata ); meshframe->AddMoveCallback( UpdateColors, &cbdata ); meshframe->Release(); meshframe=0; // --------LIGHTS-------- LPDIRECT3DRMLIGHT dlight, alight; d3drm->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL, D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00), &dlight ); d3drm->CreateLightRGB(D3DRMLIGHT_AMBIENT, D3DVALUE(0.50), D3DVALUE(0.50), D3DVALUE(0.50), &alight ); LPDIRECT3DRMFRAME lightframe; d3drm->CreateFrame( scene, &lightframe ); lightframe->SetOrientation( scene, D3DVALUE(0), D3DVALUE(-1), D3DVALUE(5), D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) ); lightframe->AddLight( dlight ); lightframe->AddLight( alight ); dlight->Release(); dlight=0; alight->Release(); alight=0; lightframe->Release(); lightframe=0; //------ CAMERA---------- d3drm->CreateFrame( scene, &camera ); camera->SetPosition( scene, D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50)); d3drm->CreateViewport( device, camera, 0, 0, device->GetWidth(), device->GetHeight(), &viewport ); return TRUE; } The CreateScene() function performs five steps:
The first step is the creation of a mesh: d3drm->CreateMesh( &mesh ); mesh->AddGroup( 12, 3, 4, vertorder, &group1 ); mesh->AddGroup( 12, 3, 4, vertorder, &group2 ); mesh->SetVertices( group1, 0, 12, vertexlist ); mesh->SetVertices( group2, 0, 12, vertexlist+12 ); mesh->Translate( D3DVALUE(-0.5), D3DVALUE(-0.5), D3DVALUE(-0.5) ); mesh->Scale( D3DVALUE(15), D3DVALUE(15), D3DVALUE(15) ); First, the mesh pointer is initialized with the Direct3DRM CreateMesh() function. Two groups are then added to the empty mesh. Each group is created with the AddGroup() function. Each group has 12 vertices: 3 faces made up of 4 vertices apiece. The vertorder array is used as the third AddGroup() argument. In the Cube demo, the vertorder array contained 24 entries; in the Cube2 demo, the array contains 12 entries: unsigned vertorder[] = { 0,1,2,3,4,5,6,7,8,9,10,11 }; The final AddGroup() argument is an address to a mesh group identifier. AddGroup() assigns a unique value for each mesh group. Next, the SetVertices() function is used to assign values to the vertices in each group. The first SetVertices() argument is the identifier for the group. The second argument is the index of the vertex where the changes are to be made. The third argument is the number of vertices that will be assigned. The fourth and final argument is an array of D3DRMVERTEX structures. Notice that we are using a single array of vertex structures to initialize two mesh groups. An offset is used in the second SetVertices() function, causing the second half of the array to be used for the second mesh group. Next, the Direct3DRMMesh Translate() and Scale() functions are called. Unlike the AddGroup() and SetVertices() functions, the Translate() and Scale() functions modify all of the groups in a mesh. These function calls are identical to those in the Cube demo. In Step 2, a texture is created and applied to both meshes: HRSRC texture_id = FindResource( NULL, MAKEINTRESOURCE(IDR_WIN95TEXTURE), "TEXTURE" ); LPDIRECT3DRMTEXTURE texture; d3drm->LoadTextureFromResource( texture_id, &texture ); mesh->SetGroupTexture( group1, texture ); mesh->SetGroupMapping( group1, D3DRMMAP_PERSPCORRECT ); mesh->SetGroupTexture( group2, texture ); mesh->SetGroupMapping( group2, D3DRMMAP_PERSPCORRECT ); texture->Release(); texture=0; Step 3 is the creation of a frame for the mesh and the installation of two callback functions: LPDIRECT3DRMFRAME meshframe; d3drm->CreateFrame( scene, &meshframe ); meshframe->AddVisual( mesh ); meshframe->SetRotation( scene, D3DVALUE(0), D3DVALUE(1), D3DVALUE(0), D3DVALUE(-.05) ); static CallbackData cbdata; cbdata.mesh=mesh; cbdata.group1=group1; cbdata.group2=group2; meshframe->AddMoveCallback( UpdateCube, &cbdata ); meshframe->AddMoveCallback( UpdateColors, &cbdata ); meshframe->Release(); meshframe=0;
|
![]() |
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. |