Pārlūkot izejas kodu

SkeletalModel rendering tests

Thomas Buck 9 gadus atpakaļ
vecāks
revīzija
2ada067d3c
4 mainītis faili ar 113 papildinājumiem un 92 dzēšanām
  1. 1
    0
      ChangeLog.md
  2. 10
    11
      include/SkeletalModel.h
  3. 92
    72
      src/SkeletalModel.cpp
  4. 10
    9
      src/loader/LoaderTR2.cpp

+ 1
- 0
ChangeLog.md Parādīt failu

@@ -5,6 +5,7 @@
5 5
     [ 20140213 ]
6 6
     * The menu can be navigated using a controller
7 7
     * Vertical camera rotation is now also clamped when using a controller
8
+    * Worked on rendering SkeletalModels, not yet working correctly
8 9
 
9 10
     [ 20140211 ]
10 11
     * Updated imgui to version 1.32

+ 10
- 11
include/SkeletalModel.h Parādīt failu

@@ -13,39 +13,38 @@
13 13
 
14 14
 class BoneTag {
15 15
   public:
16
-    BoneTag(int m, float o[3], float r[3], char f);
17
-    void display();
16
+    BoneTag(int m, glm::vec3 o, glm::vec3 r, char f) : mesh(m), off(o), rot(r), flag(f) { }
17
+    void display(glm::mat4 MVP);
18 18
 
19
-    void getOffset(float o[3]);
20
-    void getRotation(float r[3]);
21
-    char getFlag();
19
+    glm::vec3 getOffset() { return off; }
20
+    glm::vec3 getRotation() { return rot; }
21
+    char getFlag() { return flag; }
22 22
 
23 23
   private:
24 24
     int mesh;
25
-    float off[3];
26
-    float rot[3];
25
+    glm::vec3 off, rot;
27 26
     char flag;
28 27
 };
29 28
 
30 29
 class BoneFrame {
31 30
   public:
32
-    BoneFrame(float p[3]);
31
+    BoneFrame(glm::vec3 p) : pos(p) { }
33 32
     ~BoneFrame();
34 33
 
35
-    void getPosition(float p[3]);
34
+    glm::vec3 getPosition() { return pos; }
36 35
 
37 36
     unsigned long size();
38 37
     BoneTag& get(unsigned long i);
39 38
     void add(BoneTag* t);
40 39
 
41 40
   private:
42
-    float pos[3];
41
+    glm::vec3 pos;
43 42
     std::vector<BoneTag*> tag;
44 43
 };
45 44
 
