![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The RocketWin ClassThe Rocket demo provides its functionality with the RocketWin class: class RocketWin : public RMWin { public: RocketWin(); BOOL CreateScene(); protected: //{{AFX_MSG(RocketWin) 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 void OnAnimationLinear(); afx_msg void OnAnimationSpline(); afx_msg void OnUpdateAnimationLinear(CCmdUI* pCmdUI); afx_msg void OnUpdateAnimationSpline(CCmdUI* pCmdUI); afx_msg void OnSpeedFast(); afx_msg void OnSpeedMedium(); afx_msg void OnSpeedSlow(); afx_msg void OnUpdateSpeedFast(CCmdUI* pCmdUI); afx_msg void OnUpdateSpeedMedium(CCmdUI* pCmdUI); afx_msg void OnUpdateSpeedSlow(CCmdUI* pCmdUI); //}}AFX_MSG DECLARE_MESSAGE_MAP() private: static void UpdateScene(LPDIRECT3DRMFRAME, void*, D3DVALUE); static HRESULT LoadTexture(char*, void*, LPDIRECT3DRMTEXTURE*); private: LPDIRECT3DRMMESHBUILDER meshbuilder; LPDIRECT3DRMANIMATION animation; static D3DVALUE speed; }; Two public member functions are declared: a constructor and CreateScene(). The constructor initializes the classs data members to zero. The CreateScene() function constructs the demos scene, including the animation sequence. Sixteen protected message handling functions are declared. The first six provide the demos Render menu functionality. The following four functions provide the demos Animation menu support. The last six functions provide support for the Speed menu. Two private member functions are declared: UpdateScene() and LoadTexture(). Both of these functions are callback functions. The UpdateScene() function is used to update the animation sequence. The LoadTexture() function is used to load and attach a texture to the rocket mesh. Three data members are declared: LPDIRECT3DRMMESHBUILDER meshbuilder; LPDIRECT3DRMANIMATION animation; static D3DVALUE speed; The meshbuilder pointer is used to load and modify the rocket mesh. This pointer is used by the CreateScene() function and the Render menu functions. The animation pointer is used to manipulate the Direct3DRMAnimation object. The speed data member is used to control the rate that the animation sequence is executed. The RocketWin::CreateScene() FunctionThe Rocket demo CreateScene() function appears in Listing 7.2. Listing 7.2 The RocketWin::CreateScene() function. BOOL RocketWin::CreateScene() { //-------- MESH ---------- D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_ROCKETMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &meshbuilder ); meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, LoadTexture, NULL ); ScaleMesh( meshbuilder, D3DVALUE(10) ); //-------- ANIMATION -------- d3drm->CreateAnimation( &animation ); for (int i=0; i<11; i++) { D3DRMQUATERNION quat; D3DRMQuaternionFromRotation( &quat, &vect[i], rot[i] ); animation->AddRotateKey( D3DVALUE(i), &quat ); animation->AddPositionKey( D3DVALUE(i), trans[i].x, trans[i].y, trans[i].z ); } OnAnimationLinear(); //-------- MESH FRAME ------ LPDIRECT3DRMFRAME meshframe; d3drm->CreateFrame( scene, &meshframe ); meshframe->AddVisual( meshbuilder ); meshframe->AddMoveCallback( UpdateScene, animation ); animation->SetFrame( meshframe ); meshframe->Release(); meshframe=0; //-------- LIGHTS -------- LPDIRECT3DRMLIGHT dlight; LPDIRECT3DRMLIGHT alight; d3drm->CreateLightRGB(D3DRMLIGHT_AMBIENT, D3DVALUE(0.5),D3DVALUE(0.5), D3DVALUE(0.5), &alight); d3drm->CreateLightRGB(D3DRMLIGHT_DIRECTIONAL, D3DVALUE(1.0),D3DVALUE(1.0), D3DVALUE(1.0), &dlight); LPDIRECT3DRMFRAME lightframe; d3drm->CreateFrame( scene, &lightframe ); lightframe->SetOrientation( scene, D3DVALUE(0), D3DVALUE(-2), D3DVALUE(1), D3DVALUE(0), D3DVALUE(1), D3DVALUE(0) ); lightframe->AddLight( dlight ); lightframe->AddLight( alight ); dlight->Release(); dlight=0; alight->Release(); alight=0; lightframe->Release(); lightframe=0; //-------- VIEWPORT -------- d3drm->CreateFrame( scene, &camera ); camera->SetPosition(scene, D3DVALUE(0), D3DVALUE(0), D3DVALUE(-50.0)); d3drm->CreateViewport(device, camera, 0, 0, device->GetWidth(), device->GetHeight(), &viewport); return TRUE; } The CreateScene() function performs five steps:
The first step is the creation of a mesh: D3DRMLOADRESOURCE resinfo; resinfo.hModule=NULL; resinfo.lpName=MAKEINTRESOURCE( IDR_ROCKETMESH ); resinfo.lpType="MESH"; d3drm->CreateMeshBuilder( &meshbuilder ); meshbuilder->Load( &resinfo, NULL, D3DRMLOAD_FROMRESOURCE, LoadTexture, NULL ); ScaleMesh( meshbuilder, D3DVALUE(10) ); The mesh is loaded from the programs resources using the Direct3DRMMeshBuilder interface. With one exception, this code looks like the mesh loading code in all of the other demos. The difference is that we are using a callback function to apply a texture to the mesh. The fourth Direct3DRMMeshBuilder Load() argument is a pointer to an optional callback function for loading textures. Normally, we use zero for this argument, but, in the interest of variety, we will use the LoadTexture() function as a callback. The LoadTexture() function looks like this: HRESULT RocketWin::LoadTexture(char*, void*, LPDIRECT3DRMTEXTURE* texture) { HRSRC id = FindResource( NULL, MAKEINTRESOURCE(IDR_ROCKETTEXTURE), "TEXTURE" ); RMWin::d3drm->LoadTextureFromResource( id, texture ); return D3DRM_OK; }
|
![]() |
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. |