![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The MorphWin::AddMorphKey() FunctionThe AddMorphKey() function associates a morph target with a specific time within the animation. The function expects a target index and a time index as arguments. BOOL MorphWin::AddMorphKey( DWORD target, D3DVALUE time ) { if (target<0 || target>nummorphtargets) return FALSE; for (DWORD i=0;i<nummorphvertices;i++) { D3DVECTOR& pos=morphmeshdata[target][i].position; posanimation[i]->AddPositionKey( time, pos.x, pos.y, pos.z ); D3DVECTOR& norm=morphmeshdata[target][i].normal; normanimation[i]->AddPositionKey( time, norm.x, norm.y, norm.z ); } return TRUE; } First, the morph target index is checked for validity. If the index is not valid, the function returns FALSE. Otherwise, the supplied morph target index is used to add position keys into the position and normal animation objects for each vertex. The keys are added with the Direct3DRMAnimation AddPositionKey() function. Notice that the time parameter is used as the first argument to AddPositionKey(). The MorphWin::SetMorphTime() FunctionThe SetMorphTime() function is responsible for generating and installing the vertex positions and normals. The function looks like this: BOOL MorphWin::SetMorphTime( D3DVALUE time ) { for (DWORD v=0; v<nummorphvertices; v++) { posanimation[v]->SetTime( time ); D3DVECTOR pos; posframe[v]->GetPosition( scene, &pos ); morphvertex[v].position=pos; normanimation[v]->SetTime( time ); D3DVECTOR norm; normframe[v]->GetPosition( scene, &norm ); morphvertex[v].normal=norm; } morphmesh->SetVertices( 0, 0, nummorphvertices, morphvertex ); return TRUE; } A loop is used to iterate once for each vertex. The Direct3DRMAnimation SetTime() function is used to update the animation sequence for both the vertex position and the vertex normal. The frame attached to each animation object is then used to assign the morphvertex array entries. Once a new location and normal has been generated for each vertex, the new data is assigned with the Direct3DRMMesh SetVertices() function. The MorphWin::GetMorphMesh() FunctionThe GetMorphMesh() function simply returns a pointer to the mesh that is being used to represent the morph sequence. The function is defined inline like this: LPDIRECT3DRMMESH GetMorphMesh() { return morphmesh; } This function allows classes derived from MorphWin to display and manipulate the mesh. Youll see how this is done next. The MorphPlayWin ClassThe MorphPlayWin class builds on the MorphWin class to create a complete application. The class is defined this way: class MorphPlayWin : public MorphWin { public: MorphPlayWin(); BOOL CreateScene(); protected: //{{AFX_MSG(MorphPlayWin) afx_msg void OnFileOpen(); afx_msg void OnMorphForward(); afx_msg void OnMorphReverse(); afx_msg void OnMorphBoth(); afx_msg void OnUpdateMorphForward(CCmdUI* pCmdUI); afx_msg void OnUpdateMorphReverse(CCmdUI* pCmdUI); afx_msg void OnUpdateMorphBoth(CCmdUI* pCmdUI); afx_msg void OnSpeedExtrafast(); afx_msg void OnSpeedFast(); afx_msg void OnSpeedMedium(); afx_msg void OnSpeedSlow(); afx_msg void OnSpeedExtraslow(); afx_msg void OnUpdateSpeedExtrafast(CCmdUI* pCmdUI); afx_msg void OnUpdateSpeedFast(CCmdUI* pCmdUI); afx_msg void OnUpdateSpeedMedium(CCmdUI* pCmdUI); afx_msg void OnUpdateSpeedSlow(CCmdUI* pCmdUI); afx_msg void OnUpdateSpeedExtraslow(CCmdUI* pCmdUI); afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: BOOL InitMorphSequence( const CString& ); static void UpdateMorph(LPDIRECT3DRMFRAME frame, void*, D3DVALUE); static void UpdateDrag(LPDIRECT3DRMFRAME frame, void*, D3DVALUE); void OnIdle(LONG); private: LPDIRECT3DRMFRAME frame; LPDIRECT3DRMMESH mesh; int morphspeed; D3DVALUE morphtimeinc; D3DVALUE maxmorphtime; static D3DVALUE morphtime; static BOOL drag; static BOOL end_drag; static int last_x, last_y; }; Two public member functions are declared: a constructor and the CreateScene() function. The constructor initializes the classs data members. The CreateScene() function initializes the demos light sources and viewport. The class provides an OnFileOpen() function to react to File|Open menu commands. The function uses the MFC CFileDialog class to present a file selection dialog. Three menu command handler functions provide support for the demos Morph menu: OnMorphForward(), OnMorphReverse(), and OnMorphBoth(). Five menu command handler functions provide Speed menu functionality: OnSpeedExtrafast(), OnSpeedFast(), OnSpeedMedium(), OnSpeedSlow(), and OnSpeedExtraslow(). Each of these functions has a complimentary OnUpdate() function. The OnLButtonDown() and OnLButtonUp() functions will be used to initiate and terminate mouse drag operations (for mesh rotation). Next, the InitMorphSequence() function is declared. Well use this function to load new morph sequences. The UpdateMorph() callback function is used to control the speed and direction of the morph sequence. The UpdateDrag() callback function is used to implement mouse drag operations where the mesh is rotated according to mouse movement. The OnIdle() function is used to stabilize the mesh rotation during drag operations. Next, the classs data members are declared. The frame and mesh data members are pointers to the morph mesh frame and the morph mesh, respectively. The morphspeed, morphtimeinc, maxmorphtime, and morphtime data members are used to control the morph sequence. The drag, end_drag, last_x, and last_y data members are used to rotate the mesh frame during drag operations.
|
![]() |
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. |