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 MeshPick Demo

The MeshPick demo displays nine spherical meshes and allows you to use the mouse to pick and drag any of the meshes. The meshes can be placed very near to each other, allowing you to test the accuracy of the picking mechanism. The MeshPick demo appears in Figure 9.2.


Figure 9.2  The MeshPick demo.

The MeshPick demo demonstrates the following techniques:

  Using the Direct3DRMViewport Pick() function
  Using mouse input to manipulate visual objects

We’ll talk about each of these techniques as we walk through the MeshPick code.

The MeshPickWin Class

The MeshPick demo provides its functionality with the MeshPickWin class:

class MeshPickWin : public RMWin
{
public:
    MeshPickWin();
    BOOL CreateScene();
protected:
    //{{AFX_MSG(MeshPickWin)
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    static void UpdateDrag(LPDIRECT3DRMFRAME frame, void*, D3DVALUE);
    BOOL PickMesh( const CPoint& point );
private:
    static DragData drag;
};

The class declares two public member functions: a constructor and the CreateScene() function. The constructor initializes the class’s single data member, and the CreateScene() function constructs the demo’s scene.

Two protected message handling functions are declared, OnLButtonDown() and OnLButtonUp(). MFC calls these functions to notify the application of changes in the left mouse button’s status. We’ll use the OnLButtonDown() function to perform the picking procedure and initiate a drag mode that allows the picked object to be moved with the mouse. The OnLButtonUp() function is used to terminate the drag mode.

There are two private member functions: UpdateDrag() and PickMesh(). UpdateDrag() is a callback function that will be used to poll the current state of the application. If the application is in a drag mode, the UpdateDrag() function will adjust the location of the currently “picked” mesh based on the position of the mouse. The PickMesh() function is used to perform the actual picking operation.

The class’s only data member is a structure of type DragData. The structure is defined like this:

struct DragData
{
    LPDIRECT3DRMFRAME frame;
    POINT mousedown;
    D3DVALUE origx,origy;
};

The structure contains data that is pertinent during a drag operation. The structure’s frame data member will point to the frame that is attached to the mesh that is being dragged. The mousedown field is used to store the mouse location where the drag operation was initiated. The origx and origy data members are used to store the location of the frame when the drag operation started.

The MeshPickWin::CreateScene() Function

The MeshPick demo CreateScene() function appears in Listing 9.2.

Listing 9.2 The MeshPickWin::CreateScene() function.

BOOL MeshPickWin::CreateScene()
{
    // ------- MESH BUILDER --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
    resinfo.lpType="MESH";
    LPDIRECT3DRMMESHBUILDER meshbuilder;
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    meshbuilder->SetQuality( D3DRMRENDER_FLAT );

    //------ NINE MESHES ----------
    for (int x=0;x<3;x++)
    {
        for (int y=0;y<3;y++)
        {
            LPDIRECT3DRMMESH mesh;
            meshbuilder->CreateMesh( &mesh );
            mesh->SetGroupColorRGB( 0,
                    D3DVALUE(x%2), D3DVALUE(y%2), D3DVALUE(1) );

            LPDIRECT3DRMFRAME meshframe;
            d3drm->CreateFrame( scene, &meshframe );
            meshframe->AddVisual( mesh );
            int xoffset=(rand()%3)-1;
            int yoffset=(rand()%3)-1;
            meshframe->SetPosition( scene,
                    D3DVALUE((x-1)*10+xoffset),
                    D3DVALUE((y-1)*10+yoffset),
                    D3DVALUE(0) );
            meshframe->SetRotation( scene,
                    D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
                    D3DVALUE(.1) );

            meshframe->Release();
            meshframe=0;
            mesh->Release();
            mesh=0;
        }
    }

    meshbuilder->Release();
    meshbuilder=0;

    //------- CALLBACK --------
    scene->AddMoveCallback( UpdateDrag, NULL );

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

    LPDIRECT3DRMFRAME dlightframe;
    d3drm->CreateFrame( scene, &dlightframe );
    dlightframe->AddLight( dlight );
    dlightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );
    dlight->Release();
    dlight=0;
    dlightframe->Release();
    dlightframe=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.  Uses the Direct3DRMMeshBuilder interface to load a spherical mesh
2.  Creates nine meshes, each with a frame
3.  Installs the UpdateDrag() callback function
4.  Creates a light source
5.  Creates a viewport

The first step is the creation of a meshbuilder:

D3DRMLOADRESOURCE resinfo;
resinfo.hModule=NULL
resinfo.lpName=MAKEINTRESOURCE( IDR_SPHEREMESH );
resinfo.lpType="MESH";
LPDIRECT3DRMMESHBUILDER meshbuilder;
d3drm->CreateMeshBuilder( &meshbuilder );
meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,NULL, NULL );
meshbuilder->SetQuality( D3DRMRENDER_FLAT );


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.