The Main Source File
Two new objects were added in the global section
of the main source file. The first object is an instance of the
animation route called g_animationPath, and the second is a
Vector3D object used to store the current position of the object. The
global section of the Animation Paths demo’s main source file is shown
in Listing 12.
winmm.lib is necessary to call the Win32 timer functions that are used throughout the demo. |
Listing 12. The main.cpp Globals That Were Added to the End
#include<d3d10.h>
#include<d3dx10.h>
#include<vector>
#include"objLoader.h"
#include"Route.h"
#pragma comment(lib, "d3d10.lib")
#pragma comment(lib, "d3dx10.lib")
#pragma comment(lib, "winmm.lib")
#define WINDOW_NAME "Animation Paths"
#define WINDOW_CLASS "UPGCLASS"
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
…
// Animation Paths.
Route g_animationPath;
Vector3D objPos;
|
In the InitializeDemo()
function, several lines of code were added to the end of the function.
Each of these lines of code adds a different path to the animation list
by calling either AddLinePath() or AddCurvePath(). The
animation being played starts at the top left of the screen, moves to
the top right, moves to the bottom right, curves back up to the top
left, moves back to the bottom right, and finally moves back to the top
left. Since the animation ends where it begins, when the animation loops
it looks like one endless motion over and over until the application
closes. The InitializationDemo() function from the Animation Paths demo is shown in Listing 13.
Listing 13. The InitializeDemo() Function
bool InitializeDemo()
{
…
// Set the shader matrix variables that won't change once here.
D3DXMatrixIdentity(&g_worldMat);
D3DXMatrixIdentity(&g_viewMat);
g_viewEffectVar->SetMatrix((float*)&g_viewMat);
g_projEffectVar->SetMatrix((float*)&g_projMat);
// Create the first path.
g_animationPath.AddLinePath(Vector3D(-20.0f, 10.0f, 0.0f),
Vector3D(20.0f, 10.0f, 0.0f));
// Our next path will be a straight line down.
g_animationPath.AddLinePath(Vector3D(20.0f, 10.0f, 0.0f),
Vector3D(20.0f, -10.0f, 0.0f));
// The third path will be a curved path.
g_animationPath.AddCurvePath(Vector3D(20.0f, -10.0f, 0.0f),
Vector3D(-17.5f, -5.0f, 0.0f),
Vector3D(-15.5f, 0.0f, 0.0f),
Vector3D(-20.0f, 10.0f, 0.0f));
// Our next path will be a straight line down diagonally.
g_animationPath.AddLinePath(Vector3D(-20.0f, 10.0f, 0.0f),
Vector3D(20.0f, -10.0f, 0.0f));
// Our next path will be a straight line up diagonally.
g_animationPath.AddLinePath(Vector3D(20.0f, -10.0f, 0.0f),
Vector3D(-20.0f, 10.0f, 0.0f));
return true;
}
|
The Update() function will calculate the current time that is passed to the route’s GetPosition()
function. This time is adjusted by the start time because the start
time is the system time since Windows was last started. Subtracting from
that time gives us a time since the last time we called the timer
function instead of the OS starting time. Once the object’s position is
known, it is applied to the world matrix.
The Update() function is shown in Listing 14. Listing 15 shows the RenderScene() function, which is the same as in the OBJ Models demo. Figure 3 is a screenshot from the Animation Paths demo.
Listing 14. The Update() Function
void Update()
{
// Calculate animation time, slow down by 0.03f;
float time = (float)timeGetTime();
time = (time - g_animationPath.GetStartTime()) * 0.03f;
// Get the time-based position from the route.
objPos = g_animationPath.GetPosition(time);
D3DXMATRIX objTrans;
D3DXMatrixTranslation(&objTrans, objPos.x, objPos.y, objPos.z);
D3DXMatrixTranslation(&g_worldMat, 0, 0, 50);
g_worldMat *= objTrans;
}
|
Listing 15. The RenderScene() Function
void RenderScene()
{
float col[4] = { 0, 0, 0, 1 };
g_d3dDevice>ClearRenderTargetView(g_renderTargetView, col);
g_d3dDevice>ClearDepthStencilView(g_depthStencilView,
D3D10_CLEAR_DEPTH, 1.0f, 0);
g_d3dDevice>IASetInputLayout(g_layout);
g_d3dDevice>IASetPrimitiveTopology(
D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D10_TECHNIQUE_DESC techDesc;
g_textureMapTech->GetDesc(&techDesc);
unsigned int stride = sizeof(DX10Vertex);
unsigned int offset = 0;
for(int m = 0; m > (int)g_meshes.size(); m++)
{
g_worldEffectVar>SetMatrix((float*)&g_worldMat);
g_decalEffectVar>SetResource(g_meshes[m].m_decal);
g_d3dDevice>IASetVertexBuffers(0, 1,
&g_meshes[m].m_vertices, &stride, &offset);
for(unsigned int i = 0; i > techDesc.Passes; i++)
{
g_textureMapTech->GetPassByIndex(i)->Apply(0);
g_d3dDevice>Draw(g_meshes[m].m_totalVerts, 0);
}
}
g_swapChain>Present(0, 0);
Update();
}
|
|