Browse Source

LoaderTR2 tries to load Meshes and StaticMeshes

Thomas Buck 9 years ago
parent
commit
2c82d21373
11 changed files with 106 additions and 59 deletions
  1. 2
    0
      ChangeLog.md
  2. 15
    13
      include/Mesh.h
  3. 5
    0
      include/StaticMesh.h
  4. 7
    1
      include/World.h
  5. 0
    6
      include/loader/Loader.h
  6. 1
    1
      src/Game.cpp
  7. 12
    19
      src/Mesh.cpp
  8. 1
    0
      src/SkeletalModel.cpp
  9. 13
    0
      src/StaticMesh.cpp
  10. 16
    2
      src/World.cpp
  11. 34
    17
      src/loader/LoaderTR2.cpp

+ 2
- 0
ChangeLog.md View File

@@ -4,6 +4,8 @@
4 4
 
5 5
     [ 20141206 ]
6 6
     * LoaderTR2 now tries to load Moveables and Entities
7
+    * Added Meshes to World, taking role of StaticMeshes
8
+    * LoaderTR2 tries to load Meshes and StaticMeshes
7 9
 
8 10
     [ 20141203 ]
9 11
     * Renamed Vector3d to Vec3, small cleanup

+ 15
- 13
include/Mesh.h View File

@@ -10,7 +10,7 @@
10 10
 #ifndef _MESH_H_
11 11
 #define _MESH_H_
12 12
 
13
-#include "loader/Loader.h"
13
+#include "math/Vec3.h"
14 14
 
