![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The FacePickWin Color FunctionsThe FacePick demo supports two color selection dialogs. These dialogs are managed by the OnColorsFace() and OnColorsMesh() functions. The OnColorsFace() function is responsible for allowing the user to select a color that will be applied to any faces selected with the mouse. The OnColorsMesh() function allows the user to select a color to be applied to the entire mesh. Both functions use MFCs CColorDialog class. The OnColorsFace() function looks like this: void FacePickWin::OnColorsFace() { CColorDialog dialog( 0, CC_RGBINIT ); dialog.m_cc.rgbResult = D3DCOLOR_2_COLORREF( pickcolor ); if (dialog.DoModal()==IDOK) { COLORREF clr = dialog.GetColor(); pickcolor = COLORREF_2_D3DCOLOR( clr ); } } The CColorDialog allows us to specify a color that will be selected when the dialog appears. This allows us to display the dialog with the current face color selected (actually, this only works if the color you specify is one of the dialogs displayed colors). The CC_RGBINIT constant is used as an argument to the constructors dialog to indicate that we will specify a default color. The default color is assigned to the m_cc.rgbResult data member. The pickcolor variable is a MeshPickWin data member that indicates the current face color. The pickcolor data member is of type D3DCOLOR, and the dialog expects colors to be expressed as COLORREF, so a conversion function is required to make the assignment valid. The RMWin class provides the D3DCOLOR_2_COLORREF() and COLORREF_2_D3DCOLOR() for this purpose. (See Chapter 4.) We use the DoModal() member function for modal dialog operation. If the user dismisses the dialog in any way other than pressing OK, the function returns without performing further action. If the IDOK constant is returned, the new color is assigned to the pickcolor data member. The OnColorsMesh() function is similar: void FacePickWin::OnColorsMesh() { CColorDialog dialog; if (dialog.DoModal()==IDOK) { COLORREF clr = dialog.GetColor(); D3DCOLOR meshcolor = COLORREF_2_D3DCOLOR( clr ); meshbuilder->SetColor( meshcolor ); } } Unlike the OnColorsFace() function, the OnColorsMesh() function does not specify a default color for the dialog. If the user pressed the OK button, the new color is extracted from the dialog class with the GetColor() function. The resulting COLORREF is converted with the COLORREF_2_ D3D-COLOR() function and installed with the Direct3DRMMeshBuilder Setcolor() function. The FacePickWin File FunctionsThe FacePick demo allows meshes to be loaded and saved from the File menu. The FacePickWin class provides the OnFileOpen() and OnFileSave() functions for this purpose. The OnFileOpen() function looks like this: void FacePickWin::OnFileOpen() { static char BASED_CODE filter[] = "X Files (*.x)|*.x|All Files (*.*)|*.*||"; CFileDialog opendialog( TRUE, 0, 0, OFN_FILEMUSTEXIST, filter, this ); if (opendialog.DoModal()==IDOK) { CWaitCursor cur; CString filename = opendialog.GetPathName(); LPDIRECT3DRMMESHBUILDER builder; d3drm->CreateMeshBuilder( &builder ); HRESULT r=builder->Load( (void*)(LPCTSTR)filename, NULL, D3DRMLOAD_FROMFILE, NULL, NULL ); if (r!=D3DRM_OK) { CString msg; msg.Format( "Failed to load file\n'%s'", filename ); AfxMessageBox( msg ); return; } meshframe->DeleteVisual( meshbuilder ); meshbuilder->Release(); meshbuilder=builder; meshframe->AddVisual( meshbuilder ); meshscale=ScaleMesh( meshbuilder, D3DVALUE(25) ); } } The function uses MFCs CFileDialog class. Notice that the filter string is used as an argument to the classs constructor. The string informs the dialog of the types of files that will be loaded. The DoModal() function is used to execute the dialog. If the IDOK constant is returned, the filename is extracted from the dialog class with the GetPathName() function. Notice that the files existence is not verified. This is because we used the OFN_FILEMUSTEXIST constant to construct the dialog object. The constant indicates to the dialog class that file names entered manually should be checked for existence. This dialog will not allow the user to specify a file that doesnt exist (this is no guarantee that the file will be valid, only that it exists). Next, the function attempts to load the new mesh. If the attempt fails, a message box is displayed, and the function returns. This failure is fairly graceful because the scenes existing mesh will still be displayed. If the new mesh is loaded successfully, the existing mesh is removed from the scene with the Direct3DRMFrame DeleteVisual() function, and the existing meshbuilder is released. The new meshbuilder is then added to the scene with the AddVisual() function. The last step is a call to the ScaleMesh() function. Weve seen this function used in the other demos, but this is a little different. Recall that ScaleMesh() scales meshes to a desired size. In this case, we are scaling any mesh we load to 25 units. This worked fine in the other demos, and it works here, but if the scaled mesh is saved back to disk, its size will have changed. This requires that we return the mesh to its original size before saving it. The ScaleMesh() function returns a value that indicates the scale factor that was used to scale the mesh. Storing this value allows us to restore the mesh to its original size. Well see how this is done because we are going to talk about the OnFileSave() function next. The OnFileSave() function is defined this way: void FacePickWin::OnFileSave() { static char BASED_CODE filter[] = "X Files (*.x)|*.x||"; CFileDialog opendialog( FALSE, ".x", "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, filter ); if ( opendialog.DoModal() == IDOK ) { CWaitCursor cur; CString filename = opendialog.GetPathName(); D3DVALUE restorescale=D3DVALUE(1)/meshscale; meshbuilder->Scale( restorescale, restorescale, restorescale ); meshbuilder->Save( filename, D3DRMXOF_BINARY, D3DRMXOFSAVE_ALL ); meshbuilder->Scale( meshscale, meshscale, meshscale ); } }
|
![]() |
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. |