Просмотр исходного кода

LoaderTR2 tries to load Meshes and StaticMeshes

Thomas Buck 10 лет назад
Родитель
Сommit
2c82d21373
11 измененных файлов: 106 добавлений и 59 удалений
  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 Просмотреть файл

4
 
4
 
5
     [ 20141206 ]
5
     [ 20141206 ]
6
     * LoaderTR2 now tries to load Moveables and Entities
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
     [ 20141203 ]
10
     [ 20141203 ]
9
     * Renamed Vector3d to Vec3, small cleanup
11
     * Renamed Vector3d to Vec3, small cleanup

+ 15
- 13
include/Mesh.h Просмотреть файл

10
 #ifndef _MESH_H_
10
 #ifndef _MESH_H_
11
 #define _MESH_H_
11
 #define _MESH_H_
12
 
12
 
13
-#include "loader/Loader.h"
13
+#include "math/Vec3.h"
14
 
14
 
15
 /*!
15
 /*!
16
  * \brief OpenGL Mesh
16
  * \brief OpenGL Mesh
19
   public:
19
   public:
20
 
20
 
21
     struct rectangle_t {
21
     struct rectangle_t {
22
-        struct vertex_t a, b, c, d;
22
+        Vec3 a, b, c, d;
23
         uint16_t texture;
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
     Mesh();
29
     Mesh();
29
     void drawAlpha();
32
     void drawAlpha();
30
     void drawSolid();
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 Просмотреть файл

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

+ 7
- 1
include/World.h Просмотреть файл

13
 #include <vector>
13
 #include <vector>
14
 
14
 
15
 #include "Entity.h"
15
 #include "Entity.h"
16
+#include "Mesh.h"
16
 #include "Room.h"
17
 #include "Room.h"
17
 #include "SkeletalModel.h"
18
 #include "SkeletalModel.h"
18
 #include "Sprite.h"
19
 #include "Sprite.h"
54
     unsigned long sizeStaticMesh();
55
     unsigned long sizeStaticMesh();
55
     StaticMesh& getStaticMesh(unsigned long index);
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
      * \brief Find room a location is in.
63
      * \brief Find room a location is in.
59
      *
64
      *
82
     std::vector<std::unique_ptr<SpriteSequence>> mSprites;
87
     std::vector<std::unique_ptr<SpriteSequence>> mSprites;
83
     std::vector<std::unique_ptr<Entity>> mEntities;
88
     std::vector<std::unique_ptr<Entity>> mEntities;
84
     std::vector<std::unique_ptr<SkeletalModel>> mModels;
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
 World& getWorld();
94
 World& getWorld();

+ 0
- 6
include/loader/Loader.h Просмотреть файл

14
 
14
 
15
 #include "utils/binary.h"
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
 class Loader {
17
 class Loader {
24
   public:
18
   public:
25
 
19
 

+ 1
- 1
src/Game.cpp Просмотреть файл

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

+ 12
- 19
src/Mesh.cpp Просмотреть файл

11
 #include "TextureManager.h"
11
 #include "TextureManager.h"
12
 #include "Mesh.h"
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
 void Mesh::drawAlpha() {
30
 void Mesh::drawAlpha() {
41
         return;
36
         return;
42
     }
37
     }
43
 
38
 
44
-    // TODO replicate drawAlphaOld, but get object texture coord from World
45
 }
39
 }
46
 
40
 
47
 void Mesh::drawSolid() {
41
 void Mesh::drawSolid() {
53
         return;
47
         return;
54
     }
48
     }
55
 
49
 
56
-
57
 }
50
 }
58
 
51
 
59
 
52
 

+ 1
- 0
src/SkeletalModel.cpp Просмотреть файл

60
 
60
 
61
 void BoneTag::display() {
61
 void BoneTag::display() {
62
     getWorld().getStaticMesh(mesh).display();
62
     getWorld().getStaticMesh(mesh).display();
63
+    //getWorld().getMesh(mesh).drawSolid(); // TODO ?
63
 }
64
 }
64
 
65
 
65
 void BoneTag::getOffset(float o[3]) {
66
 void BoneTag::getOffset(float o[3]) {

+ 13
- 0
src/StaticMesh.cpp Просмотреть файл

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

+ 16
- 2
src/World.cpp Просмотреть файл

68
 }
68
 }
69
 
69
 
70
 void World::addStaticMesh(StaticMesh* model) {
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
 unsigned long World::sizeStaticMesh() {
74
 unsigned long World::sizeStaticMesh() {
75
-    return mMeshes.size();
75
+    return mStaticMeshes.size();
76
 }
76
 }
77
 
77
 
78
 StaticMesh& World::getStaticMesh(unsigned long index) {
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
     assert(index < mMeshes.size());
92
     assert(index < mMeshes.size());
80
     return *mMeshes.at(index);
93
     return *mMeshes.at(index);
81
 }
94
 }
114
     mSprites.clear();
127
     mSprites.clear();
115
     mEntities.clear();
128
     mEntities.clear();
116
     mModels.clear();
129
     mModels.clear();
130
+    mStaticMeshes.clear();
117
     mMeshes.clear();
131
     mMeshes.clear();
118
 }
132
 }
119
 
133
 

+ 34
- 17
src/loader/LoaderTR2.cpp Просмотреть файл

17
 #include "SoundManager.h"
17
 #include "SoundManager.h"
18
 #include "TextureManager.h"
18
 #include "TextureManager.h"
19
 #include "World.h"
19
 #include "World.h"
20
+#include "math/Vec3.h"
20
 #include "system/Sound.h"
21
 #include "system/Sound.h"
21
 #include "utils/pixel.h"
22
 #include "utils/pixel.h"
22
 #include "loader/LoaderTR2.h"
23
 #include "loader/LoaderTR2.h"
191
         // Number of data words (2 bytes) to follow
192
         // Number of data words (2 bytes) to follow
192
         uint32_t dataToFollow = file.readU32();
193
         uint32_t dataToFollow = file.readU32();
193
 
194
 
194
-        std::vector<vertex_t> vertices;
195
-
196
         uint16_t numVertices = file.readU16();
195
         uint16_t numVertices = file.readU16();
196
+        std::vector<Vec3> vertices;
197
         for (unsigned int v = 0; v < numVertices; v++) {
197
         for (unsigned int v = 0; v < numVertices; v++) {
198
-            vertex_t vert;
199
             // Vertex coordinates, relative to x/zOffset
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
             // Set of flags for special rendering effects
205
             // Set of flags for special rendering effects
207
             // 0x8000 - Something to do with water surface?
206
             // 0x8000 - Something to do with water surface?
208
             // 0x4000 - Underwater lighting modulation/movement if seen from above
207
             // 0x4000 - Underwater lighting modulation/movement if seen from above
209
             // 0x2000 - Water/Quicksand surface movement
208
             // 0x2000 - Water/Quicksand surface movement
210
             // 0x0010 - Normal?
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
         Room* room = new Room();
217
         Room* room = new Room();
229
             uint16_t texture = file.readU16();
228
             uint16_t texture = file.readU16();
230
 
229
 
231
             room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
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
         uint16_t numTriangles = file.readU16();
235
         uint16_t numTriangles = file.readU16();
444
         int16_t mx = mem.read16();
444
         int16_t mx = mem.read16();
445
         int16_t my = mem.read16();
445
         int16_t my = mem.read16();
446
         int16_t mz = mem.read16();
446
         int16_t mz = mem.read16();
447
-
448
         int32_t collisionSize = mem.read32();
447
         int32_t collisionSize = mem.read32();
448
+        // TODO store mesh collision info somewhere
449
 
449
 
450
         uint16_t numVertices = mem.readU16();
450
         uint16_t numVertices = mem.readU16();
451
+        std::vector<Vec3> vertices;
451
         for (int v = 0; v < numVertices; v++) {
452
         for (int v = 0; v < numVertices; v++) {
452
             int16_t x = mem.read16();
453
             int16_t x = mem.read16();
453
             int16_t y = mem.read16();
454
             int16_t y = mem.read16();
454
             int16_t z = mem.read16();
455
             int16_t z = mem.read16();
455
-
456
+            vertices.emplace_back(x, y, z);
456
         }
457
         }
457
 
458
 
458
         int16_t numNormals = mem.read16();
459
         int16_t numNormals = mem.read16();
466
                 int16_t x = mem.read16();
467
                 int16_t x = mem.read16();
467
                 int16_t y = mem.read16();
468
                 int16_t y = mem.read16();
468
                 int16_t z = mem.read16();
469
                 int16_t z = mem.read16();
469
-
470
+                // TODO store normals somewhere
470
             }
471
             }
471
         } else if (numNormals < 0) {
472
         } else if (numNormals < 0) {
472
             // Internal vertex lighting is used,
473
             // Internal vertex lighting is used,
473
             // using the data included with the mesh
474
             // using the data included with the mesh
474
             for (int l = 0; l < (numNormals * -1); l++) {
475
             for (int l = 0; l < (numNormals * -1); l++) {
475
                 int16_t light = mem.read16();
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
         int16_t numTexturedRectangles = mem.read16();
483
         int16_t numTexturedRectangles = mem.read16();
481
         for (int r = 0; r < numTexturedRectangles; r++) {
484
         for (int r = 0; r < numTexturedRectangles; r++) {
482
             uint16_t vertex1 = mem.readU16();
485
             uint16_t vertex1 = mem.readU16();
485
             uint16_t vertex4 = mem.readU16();
488
             uint16_t vertex4 = mem.readU16();
486
             uint16_t texture = mem.readU16();
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
         int16_t numTexturedTriangles = mem.read16();
496
         int16_t numTexturedTriangles = mem.read16();
494
             uint16_t vertex3 = mem.readU16();
500
             uint16_t vertex3 = mem.readU16();
495
             uint16_t texture = mem.readU16();
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
         int16_t numColoredRectangles = mem.read16();
507
         int16_t numColoredRectangles = mem.read16();
504
             uint16_t vertex4 = mem.readU16();
512
             uint16_t vertex4 = mem.readU16();
505
             uint16_t texture = mem.readU16();
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
         int16_t numColoredTriangles = mem.read16();
522
         int16_t numColoredTriangles = mem.read16();
513
             uint16_t vertex3 = mem.readU16();
526
             uint16_t vertex3 = mem.readU16();
514
             uint16_t texture = mem.readU16();
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
     if (numMeshPointers > 0)
538
     if (numMeshPointers > 0)
550
         // travel through, like TR2s skeletons and underwater plants
567
         // travel through, like TR2s skeletons and underwater plants
551
         uint16_t flags = file.readU16();
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
     if (numStaticMeshes > 0)
573
     if (numStaticMeshes > 0)

Загрузка…
Отмена
Сохранить