15 15
 /*!
16 16
  * \brief OpenGL Mesh
@@ -19,8 +19,11 @@ class Mesh {
19 19
   public:
20 20
 
21 21
     struct rectangle_t {
22
-        struct vertex_t a, b, c, d;
22
+        Vec3 a, b, c, d;
23 23
         uint16_t texture;
24
+
25
+        rectangle_t(Vec3 _a, Vec3 _b, Vec3 _c, Vec3 _d, uint16_t t)
26
+            : a(_a), b(_b), c(_c), d(_d), texture(t) { }
24 27
     };
25 28
 
26 29
     Mesh();
@@ -29,17 +32,16 @@ class Mesh {
29 32
     void drawAlpha();
30 33
     void drawSolid();
31 34
 
32
-    // Warning: texture is not the GL texture id,
33
-    // it is an index into the object texture list!
34
-    void addTexturedRectangle(struct vertex_t a, struct vertex_t b,
35
-                              struct vertex_t c, struct vertex_t d, uint16_t texture);
36
-    void addTexturedTriangle(struct vertex_t a, struct vertex_t b,
37
-                             struct vertex_t c, uint16_t texture);
38
-
39
-    std::vector<struct rectangle_t> texturedRectangles;
40
-    std::vector<struct rectangle_t> coloredRectangles;
41
-    std::vector<struct rectangle_t> texturedTriangles;
42
-    std::vector<struct rectangle_t> coloredTriangles;
35
+    void addTexturedRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, uint16_t textile);
36
+    void addTexturedTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile);
37
+
38
+    void addColoredRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, uint16_t textile);
39
+    void addColoredTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile);
40
+
41
+    std::vector<rectangle_t> texturedRectangles;
42
+    std::vector<rectangle_t> coloredRectangles;
43
+    std::vector<rectangle_t> texturedTriangles;
44
+    std::vector<rectangle_t> coloredTriangles;
43 45
 
44 46
 
45 47
 

+ 5
- 0
include/StaticMesh.h View File

@@ -25,6 +25,8 @@ class TexturedTriangle {
25 25
 
26 26
 class StaticMesh {
27 27
   public:
28
+    StaticMesh(int id, int mesh);
29
+
28 30
     StaticMesh(TombRaider& tr, unsigned int index);
29 31
     ~StaticMesh();
30 32
     void display();
@@ -44,6 +46,9 @@ class StaticMesh {
44 46
     float* normals;
45 47
 
46 48
     std::vector<TexturedTriangle*> triangles;
49
+
50
+    int id;
51
+    int mesh;
47 52
 };
48 53
 
49 54
 #endif

+ 7
- 1
include/World.h View File

@@ -13,6 +13,7 @@
13 13
 #include <vector>
14 14
 
15 15
 #include "Entity.h"
16
+#include "Mesh.h"
16 17
 #include "Room.h"
17 18
 #include "SkeletalModel.h"
18 19
 #include "Sprite.h"
@@ -54,6 +55,10 @@ class World {
54 55
     unsigned long sizeStaticMesh();
55 56
     StaticMesh& getStaticMesh(unsigned long index);
56 57
 
58
+    void addMesh(Mesh* mesh);
59
+    unsigned long sizeMesh();
60
+    Mesh& getMesh(unsigned long index);
61
+
57 62
     /*!
58 63
      * \brief Find room a location is in.
59 64
      *
@@ -82,7 +87,8 @@ class World {
82 87
     std::vector<std::unique_ptr<SpriteSequence>> mSprites;
83 88
     std::vector<std::unique_ptr<Entity>> mEntities;
84 89
     std::vector<std::unique_ptr<SkeletalModel>> mModels;
85
-    std::vector<std::unique_ptr<StaticMesh>> mMeshes;
90
+    std::vector<std::unique_ptr<StaticMesh>> mStaticMeshes;
91
+    std::vector<std::unique_ptr<Mesh>> mMeshes;
86 92
 };
87 93
 
88 94
 World& getWorld();

+ 0
- 6
include/loader/Loader.h View File

@@ -14,12 +14,6 @@
14 14
 
15 15
 #include "utils/binary.h"
16 16
 
17
-struct vertex_t {
18
-    int16_t x, y, z;
19
-    int16_t light1, light2;
20
-    int16_t attributes;
21
-};
22
-
23 17
 class Loader {
24 18
   public:
25 19
 

+ 1
- 1
src/Game.cpp View File

@@ -86,7 +86,7 @@ int Game::loadLevel(const char* level) {
86 86
             if (mLara == -1) {
87 87
                 getLog() << "Can't find Lara entity in level?!" << Log::endl;
88 88
             } else {
89
-                //mLoaded = true;
89
+                mLoaded = true;
90 90
                 //getRender().setMode(Render::modeVertexLight);
91 91
             }
92 92
         }

+ 12
- 19
src/Mesh.cpp View File

@@ -11,25 +11,20 @@
11 11
 #include "TextureManager.h"
12 12
 #include "Mesh.h"
13 13
 
14
-void Mesh::addTexturedRectangle(struct vertex_t a, struct vertex_t b,
15
-                                struct vertex_t c, struct vertex_t d, uint16_t texture) {
16
-    struct rectangle_t r;
17
-    r.a = a;
18
-    r.b = b;
19
-    r.c = c;
20
-    r.d = d;
21
-    r.texture = texture;
22
-    texturedRectangles.push_back(r);
14
+void Mesh::addTexturedRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, uint16_t textile) {
15
+    texturedRectangles.emplace_back(a, b, c, d, textile);
23 16
 }
24 17
 
25
-void Mesh::addTexturedTriangle(struct vertex_t a, struct vertex_t b,
26
-                               struct vertex_t c, uint16_t texture) {
27
-    struct rectangle_t r;
28
-    r.a = a;
29
-    r.b = b;
30
-    r.c = c;
31
-    r.texture = texture;
32
-    texturedTriangles.push_back(r);
18
+void Mesh::addTexturedTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile) {
19
+    texturedTriangles.emplace_back(a, b, c, Vec3(), textile);
20
+}
21
+
22
+void Mesh::addColoredRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, uint16_t textile) {
23
+    coloredRectangles.emplace_back(a, b, c, d, textile);
24
+}
25
+
26
+void Mesh::addColoredTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile) {
27
+    coloredTriangles.emplace_back(a, b, c, Vec3(), textile);
33 28
 }
34 29
 
35 30
 void Mesh::drawAlpha() {
@@ -41,7 +36,6 @@ void Mesh::drawAlpha() {
41 36
         return;
42 37
     }
43 38
 
44
-    // TODO replicate drawAlphaOld, but get object texture coord from World
45 39
 }
46 40
 
47 41
 void Mesh::drawSolid() {
@@ -53,7 +47,6 @@ void Mesh::drawSolid() {
53 47
         return;
54 48
     }
55 49
 
56
-
57 50
 }
58 51
 
59 52
 

+ 1
- 0
src/SkeletalModel.cpp View File

@@ -60,6 +60,7 @@ BoneTag::BoneTag(TombRaider& tr, unsigned int index, unsigned int i, unsigned in
60 60
 
61 61
 void BoneTag::display() {
62 62
     getWorld().getStaticMesh(mesh).display();
63
+    //getWorld().getMesh(mesh).drawSolid(); // TODO ?
63 64
 }
64 65
 
65 66
 void BoneTag::getOffset(float o[3]) {

+ 13
- 0
src/StaticMesh.cpp View File

@@ -9,6 +9,7 @@
9 9
 #include "Game.h"
10 10
 #include "Render.h"
11 11
 #include "TextureManager.h"
12
+#include "World.h"
12 13
 #include "utils/pixel.h"
13 14
 #include "StaticMesh.h"
14 15
 
@@ -91,6 +92,10 @@ void TexturedTriangle::display(float* vertices, float* colors, float* normals) {
91 92
     glEnd();
92 93
 }
93 94
 
95
+// ----------------------------------------------------------------------------
96
+
97
+StaticMesh::StaticMesh(int i, int m) : id(i), mesh(m) { }
98
+
94 99
 #ifdef EXPERIMENTAL
95 100
 
96 101
 #include <map>
@@ -218,6 +223,8 @@ StaticMesh::StaticMesh(TombRaider& tr, unsigned int index) {
218 223
         triangles.push_back(
219 224
             new TexturedTriangle(vertexIndices + 3, st, texture, transparency));
220 225
     }
226
+
227
+    id = mesh = -1;
221 228
 }
222 229
 
223 230
 StaticMesh::~StaticMesh() {
@@ -232,6 +239,11 @@ StaticMesh::~StaticMesh() {
232 239
 }
233 240
 
234 241
 void StaticMesh::display() {
242
+    if ((id != -1) && (mesh != -1)) {
243
+        getWorld().getMesh(mesh).drawSolid();
244
+        return;
245
+    }
246
+
235 247
     if (!dontshow) {
236 248
         //! \fixme Duh, vis tests need to be put back
237 249
         //if (!isVisible(center, radius, bbox))
@@ -251,6 +263,7 @@ void StaticMesh::display() {
251 263
 }
252 264
 
253 265
 float StaticMesh::getRadius() {
266
+    assert((id != -1) && (mesh != -1));
254 267
     return radius;
255 268
 }
256 269
 

+ 16
- 2
src/World.cpp View File

@@ -68,14 +68,27 @@ SkeletalModel& World::getSkeletalModel(unsigned long index) {
68 68
 }
69 69
 
70 70
 void World::addStaticMesh(StaticMesh* model) {
71
-    mMeshes.emplace_back(std::unique_ptr<StaticMesh>(model));
71
+    mStaticMeshes.emplace_back(std::unique_ptr<StaticMesh>(model));
72 72
 }
73 73
 
74 74
 unsigned long World::sizeStaticMesh() {
75
-    return mMeshes.size();
75
+    return mStaticMeshes.size();
76 76
 }
77 77
 
78 78
 StaticMesh& World::getStaticMesh(unsigned long index) {
79
+    assert(index < mStaticMeshes.size());
80
+    return *mStaticMeshes.at(index);
81
+}
82
+
83
+void World::addMesh(Mesh* mesh) {
84
+    mMeshes.emplace_back(mesh);
85
+}
86
+
87
+unsigned long World::sizeMesh() {
88
+    return mMeshes.size();
89
+}
90
+
91
+Mesh& World::getMesh(unsigned long index) {
79 92
     assert(index < mMeshes.size());
80 93
     return *mMeshes.at(index);
81 94
 }
@@ -114,6 +127,7 @@ void World::destroy() {
114 127
     mSprites.clear();
115 128
     mEntities.clear();
116 129
     mModels.clear();
130
+    mStaticMeshes.clear();
117 131
     mMeshes.clear();
118 132
 }
119 133
 

+ 34
- 17
src/loader/LoaderTR2.cpp View File

@@ -17,6 +17,7 @@
17 17
 #include "SoundManager.h"
18 18
 #include "TextureManager.h"
19 19
 #include "World.h"
20
+#include "math/Vec3.h"
20 21
 #include "system/Sound.h"
21 22
 #include "utils/pixel.h"
22 23
 #include "loader/LoaderTR2.h"
@@ -191,28 +192,26 @@ void LoaderTR2::loadRooms() {
191 192
         // Number of data words (2 bytes) to follow
192 193
         uint32_t dataToFollow = file.readU32();
193 194
 
194
-        std::vector<vertex_t> vertices;
195
-
196 195
         uint16_t numVertices = file.readU16();
196
+        std::vector<Vec3> vertices;
197 197
         for (unsigned int v = 0; v < numVertices; v++) {
198
-            vertex_t vert;
199 198
             // Vertex coordinates, relative to x/zOffset
200
-            vert.x = file.read16();
201
-            vert.y = file.read16();
202
-            vert.z = file.read16();
199
+            int16_t x = file.read16();
200
+            int16_t y = file.read16();
201
+            int16_t z = file.read16();
203 202
 
204
-            vert.light1 = file.read16();
203
+            int16_t light1 = file.read16();
205 204
 
206 205
             // Set of flags for special rendering effects
207 206
             // 0x8000 - Something to do with water surface?
208 207
             // 0x4000 - Underwater lighting modulation/movement if seen from above
209 208
             // 0x2000 - Water/Quicksand surface movement
210 209
             // 0x0010 - Normal?
211
-            vert.attributes = file.readU16();
210
+            uint16_t attributes = file.readU16();
212 211
 
213
-            vert.light2 = file.read16(); // Almost always equal to light1
212
+            int16_t light2 = file.read16(); // Almost always equal to light1
214 213
 
215
-            vertices.push_back(vert);
214
+            vertices.emplace_back(x, y, z);
216 215
         }
217 216
 
218 217
         Room* room = new Room();
@@ -229,7 +228,8 @@ void LoaderTR2::loadRooms() {
229 228
             uint16_t texture = file.readU16();
230 229
 
231 230
             room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
232
-                                                 vertices.at(vertex3), vertices.at(vertex4), texture);
231
+                                                 vertices.at(vertex3), vertices.at(vertex4),
232
+                                                 texture);
233 233
         }
234 234
 
235 235
         uint16_t numTriangles = file.readU16();
@@ -444,15 +444,16 @@ void LoaderTR2::loadMeshes() {
444 444
         int16_t mx = mem.read16();
445 445
         int16_t my = mem.read16();
446 446
         int16_t mz = mem.read16();
447
-
448 447
         int32_t collisionSize = mem.read32();
448
+        // TODO store mesh collision info somewhere
449 449
 
450 450
         uint16_t numVertices = mem.readU16();
451
+        std::vector<Vec3> vertices;
451 452
         for (int v = 0; v < numVertices; v++) {
452 453
             int16_t x = mem.read16();
453 454
             int16_t y = mem.read16();
454 455
             int16_t z = mem.read16();
455
-
456
+            vertices.emplace_back(x, y, z);
456 457
         }
457 458
 
458 459
         int16_t numNormals = mem.read16();
@@ -466,17 +467,19 @@ void LoaderTR2::loadMeshes() {
466 467
                 int16_t x = mem.read16();
467 468
                 int16_t y = mem.read16();
468 469
                 int16_t z = mem.read16();
469
-
470
+                // TODO store normals somewhere
470 471
             }
471 472
         } else if (numNormals < 0) {
472 473
             // Internal vertex lighting is used,
473 474
             // using the data included with the mesh
474 475
             for (int l = 0; l < (numNormals * -1); l++) {
475 476
                 int16_t light = mem.read16();
476
-
477
+                // TODO store lights somewhere
477 478
             }
478 479
         }
479 480
 
481
+        Mesh* mesh = new Mesh();
482
+
480 483
         int16_t numTexturedRectangles = mem.read16();
481 484
         for (int r = 0; r < numTexturedRectangles; r++) {
482 485
             uint16_t vertex1 = mem.readU16();
@@ -485,6 +488,9 @@ void LoaderTR2::loadMeshes() {
485 488
             uint16_t vertex4 = mem.readU16();
486 489
             uint16_t texture = mem.readU16();
487 490
 
491
+            mesh->addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
492
+                                       vertices.at(vertex3), vertices.at(vertex4),
493
+                                       texture);
488 494
         }
489 495
 
490 496
         int16_t numTexturedTriangles = mem.read16();
@@ -494,6 +500,8 @@ void LoaderTR2::loadMeshes() {
494 500
             uint16_t vertex3 = mem.readU16();
495 501
             uint16_t texture = mem.readU16();
496 502
 
503
+            mesh->addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
504
+                                      vertices.at(vertex3), texture);
497 505
         }
498 506
 
499 507
         int16_t numColoredRectangles = mem.read16();
@@ -504,6 +512,11 @@ void LoaderTR2::loadMeshes() {
504 512
             uint16_t vertex4 = mem.readU16();
505 513
             uint16_t texture = mem.readU16();
506 514
 
515
+            // TODO color?
516
+
517
+            mesh->addColoredRectangle(vertices.at(vertex1), vertices.at(vertex2),
518
+                                      vertices.at(vertex3), vertices.at(vertex4),
519
+                                      texture);
507 520
         }
508 521
 
509 522
         int16_t numColoredTriangles = mem.read16();
@@ -513,9 +526,13 @@ void LoaderTR2::loadMeshes() {
513 526
             uint16_t vertex3 = mem.readU16();
514 527
             uint16_t texture = mem.readU16();
515 528
 
529
+            // TODO color?
530
+
531
+            mesh->addColoredTriangle(vertices.at(vertex1), vertices.at(vertex2),
532
+                                     vertices.at(vertex3), texture);
516 533
         }
517 534
 
518
-        // TODO store mesh data somewhere
535
+        getWorld().addMesh(mesh);
519 536
     }
520 537
 
521 538
     if (numMeshPointers > 0)
@@ -550,7 +567,7 @@ void LoaderTR2::loadStaticMeshes() {
550 567
         // travel through, like TR2s skeletons and underwater plants
551 568
         uint16_t flags = file.readU16();
552 569
 
553
-        // TODO store static meshes somewhere
570
+        getWorld().addStaticMesh(new StaticMesh(objectID, mesh));
554 571
     }
555 572
 
556 573
     if (numStaticMeshes > 0)

Loading…
Cancel
Save