Skip to content

Commit 0b6e3dc

Browse files
committed
Chapter 13 done
1 parent c7901a5 commit 0b6e3dc

102 files changed

Lines changed: 646 additions & 47065 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Chapter13/Actor.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#include "Actor.h"
@@ -17,7 +17,7 @@ Actor::Actor(Game* game)
1717
,mRotation(Quaternion::Identity)
1818
,mScale(1.0f)
1919
,mGame(game)
20-
,mRecomputeTransform(true)
20+
,mRecomputeWorldTransform(true)
2121
{
2222
mGame->AddActor(this);
2323
}
@@ -37,12 +37,12 @@ void Actor::Update(float deltaTime)
3737
{
3838
if (mState == EActive)
3939
{
40-
if (mRecomputeTransform)
41-
{
42-
ComputeWorldTransform();
43-
}
40+
ComputeWorldTransform();
41+
4442
UpdateComponents(deltaTime);
4543
UpdateActor(deltaTime);
44+
45+
ComputeWorldTransform();
4646
}
4747
}
4848

@@ -58,21 +58,25 @@ void Actor::UpdateActor(float deltaTime)
5858
{
5959
}
6060

61-
void Actor::ComputeWorldTransform()
61+
void Actor::ProcessInput(const uint8_t* keyState)
6262
{
63-
mRecomputeTransform = false;
64-
// Scale, then rotate, then translate
65-
mWorldTransform = Matrix4::CreateScale(mScale);
66-
mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
67-
mWorldTransform *= Matrix4::CreateTranslation(mPosition);
68-
69-
// Inform components world transform updated
70-
for (auto comp : mComponents)
63+
if (mState == EActive)
7164
{
72-
comp->OnUpdateWorldTransform();
65+
// First process input for components
66+
for (auto comp : mComponents)
67+
{
68+
comp->ProcessInput(keyState);
69+
}
70+
71+
ActorInput(keyState);
7372
}
7473
}
7574

75+
void Actor::ActorInput(const uint8_t* keyState)
76+
{
77+
78+
}
79+
7680
void Actor::RotateToNewForward(const Vector3& forward)
7781
{
7882
// Figure out difference between original (unit x) and new
@@ -97,12 +101,42 @@ void Actor::RotateToNewForward(const Vector3& forward)
97101
}
98102
}
99103

104+
void Actor::ComputeWorldTransform()
105+
{
106+
if (mRecomputeWorldTransform)
107+
{
108+
mRecomputeWorldTransform = false;
109+
// Scale, then rotate, then translate
110+
mWorldTransform = Matrix4::CreateScale(mScale);
111+
mWorldTransform *= Matrix4::CreateFromQuaternion(mRotation);
112+
mWorldTransform *= Matrix4::CreateTranslation(mPosition);
113+
114+
// Inform components world transform updated
115+
for (auto comp : mComponents)
116+
{
117+
comp->OnUpdateWorldTransform();
118+
}
119+
}
120+
}
121+
100122
void Actor::AddComponent(Component* component)
101123
{
102-
mComponents.emplace_back(component);
103-
std::sort(mComponents.begin(), mComponents.end(), [](Component* a, Component* b) {
104-
return a->GetUpdateOrder() < b->GetUpdateOrder();
105-
});
124+
// Find the insertion point in the sorted vector
125+
// (The first element with a order higher than me)
126+
int myOrder = component->GetUpdateOrder();
127+
auto iter = mComponents.begin();
128+
for (;
129+
iter != mComponents.end();
130+
++iter)
131+
{
132+
if (myOrder < (*iter)->GetUpdateOrder())
133+
{
134+
break;
135+
}
136+
}
137+
138+
// Inserts element before position of iterator
139+
mComponents.insert(iter, component);
106140
}
107141

108142
void Actor::RemoveComponent(Component* component)

Chapter13/Actor.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
44
//
55
// Released under the BSD License
6-
// See LICENSE.txt for full details.
6+
// See LICENSE in root directory for full details.
77
// ----------------------------------------------------------------
88

