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
 
4
 
5
     [ 20141207 ]
5
     [ 20141207 ]
6
     * LoaderTR2 now also loads all important parts of the Room structures
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
     [ 20141206 ]
9
     [ 20141206 ]
9
     * LoaderTR2 now tries to load Moveables and Entities
10
     * LoaderTR2 now tries to load Moveables and Entities

+ 9
- 5
include/Mesh.h View File

21
     struct rectangle_t {
21
     struct rectangle_t {
22
         Vec3 a, b, c, d;
22
         Vec3 a, b, c, d;
23
         uint16_t texture;
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
     Mesh();
31
     Mesh();
35
     void addTexturedRectangle(Vec3 a, Vec3 b, Vec3 c, Vec3 d, uint16_t textile);
37
     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);
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
     std::vector<rectangle_t> texturedRectangles;
45
     std::vector<rectangle_t> texturedRectangles;
42
     std::vector<rectangle_t> coloredRectangles;
46
     std::vector<rectangle_t> coloredRectangles;
43
     std::vector<rectangle_t> texturedTriangles;
47
     std::vector<rectangle_t> texturedTriangles;
44
     std::vector<rectangle_t> coloredTriangles;
48
     std::vector<rectangle_t> coloredTriangles;
45
-
49
+    std::vector<Vec3> normals;
46
 
50
 
47
 
51
 
48
     // Old API
52
     // Old API

+ 5
- 0
include/TextureManager.h View File

12
 #include <cstdint>
12
 #include <cstdint>
13
 #include <vector>
13
 #include <vector>
14
 
14
 
15
+#include "math/Vec3.h"
16
+
15
 // These are loaded into TextureStorage::SYSTEM by initialize()!
17
 // These are loaded into TextureStorage::SYSTEM by initialize()!
16
 #define TEXTURE_WHITE 0
18
 #define TEXTURE_WHITE 0
17
 #define TEXTURE_SPLASH 1
19
 #define TEXTURE_SPLASH 1
33
     bool isTriangle();
35
     bool isTriangle();
34
     void display(float x, float y, float w, float h, float z);
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
   private:
41
   private:
37
     void displayTriangle(float x, float y, float w, float h, float z);
42
     void displayTriangle(float x, float y, float w, float h, float z);
38
     void displayRectangle(float x, float y, float w, float h, float z);
43
     void displayRectangle(float x, float y, float w, float h, float z);

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

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

+ 31
- 6
src/Mesh.cpp View File

19
     texturedTriangles.emplace_back(a, b, c, Vec3(), textile);
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
 void Mesh::drawAlpha() {
34
 void Mesh::drawAlpha() {
31
     if ((texturedRectangles.size() == 0)
35
     if ((texturedRectangles.size() == 0)
32
         && (texturedTriangles.size() == 0)
36
         && (texturedTriangles.size() == 0)
33
         && (coloredRectangles.size() == 0)
37
         && (coloredRectangles.size() == 0)
34
-        && (coloredTriangles.size() == 0)) {
38
+        && (coloredTriangles.size() == 0)
39
+        && (normals.size() == 0)) {
35
         drawAlphaOld();
40
         drawAlphaOld();
36
         return;
41
         return;
37
     }
42
     }
38
 
43
 
44
+    // TODO
39
 }
45
 }
40
 
46
 
41
 void Mesh::drawSolid() {
47
 void Mesh::drawSolid() {
42
     if ((texturedRectangles.size() == 0)
48
     if ((texturedRectangles.size() == 0)
43
         && (texturedTriangles.size() == 0)
49
         && (texturedTriangles.size() == 0)
44
         && (coloredRectangles.size() == 0)
50
         && (coloredRectangles.size() == 0)
45
-        && (coloredTriangles.size() == 0)) {
51
+        && (coloredTriangles.size() == 0)
52
+        && (normals.size() == 0)) {
46
         drawSolidOld();
53
         drawSolidOld();
47
         return;
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
 }
76
 }
77
 
77
 
78
 void TextureTile::displayRectangle(float x, float y, float w, float h, float z) {
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
     float xmin = 256.0f, xmax = 0.0f;
83
     float xmin = 256.0f, xmax = 0.0f;
80
     float ymin = 256.0f, ymax = 0.0f;
84
     float ymin = 256.0f, ymax = 0.0f;
81
     for (int i = 0; i < 4; i++) {
85
     for (int i = 0; i < 4; i++) {
96
 
100
 
97
     glBegin(GL_QUADS);
101
     glBegin(GL_QUADS);
98
     glTexCoord2f(xmin / 256.0f, ymin / 256.0f);
102
     glTexCoord2f(xmin / 256.0f, ymin / 256.0f);
99
-    glVertex3f(x, y, z);
103
+    glVertex3f(a.x, a.y, a.z);
100
     glTexCoord2f(xmax / 256.0f, ymin / 256.0f);
104
     glTexCoord2f(xmax / 256.0f, ymin / 256.0f);
101
-    glVertex3f(x + w, y, z);
105
+    glVertex3f(b.x, b.y, b.z);
102
     glTexCoord2f(xmax / 256.0f, ymax / 256.0f);
106
     glTexCoord2f(xmax / 256.0f, ymax / 256.0f);
103
-    glVertex3f(x + w, y + h, z);
107
+    glVertex3f(c.x, c.y, c.z);
104
     glTexCoord2f(xmin / 256.0f, ymax / 256.0f);
108
     glTexCoord2f(xmin / 256.0f, ymax / 256.0f);
105
-    glVertex3f(x, y + h, z);
109
+    glVertex3f(d.x, d.y, d.z);
106
     glEnd();
110
     glEnd();
107
 }
111
 }
108
 
112
 
109
 void TextureTile::displayTriangle(float x, float y, float w, float h, float z) {
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
     glEnd();
128
     glEnd();
130
 }
129
 }
131
 
130
 

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

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

Loading…
Cancel
Save