Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Cutting Edge Direct 3D Programming
(Publisher: The Coriolis Group)
Author(s): Stan Trujillo
ISBN: 1576100502
Publication Date: 11/01/96

Bookmark It

Search this book:
 
Previous Table of Contents Next


The CubeWin::CreateScene() Function

The Cube demo’s scene is constructed by the CreateScene() function. The function appears in Listing 8.1.

Listing 8.1 The CubeWin::CreateScene() function.

BOOL CubeWin::CreateScene()
{
    // ------- MESH --------
    d3drm->CreateMesh( &mesh );
    mesh->AddGroup( 24, 6, 4, vertorder, &group );
    mesh->SetVertices( group, 0, 24, vertexlist );
    mesh->Translate( D3DVALUE(-0.5), D3DVALUE(-0.5), D3DVALUE(-0.5) );
    mesh->Scale( D3DVALUE(12), D3DVALUE(12), D3DVALUE(12) );

    //-------- TEXTURE ------
    HRSRC texture_id = FindResource( NULL,
            MAKEINTRESOURCE(IDR_WIN95TEXTURE), "TEXTURE" );
    LPDIRECT3DRMTEXTURE texture;
    d3drm->LoadTextureFromResource( texture_id, &texture );
    mesh->SetGroupTexture( group, texture );
    mesh->SetGroupMapping( group, D3DRMMAP_PERSPCORRECT );
    texture->Release();
    texture=0;

    //------- FRAME --------
    LPDIRECT3DRMFRAME frame;
    d3drm->CreateFrame( scene, &frame );
    frame->AddVisual( mesh );
    frame->SetRotation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(.04) );

    static CallbackData cbdata;
    cbdata.mesh=mesh;
    cbdata.group=group;
    frame->AddMoveCallback( UpdateCube, &cbdata );
    frame->Release();
    frame=0;

    // --------- LIGHTS --------
    LPDIRECT3DRMLIGHT dlight;
    d3drm->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL,
            D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
            &dlight );

    LPDIRECT3DRMLIGHT alight;
    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(1),
            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:

1.  Creates the cube mesh
2.  Creates and applies a texture to the mesh
3.  Creates a frame for the mesh and installs a callback function
4.  Creates two light sources
5.  Creates a viewport

The first step uses the Direct3DRMMesh interface to create the cube mesh. Let’s take a closer look:

d3drm->CreateMesh( &mesh );
mesh->AddGroup( 24, 6, 4, vertorder, &group );
mesh->SetVertices( group, 0, 24, vertexlist );
mesh->Translate( D3DVALUE(-0.5), D3DVALUE(-0.5), D3DVALUE(-0.5) );
mesh->Scale( D3DVALUE(12), D3DVALUE(12), D3DVALUE(12) );

First, the Direct3DRM Createmesh() function is used to initialize the mesh pointer. Notice that in previous demos that used the Direct3DRMMesh interface, the mesh was created with the Direct3DRMMeshBuilder CreateMesh() function and not Direct3DRM CreateMesh(). Creating a mesh with a meshbuilder is easy because the Direct3DRMMeshBuilder Load() function can be used in advance to load a mesh from a file. Because we are using the Direct3DRM CreateMesh() function, we have created an empty mesh.

Next, the Direct3DRMMesh AddGroup() function is used to initialize the mesh. This step creates a mesh group. A mesh group is a set of faces within a mesh that can be manipulated as a single entity. The first AddGroup() argument is the number of vertices in the group. Our cube has 24 vertices because it contains 6 faces, each defined with 4 vertices. The second AddGroup() argument is the number of faces in the group, and the third argument is the number of vertices used to represent each face. The fourth AddGroup() argument is an array of vertex indices. The vertorder array used as the fourth argument is defined like this:

unsigned vertorder[] = { 0,1,2,3,4,5,6,7,8,9,10,11,
        12,13,14,15,16,17,18,19,20,21,22,23 };

This array establishes the order of the vertices. The indices shown represent the simplest possible order because the order is sequential. When constructing meshes from scratch, like we are doing, there would be little advantage in using non-sequential vertex ordering. Mesh data exported from 3D modelers such as 3D Studio, however, is not likely to use sequential vertex ordering.

The fifth and final AddGroup() argument is a pointer to the group data member. The AddGroup() function initializes the group data member with an identifier that can be used to identify the new mesh group in subsequent function calls. Meshes that contain only one group (such as the one we are creating) use zero as the group identifier.

The AddGroup() function adds faces to a mesh and establishes vertex ordering, but the faces all have default values. All of the vertices in a new group have position and normal values of zero, and the new faces have a default color of white and no assigned texture. In order to assign initial vertex settings, we will use the SetVertices() function:

mesh->SetVertices( group, 0, 24, vertexlist );

The SetVertices() function is used to assign the position, normal, and texture coordinates of one or more vertices within a mesh group. The first SetVertices() argument specifies the mesh group to be modified. We are using group data member that was initialized by the AddGroup() function. The second argument is the index of the vertex where the changes are to begin (the index of the first vertex to be modified). The third argument is the number of vertices that are to be modified. We are going to assign all 24 vertices in the mesh group, so the values 0 and 24 are used. The fourth argument is an array of D3DRMVERTEX structures that contain the new vertex properties.

Before we look at the vertexlist array (used as the fourth SetVertices() argument), we should talk about the D3DRMVERTEX structure. Direct3D defines the structure like this:

typedef struct _D3DRMVERTEX
{
    D3DVECTOR position;
    D3DVECTOR normal;
    D3DVALUE  tu, tv;
    D3DCOLOR  color;
} D3DRMVERTEX;


Previous Table of Contents 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.