Browse Source

Continued work on new Mesh implementation

Thomas Buck 10 years ago
parent
commit
448783e71a
7 changed files with 89 additions and 47 deletions
  1. 1
    0
      ChangeLog.md
  2. 9
    5
      include/Mesh.h
  3. 5
    0
      include/TextureManager.h
  4. 5
    0
      include/loader/LoaderTR2.h
  5. 31
    6
      src/Mesh.cpp
  6. 21
    22
      src/TextureManager.cpp
  7. 17
    14
      src/loader/LoaderTR2.cpp

+ 1
- 0
ChangeLog.md View File

@@ -4,6 +4,7 @@
4 4
 
5 5
     [ 20141207 ]
6 6
     * LoaderTR2 now also loads all important parts of the Room structures
7
+    * Started work on new Mesh implementation used with LoaderTR2
7 8
 
8 9
     [ 20141206 ]
9 10
     * LoaderTR2 now tries to load Moveables and Entities

+ 9
- 5
include/Mesh.h View File

@@ -21,9 +21,11 @@ class Mesh {
21 21
     struct rectangle_t {
22 22
         Vec3 a, b, c, d;
23 23
         uint16_t texture;
24
+        float red, green, blue;
24 25
 
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) { }
26
+        rectangle_t(Vec3 _a, Vec3 _b, Vec3 _c, Vec3 _d, uint16_t t,
27
+                    float re = 0.0f, float gr = 0.0f, float bl = 0.0f)
28
+            : a(_a), b(_b), c(_c), d(_d), texture(t), red(re), green(gr), blue(bl) { }
27 29
     };
28 30
 
29 31
     Mesh();
@@ -35,14 +37,16 @@ class Mesh {
35 37
     void addTexturedRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, uint16_t textile);
36 38
     void addTexturedTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile);
37 39
 
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
+    void addColoredRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, float re, float gr, float bl);
41
+    void addColoredTriangle(Vec3 a, Vec3 b, Vec3 c, float re, float gr, float bl);
42
+
43
+    void addNormal(Vec3 n);
40 44
 
41 45
     std::vector<rectangle_t> texturedRectangles;
42 46
     std::vector<rectangle_t> coloredRectangles;
43 47
     std::vector<rectangle_t> texturedTriangles;
44 48
     std::vector<rectangle_t> coloredTriangles;
45
-
49
+    std::vector<Vec3> normals;
46 50
 
47 51
 
48 52
     // Old API

+ 5
- 0
include/TextureManager.h View File

@@ -12,6 +12,8 @@
12 12
 #include <cstdint>
13 13
 #include <vector>
14 14
 
15
+#include "math/Vec3.h"
16
+
15 17
 // These are loaded into TextureStorage::SYSTEM by initialize()!
16 18
 #define TEXTURE_WHITE 0
17 19
 #define TEXTURE_SPLASH 1
@@ -33,6 +35,9 @@ class TextureTile {
33 35
     bool isTriangle();
34 36
     void display(float x, float y, float w, float h, float z);
35 37
 
38
+    void displayTriangle(Vec3 a, Vec3 b, Vec3 c);
39
+    void displayRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d);
40
+
36 41
   private:
37 42
     void displayTriangle(float x, float y, float w, float h, float z);
38 43
     void displayRectangle(float x, float y, float w, float h, float z);

+ 5
- 0
include/loader/LoaderTR2.h View File

@@ -8,6 +8,9 @@
8 8
 #ifndef _LOADER_LOADER_TR2_H_
9 9
 #define _LOADER_LOADER_TR2_H_
10 10
 
11
+#include <array>
12
+#include <cstdint>
13
+
11 14
 #include "loader/Loader.h"
12 15
 
13 16
 class LoaderTR2 : public Loader {
@@ -38,6 +41,8 @@ class LoaderTR2 : public Loader {
38 41
     void loadSampleIndices();
39 42
 
40 43
     void loadExternalSoundFile(std::string f);
44
+
45
+    std::array<uint32_t, 256> palette;
41 46
 };
42 47
 
43 48
 #endif

+ 31
- 6
src/Mesh.cpp View File

@@ -19,34 +19,59 @@ void Mesh::addTexturedTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile) {
19 19
     texturedTriangles.emplace_back(a, b, c, Vec3(), textile);
20 20
 }
21 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);
22
+void Mesh::addColoredRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, float re, float gr, float bl) {
23
+    coloredRectangles.emplace_back(a, b, c, d, -1, re, gr, bl);
24 24
 }
25 25
 
