Browse Source

LoaderTR2 starts filling Rooms

Thomas Buck 9 years ago
parent
commit
c3c681e66f
14 changed files with 192 additions and 48 deletions
  1. 3
    0
      ChangeLog.md
  2. 31
    13
      include/Mesh.h
  3. 9
    1
      include/Room.h
  4. 1
    0
      include/RoomData.h
  5. 6
    0
      include/Sprite.h
  6. 7
    0
      include/loader/Loader.h
  7. 2
    1
      src/Game.cpp
  8. 47
    2
      src/Mesh.cpp
  9. 39
    9
      src/Room.cpp
  10. 4
    0
      src/RoomData.cpp
  11. 4
    0
      src/Sprite.cpp
  12. 3
    1
      src/UI.cpp
  13. 20
    11
      src/loader/LoaderTR2.cpp
  14. 16
    10
      src/utils/pixel.cpp

+ 3
- 0
ChangeLog.md View File

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141124 ]
6
+    * LoaderTR2 is starting to populate Room structures
7
+
5 8
     [ 20141123 ]
6 9
     * Added texture viewer debug UI
7 10
 

+ 31
- 13
include/Mesh.h View File

@@ -10,12 +10,41 @@
10 10
 #ifndef _MESH_H_
11 11
 #define _MESH_H_
12 12
 
13
+#include "loader/Loader.h"
14
+
13 15
 /*!
14 16
  * \brief OpenGL Mesh
15 17
  */
