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 CreateScene() function performs seven steps:

1.  Creation of 15 textures
2.  Creation and configuration of a meshbuilder
3.  Creation and application of a flat texture wrap
4.  Creation of the Direct3DRMMesh interface
5.  Creation of a frame for the mesh
6.  Creation of a light source
7.  Creation of a viewport

The first portion of the CreateScene() function is straightforward (and tedious). The 15 textures are loaded, and a pointer to each texture is stored in the texture array.

Next, a cube mesh is loaded using the Direct3DRMMeshBuilder interface. The cube is scaled using the Scale() function:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL;
resinfo.lpName=MAKEINTRESOURCE( IDR_BOXMESH );
resinfo.lpType="MESH";
LPDIRECT3DRMMESHBUILDER meshbuilder;
d3drm->CreateMeshBuilder( &meshbuilder );
meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
    NULL, NULL );
meshbuilder->Scale( D3DVALUE(1), D3DVALUE(1), D3DVALUE(.1) );
ScaleMesh( meshbuilder, D3DVALUE(20) );
meshbuilder->SetPerspective( TRUE );
meshbuilder->SetQuality( D3DRMRENDER_FLAT );
meshbuilder->SetTexture( texture[0] );

This is done because we want a nearly flat rectangular mesh to which we can apply the textures. Rather than create a rectangular mesh, we can use a cube with its Z dimension scaled to one-tenth its original size. The new mesh is then configured to use perspective correction and the flat rendering mode. The first texture in the texture array is associated with the new mesh.

Step 3 is the creation and application of a texture wrap:

D3DRMBOX box;
meshbuilder->GetBox(&box);
D3DVALUE width=box.max.x-box.min.x;
D3DVALUE height=box.max.y-box.min.y;

LPDIRECT3DRMWRAP wrap;
d3drm->CreateWrap( D3DRMWRAP_FLAT, NULL,
        D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0),  // wrap origin
        D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0),  // z axis
        D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0),  // y axis
        D3DVALUE(0.5), D3DVALUE(0.5),                 // origin
        D3DDivide(1,width),D3DDivide(1,height),       // scale
        &wrap );
wrap->Apply( meshbuilder );
wrap->Release();
wrap=0;

A flat texture wrap is created that sizes the texture to fit snugly on the mesh. The texture wrap is applied to the meshbuilder with the Apply() function.

Next, the previously loaded meshbuilder is used to initialize a pointer to the Direct3DRMMesh interface:

meshbuilder->CreateMesh( &mesh );
meshbuilder->Release();
meshbuilder=0;

The new mesh is created with all of the settings of the meshbuilder.

Next, a frame is created and the mesh is attached:

LPDIRECT3DRMFRAME meshframe;
d3drm->CreateFrame( scene, &meshframe );
meshframe->AddVisual( mesh );
meshframe->SetRotation( scene,
        D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
        D3DVALUE(0.05) );
meshframe->AddMoveCallback( UpdateTexture, NULL );
meshframe->Release();
meshframe=0;

The new frame is given a rotation attribute around the Y axis. Also, the UpdateTexture() callback function is installed.

The ShowRoom::UpdateTexture() Function

The UpdateTexture() callback function is responsible for installing a new texture for each screen update. The UpdateTexture() function looks like this:

void ShowRoomWin::UpdateTexture(LPDIRECT3DRMFRAME frame,
    void*, D3DVALUE)
{
    static UINT count;
    int curtex=count%15;
    frame->DeleteVisual( mesh );
    mesh->SetGroupTexture( 0, texture[curtex] );
    frame->AddVisual( mesh );
    count++;
}

A static counter variable is used to determine which texture is to be applied to the mesh. The new texture is applied with the Direct3DRMMesh SetGroupTexture() function, but the meshbuilder must be removed from the frame and added after the SetGroupTexture() function is called. This is because the SetGroupTexture() function has no effect once a meshbuilder has been added to a frame.

The Direct3DRMMesh SetGroupTexture() function is similar to the Direct3DRMMeshBuilder SetTexture() function that we used in the previous demos. The difference is that the Direct3DRMMesh interface supports groups, and the Direct3DRMMeshBuilder interface does not. A group is a set of faces than can be manipulated as a single entity. We’ll talk about the Direct3DRMMesh interface in more detail in Chapter 8.

Conclusion

The ShowRoom demo concludes our discussion of texture mapping. As usual, you are strongly encouraged to experiment with the demos—that’s what they’re for. When you are finished experimenting, you’ll be ready to move on to the next chapter and learn all about light sources and shadows.


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.