99
#pragma once
1010
#include <vector>
1111
#include "Math.h"
12+
#include <cstdint>
13+
1214
class Actor
1315
{
1416
public:
@@ -28,16 +30,19 @@ class Actor
2830
void UpdateComponents(float deltaTime);
2931
// Any actor-specific update code (overridable)
3032
virtual void UpdateActor(float deltaTime);
33+
34+
// ProcessInput function called from Game (not overridable)
35+
void ProcessInput(const uint8_t* keyState);
3136
// Any actor-specific input code (overridable)
32-
virtual void ProcessInput(const uint8_t* keys) { }
37+
virtual void ActorInput(const uint8_t* keyState);
3338

3439
// Getters/setters
3540
const Vector3& GetPosition() const { return mPosition; }
36-
void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeTransform = true; }
41+
void SetPosition(const Vector3& pos) { mPosition = pos; mRecomputeWorldTransform = true; }
3742
float GetScale() const { return mScale; }
38-
void SetScale(float scale) { mScale = scale; mRecomputeTransform = true; }
43+
void SetScale(float scale) { mScale = scale; mRecomputeWorldTransform = true; }
3944
const Quaternion& GetRotation() const { return mRotation; }
40-
void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeTransform = true; }
45+
void SetRotation(const Quaternion& rotation) { mRotation = rotation; mRecomputeWorldTransform = true; }
4146

4247
void ComputeWorldTransform();
4348
const Matrix4& GetWorldTransform() const { return mWorldTransform; }
@@ -65,7 +70,7 @@ class Actor
6570
Vector3 mPosition;
6671
Quaternion mRotation;
6772
float mScale;
68-
bool mRecomputeTransform;
73+
bool mRecomputeWorldTransform;
6974

7075
std::vector<class Component*> mComponents;
7176
class Game* mGame;

Chapter13/Animation.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
19
#include "Animation.h"
210
#include "Skeleton.h"
311
#include <fstream>
@@ -55,8 +63,9 @@ bool Animation::Load(const std::string& fileName)
5563
}
5664

5765
mNumFrames = frames.GetUint();
58-
mLength = length.GetDouble();
66+
mDuration = length.GetDouble();
5967
mNumBones = bonecount.GetUint();
68+
mFrameDuration = mDuration / (mNumFrames - 1);
6069

6170
mTracks.resize(mNumBones);
6271

@@ -120,21 +129,19 @@ bool Animation::Load(const std::string& fileName)
120129
return true;
121130
}
122131