16 18
 class Mesh {
17 19
   public:
18 20
 
21
+    struct rectangle_t {
22
+        struct vertex_t a, b, c, d;
23
+        uint16_t texture;
24
+    }
25
+
26
+    Mesh();
27
+    ~Mesh();
28
+
29
+    void drawAlpha();
30
+    void drawSolid();
31
+
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;
43
+
44
+
45
+
46
+    // Old API
47
+
19 48
     typedef enum {
20 49
         MeshModeSolid,
21 50
         MeshModeWireframe,
@@ -77,19 +106,8 @@ class Mesh {
77 106
 
78 107
     } rect_t;
79 108
 
80
-    /*!
81
-     * \brief Constructs an object of Mesh
82
-     */
83
-    Mesh();
84
-
85
-    /*!
86
-     * \brief Deconstructs an object of Mesh
87
-     */
88
-    ~Mesh();
89
-
90
-    void drawAlpha();
91
-
92
-    void drawSolid();
109
+    void drawAlphaOld();
110
+    void drawSolidOld();
93 111
 
94 112
     void allocateColors(unsigned int n);
95 113
 

+ 9
- 1
include/Room.h View File

@@ -21,10 +21,11 @@ typedef enum {
21 21
 class Room {
22 22
   public:
23 23
     Room(TombRaider& tr, unsigned int index);
24
+
25
+    Room();
24 26
     ~Room();
25 27
 
26 28
     BoundingBox& getBoundingBox();
27
-    Mesh& getMesh();
28 29
     void display(bool alpha);
29 30
 
30 31
     bool isWall(unsigned long sector);
@@ -43,24 +44,31 @@ class Room {
43 44
 
44 45
     unsigned long sizeAdjacentRooms();
45 46
     long getAdjacentRoom(unsigned long index);
47
+    void addAdjacentRoom(long r);
46 48
 
47 49
     unsigned long sizePortals();
48 50
     Portal& getPortal(unsigned long index);
51
+    void addPortal(Portal* p);
49 52
 
50 53
     unsigned long sizeSectors();
51 54
     Sector& getSector(unsigned long index);
55
+    void addSector(Sector* s);
52 56
 
53 57
     unsigned long sizeBox();
54 58
     Box& getBox(unsigned long index);
59
+    void addBox(Box* b);
55 60
 
56 61
     unsigned long sizeModels();
57 62
     StaticModel& getModel(unsigned long index);
63
+    void addModel(StaticModel* s);
58 64
 
59 65
     unsigned long sizeLights();
60 66
     Light& getLight(unsigned long index);
67
+    void addLight(Light* l);
61 68
 
62 69
     unsigned long sizeSprites();
63 70
     Sprite& getSprite(unsigned long index);
71
+    void addSprite(Sprite* s);
64 72
 
65 73
   private:
66 74
     void sortModels();

+ 1
- 0
include/RoomData.h View File

@@ -75,6 +75,7 @@ class StaticModel {
75 75
 
76 76
 class Portal {
77 77
   public:
78
+    Portal(float vert[4][3], float norm[3], int adj);
78 79
     Portal(TombRaider& tr, unsigned int room, unsigned int index, Matrix& transform);
79 80
 
80 81
     void getVertices(float vert[4][3]);

+ 6
- 0
include/Sprite.h View File

@@ -8,10 +8,16 @@
8 8
 #ifndef _SPRITE_H_
9 9
 #define _SPRITE_H_
10 10
 
11
+#include <cstdint>
12
+
13
+#include "loader/Loader.h"
11 14
 #include "TombRaider.h"
12 15
 
13 16
 class Sprite {
14 17
   public:
18
+    Sprite(struct vertex_t vert, uint16_t tex);
19
+
20
+    // Old API
15 21
     Sprite(TombRaider& tr, unsigned int room, unsigned int index);
16 22
     Sprite(TombRaider& tr, unsigned int item, unsigned int sequence, unsigned int index);
17 23
     void display();

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

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

+ 2
- 1
src/Game.cpp View File

@@ -120,9 +120,10 @@ int Game::loadLevel(const char* level) {
120 120
         } else {
121 121
             mLoaded = true;
122 122
             getRender().setMode(Render::modeVertexLight);
123
-            return 0;
124 123
         }
125 124
     }
125
+
126
+    return 0;
126 127
 }
127 128
 
128 129
 void Game::handleAction(ActionEvents action, bool isFinished) {

+ 47
- 2
src/Mesh.cpp View File

@@ -11,6 +11,51 @@
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);
23
+}
24
+
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);
33
+}
34
+
35
+void Mesh::drawAlpha() {
36
+    if ((texturedRectangles.size() == 0)
37
+            && (texturedTriangles.size() == 0)
38
+            && (coloredRectangles.size() == 0)
39
+            && (coloredTriangles.size() == 0)) {
40
+        drawAlphaOld();
41
+        return;
42
+    }
43
+
44
+    // TODO replicate drawAlphaOld, but get object texture coord from World
45
+}
46
+
47
+void Mesh::drawSolid() {
48
+    if ((texturedRectangles.size() == 0)
49
+            && (texturedTriangles.size() == 0)
50
+            && (coloredRectangles.size() == 0)
51
+            && (coloredTriangles.size() == 0)) {
52
+        drawSolidOld();
53
+        return;
54
+    }
55
+
56
+
57
+}
58
+
14 59
 
15 60
 ////////////////////////////////////////////////////////////
16 61
 // Constructors
@@ -114,7 +159,7 @@ Mesh::~Mesh() {
114 159
 // Public Accessors
115 160
 ////////////////////////////////////////////////////////////
116 161
 
117
-void Mesh::drawAlpha() {
162
+void Mesh::drawAlphaOld() {
118 163
     unsigned int i, j, k, index;
119 164
 
120 165
 
@@ -186,7 +231,7 @@ void Mesh::drawAlpha() {
186 231
 }
187 232
 
188 233
 
189
-void Mesh::drawSolid() {
234
+void Mesh::drawSolidOld() {
190 235
     unsigned int i, j, k, index;
191 236
 
192 237
 

+ 39
- 9
src/Room.cpp View File

@@ -391,6 +391,12 @@ Room::Room(TombRaider& tr, unsigned int index) {
391 391
 #endif
392 392
 }
393 393
 
394
+Room::Room() : flags(0), numXSectors(0), numZSectors(0) {
395
+    pos[0] = 0;
396
+    pos[1] = 0;
397
+    pos[2] = 0;
398
+}
399
+
394 400
 #define EMPTY_VECTOR(x)     \
395 401
 while (!x.empty()) {        \
396 402
     delete x[x.size() - 1]; \
@@ -443,20 +449,20 @@ void Room::display(bool alpha) {
443 449
 
444 450
     switch (getRender().getMode()) {
445 451
         case Render::modeWireframe:
446
-            getMesh().mMode = Mesh::MeshModeWireframe;
452
+            mesh.mMode = Mesh::MeshModeWireframe;
447 453
             break;
448 454
         case Render::modeSolid:
449
-            getMesh().mMode = Mesh::MeshModeSolid;
455
+            mesh.mMode = Mesh::MeshModeSolid;
450 456
             break;
451 457
         default:
452
-            getMesh().mMode = Mesh::MeshModeTexture;
458
+            mesh.mMode = Mesh::MeshModeTexture;
453 459
             break;
454 460
     }
455 461
 
456 462
     if (alpha)
457
-        getMesh().drawAlpha();
463
+        mesh.drawAlpha();
458 464
     else
459
-        getMesh().drawSolid();
465
+        mesh.drawSolid();
460 466
 
461 467
     glPopMatrix();
462 468
 
@@ -553,6 +559,10 @@ long Room::getAdjacentRoom(unsigned long index) {
553 559
     return adjacentRooms.at(index);
554 560
 }
555 561
 
562
+void Room::addAdjacentRoom(long r) {
563
+    adjacentRooms.push_back(r);
564
+}
565
+
556 566
 unsigned long Room::sizePortals() {
557 567
     return portals.size();
558 568
 }
@@ -562,6 +572,10 @@ Portal& Room::getPortal(unsigned long index) {
562 572
     return *portals.at(index);
563 573
 }
564 574
 
575
+void Room::addPortal(Portal* p) {
576
+    portals.push_back(p);
577
+}
578
+
565 579
 unsigned long Room::sizeSectors() {
566 580
     return sectors.size();
567 581
 }
@@ -571,6 +585,10 @@ Sector& Room::getSector(unsigned long index) {
571 585
     return *sectors.at(index);
572 586
 }
573 587
 
588
+void Room::addSector(Sector* s) {
589
+    sectors.push_back(s);
590
+}
591
+
574 592
 unsigned long Room::sizeBox() {
575 593
     return boxes.size();
576 594
 }
@@ -580,6 +598,10 @@ Box& Room::getBox(unsigned long index) {
580 598
     return *boxes.at(index);
581 599
 }
582 600
 
601
+void Room::addBox(Box* b) {
602
+    boxes.push_back(b);
603
+}
604
+
583 605
 unsigned long Room::sizeModels() {
584 606
     return models.size();
585 607
 }
@@ -589,6 +611,10 @@ StaticModel& Room::getModel(unsigned long index) {
589 611
     return *models.at(index);
590 612
 }
591 613
 
614
+void Room::addModel(StaticModel* s) {
615
+    models.push_back(s);
616
+}
617
+
592 618
 void Room::sortModels() {
593 619
     std::sort(models.begin(), models.end(), StaticModel::compare);
594 620
 }
@@ -602,6 +628,10 @@ Light& Room::getLight(unsigned long index) {
602 628
     return *lights.at(index);
603 629
 }
604 630
 
631
+void Room::addLight(Light* l) {
632
+    lights.push_back(l);
633
+}
634
+
605 635
 unsigned long Room::sizeSprites() {
606 636
     return sprites.size();
607 637
 }
@@ -611,11 +641,11 @@ Sprite& Room::getSprite(unsigned long index) {
611 641
     return *sprites.at(index);
612 642
 }
613 643
 
614
-BoundingBox& Room::getBoundingBox() {
615
-    return bbox;
644
+void Room::addSprite(Sprite* s) {
645
+    sprites.push_back(s);
616 646
 }
617 647
 
618
-Mesh& Room::getMesh() {
619
-    return mesh;
648
+BoundingBox& Room::getBoundingBox() {
649
+    return bbox;
620 650
 }
621 651
 

+ 4
- 0
src/RoomData.cpp View File

@@ -218,6 +218,10 @@ Portal::Portal(TombRaider& tr, unsigned int room, unsigned int index, Matrix& tr
218 218
     }
219 219
 }
220 220
 
221
+Portal::Portal(float vert[4][3], float norm[3], int adj) {
222
+
223
+}
224
+
221 225
 void Portal::getVertices(float vert[4][3]) {
222 226
     for (unsigned int i = 0; i < 4; i++) {
223 227
         for (unsigned int j = 0; j < 3; j++) {

+ 4
- 0
src/Sprite.cpp View File

@@ -100,6 +100,10 @@ Sprite::Sprite(TombRaider& tr, unsigned int room, unsigned int index) {
100 100
     radius = 0.0f;
101 101
 }
102 102
 
103
+Sprite::Sprite(struct vertex_t vert, uint16_t tex) {
104
+
105
+}
106
+
103 107
 void Sprite::display() {
104 108
     if (!getRender().isVisible(pos[0], pos[1], pos[2], radius))
105 109
         return;

+ 3
- 1
src/UI.cpp View File

@@ -200,7 +200,9 @@ void UI::display() {
200 200
             ImGui::PopItemWidth();
201 201
             ImGui::SameLine();
202 202
             if (ImGui::Checkbox("Game", &game)) {
203
-                if (game && !getGame().isLoaded())
203
+                if (game && (getTextureManager().numTextures(
204
+                        game ? TextureManager::TextureStorage::GAME
205
+                        : TextureManager::TextureStorage::SYSTEM) <= 0))
204 206
                     game = false;
205 207
             }
206 208
             ImGui::SameLine();

+ 20
- 11
src/loader/LoaderTR2.cpp View File

@@ -11,6 +11,7 @@
11 11
 
12 12
 #include "global.h"
13 13
 #include "Log.h"
14
+#include "Mesh.h"
14 15
 #include "TextureManager.h"
15 16
 #include "utils/pixel.h"
16 17
 #include "loader/LoaderTR2.h"
@@ -55,7 +56,7 @@ int LoaderTR2::load(std::string f) {
55 56
     loadSoundDetails();
56 57
     loadSampleIndices();
57 58
 
58
-    return 42; // TODO Not finished with implementation!
59
+    return 0; // TODO Not finished with implementation!
59 60
 }
60 61
 
61 62
 void LoaderTR2::loadPaletteTextiles() {
@@ -101,27 +102,33 @@ void LoaderTR2::loadRooms() {
101 102
         // Number of data words (2 bytes) to follow
102 103
         uint32_t dataToFollow = file.readU32();
103 104
 
105
+
106
+        std::vector<struct vertex> vertices;
107
+
104 108
         uint16_t numVertices = file.readU16();
105 109
         for (unsigned int v = 0; v < numVertices; v++) {
110
+            struct vertex v;
106 111
             // Vertex coordinates, relative to x/zOffset
107
-            int16_t relativeX = file.read16();
108
-            int16_t relativeY = file.read16();
109
-            int16_t relativeZ = file.read16();
112
+            v.x = file.read16();
113
+            v.y = file.read16();
114
+            v.z = file.read16();
110 115
 
111
-            int16_t lighting1 = file.read16();
116
+            v.light1 = file.read16();
112 117
 
113 118
             // Set of flags for special rendering effects
114 119
             // 0x8000 - Something to do with water surface?
115 120
             // 0x4000 - Underwater lighting modulation/movement if seen from above
116 121
             // 0x2000 - Water/Quicksand surface movement
117 122
             // 0x0010 - Normal?
118
-            uint16_t attributes = file.readU16();
123
+            v.attributes = file.readU16();
119 124
 
120
-            int16_t lighting2 = file.read16(); // Almost always equal to lighting1
125
+            v.light2 = file.read16(); // Almost always equal to light1
121 126
 
122
-            // TODO store vertex somewhere
127
+            vertices.push_back(v);
123 128
         }
124 129
 
130
+        Room* room = new Room();
131
+
125 132
         uint16_t numRectangles = file.readU16();
126 133
         for (unsigned int r = 0; r < numRectangles; r++) {
127 134
             // Indices into the vertex list read just before
@@ -133,7 +140,8 @@ void LoaderTR2::loadRooms() {
133 140
             // Index into the object-texture list
134 141
             uint16_t texture = file.readU16();
135 142
 
136
-            // TODO store rectangles somewhere
143
+            room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
144
+                    vertices.at(vertex3), vertices.at(vertex4), texture);
137 145
         }
138 146
 
139 147
         uint16_t numTriangles = file.readU16();
@@ -146,7 +154,8 @@ void LoaderTR2::loadRooms() {
146 154
             // Index into the object-texture list
147 155
             uint16_t texture = file.readU16();
148 156
 
149
-            // TODO store triangles somewhere
157
+            room->getMesh().addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
158
+                    vertices.at(vertex3), texture);
150 159
         }
151 160
 
152 161
         uint16_t numSprites = file.readU16();
@@ -154,7 +163,7 @@ void LoaderTR2::loadRooms() {
154 163
             uint16_t vertex = file.readU16(); // Index into vertex list
155 164
             uint16_t texture = file.readU16(); // Index into object-texture list
156 165
 
157
-            // TODO store sprites somewhere
166
+            room->addSprite(new Sprite(vertices.at(vertex), texture));
158 167
         }
159 168
 
160 169
         uint16_t numPortals = file.readU16();

+ 16
- 10
src/utils/pixel.cpp View File

@@ -32,8 +32,8 @@ void bgr2rgb24(unsigned char* image, unsigned int w, unsigned int h) {
32 32
     for (unsigned int i = 0; i < (w * h); ++i) {
33 33
         /* 24-bit BGR to RGB */
34 34
         unsigned char swap = image[(i * 3) + 2];
35
-        image[(i * 3) + 2] = image[(i * 3)];
36
-        image[(i * 3)] = swap;
35
+        image[(i * 3) + 2] = image[i * 3];
36
+        image[i * 3] = swap;
37 37
     }
38 38
 }
39 39
 
@@ -46,7 +46,7 @@ void bgra2rgba32(unsigned char* image, unsigned int w, unsigned int h) {
46 46
         /* 32-bit BGRA to RGBA */
47 47
         unsigned char swap = image[(i * 4) + 2];
48 48
         image[(i * 4) + 2] = image[(i * 4)];
49
-        image[(i * 4)] = swap;
49
+        image[i * 4] = swap;
50 50
     }
51 51
 }
52 52
 
@@ -56,9 +56,9 @@ void argb2rgba32(unsigned char* image, unsigned int w, unsigned int h) {
56 56
     assert(h > 0);
57 57
 
58 58
     for (unsigned int i = 0; i < (w * h); ++i) {
59
-        /* 32-bit ARGB to RGBA */
60
-        unsigned char swap = image[(i * 4) + 3];
61
-        image[(i * 4)] = image[(i * 4) + 1];
59
+        // 32-bit ARGB to RGBA
60
+        unsigned char swap = image[i * 4];
61
+        image[i * 4] = image[(i * 4) + 1];
62 62
         image[(i * 4) + 1] = image[(i * 4) + 2];
63 63
         image[(i * 4) + 2] = image[(i * 4) + 3];
64 64
         image[(i * 4) + 3] = swap;
@@ -72,10 +72,16 @@ unsigned char* argb16to32(unsigned char* image, unsigned int w, unsigned int h)
72 72
 
73 73
     unsigned char* img = new unsigned char[w * h * 4];
74 74
     for (unsigned int i = 0; i < (w * h); ++i) {
75
-        img[i * 4] = ((image[i] >> 10) & 0x1F) * 8;
76
-        img[(i * 4) + 1] = ((image[i] >> 5) & 0x1F) * 8;
77
-        img[(i * 4) + 2] = (image[i] & 0x1F) * 8;
78
-        img[(i * 4) + 3] = (image[i] & 0x8000) ? 0xFF : 0;
75
+        // arrr.rrgg gggb.bbbb shift to 5bit
76
+        img[i * 4] = (image[(i * 2) + 1] & 0x80) ? 0xFF : 0; // A
77
+        img[(i * 4) + 1] = (image[(i * 2) + 1] & 0x7C) >> 2; // R
78
+        img[(i * 4) + 2] = (image[(i * 2) + 1] & 0x03) << 3;
79
+        img[(i * 4) + 2] |= (image[i * 2] & 0xE0) >> 5; // G
80
+        img[(i * 4) + 3] = image[i * 2] & 0x1F; // B
81
+
82
+        img[(i * 4) + 1] <<= 3; // R
83
+        img[(i * 4) + 2] <<= 3; // G
84
+        img[(i * 4) + 3] <<= 3; // B
79 85
     }
80 86
     return img;
81 87
 }

Loading…
Cancel
Save