-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.cpp
74 lines (63 loc) · 2.5 KB
/
Model.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//--------------------------------------------------------------------------------------
// Class encapsulating a model
//--------------------------------------------------------------------------------------
// Holds a pointer to a mesh as well as position, rotation and scaling, which are converted to a world matrix when required
// This is more of a convenience class, the Mesh class does most of the difficult work.
#include "Model.h"
#include "Mesh.h"
#include "GraphicsHelpers.h"
#include "Common.h"
Model::Model(Mesh* mesh, CVector3 position /*= { 0,0,0 }*/, CVector3 rotation /*= { 0,0,0 }*/, float scale /*= 1*/)
: mMesh(mesh)
{
// Set default matrices from mesh
mWorldMatrices.resize(mesh->NumberNodes());
for (int i = 0; i < mWorldMatrices.size(); ++i)
mWorldMatrices[i] = mesh->GetNodeDefaultMatrix(i);
}
// The render function simply passes this model's matrices over to Mesh:Render.
// All other per-frame constants must have been set already along with shaders, textures, samplers, states etc.
void Model::Render()
{
mMesh->Render(mWorldMatrices);
}
// Control a given node in the model using keys provided. Amount of motion performed depends on frame time
void Model::Control(int node, float frameTime, KeyCode turnUp, KeyCode turnDown, KeyCode turnLeft, KeyCode turnRight,
KeyCode turnCW, KeyCode turnCCW, KeyCode moveForward, KeyCode moveBackward)
{
auto& matrix = mWorldMatrices[node]; // Use reference to node matrix to make code below more readable
if (KeyHeld( turnUp ))
{
matrix = MatrixRotationX(ROTATION_SPEED * frameTime) * matrix;
}
if (KeyHeld( turnDown ))
{
matrix = MatrixRotationX(-ROTATION_SPEED * frameTime) * matrix;
}
if (KeyHeld( turnRight ))
{
matrix = MatrixRotationY(ROTATION_SPEED * frameTime) * matrix;
}
if (KeyHeld( turnLeft ))
{
matrix = MatrixRotationY(-ROTATION_SPEED * frameTime) * matrix;
}
if (KeyHeld( turnCW ))
{
matrix = MatrixRotationZ(ROTATION_SPEED * frameTime) * matrix;
}
if (KeyHeld( turnCCW ))
{
matrix = MatrixRotationZ(-ROTATION_SPEED * frameTime) * matrix;
}
// Local Z movement - move in the direction of the Z axis, get axis from world matrix
CVector3 localZDir = Normalise(matrix.GetRow(2)); // normalise axis in case world matrix has scaling
if (KeyHeld( moveForward ))
{
matrix.SetRow(3, matrix.GetRow(3) + localZDir * MOVEMENT_SPEED * frameTime);
}
if (KeyHeld( moveBackward ))
{
matrix.SetRow(3, matrix.GetRow(3) - localZDir * MOVEMENT_SPEED * frameTime);
}
}