![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
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 doesnt 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).
No matter what object you selected, it probably doesnt 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:
The AmbientLightWin ClassThe 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 classs 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. Well look at these functions later. The AmbientLightWin::CreateScene() FunctionThe 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:
The first step uses the Direct3DRMMeshBuilder interface to load a mesh from a file. Lets 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;
|
![]() |
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. |