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 TextureDriftWin::MoveTexture() Function

MoveTexture() is a callback function that creates and applies a texture wrap that differs for each invocation. First, however, the Direct3DRMMesh interface that was added to the frame by the CreateScene() function must be retrieved. The MoveTexture() callback function appears as Listing 5.6.

Listing 5.6 The TextureDriftWin::MoveTexture() function.

void TextureDriftWin::MoveTexture(LPDIRECT3DRMFRAME frame,
    void*, D3DVALUE)
{
    static D3DVALUE xtex;
    xtex+=D3DVALUE(.02);

    LPDIRECT3DRMVISUALARRAY visualarray;
    frame->GetVisuals( &visualarray );
    int nvisuals = visualarray->GetSize();
    for ( int i = 0; i < nvisuals; i++ )
    {
        LPDIRECT3DRMVISUAL visual;
        visualarray->GetElement( i, &visual );
        LPDIRECT3DRMMESH mesh;
        if (visual->QueryInterface( IID_IDirect3DRMMesh,
            (void**)&mesh) == 0)
        {
            D3DRMBOX box;
            mesh->GetBox( &box );
            D3DVALUE w=box.max.x-box.min.x;
            D3DVALUE h=box.max.y-box.min.y;

            LPDIRECT3DRMWRAP wrap;
            d3drm->CreateWrap( D3DRMWRAP_FLAT, NULL,
                    D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(0.0),
                    D3DVALUE(0.0), D3DVALUE(0.0), D3DVALUE(1.0),
                    D3DVALUE(0.0), D3DVALUE(1.0), D3DVALUE(0.0),
                    xtex, D3DVALUE(0.5),             // texture origin
                    D3DDivide(1,w), D3DDivide(1,h),  // texture scale
                    &wrap );
            wrap->Apply( mesh );
            wrap->Release();
            wrap=0;
            mesh->Release();
            mesh=0;
        }
        visual->Release();
        visual=0;
    }
    visualarray->Release();
    visualarray=0;
}

The MoveTexture() function first declares a static D3DVALUE that it increments on each invocation. This value is used as the texture’s X origin value, so incrementing the value causes the texture to drift across in the mesh.

Next, the Direct3DRMFrame GetVisuals() function is used to retrieve an array of visual objects that are attached to the frame (the frame pointer is passed to the callback function by Direct3D as the function’s first parameter).

The GetVisuals() function initializes a pointer to the Direct3DRMVisualArray interface. Direct3DRMVisualArray is an interface that supports only two member functions: GetSize() and GetElement().

A loop is used to iterate through the array of visual objects. The number of items in the array is determined with the GetSize() function, and each item in the array is retrieved with the GetElement() function. The pointers retrieved from the array are pointers to the Direct3DRMVisual interface (which provides no member functions). The QueryInterface() is used to inquire as to whether the object supports the Direct3DRMMesh interface. We know that one, and only one, of the objects retrieved by GetElement() will support the Direct3DRMMesh interface because we created and attached the mesh to this frame in the CreateScene() function.

Once a pointer to the Direct3DRMMesh interface has been acquired, a Direct3DRMWrap interface is created and applied to the mesh.

The ShowRoom Demo

The ShowRoom demo performs texture animation by using multiple textures on a single mesh, one at a time. The mesh used in the ShowRoom demo is a simple box. The textures are rendered images of a car facing different directions. A different texture is used for each screen update, so the car appears to be animated. The mesh that the textures are applied to is rotating, so both the mesh and the texture are animated. The ShowRoom demo appears in Figure 5.11.


Figure 5.11  The ShowRoom demo.

The ShowRoom demo demonstrates the following techniques:

  Performing texture animation with multiple textures
  Using the Direct3DRMMesh interface
  Animation with rotation attributes

Like the TextureDrift demo, the ShowRoom demo used the Direct3DRMMesh interface to avoid the extra overhead of the Direct3DRMMeshBuilder interface.

The ShowRoomWin Class

The ShowRoom demo’s functionality is provided in the ShowRoomWin class. The class is defined like this:

class ShowRoomWin : public RMWin
{
public:
    BOOL CreateScene();
    static void UpdateTexture(LPDIRECT3DRMFRAME frame, void*, D3DVALUE);
protected:
    //{{AFX_MSG(ShowRoomWin)
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    static LPDIRECT3DRMMESH mesh;
    static LPDIRECT3DRMTEXTURE texture[15];
};

Two public member functions are declared: CreateScene() and UpdateTexture(). The CreateScene() function is used to construct the scene and the UpdateTexture() function is a callback function that is used to change the texture that is currently applied the to demo’s mesh.

Two private data members are declared: mesh and texture. The mesh data member is a pointer to the Direct3DRMMesh interface and will be used to access the mesh on which our animated texture will appear. The texture data member is an array of textures that will each appear on the mesh.


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.