123-
void Animation::GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, Skeleton* inSkeleton, float inTime)
132+
void Animation::GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, const Skeleton* inSkeleton, float inTime) const
124133
{
125134
if (outPoses.size() != mNumBones)
126135
{
127136
outPoses.resize(mNumBones);
128137
}
129138

130-
// Duration of each frame
131-
float timePerFrame = (mLength / (mNumFrames - 1));
132139
// Figure out the current frame index and next frame
133140
// (This assumes inTime is bounded by [0, AnimDuration]
134-
size_t frame = static_cast<size_t>(inTime / timePerFrame);
141+
size_t frame = static_cast<size_t>(inTime / mFrameDuration);
135142
size_t nextFrame = frame + 1;
136143
// Calculate fractional value between frame and next frame
137-
float pct = Math::Abs(frame - inTime / timePerFrame);
144+
float pct = inTime / mFrameDuration - frame;
138145

139146
// Setup the pose for the root
140147
if (mTracks[0].size() > 0)
@@ -153,7 +160,7 @@ void Animation::GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, Skeleton* in
153160
// Now setup the poses for the rest
154161
for (size_t bone = 1; bone < mNumBones; bone++)
155162
{
156-
Matrix4 localMat;
163+
Matrix4 localMat; // (Defaults to identity)
157164
if (mTracks[bone].size() > 0)
158165
{
159166
BoneTransform interp = BoneTransform::Interpolate(mTracks[bone][frame],

Chapter13/Animation.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
// ----------------------------------------------------------------
2+
// From Game Programming in C++ by Sanjay Madhav
3+
// Copyright (C) 2017 Sanjay Madhav. All rights reserved.
4+
//
5+
// Released under the BSD License
6+
// See LICENSE in root directory for full details.
7+
// ----------------------------------------------------------------
8+
19
#pragma once
210
#include "BoneTransform.h"
311
#include <vector>
@@ -10,19 +18,22 @@ class Animation
1018

1119
size_t GetNumBones() const { return mNumBones; }
1220
size_t GetNumFrames() const { return mNumFrames; }
13-
float GetLength() const { return mLength; }
21+
float GetDuration() const { return mDuration; }
22+
float GetFrameDuration() const { return mFrameDuration; }
1423

1524
// Fills the provided vector with the global (current) pose matrices for each
1625
// bone at the specified time in the animation. It is expected that the time
17-
// is >= 0.0f and <= mLength
18-
void GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, class Skeleton* inSkeleton, float inTime);
26+
// is >= 0.0f and <= mDuration
27+
void GetGlobalPoseAtTime(std::vector<Matrix4>& outPoses, const class Skeleton* inSkeleton, float inTime) const;
1928
private:
2029
// Number of bones for the animation
2130
size_t mNumBones;
2231
// Number of frames in the animation
2332
size_t mNumFrames;
24-
// Length of the animation in seconds
25-
float mLength;
33+
// Duration of the animation in seconds
34+
float mDuration;
35+
// Duration of each frame in the animation
36+
float mFrameDuration;
2637
// Transform information for each frame on the track
2738
// Each index in the outer vector is a bone, inner vector
2839
// is a frame

Chapter13/Assets/CatWarrior.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/CatWarrior.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[1.017971,17.904892,187.382324,-0.058823,0.921569,-0.380392,48,48,48,48,255,0,0,0,0.222700,0.301400],
1011
[0.323929,17.401428,187.014023,-0.380392,0.647059,-0.662745,48,48,48,48,255,0,0,0,0.216700,0.307200],

Chapter13/Assets/Cube.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/Cube.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[-0.5,-0.5,-0.5,0,0,-1,0,0],
1011
[0.5,-0.5,-0.5,0,0,-1,1,0],

Chapter13/Assets/English.gptext

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"TextMap":[
3-
{ "key": "PauseTitle", "text": "PAUSED" },
4-
{ "key": "ResumeButton", "text": "Resume" },
5-
{ "key": "QuitButton", "text": "Quit" },
6-
{ "key": "QuitText", "text": "Do you want to quit?" },
7-
{ "key": "OKButton", "text": "OK" },
8-
{ "key": "CancelButton", "text": "Cancel" }
9-
]
2+
"TextMap":{
3+
"PauseTitle": "PAUSED",
4+
"ResumeButton": "Resume",
5+
"QuitButton": "Quit",
6+
"QuitText": "Do you want to quit?",
7+
"OKButton": "OK",
8+
"CancelButton": "Cancel"
9+
}
1010
}

Chapter13/Assets/Plane.gpmesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"textures":[
66
"Assets/Plane.png"
77
],
8+
"specularPower":100.0,
89
"vertices":[
910
[50.000000,50.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,1.000000],
1011
[50.000000,25.000000,-0.000000,-0.003922,-0.003922,1.000000,1.000000,0.750000],

Chapter13/Assets/PointLight.gpmesh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
"shader":"BasicMesh",
55
"textures":[
66
"Assets/Sphere.png",
7-
"Assets/Default.png"
7+
"Assets/Default.png"
88
],
9+
"specularPower":100.0,
910
"vertices":[
1011
[0.000000,0.000001,12.500000,-0.003922,-0.003922,0.992157,0.968750,0.000000],
1112
[0.475748,-2.391772,12.259815,0.027451,-0.215686,0.968628,0.968750,0.062500],

0 commit comments

Comments
 (0)