46 45
 class AnimationFrame {
47 46
   public:
48
-    AnimationFrame(char r);
47
+    AnimationFrame(char r) : rate(r) { }
49 48
     ~AnimationFrame();
50 49
 
51 50
     unsigned long size();

+ 92
- 72
src/SkeletalModel.cpp Parādīt failu

@@ -11,47 +11,14 @@
11 11
 #include "SkeletalModel.h"
12 12
 #include "World.h"
13 13
 
14
-BoneTag::BoneTag(int m, float o[3], float r[3], char f) {
15
-    mesh = m;
16
-    flag = f;
17
-    for (int i = 0; i < 3; i++) {
18
-        off[i] = o[i];
19
-        rot[i] = r[i];
20
-    }
21
-}
22
-
23
-void BoneTag::display() {
24
-    /*
25
-    if (getWorld().sizeMesh() > 0)
26
-        getWorld().getMesh(mesh).drawSolid(); // TODO ?
27
-    else
28
-        getWorld().getStaticMesh(mesh).display();
29
-    */
30
-}
31
-
32
-void BoneTag::getOffset(float o[3]) {
33
-    o[0] = off[0];
34
-    o[1] = off[1];
35
-    o[2] = off[2];
36
-}
37
-
38
-void BoneTag::getRotation(float r[3]) {
39
-    r[0] = rot[0];
40
-    r[1] = rot[1];
41
-    r[2] = rot[2];
42
-}
14
+#include <glm/gtc/matrix_transform.hpp>
43 15
 
44
-char BoneTag::getFlag() {
45
-    return flag;
16
+void BoneTag::display(glm::mat4 MVP) {
17
+    getWorld().getMesh(mesh).display(MVP);
46 18
 }
47 19
 
48 20
 // ----------------------------------------------------------------------------
49 21
 
50
-BoneFrame::BoneFrame(float p[3]) {
51
-    for (int i = 0; i < 3; i++)
52
-        pos[i] = p[i];
53
-}
54
-
55 22
 BoneFrame::~BoneFrame() {
56 23
     for (unsigned long i = 0; i < tag.size(); i++)
57 24
         delete tag[i];
@@ -70,18 +37,8 @@ void BoneFrame::add(BoneTag* t) {
70 37
     tag.push_back(t);
71 38
 }
72 39
 
73
-void BoneFrame::getPosition(float p[3]) {
74
-    p[0] = pos[0];
75
-    p[1] = pos[1];
76
-    p[2] = pos[2];
77
-}
78
-
79 40
 // ----------------------------------------------------------------------------
80 41
 
81
-AnimationFrame::AnimationFrame(char r) {
82
-    rate = r;
83
-}
84
-
85 42
 AnimationFrame::~AnimationFrame() {
86 43
     for (unsigned long i = 0; i < frame.size(); i++)
87 44
         delete frame[i];
@@ -102,50 +59,113 @@ void AnimationFrame::add(BoneFrame* f) {
102 59
 
103 60
 // ----------------------------------------------------------------------------
104 61
 
62
+class MatrixStack {
63
+  public:
64
+    MatrixStack(glm::mat4 start) : startVal(start) { stack.push_back(startVal); }
65
+
66
+    void push() {
67
+        //assert(stack.size() > 0);
68
+        if (stack.size() > 0)
69
+            stack.push_back(stack.at(stack.size() - 1));
70
+    }
71
+
72
+    void pop() {
73
+        //assert(stack.size() > 0);
74
+        if (stack.size() > 0)
75
+            stack.pop_back();
76
+    }
77
+
78
+    glm::mat4 get() {
79
+        //assert(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
+
105 92
 SkeletalModel::~SkeletalModel() {
106 93
     for (unsigned long i = 0; i < animation.size(); i++)
107 94
         delete animation[i];
108 95
 }
109 96
 
97
+//#define DEBUG_MODELS
98
+
99
+#ifdef DEBUG_MODELS
100
+#include <bitset>
101
+#endif
102
+
110 103
 void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe) {
111
-    /*
112 104
     assert(aframe < size());
113 105
     assert(bframe < get(aframe).size());
114 106
 
115 107
     AnimationFrame& anim = get(aframe);
116 108
     BoneFrame& boneframe = anim.get(bframe);
117 109
 
118
-    float pos[3];
119
-    boneframe.getPosition(pos);
120
-    glTranslatef(pos[0], pos[1], pos[2]);
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
121 119
 
122 120
     for (unsigned int a = 0; a < boneframe.size(); a++) {
123 121
         BoneTag& tag = boneframe.get(a);
124
-        float rot[3], off[3];
125
-
126
-        tag.getRotation(rot);
127
-        tag.getOffset(off);
128
-
129
-        if (a == 0) {
130
-            glRotatef(rot[1], 0, 1, 0);
131
-            glRotatef(rot[0], 1, 0, 0);
132
-            glRotatef(rot[2], 0, 0, 1);
133
-        } else {
134
-            if (tag.getFlag() & 0x01)
135
-                glPopMatrix();
136
-
137
-            if (tag.getFlag() & 0x02)
138
-                glPushMatrix();
139
-
140
-            glTranslatef(off[0], off[1], off[2]);
141
-            glRotatef(rot[1], 0, 1, 0);
142
-            glRotatef(rot[0], 1, 0, 0);
143
-            glRotatef(rot[2], 0, 0, 1);
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));
144 151
         }
145 152
 
146
-        tag.display();
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);
161
+#ifdef DEBUG_MODELS
162
+        Log::get(LOG_DEBUG) << "  --> get (" << cnt << ")" << Log::endl;
163
+#endif
147 164
     }
148
-    */
165
+
166
+#ifdef DEBUG_MODELS
167
+    Log::get(LOG_DEBUG) << "Done!" << Log::endl;
168
+#endif
149 169
 }
150 170
 
151 171
 unsigned long SkeletalModel::size() {

+ 10
- 9
src/loader/LoaderTR2.cpp Parādīt failu

@@ -833,15 +833,15 @@ void LoaderTR2::loadMoveables() {
833 833
         // Just add the frame indicated in frameOffset, nothing else
834 834
         char* tmp = reinterpret_cast<char*>(&frames[0]) + frameOffset;
835 835
         BinaryMemory frame(tmp + 12, (numFrames * 2) - frameOffset - 12); // skip two BBs
836
-        float pos[3];
837
-        pos[0] = frame.read16();
838
-        pos[1] = frame.read16();
839
-        pos[2] = frame.read16();
836
+        glm::vec3 pos;
837
+        pos.x = frame.read16();
838
+        pos.y = frame.read16();
839
+        pos.z = frame.read16();
840 840
         BoneFrame* bf = new BoneFrame(pos);
841 841
 
842 842
         for (int i = 0; i < numMeshes; i++) {
843 843
             int mesh = startingMesh + i;
844
-            float offset[3] = { 0.0f, 0.0f, 0.0f };
844
+            glm::vec3 offset;
845 845
             float rotation[3] = { 0.0f, 0.0f, 0.0f };
846 846
             char flag = (i == 0) ? 2 : 0;
847 847
 
@@ -851,9 +851,9 @@ void LoaderTR2::loadMoveables() {
851 851
                 tmp += (i - 1) * 16; // TODO ?
852 852
                 BinaryMemory tree(tmp, (numMeshTrees * 4) - meshTree - ((i - 1) * 16));
853 853
                 flag = (char)tree.readU32();
854
-                offset[0] = tree.read32();
855
-                offset[1] = tree.read32();
856
-                offset[2] = tree.read32();
854
+                offset.x = tree.read32();
855
+                offset.y = tree.read32();
856
+                offset.z = tree.read32();
857 857
 
858 858
                 uint16_t a = frame.readU16();
859 859
                 if (a & 0xC000) {
@@ -875,7 +875,8 @@ void LoaderTR2::loadMoveables() {
875 875
                 }
876 876
             }
877 877
 
878
-            BoneTag* bt = new BoneTag(mesh, offset, rotation, flag);
878
+            glm::vec3 rot(rotation[0], rotation[1], rotation[2]);
879
+            BoneTag* bt = new BoneTag(mesh, offset, rot, flag);
879 880
             bf->add(bt);
880 881
         }
881 882
 

Notiek ielāde…
Atcelt
Saglabāt