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


Picking Faces

Now we know how to use viewports to pick meshes. What if, however, we wanted to pick individual faces in a mesh? No problem. In fact, the code looks very similar to the code for picking meshes.

The FacePick Demo

The FacePick demo displays a mesh and allows you to pick individual faces within the mesh. Selected faces change to a color that you can specify from the Colors menu. The mesh can be rotated by selecting any portion of the viewport not occupied by the mesh and moving the mouse. This allows you to reposition the mesh, giving you the ability to change the color of any face in the mesh. Also, the demo’s File menu allows you to load and save meshes. The FacePick demo appears as Figure 9.3.


Figure 9.3  The FacePick demo.

The FacePick demo demonstrates the following techniques:

  Using the Direct3DRMViewport Pick() function for face-picking operations
  Using mouse input to manipulate visual objects
  Loading and saving mesh files
  Using MFC’s CFileDialog class
  Using MFC’s CColorDialog class

The FacePickWin Class

The FacePickWin class provides the FacePick demo’s functionality:

class FacePickWin : public RMWin
{
public:
    FacePickWin();
    BOOL CreateScene();
protected:
    //{{AFX_MSG(FacePickWin)
    afx_msg void OnRenderWireframe();
    afx_msg void OnRenderGouraud();
    afx_msg void OnRenderFlat();
    afx_msg void OnUpdateRenderWireframe(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderFlat(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderGouraud(CCmdUI* pCmdUI);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnColorsFace();
    afx_msg void OnColorsMesh();
    afx_msg void OnFileOpen();
    afx_msg void OnFileSave();
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    static void UpdateDrag(LPDIRECT3DRMFRAME, void*, D3DVALUE);
    int PickFace(const CPoint& point);
    void OnIdle(long);
private:
    LPDIRECT3DRMMESHBUILDER meshbuilder;
    LPDIRECT3DRMFRAME meshframe;
    D3DCOLOR pickcolor;
    D3DVALUE meshscale;
    static BOOL drag;
    static BOOL end_drag;
    static int last_x, last_y;
};

The class declares two public member functions: a constructor and the CreateScene() function. The constructor is used to initialize the class’s non-static data members. The CreateScene() function constructs the demo’s initial scene.

Twelve protected member functions are declared. The first six are the Render menu message handlers that are present in most of the demos. The OnLButtonDown() and OnLButtonUp() functions are used to react to changes in the mouse button’s status.

The OnColorsFace() and OnColorsMesh() functions implement the Colors menu functionality. Both functions display color selection dialogs using MFC’s CColorDialog.

The OnFileOpen() and OnFileSave() both use MFC’s CFileDialog to allow the user to select or enter file names.

Three private member functions are declared: UpdateDrag(), PickFace(), and OnIdle(). The UpdateDrag() function is a callback function that is used to regularly update the mesh’s position. The PickFace() function is similar to the PickMesh() function that we used in the PickMesh demo. PickFace() is responsible for the demos’ picking operations.

The FacePickWin::CreateScene() Function

The FacePick demo scene is constructed with the FacePickWin::CreateScene() function, as shown in Listing 9.3.

Listing 9.3 The FacePickWin::CreateScene() function.

BOOL FacePickWin::CreateScene()
{
    // ------- MESH --------
    D3DRMLOADRESOURCE resinfo;
    resinfo.hModule=NULL;
    resinfo.lpName=MAKEINTRESOURCE( IDR_D3DMESH );
    resinfo.lpType="MESH";
    d3drm->CreateMeshBuilder( &meshbuilder );
    meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE,
            NULL, NULL );
    meshbuilder->SetQuality( D3DRMRENDER_FLAT );
    ScaleMesh( meshbuilder, D3DVALUE(25) );

    //------- FRAME ------
    d3drm->CreateFrame( scene, &meshframe );
    meshframe->SetRotation( scene,
            D3DVALUE(1), D3DVALUE(0), D3DVALUE(0),
            D3DVALUE(.05) );
    meshframe->AddVisual( meshbuilder );
    meshframe->AddMoveCallback( UpdateDrag, NULL );

    // --------- 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.40), D3DVALUE(0.40), D3DVALUE(0.40),
            &alight );

    LPDIRECT3DRMFRAME lightframe;
    d3drm->CreateFrame( scene, &lightframe );
    lightframe->AddLight( dlight );
    lightframe->AddLight( alight );
    lightframe->SetOrientation( scene,
            D3DVALUE(0), D3DVALUE(-1), D3DVALUE(1),
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) );

    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 four steps:

1.  Creates a mesh
2.  Creates a frame for the mesh
3.  Creates two light sources
4.  Creates a viewport


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.