![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Cutting Edge Direct 3D Programming
The CubeWin::UpdateCube() FunctionThe UpdateCube() function is a callback function that performs the vertex animation for the Cube demo. The UpdateCube() function is defined like this: void CubeWin::UpdateCube(LPDIRECT3DRMFRAME frame, void* p, D3DVALUE) { CallbackData* data=(CallbackData*)p; static const D3DVALUE lim=D3DVALUE(5); static D3DVALUE control; static D3DVALUE inc=D3DVALUE(.25); static D3DRMVERTEX vert[24]; data->mesh->GetVertices( data->group, 0, 24, vert ); vert[0].position.x+=inc; vert[0].position.y+=inc; vert[0].position.z+=inc; vert[6].position.x+=inc; vert[6].position.y+=inc; vert[6].position.z+=inc; vert[8].position.x+=inc; vert[8].position.y+=inc; vert[8].position.z+=inc; vert[14].position.x+=inc; vert[14].position.y+=inc; vert[14].position.z+=inc; vert[18].position.x+=inc; vert[18].position.y+=inc; vert[18].position.z+=inc; vert[20].position.x+=inc; vert[20].position.y+=inc; vert[20].position.z+=inc; data->mesh->SetVertices( data->group, 0, 24, vert ); control+=inc; if (control>lim || control<-lim) inc=-inc; static UINT delay; if (++delay<20) return; delay=0; LPDIRECT3DRMFRAME scene; frame->GetScene( &scene ); D3DVECTOR spinvect; D3DRMVectorRandom( &spinvect ); D3DVALUE spin=D3DDivide( rand()%50+1, 400 ); frame->SetRotation( scene, spinvect.x, spinvect.y, spinvect.z, spin ); } The function first prepares a pointer to the CallbackData structure: CallbackData* data=(CallbackData*)p; The p parameter is a pointer to the static cbdata structure that is declared by the CreateScene() function. The p pointer, however, is a pointer to void, so we are declaring a local pointer. Well use the local data pointer to access the callback data later in the function. Next, four static variables are declared: static const D3DVALUE lim=D3DVALUE(5); static D3DVALUE control; static D3DVALUE inc=D3DVALUE(.25); static D3DRMVERTEX vert[24]; The lim variable is a constant well use to limit the distance that the vertices will travel. The control variable is used to determine the current location of the animated vertices. The inc variable is used to increment the control variable. Finally, the vert variable is an array of D3DRMVERTEX structures. Well use the vert array to retrieve, manipulate, and assign vertex data. Next, the Direct3DRMMesh GetVertices() function is used to retrieve the current vertex settings: data->mesh->GetVertices( data->group, 0, 24, vert ); The argument list for the GetVertices() function is identical to the SetVertices() function. Notice that both the pointer to the mesh and the mesh group identifier are retrieved via the data pointer. The GetVertices() function fills the vert array with the meshs current vertex settings. The newly retrieved vertex positions can now be updated: vert[0].position.x+=inc; vert[0].position.y+=inc; vert[0].position.z+=inc; vert[6].position.x+=inc; vert[6].position.y+=inc; vert[6].position.z+=inc; vert[8].position.x+=inc; vert[8].position.y+=inc; vert[8].position.z+=inc; vert[14].position.x+=inc; vert[14].position.y+=inc; vert[14].position.z+=inc; vert[18].position.x+=inc; vert[18].position.y+=inc; vert[18].position.z+=inc; vert[20].position.x+=inc; vert[20].position.y+=inc; vert[20].position.z+=inc; The inc variable is used to increment specific vertex positions. The order that the vertices are updated is irrelevant because the changes dont take place until the SetVertices() function is called: data->mesh->SetVertices( data->group, 0, 24, vert ); The SetVertices() function updates the mesh given the modified vertex data. Next, the control variable is updated: control+=inc; if (control>lim || control<-lim) inc=-inc; This code uses the lim variable to change the sign of the inc variable when the control value reaches its limit. The remainder of the UpdateCube() function is dedicated to periodically changing the frames rotation attribute: static UINT delay; if (++delay<20) return; delay=0; LPDIRECT3DRMFRAME scene; frame->GetScene( &scene ); D3DVECTOR spinvect; D3DRMVectorRandom( &spinvect ); D3DVALUE spin=D3DDivide( rand()%50+1, 400 ); frame->SetRotation( scene, spinvect.x, spinvect.y, spinvect.z, spin ); Every 20 system updates, this code calculates a new rotation vector and speed. The new values are installed with the Direct3DRMFrame SetRotation() function.
|
![]() |
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. |