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


Accept the default settings on the following dialog, and press the Finish button. The AppWizard will then display a confirmation dialog. Press the OK button. Developer Studio will now create a new application according to the settings you have specified. When Developer Studio is finished (it doesn’t take long), compile and run the new application. The new application will look like the one in Figure 6.3 (particularly if you choose the sphere1.x file).


Figure 6.3  The application created by the Direct3D AppWizard.


TIP:  Dependency warnings
Visual C++ often displays a number of warnings when a new project is first compiled or when Update All Dependencies is selected from the Build menu. This is because Visual tries to locate all H files that are used with the #include directive, and a message is displayed whenever an H file cannot be found. Unfortunately, Visual does not preprocess files prior to searching for the names of H files. This means that H files that are not used by a project because of an #ifdef conditional are searched for along with the H files that are used by the project. These warnings can safely be ignored.

No matter what object you selected, it probably doesn’t look very good. This is because the ambient light source illuminates each part of the mesh with the same intensity. The result is a silhouette of the object. If you apply a texture to the mesh, it will look a little better, but it will still appear dull and flat.

We will use the project that we have just created to demonstrate the following techniques:

  Using an ambient light source
  Using the Direct3DRMMeshBuilder interface
  Changing a mesh’s rendering method at runtime

The AmbientLightWin Class

The majority of the functionality in our new project is supplied by the AmbientLightWin class. The class is defined like this:

class AmbientLightWin : public RMWin
{
public:
    AmbientLightWin();
    BOOL CreateScene();
protected:
    //{{AFX_MSG(AmbientLightWin)
    afx_msg void OnRenderWireframe();
    afx_msg void OnRenderFlat();
    afx_msg void OnRenderGouraud();
    afx_msg void OnUpdateRenderFlat(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderGouraud(CCmdUI* pCmdUI);
    afx_msg void OnUpdateRenderWireframe(CCmdUI* pCmdUI);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
private:
    LPDIRECT3DRMMESHBUILDER meshbuilder;
};

The AmbientLightWin class is derived from the RMWin class. It declares two public functions: a constructor and a CreateScene() function. The constructor initializes the class’s only data member:

AmbientLightWin::AmbientLightWin()
{
    meshbuilder=0;
}

The meshbuilder data member is a pointer to the Direct3DRMMeshBuilder interface and will be used to point to the mesh in the application. The meshbuilder pointer is initialized by the CreateScene() function, which we will look at soon.

The class declares six protected member functions that serve as message handlers. The first three functions, OnRenderWireframe(), OnRenderFlat(), and OnRenderGouraud(), are called when one of the Render menu items is selected. The last three functions are called by MFC just before a menu is displayed. These functions are used to activate the check mark that appears next to the currently active menu entry method. We’ll look at these functions later.

The AmbientLightWin::CreateScene() Function

The scene is constructed by the CreateScene() function. If you named your project “AmbientLight”, the CreateScene() function will look similar to Listing 6.1.

Listing 6.1 The AmbientLightWin::CreateScene() function.

BOOL AmbientLightWin::CreateScene()
{
    HRESULT r;
    // ------MESH--------
    d3drm->CreateMeshBuilder( &meshbuilder );
    r=meshbuilder->Load( meshname, NULL, D3DRMLOAD_FROMFILE,
                NULL, NULL );
    if (r!=D3DRM_OK)
    {
        CString msg;
        msg.Format( "Failed to load file '%s'\n", meshname );
        AfxMessageBox( msg );
        return FALSE;
    }
    ScaleMesh( meshbuilder, D3DVALUE(25) );

    //------- MESH FRAME --------
    LPDIRECT3DRMFRAME meshframe;
    d3drm->CreateFrame( scene, &meshframe );
    meshframe->AddVisual( meshbuilder );
    meshframe->SetRotation( scene,
            D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
            D3DVALUE(.1) );
    meshframe->Release();
    meshframe=0;

    // --------AMBIENT LIGHT--------
    LPDIRECT3DRMLIGHT alight;
    d3drm->CreateLightRGB( D3DRMLIGHT_AMBIENT,
            D3DVALUE(1.00), D3DVALUE(1.00), D3DVALUE(1.00),
            &alight );
    scene->AddLight( alight );
    alight->Release();
    alight=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 these steps:

1.  Creates and loads a mesh
2.  Creates a frame for the mesh
3.  Creates an ambient light source
4.  Creates a viewport

The first step uses the Direct3DRMMeshBuilder interface to load a mesh from a file. Let’s take a closer look:

d3drm->CreateMeshBuilder( &meshbuilder );
r=meshbuilder->Load( meshname, NULL, D3DRMLOAD_FROMFILE, NULL, NULL );
if (r!=D3DRM_OK)
{
    CString msg;
    msg.Format( "Failed to load file '%s'\n", meshname );
    AfxMessageBox( msg );
    return FALSE;
}
ScaleMesh( meshbuilder, D3DVALUE(25) );

First, the meshbuilder data member is initialized with the Direct3DRM CreateMeshBuilder() function. The Load() function is then used to load the mesh file from disk (the meshname variable identifies the mesh file that you selected with the AppWizard). If the Load() function fails, a message box is displayed and FALSE is returned. If the Load() function succeeds, the ScaleMesh() function is used to determine an ideal size for the mesh.

The second step of the CreateScene() function is the creation of a frame to which the mesh will be attached. The code looks like this:

LPDIRECT3DRMFRAME meshframe;
d3drm->CreateFrame( scene, &meshframe );
meshframe->AddVisual( meshbuilder );
meshframe->SetRotation( scene,
        D3DVALUE(0), D3DVALUE(1), D3DVALUE(0),
        D3DVALUE(.1) );
meshframe->Release();
meshframe=0;


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.