26
-void Mesh::addColoredTriangle(Vec3 a, Vec3 b, Vec3 c, uint16_t textile) {
27
-    coloredTriangles.emplace_back(a, b, c, Vec3(), textile);
26
+void Mesh::addColoredTriangle(Vec3 a, Vec3 b, Vec3 c, float re, float gr, float bl) {
27
+    coloredTriangles.emplace_back(a, b, c, Vec3(), -1, re, gr, bl);
28
+}
29
+
30
+void Mesh::addNormal(Vec3 n) {
31
+    normals.emplace_back(n);
28 32
 }
29 33
 
30 34
 void Mesh::drawAlpha() {
31 35
     if ((texturedRectangles.size() == 0)
32 36
         && (texturedTriangles.size() == 0)
33 37
         && (coloredRectangles.size() == 0)
34
-        && (coloredTriangles.size() == 0)) {
38
+        && (coloredTriangles.size() == 0)
39
+        && (normals.size() == 0)) {
35 40
         drawAlphaOld();
36 41
         return;
37 42
     }
38 43
 
44
+    // TODO
39 45
 }
40 46
 
41 47
 void Mesh::drawSolid() {
42 48
     if ((texturedRectangles.size() == 0)
43 49
         && (texturedTriangles.size() == 0)
44 50
         && (coloredRectangles.size() == 0)
45
-        && (coloredTriangles.size() == 0)) {
51
+        && (coloredTriangles.size() == 0)
52
+        && (normals.size() == 0)) {
46 53
         drawSolidOld();
47 54
         return;
48 55
     }
49 56
 
57
+    // Render textured quads
58
+    for (auto& q : texturedRectangles) {
59
+        if (mMode == MeshModeWireframe) {
60
+            getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
61
+            glBegin(GL_QUADS);
62
+            glTexCoord2f(0.0f, 0.0f);
63
+            glVertex3f(q.a.x, q.a.y, q.a.z);
64
+            glTexCoord2f(1.0f, 0.0f);
65
+            glVertex3f(q.b.x, q.b.y, q.b.z);
66
+            glTexCoord2f(1.0f, 1.0f);
67
+            glVertex3f(q.c.x, q.c.y, q.c.z);
68
+            glTexCoord2f(0.0f, 1.0f);
69
+            glVertex3f(q.d.x, q.d.y, q.d.z);
70
+            glEnd();
71
+        } else if (mMode == MeshModeTexture) {
72
+            getTextureManager().getTile(q.texture).displayRectangle(q.a, q.b, q.c, q.d);
73
+        }
74
+    }
50 75
 }
51 76
 
52 77
 

+ 21
- 22
src/TextureManager.cpp View File

@@ -76,6 +76,10 @@ void TextureTile::display(float x, float y, float w, float h, float z) {
76 76
 }
77 77
 
78 78
 void TextureTile::displayRectangle(float x, float y, float w, float h, float z) {
79
+    displayRectangle(Vec3(x, y, z), Vec3(x + w, y, z), Vec3(x + w, y + h, z), Vec3(x, y + h, z));
80
+}
81
+
82
+void TextureTile::displayRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d) {
79 83
     float xmin = 256.0f, xmax = 0.0f;
80 84
     float ymin = 256.0f, ymax = 0.0f;
81 85
     for (int i = 0; i < 4; i++) {
@@ -96,36 +100,31 @@ void TextureTile::displayRectangle(float x, float y, float w, float h, float z)
96 100
 
97 101
     glBegin(GL_QUADS);
98 102
     glTexCoord2f(xmin / 256.0f, ymin / 256.0f);
99
-    glVertex3f(x, y, z);
103
+    glVertex3f(a.x, a.y, a.z);
100 104
     glTexCoord2f(xmax / 256.0f, ymin / 256.0f);
101
-    glVertex3f(x + w, y, z);
105
+    glVertex3f(b.x, b.y, b.z);
102 106
     glTexCoord2f(xmax / 256.0f, ymax / 256.0f);
103
-    glVertex3f(x + w, y + h, z);
107
+    glVertex3f(c.x, c.y, c.z);
104 108
     glTexCoord2f(xmin / 256.0f, ymax / 256.0f);
105
-    glVertex3f(x, y + h, z);
109
+    glVertex3f(d.x, d.y, d.z);
106 110
     glEnd();
107 111
 }
108 112
 
109 113
 void TextureTile::displayTriangle(float x, float y, float w, float h, float z) {
110
-    glBegin(GL_TRIANGLE_STRIP);
111
-    for (int i = 0; i < 3; i++) {
112
-        glTexCoord2f(vertices.at(i)->xPixel / 256.0f,
113
-                     vertices.at(i)->yPixel / 256.0f);
114
+    displayTriangle(Vec3(x, y, z), Vec3(x + w, y, z), Vec3(x + w, y + h, z));
115
+}
114 116
 
115
-        if (vertices.at(i)->xCoordinate == 255) {
116
-            if (vertices.at(i)->yCoordinate == 255) {
117
-                glVertex3f(x + w, y + h, z);
118
-            } else {
119
-                glVertex3f(x + w, y, z);
120
-            }
121
-        } else {
122
-            if (vertices.at(i)->yCoordinate == 255) {
123
-                glVertex3f(x, y + h, z);
124
-            } else {
125
-                glVertex3f(x, y, z);
126
-            }
127
-        }
128
-    }
117
+void TextureTile::displayTriangle(Vec3 a, Vec3 b, Vec3 c) {
118
+    glBegin(GL_TRIANGLE_STRIP);
119
+    glTexCoord2f(vertices.at(0)->xPixel / 256.0f,
120
+                 vertices.at(0)->yPixel / 256.0f);
121
+    glVertex3f(a.x, a.y, a.z);
122
+    glTexCoord2f(vertices.at(1)->xPixel / 256.0f,
123
+                 vertices.at(1)->yPixel / 256.0f);
124
+    glVertex3f(b.x, b.y, b.z);
125
+    glTexCoord2f(vertices.at(2)->xPixel / 256.0f,
126
+                 vertices.at(2)->yPixel / 256.0f);
127
+    glVertex3f(c.x, c.y, c.z);
129 128
     glEnd();
130 129
 }
131 130
 

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

@@ -5,8 +5,6 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
-#include <array>
9
-#include <cstdint>
10 8
 #include <vector>
11 9
 
12 10
 #include "global.h"
@@ -74,12 +72,9 @@ void LoaderTR2::loadPaletteTextiles() {
74 72
     file.seek(file.tell() + 768); // Skip 8bit palette, 256 * 3 bytes
75 73
 
76 74
     // Read the 16bit palette, 256 * 4 bytes, RGBA, A unused
77
-    std::array<uint32_t, 256> palette; //!< RGBA, A unused
78 75
     for (auto& x : palette)
79 76
         x = file.readU32();
80 77
 
81
-    // TODO store palette somewhere?
82
-
83 78
     uint32_t numTextiles = file.readU32();
84 79
 
85 80
     file.seek(file.tell() + (numTextiles * 256 * 256)); // Skip 8bit textiles
@@ -443,8 +438,8 @@ void LoaderTR2::loadRooms() {
443 438
 
444 439
         getWorld().addRoom(room);
445 440
 
446
-        if ((numPortals == 0) || (numVertices == 0)
447
-            || ((numRectangles == 0) && (numTriangles == 0)))
441
+        if ((numPortals == 0) && (numVertices == 0)
442
+            && (numRectangles == 0) && (numTriangles == 0))
448 443
             getLog() << "LoaderTR2: Room " << i << " seems invalid: " << numPortals << "p "
449 444
                      << numRectangles << "r " << numTriangles << "t " << numVertices
450 445
                      << "v" << Log::endl;
@@ -555,6 +550,8 @@ void LoaderTR2::loadMeshes() {
555 550
             vertices.emplace_back(x, y, z);
556 551
         }
557 552
 
553
+        Mesh* mesh = new Mesh();
554
+
558 555
         int16_t numNormals = mem.read16();
559 556
         if (numNormals > 0) {
560 557
             // External vertex lighting is used, with the lighting calculated
@@ -566,7 +563,8 @@ void LoaderTR2::loadMeshes() {
566 563
                 int16_t x = mem.read16();
567 564
                 int16_t y = mem.read16();
568 565
                 int16_t z = mem.read16();
569
-                // TODO store normals somewhere
566
+
567
+                mesh->addNormal(Vec3(x, y, z));
570 568
             }
571 569
         } else if (numNormals < 0) {
572 570
             // Internal vertex lighting is used,
@@ -577,8 +575,6 @@ void LoaderTR2::loadMeshes() {
577 575
             }
578 576
         }
579 577
 
580
-        Mesh* mesh = new Mesh();
581
-
582 578
         int16_t numTexturedRectangles = mem.read16();
583 579
         for (int r = 0; r < numTexturedRectangles; r++) {
584 580
             uint16_t vertex1 = mem.readU16();
@@ -611,11 +607,15 @@ void LoaderTR2::loadMeshes() {
611 607
             uint16_t vertex4 = mem.readU16();
612 608
             uint16_t texture = mem.readU16();
613 609
 
614
-            // TODO color?
610
+            int index = (texture & 0xFF00) >> 8;
611
+            float red = (palette.at(index) & 0xFF000000) >> 24,
612
+                  green = (palette.at(index) & 0x00FF0000) >> 16,
613
+                  blue = (palette.at(index) & 0x0000FF00) >> 8;
614
+
615 615
 
616 616
             mesh->addColoredRectangle(vertices.at(vertex1), vertices.at(vertex2),
617 617
                                       vertices.at(vertex3), vertices.at(vertex4),
618
-                                      texture);
618
+                                      red, green, blue);
619 619
         }
620 620
 
621 621
         int16_t numColoredTriangles = mem.read16();
@@ -625,10 +625,13 @@ void LoaderTR2::loadMeshes() {
625 625
             uint16_t vertex3 = mem.readU16();
626 626
             uint16_t texture = mem.readU16();
627 627
 
628
-            // TODO color?
628
+            int index = (texture & 0xFF00) >> 8;
629
+            float red = (palette.at(index) & 0xFF000000) >> 24,
630
+                  green = (palette.at(index) & 0x00FF0000) >> 16,
631
+                  blue = (palette.at(index) & 0x0000FF00) >> 8;
629 632
 
630 633
             mesh->addColoredTriangle(vertices.at(vertex1), vertices.at(vertex2),
631
-                                     vertices.at(vertex3), texture);
634
+                                     vertices.at(vertex3), red, green, blue);
632 635
         }
633 636
 
634 637
         getWorld().addMesh(mesh);

Loading…
Cancel
Save