OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
SkeletalModel.cpp
Go to the documentation of this file.
1 
9 #include "global.h"
10 #include "Log.h"
11 #include "SkeletalModel.h"
12 #include "World.h"
13 
14 #include <glm/gtc/matrix_transform.hpp>
15 
16 void BoneTag::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
17  getWorld().getMesh(mesh).display(MVP, shaderTexture);
18 }
19 
20 // ----------------------------------------------------------------------------
21 
23  for (unsigned long i = 0; i < tag.size(); i++)
24  delete tag[i];
25 }
26 
27 unsigned long BoneFrame::size() {
28  return tag.size();
29 }
30 
31 BoneTag& BoneFrame::get(unsigned long i) {
32  assertLessThan(i, tag.size());
33  return *tag.at(i);
34 }
35 
37  tag.push_back(t);
38 }
39 
40 // ----------------------------------------------------------------------------
41 
43  for (unsigned long i = 0; i < frame.size(); i++)
44  delete frame[i];
45 }
46 
47 unsigned long AnimationFrame::size() {
48  return frame.size();
49 }
50 
51 BoneFrame& AnimationFrame::get(unsigned long i) {
52  assertLessThan(i, frame.size());
53  return *frame.at(i);
54 }
55 
57  frame.push_back(f);
58 }
59 
60 // ----------------------------------------------------------------------------
61 
62 class MatrixStack {
63  public:
64  MatrixStack(glm::mat4 start) : startVal(start) { stack.push_back(startVal); }
65 
66  void push() {
67  //assertGreaterThan(stack.size(), 0);
68  if (stack.size() > 0)
69  stack.push_back(stack.at(stack.size() - 1));
70  }
71 
72  void pop() {
73  //assertGreaterThan(stack.size(), 0);
74  if (stack.size() > 0)
75  stack.pop_back();
76  }
77 
78  glm::mat4 get() {
79  //assertGreaterThan(stack.size(), 0);
80  if (stack.size() > 0)
81  return stack.at(stack.size() - 1);
82  return startVal;
83  }
84 
85  private:
86  std::vector<glm::mat4> stack;
87  glm::mat4 startVal;
88 };
89 
90 // ----------------------------------------------------------------------------
91 
93  for (unsigned long i = 0; i < animation.size(); i++)
94  delete animation[i];
95 }
96 
97 //#define DEBUG_MODELS
98 
99 #ifdef DEBUG_MODELS
100 #include <bitset>
101 #endif
102 
103 void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe, ShaderTexture* shaderTexture) {
104  assertLessThan(aframe, size());
105  assertLessThan(bframe, get(aframe).size());
106 
107  AnimationFrame& anim = get(aframe);
108  BoneFrame& boneframe = anim.get(bframe);
109 
110  glm::vec3 pos = boneframe.getPosition();
111  glm::mat4 frameTrans = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
112 
113  MatrixStack stack(MVP * frameTrans);
114 
115 #ifdef DEBUG_MODELS
116  Log::get(LOG_DEBUG) << "Starting SkeletalModel:" << Log::endl;
117  int cnt = 0;
118 #endif
119 
120  for (unsigned int a = 0; a < boneframe.size(); a++) {
121  BoneTag& tag = boneframe.get(a);
122 
123  glm::mat4 translate(1.0f);
124 
125  if (a != 0) {
126  if (tag.getFlag() & 0x01) {
127  stack.pop();
128 #ifdef DEBUG_MODELS
129  Log::get(LOG_DEBUG) << " --> pop" << Log::endl;
130  cnt--;
131 #endif
132  }
133 
134  if (tag.getFlag() & 0x02) {
135  stack.push();
136 #ifdef DEBUG_MODELS
137  Log::get(LOG_DEBUG) << " --> push" << Log::endl;
138  cnt++;
139 #endif
140  }
141 
142 #ifdef DEBUG_MODELS
143  if (tag.getFlag() & ~0x03) {
144  std::bitset<8> bs(tag.getFlag());
145  Log::get(LOG_DEBUG) << " Unexpected flag " << bs << Log::endl;
146  }
147 #endif
148 
149  glm::vec3 off = tag.getOffset();
150  translate = glm::translate(glm::mat4(1.0f), glm::vec3(off.x, -off.y, off.z));
151  }
152 
153  glm::vec3 rot = tag.getRotation();
154  glm::mat4 rotY = glm::rotate(glm::mat4(1.0f), rot[1], glm::vec3(0.0f, 1.0f, 0.0f));
155  glm::mat4 rotX = glm::rotate(glm::mat4(1.0f), rot[0], glm::vec3(1.0f, 0.0f, 0.0f));
156  glm::mat4 rotZ = glm::rotate(glm::mat4(1.0f), rot[2], glm::vec3(0.0f, 0.0f, 1.0f));
157  glm::mat4 rotate = rotZ * rotX * rotY;
158 
159  glm::mat4 mod = translate * rotate;
160  tag.display(stack.get() * mod, shaderTexture);
161 #ifdef DEBUG_MODELS
162  Log::get(LOG_DEBUG) << " --> get (" << cnt << ")" << Log::endl;
163 #endif
164  }
165 
166 #ifdef DEBUG_MODELS
167  Log::get(LOG_DEBUG) << "Done!" << Log::endl;
168 #endif
169 }
170 
171 unsigned long SkeletalModel::size() {
172  return animation.size();
173 }
174 
176  assertLessThan(i, animation.size());
177  return *animation.at(i);
178 }
179 
181  animation.push_back(f);
182 }
183 
void display(glm::mat4 MVP, ShaderTexture *shaderTexture=nullptr)
char getFlag()
Definition: SkeletalModel.h:23
unsigned long size()
void add(BoneTag *t)
void add(AnimationFrame *f)
void display(glm::mat4 MVP, int aframe, int bframe, ShaderTexture *shaderTexture=nullptr)
World Model.
World & getWorld()
Definition: main.cpp:32
Included everywhere.
static LogLevel & get(int level)
Definition: Log.cpp:14
Global Logging Utility.
#define LOG_DEBUG
Definition: Log.h:22
void display(glm::mat4 MVP, ShaderTexture *shaderTexture=nullptr)
Definition: Mesh.cpp:129
Moveable Mesh Geometry.
static const char endl
Definition: Log.h:35
BoneTag & get(unsigned long i)
AnimationFrame & get(unsigned long i)
unsigned long size()
BoneFrame & get(unsigned long i)
glm::vec3 getOffset()
Definition: SkeletalModel.h:21
glm::vec3 getRotation()
Definition: SkeletalModel.h:22
std::vector< AnimationFrame * > animation
Definition: SkeletalModel.h:75
#define assertLessThan(x, y)
Definition: global.h:146
void add(BoneFrame *f)
glm::vec3 getPosition()
Definition: SkeletalModel.h:36
std::vector< BoneFrame * > frame
Definition: SkeletalModel.h:58
Mesh & getMesh(unsigned long index)
Definition: World.cpp:111
unsigned long size()
std::vector< BoneTag * > tag
Definition: SkeletalModel.h:44