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 mesh is loaded from the demo’s resources with the Direct3DRMMeshBuilder Load() function. The SetQuality() function is used to demote the meshbuilder’s default Gouraud rendering quality setting to flat.

In Step 2, a loop is used to create nine meshes. The previously initialized meshbuilder is used:

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;

The nine meshes are created with a nested loop. The body of the inner loop uses the Direct3DRMMeshBuilder CreateMesh() function to create a mesh. The mesh’s color is assigned depending on the loop’s current iteration. Next, a frame is created and attached to the mesh with the AddVisual() function. The frame’s location is dependent on the loop’s current iteration but is altered with random values (this is done to suggest to the user that the meshes can be moved). Each frame is given a rotation attribute before the meshframe and mesh pointers are released.

Next, a callback function is installed:

scene->AddMoveCallback( UpdateDrag, NULL );

The callback function is installed using the scene frame (the root frame). This is arbitrary; any frame in the scene will do.

Step 4 is the creation of a light source:

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;

This code creates a directional light and positions it facing between the positive Z and negative Y axes. The light is attached to the frame using the Direct3DRMFrame AddLight() function.

Finally, a viewport is created:

d3drm->CreateFrame( scene, &camera );
camera->SetPosition( scene,
        D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50) );
d3drm->CreateViewport( device, camera, 0, 0,
        device->GetWidth(), device->GetHeight(),
        &viewport );

The camera frame pointer is initialized with the Direct3DRM CreateFrame() function and positioned with the Direct3DRMFrame SetPosition() function. The viewport pointer is initialized with the Direct3DRM CreateViewport() function.

The MeshPickWin Mouse Functions

The MeshPick demo uses two message handling member functions to react to changes of the mouse button state. The OnLButtonDown() function is invoked by MFC whenever the user presses the left mouse button. The function looks like this:

void MeshPickWin::OnLButtonDown(UINT nFlags, CPoint point)
{
    if (PickMesh( point ))
    {
        ShowCursor( FALSE );
        SetCapture();
    }
    RMWin::OnLButtonDown( nFlags, point );
}

MFC passes two arguments to the OnLButtonDown() function. The first is a set of flags that indicates the status of certain keys (CRTL, SHIFT, etc.) at the time of the mouse click. The second parameter, point, indicates the location of the mouse when the button was pressed. The OnLButtonDown() function uses the point parameter as an argument to the PickMesh() function. PickMesh() determines whether an object is present at the specified location, and, if so, initiates a drag sequence. If such a sequence is initiated, PickMesh() returns TRUE, and the ShowCursor() function is used to hide the mouse cursor (the cursor is hidden during the drag operation). Also, the SetCapture() function is used to notify Windows that we expect to receive all mouse-related messages, even if the mouse is moved outside of our window. Finally, the base class version of OnLButtonDown() is called.

The OnLButtonUp() function is called whenever the left mouse button is released. The function looks like this:

void MeshPickWin::OnLButtonUp(UINT nFlags, CPoint point)
{
    if (drag.frame)
    {
        drag.frame=0;
        ShowCursor( TRUE );
        ReleaseCapture();
    }
    RMWin::OnLButtonUp( nFlags, point );
}

The function checks the value of the drag.frame data member. This member is used to indicate (1) whether a drag sequence is currently in effect, and (2) what frame is being dragged. The drag.frame data member is initialized by the PickMesh() function if an object is detected at the given mouse position.

If the drag.frame data member is not equal to zero, a drag sequence is currently in effect and must now be terminated (because the mouse button was released). The data member is assigned to zero, the mouse cursor is displayed, and the mouse capture is released.


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.