Ver código fonte

Render StaticModels

Thomas Buck 10 anos atrás
pai
commit
6625071dfd

+ 2
- 0
ChangeLog.md Ver arquivo

@@ -7,6 +7,8 @@
7 7
     * Mesh is now called RoomMesh
8 8
     * Removed non-functional GLUT windowing implementation
9 9
     * Created new windowing implementation using GLFW3
10
+    * World Meshes are loaded properly
11
+    * StaticModel/StaticMesh in Rooms are displayed (albeit not properly)
10 12
 
11 13
     [ 20141216 ]
12 14
     * Allow navigation with a free-floating Camera

+ 55
- 1
include/Mesh.h Ver arquivo

@@ -9,11 +9,51 @@
9 9
 #define _MESH_H_
10 10
 
11 11
 #include <map>
12
+#include <vector>
13
+#include <glm/mat4x4.hpp>
12 14
 #include <glm/vec2.hpp>
13 15
 #include <glm/vec3.hpp>
14 16
 
17
+struct IndexedRectangle {
18
+    unsigned int v1, v2, v3, v4; // Vertex list indices
19
+    unsigned int texture; // Index into object-texture list
20
+
21
+    IndexedRectangle(unsigned int t, unsigned int _v1,
22
+                     unsigned int _v2, unsigned int _v3, unsigned int _v4 = 0)
23
+        : v1(_v1), v2(_v2), v3(_v3), v4(_v4), texture(t) { }
24
+};
25
+
26
+struct IndexedColoredRectangle {
27
+    unsigned int v1, v2, v3, v4;
28
+    unsigned char r, g, b;
29
+
30
+    IndexedColoredRectangle(unsigned char _r, unsigned char _g, unsigned char _b,
31
+                            unsigned int _v1, unsigned int _v2,
32
+                            unsigned int _v3, unsigned int _v4 = 0)
33
+        : v1(_v1), v2(_v2), v3(_v3), v4(_v4), r(_r), g(_g), b(_b) { }
34
+};
35
+
36
+// --------------------------------------
37
+
15 38
 class Mesh {
39
+  public:
40
+    Mesh(const std::vector<glm::vec3>& vertices,
41
+         const std::vector<IndexedRectangle>& rectangles,
42
+         const std::vector<IndexedRectangle>& triangles,
43
+         const std::vector<IndexedColoredRectangle>& coloredRectangles,
44
+         const std::vector<IndexedColoredRectangle>& coloredTriangles);
45
+    void prepare();
46
+    void display(glm::mat4 model, glm::mat4 view, glm::mat4 projection);
47
+
48
+  private:
49
+    std::vector<unsigned short> indices;
50
+    std::vector<glm::vec3> vertices;
51
+    std::vector<glm::vec2> uvs;
52
+    std::vector<unsigned int> textures;
16 53
 
54
+    std::vector<unsigned short> indicesColor;
55
+    std::vector<glm::vec3> verticesColor;
56
+    std::vector<glm::vec3> colors;
17 57
 };
18 58
 
19 59
 // --------------------------------------
@@ -24,7 +64,21 @@ struct PackedVertex {
24 64
     unsigned int tex;
25 65
 
26 66
     PackedVertex(glm::vec3 p, glm::vec2 u, unsigned int t) : pos(p), uv(u), tex(t) { }
27
-    bool operator<(const PackedVertex& v) const { return memcmp(this, &v, sizeof(PackedVertex)) > 0; }
67
+
68
+    bool operator<(const PackedVertex& v) const {
69
+        return memcmp(this, &v, sizeof(PackedVertex)) > 0;
70
+    }
71
+};
72
+
73
+struct PackedColoredVertex {
74
+    glm::vec3 pos;
75
+    glm::vec3 col;
76
+
77
+    PackedColoredVertex(glm::vec3 p, glm::vec3 c) : pos(p), col(c) { }
78
+
79
+    bool operator<(const PackedColoredVertex& v) const {
80
+        return memcmp(this, &v, sizeof(PackedColoredVertex)) > 0;
81
+    }
28 82
 };
29 83
 
30 84
 template <typename T>

+ 22
- 16
include/RoomData.h Ver arquivo

@@ -14,15 +14,30 @@
14 14
 
15 15
 class BoundingBox {
16 16
   public:
17
-    BoundingBox(glm::vec3 min, glm::vec3 max);
18
-    void display(bool points, const unsigned char c1[4], const unsigned char c2[4]);
19
-    bool inBox(float x, float y, float z);
20
-    bool inBoxPlane(float x, float z);
17
+    BoundingBox(glm::vec3 min, glm::vec3 max) : a(min), b(max) { }
18
+    bool inBox(float x, float y, float z) { return ((y > a.y) && (y < b.y) && inBoxPlane(x, z)); }
19
+    bool inBoxPlane(float x, float z) { return ((x > a.x) && (x < b.x) && (z > a.z) && (z < b.z)); }
21 20
 
22 21
   private:
23 22
     glm::vec3 a, b;
24 23
 };
25 24
 
25
+// --------------------------------------
26
+
27
+class StaticModel {
28
+  public:
29
+    StaticModel(glm::vec3 p, float a, int i) : pos(p), angle(a), id(i), cache(-1) { }
30
+    void display(glm::mat4 view, glm::mat4 projection);
31
+
32
+  private:
33
+    glm::vec3 pos;
34
+    float angle;
35
+    int id;
36
+    int cache;
37
+};
38
+
39
+// --------------------------------------
40
+
26 41
 class Light {
27 42
   public:
28 43
     /*!
@@ -50,18 +65,7 @@ class Light {
50 65
     LightType type; //! Type of light
51 66
 };
52 67
 
53
-class StaticModel {
54
-  public:
55
-    void display();
56
-
57
-  private:
58
-    int index;
59
-    float yaw;
60
-    float pos[3];
61
-
62
-    // ?
63
-    //float bbox[2][3];
64
-};
68
+// --------------------------------------
65 69
 
66 70
 class Portal {
67 71
   public:
@@ -76,6 +80,8 @@ class Portal {
76 80
     int adjoiningRoom;
77 81
 };
78 82
 
83
+// --------------------------------------
84
+
79 85
 class Sector {
80 86
   public:
81 87
     Sector(float f, float c, bool w) : floor(f), ceiling(c), wall(w) { }

+ 7
- 16
include/RoomMesh.h Ver arquivo

@@ -8,40 +8,31 @@
8 8
 #ifndef _ROOM_MESH_H_
9 9
 #define _ROOM_MESH_H_
10 10
 
11
-#include <cstdint>
12 11
 #include <vector>
13 12
 #include <glm/mat4x4.hpp>
14 13
 #include <glm/vec2.hpp>
15 14
 #include <glm/vec3.hpp>
16 15
 
16
+#include "Mesh.h"
17
+
17 18
 struct RoomVertexTR2 {
18
-    int16_t x, y, z; // Vertex coordinates, relative to x/zOffset
19
-    int16_t light1, light2; // Almost always equal
19
+    int x, y, z; // Vertex coordinates, relative to x/zOffset
20
+    int light1, light2; // Almost always equal
20 21
 
21 22
     // Set of flags for special rendering effects
22 23
     // 0x8000 - Something to do with water surface?
23 24
     // 0x4000 - Underwater lighting modulation/movement if seen from above
24 25
     // 0x2000 - Water/Quicksand surface movement
25 26
     // 0x0010 - Normal?
26
-    uint16_t attributes;
27
-};
28
-
29
-struct IndexedRectangle {
30
-    unsigned int v1, v2, v3, v4; // Vertex list indices
31
-    unsigned int texture; // Index into object-texture list
32
-
33
-    IndexedRectangle(uint16_t t, uint16_t _v1, uint16_t _v2, uint16_t _v3, uint16_t _v4 = 0)
34
-        : v1(_v1), v2(_v2), v3(_v3), v4(_v4), texture(t) { }
27
+    unsigned int attributes;
35 28
 };
36 29
 
37 30
 class RoomMesh {
38 31
   public:
39 32
     RoomMesh(const std::vector<RoomVertexTR2>& vertices,
40
-         const std::vector<IndexedRectangle>& rectangles,
41
-         const std::vector<IndexedRectangle>& triangles);
42
-
33
+             const std::vector<IndexedRectangle>& rectangles,
34
+             const std::vector<IndexedRectangle>& triangles);
43 35
     void prepare();
44
-
45 36
     void display(glm::mat4 model, glm::mat4 view, glm::mat4 projection);
46 37
 
47 38
   private:

+ 4
- 32
include/StaticMesh.h Ver arquivo

@@ -8,42 +8,14 @@
8 8
 #ifndef _STATIC_MODEL_H_
9 9
 #define _STATIC_MODEL_H_
10 10
 
11
-#include <vector>
12
-
13
-class TexturedTriangle {
14
-  public:
15
-    TexturedTriangle(int i[3], float s[6], int tex, unsigned short trans);
16
-    void display(float* vertices, float* colors, float* normals);
17
-
18
-  private:
19
-    int index[3];
20
-    float st[6];
21
-    int texture;
22
-    unsigned short transparency;
23
-};
24
-
25 11
 class StaticMesh {
26 12
   public:
27
-    StaticMesh(int id, int mesh);
28
-    ~StaticMesh();
29
-    void display();
30
-    float getRadius();
13
+    StaticMesh(int i, int m) : id(i), mesh(m) { }
14
+    void display(glm::mat4 model, glm::mat4 view, glm::mat4 projection);
31 15
 
32
-  private:
33
-    bool dontshow;
34
-    float center[3];
35
-    float radius;
36
-
37
-    unsigned int vertexCount;
38
-    unsigned int colorCount;
39
-    unsigned int normalCount;
40
-
41
-    float* vertices;
42
-    float* colors;
43
-    float* normals;
44
-
45
-    std::vector<TexturedTriangle*> triangles;
16
+    int getID() { return id; }
46 17
 
18
+  private:
47 19
     int id;
48 20
     int mesh;
49 21
 };

+ 7
- 0
include/system/Window.h Ver arquivo

@@ -68,6 +68,9 @@ class Window {
68 68
     static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
69 69
                        std::vector<unsigned short>& indices, glm::mat4 MVP, unsigned int texture);
70 70
 
71
+    static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
72
+                       std::vector<unsigned short>& indices, glm::mat4 MVP);
73
+
71 74
   protected:
72 75
     bool mInit;
73 76
     bool mFullscreen;
@@ -89,6 +92,10 @@ class Window {
89 92
     static const char* textureShaderVertex;
90 93
     static const char* textureShaderFragment;
91 94
 
95
+    static Shader colorShader;
96
+    static const char* colorShaderVertex;
97
+    static const char* colorShaderFragment;
98
+
92 99
     static unsigned int vertexArrayID;
93 100
 
94 101
     friend class UI;

+ 4
- 0
src/Game.cpp Ver arquivo

@@ -93,6 +93,10 @@ int Game::loadLevel(const char* level) {
93 93
             return -2;
94 94
         }
95 95
 
96
+        for (int i = 0; i < getWorld().sizeMesh(); i++) {
97
+            getWorld().getMesh(i).prepare();
98
+        }
99
+
96 100
         for (int i = 0; i < getWorld().sizeRoom(); i++) {
97 101
             getWorld().getRoom(i).prepare();
98 102
         }

+ 1
- 1
src/MenuFolder.cpp Ver arquivo

@@ -82,7 +82,7 @@ void MenuFolder::display() {
82 82
     // Draw half-transparent overlay
83 83
     glm::vec4 color(0.0f, 0.0f, 0.0f, 0.75f);
84 84
     Render::drawTexture(0.0f, 0.0f, getWindow().getWidth(), getWindow().getHeight(),
85
-                    color, TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
85
+                        color, TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
86 86
 
87 87
     // Draw heading
88 88
     Font::drawTextCentered(0, 10, 1.2f, BLUE, getWindow().getWidth(), VERSION);

+ 142
- 0
src/Mesh.cpp Ver arquivo

@@ -5,3 +5,145 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include "global.h"
9
+#include "TextureManager.h"
10
+#include "system/Window.h"
11
+#include "Mesh.h"
12
+
13
+Mesh::Mesh(const std::vector<glm::vec3>& vert,
14
+           const std::vector<IndexedRectangle>& rect,
15
+           const std::vector<IndexedRectangle>& tri,
16
+           const std::vector<IndexedColoredRectangle>& coloredRect,
17
+           const std::vector<IndexedColoredRectangle>& coloredTri) {
18
+    for (auto& t : rect) {
19
+        indices.push_back(0);
20
+        vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
21
+        vertices.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
22
+        vertices.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
23
+        vertices.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
24
+        textures.push_back(t.texture);
25
+    }
26
+
27
+    for (auto& t : tri) {
28
+        indices.push_back(1);
29
+        vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
30
+        vertices.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
31
+        vertices.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
32
+        textures.push_back(t.texture);
33
+    }
34
+
35
+    for (auto& t : coloredRect) {
36
+        indicesColor.push_back(0);
37
+        verticesColor.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
38
+        verticesColor.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
39
+        verticesColor.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
40
+        verticesColor.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
41
+        colors.push_back(glm::vec3(t.r, t.g, t.b));
42
+    }
43
+
44
+    for (auto& t : coloredTri) {
45
+        indicesColor.push_back(1);
46
+        verticesColor.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
47
+        verticesColor.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
48
+        verticesColor.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
49
+        colors.push_back(glm::vec3(t.r, t.g, t.b));
50
+    }
51
+}
52
+
53
+void Mesh::prepare() {
54
+    std::vector<unsigned short> ind;
55
+    std::vector<glm::vec3> vert;
56
+    std::vector<unsigned int> tex;
57
+    std::map<PackedVertex, unsigned short> vertexMap;
58
+    int vertIndex = 0;
59
+    for (int i = 0; i < indices.size(); i++) {
60
+        unsigned int texture = getTextureManager().getTile(textures.at(i)).getTexture();
61
+        for (int v = 0; v < ((indices.at(i) == 0) ? 4 : 3); v++) {
62
+            glm::vec2 uv = getTextureManager().getTile(textures.at(i)).getUV(v);
63
+            PackedVertex p(vertices.at(vertIndex + v), uv, texture);
64
+            unsigned short s;
65
+            if (findSimilarVertex(p, vertexMap, s)) {
66
+                ind.push_back(s); // Vertex already cached
67
+            } else {
68
+                vertexMap[p] = vert.size();
69
+                ind.push_back(vert.size());
70
+                vert.push_back(p.pos);
71
+                uvs.push_back(p.uv);
72
+                tex.push_back(p.tex);
73
+            }
74
+        }
75
+
76
+        if (indices.at(i) == 0) {
77
+            ind.push_back(ind.at(ind.size() - 2));
78
+            ind.push_back(ind.at(ind.size() - 5));
79
+        }
80
+
81
+        vertIndex += (indices.at(i) == 0) ? 4 : 3;
82
+    }
83
+    assert((ind.size() % 3) == 0);
84
+    assert(vert.size() == tex.size());
85
+    assert(vert.size() == uvs.size());
86
+    indices = ind;
87
+    vertices = vert;
88
+    textures = tex;
89
+
90
+    std::vector<unsigned short> indC;
91
+    std::vector<glm::vec3> vertC;
92
+    std::vector<glm::vec3> col;
93
+    std::map<PackedColoredVertex, unsigned short> vertexMapC;
94
+    vertIndex = 0;
95
+    for (int i = 0; i < indicesColor.size(); i++) {
96
+        for (int v = 0; v < ((indicesColor.at(i) == 0) ? 4 : 3); v++) {
97
+            PackedColoredVertex p(verticesColor.at(vertIndex + v), colors.at(i));
98
+            unsigned short s;
99
+            if (findSimilarVertex(p, vertexMapC, s)) {
100
+                indC.push_back(s); // Vertex already cached
101
+            } else {
102
+                vertexMapC[p] = vertC.size();
103
+                indC.push_back(vertC.size());
104
+                vertC.push_back(p.pos);
105
+                col.push_back(p.col);
106
+            }
107
+        }
108
+
109
+        if (indicesColor.at(i) == 0) {
110
+            indC.push_back(indC.at(indC.size() - 2));
111
+            indC.push_back(indC.at(indC.size() - 5));
112
+        }
113
+
114
+        vertIndex += (indicesColor.at(i) == 0) ? 4 : 3;
115
+    }
116
+    assert((indC.size() % 3) == 0);
117
+    assert(vertC.size() == col.size());
118
+    indicesColor = indC;
119
+    verticesColor = vertC;
120
+    colors = col;
121
+}
122
+
123
+void Mesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {
124
+    glm::mat4 MVP = projection * view * model;
125
+
126
+    if (indices.size() > 0) {
127
+        unsigned int indexStart = 0;
128
+        unsigned int indexPos = 1;
129
+        unsigned int texture = textures.at(indices.at(0));
130
+
131
+        while ((indexStart != indexPos) && (indexPos < indices.size())) {
132
+            while ((indexPos < indices.size()) && (textures.at(indices.at(indexPos)) == texture))
133
+                indexPos++;
134
+
135
+            std::vector<unsigned short> ind(indices.begin() + indexStart, indices.begin() + indexPos);
136
+            Window::drawGL(vertices, uvs, ind, MVP, texture);
137
+
138
+            if (indexPos < indices.size()) {
139
+                indexStart = indexPos;
140
+                indexPos += 1;
141
+                texture = textures.at(indices.at(indexStart));
142
+            }
143
+        }
144
+    }
145
+
146
+    if (indicesColor.size() > 0)
147
+        Window::drawGL(verticesColor, colors, indicesColor, MVP);
148
+}
149
+

+ 4
- 4
src/Render.cpp Ver arquivo

@@ -71,10 +71,10 @@ void Render::display() {
71 71
     static unsigned int w = getWindow().getWidth();
72 72
     static unsigned int h = getWindow().getHeight();
73 73
     static glm::mat4 projection = glm::perspective(45.0f, // Field of View
74
-                                                   (float)getWindow().getWidth()
75
-                                                 / (float)getWindow().getHeight(),
76
-                                                   0.1f, // Min Distance
77
-                                                   100000.0f); // Max Distance
74
+                                  (float)getWindow().getWidth()
75
+                                  / (float)getWindow().getHeight(),
76
+                                  0.1f, // Min Distance
77
+                                  100000.0f); // Max Distance
78 78
 
79 79
     if ((w != getWindow().getWidth()) || (h != getWindow().getHeight())) {
80 80
         w = getWindow().getWidth();

+ 4
- 0
src/Room.cpp Ver arquivo

@@ -19,6 +19,10 @@
19 19
 void Room::display(glm::mat4 view, glm::mat4 projection) {
20 20
     glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
21 21
     mesh->display(model, view, projection);
22
+
23
+    for (auto& m : models) {
24
+        m->display(view, projection);
25
+    }
22 26
 }
23 27
 
24 28
 bool Room::isWall(unsigned long sector) {

+ 13
- 99
src/RoomData.cpp Ver arquivo

@@ -5,95 +5,26 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include <glm/gtc/matrix_transform.hpp>
9
+
8 10
 #include "global.h"
9 11
 #include "SkeletalModel.h"
10 12
 #include "World.h"
11 13
 #include "RoomData.h"
12 14
 
13
-BoundingBox::BoundingBox(glm::vec3 min, glm::vec3 max) : a(min), b(max) { }
14
-
15
-bool BoundingBox::inBox(float x, float y, float z) {
16
-    return ((y > a.y) && (y < b.y) && inBoxPlane(x, z));
17
-}
18
-
19
-bool BoundingBox::inBoxPlane(float x, float z) {
20
-    return ((x > a.x) && (x < b.x)
21
-            && (z > a.z) && (z < b.z));
22
-}
23
-
24
-void BoundingBox::display(bool points, const unsigned char c1[4], const unsigned char c2[4]) {
25
-    /*
26
-    // Bind before entering now
27
-    //glBindTexture(GL_TEXTURE_2D, 1);
28
-    glPointSize(4.0);
29
-    //glLineWidth(1.25);
30
-
31
-    //! \fixme Need to make custom color key for this
32
-    glColor3ubv(c1);
33
-
34
-    glBegin(GL_POINTS);
35
-    glVertex3f(b[0], b[1], b[2]);
36
-    glVertex3f(a[0], a[1], a[2]);
37
-
38
-    if (points) {
39
-        glVertex3f(b[0], a[1], b[2]);
40
-        glVertex3f(a[0], b[1], b[2]);
41
-        glVertex3f(b[0], b[1], a[2]);
42
-        glVertex3f(a[0], a[1], b[2]);
43
-        glVertex3f(a[0], b[1], a[2]);
44
-        glVertex3f(b[0], a[1], a[2]);
15
+void StaticModel::display(glm::mat4 view, glm::mat4 projection) {
16
+    if (cache < 0) {
17
+        for (int i = 0; i < getWorld().sizeStaticMesh(); i++) {
18
+            if (getWorld().getStaticMesh(i).getID() == id) {
19
+                cache = i;
20
+            }
21
+        }
22
+        assert(cache >= 0);
45 23
     }
46 24
 
47
-    glEnd();
48
-
49
-    glColor3ubv(c2);
50
-
51
-    glBegin(GL_LINES);
52
-    // max, top quad
53
-    glVertex3f(b[0], b[1], b[2]);
54
-    glVertex3f(b[0], a[1], b[2]);
55
-
56
-    glVertex3f(b[0], b[1], b[2]);
57
-    glVertex3f(a[0], b[1], b[2]);
58
-
59
-    glVertex3f(b[0], b[1], b[2]);
60
-    glVertex3f(b[0], b[1], a[2]);
61
-
62
-    // max-min, vertical quads
63
-    glVertex3f(a[0], b[1], b[2]);
64
-    glVertex3f(a[0], b[1], a[2]);
65
-
66
-    glVertex3f(b[0], a[1], b[2]);
67
-    glVertex3f(b[0], a[1], a[2]);
68
-
69
-    glVertex3f(b[0], a[1], b[2]);
70
-    glVertex3f(a[0], a[1], b[2]);
71
-
72
-    // min-max, vertical quads
73
-    glVertex3f(b[0], b[1], a[2]);
74
-    glVertex3f(b[0], a[1], a[2]);
75
-
76
-    glVertex3f(b[0], b[1], a[2]);
77
-    glVertex3f(a[0], b[1], a[2]);
78
-
79
-    glVertex3f(a[0], b[1], b[2]);
80
-    glVertex3f(a[0], a[1], b[2]);
81
-
82
-
83
-    // min, bottom quad
84
-    glVertex3f(a[0], a[1], a[2]);
85
-    glVertex3f(a[0], b[1], a[2]);
86
-
87
-    glVertex3f(a[0], a[1], a[2]);
88
-    glVertex3f(b[0], a[1], a[2]);
89
-
90
-    glVertex3f(a[0], a[1], a[2]);
91
-    glVertex3f(a[0], a[1], b[2]);
92
-    glEnd();
93
-
94
-    glPointSize(1.0);
95
-    //glLineWidth(1.0);
96
-    */
25
+    glm::mat4 model = glm::rotate(glm::translate(glm::mat4(1.0f), pos),
26
+                                  angle, glm::vec3(0.0f, 1.0f, 0.0f));
27
+    getWorld().getStaticMesh(cache).display(model, view, projection);
97 28
 }
98 29
 
99 30
 // ----------------------------------------------------------------------------
@@ -132,23 +63,6 @@ Light::LightType Light::getType() {
132 63
 
133 64
 // ----------------------------------------------------------------------------
134 65
 
135
-void StaticModel::display() {
136
-    StaticMesh& mesh = getWorld().getStaticMesh(index);
137
-
138
-    //if (!getRender().isVisible(pos[0], pos[1], pos[2], mesh.getRadius()))
139
-    //    return;
140
-
141
-    /*
142
-    glPushMatrix();
143
-    glTranslated(pos[0], pos[1], pos[2]);
144
-    glRotated(yaw, 0, 1, 0);
145
-    mesh.display();
146
-    glPopMatrix();
147
-    */
148
-}
149
-
150
-// ----------------------------------------------------------------------------
151
-
152 66
 Portal::Portal(glm::vec3 vert[4], float norm[3], int adj) {
153 67
     for (unsigned int i = 0; i < 4; i++) {
154 68
         for (unsigned int j = 0; j < 3; j++) {

+ 2
- 2
src/RoomMesh.cpp Ver arquivo

@@ -12,8 +12,8 @@
12 12
 #include "RoomMesh.h"
13 13
 
14 14
 RoomMesh::RoomMesh(const std::vector<RoomVertexTR2>& vert,
15
-     const std::vector<IndexedRectangle>& rect,
16
-     const std::vector<IndexedRectangle>& tri) {
15
+                   const std::vector<IndexedRectangle>& rect,
16
+                   const std::vector<IndexedRectangle>& tri) {
17 17
     for (auto& t : rect) {
18 18
         indices.push_back(0);
19 19
         vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));

+ 2
- 131
src/StaticMesh.cpp Ver arquivo

@@ -6,139 +6,10 @@
6 6
  */
7 7
 
8 8
 #include "global.h"
9
-#include "Game.h"
10
-#include "Render.h"
11
-#include "TextureManager.h"
12 9
 #include "World.h"
13
-#include "utils/pixel.h"
14 10
 #include "StaticMesh.h"
15 11
 
16
-TexturedTriangle::TexturedTriangle(int i[3], float s[6], int tex, unsigned short trans) {
17
-    index[0] = i[0];
18
-    index[1] = i[1];
19
-    index[2] = i[2];
20
-    st[0] = s[0];
21
-    st[1] = s[1];
22
-    st[2] = s[2];
23
-    st[3] = s[3];
24
-    st[4] = s[4];
25
-    st[5] = s[5];
26
-    texture = tex;
27
-    transparency = trans;
28
-}
29
-
30
-void TexturedTriangle::display(float* vertices, float* colors, float* normals) {
31
-    /*
32
-    assert(vertices != nullptr);
33
-
34
-    if ((Render::getMode() != RenderMode::Wireframe)
35
-        && (Render::getMode() != RenderMode::Solid)) {
36
-        getTextureManager().bindTextureId(texture);
37
-    }
38
-
39
-    glBegin(GL_TRIANGLES);
40
-
41
-    switch (Render::getMode()) {
42
-        case RenderMode::Solid:
43
-            //case RenderMode::VertexLight:
44
-            if (colors != nullptr) {
45
-                glColor3fv(colors + index[0]);
46
-                glTexCoord2fv(st);
47
-                glVertex3fv(vertices + (index[0] * 3));
48
-
49
-                glColor3fv(colors + index[1]);
50
-                glTexCoord2fv(st + 2);
51
-                glVertex3fv(vertices + (index[1] * 3));
52
-
53
-                glColor3fv(colors + index[2]);
54
-                glTexCoord2fv(st + 4);
55
-                glVertex3fv(vertices + (index[2] * 3));
56
-            } else if (normals != nullptr) {
57
-                glNormal3fv(normals + (index[0] * 3));
58
-                glTexCoord2fv(st);
59
-                glVertex3fv(vertices + (index[0] * 3));
60
-
61
-                glNormal3fv(normals + (index[1] * 3));
62
-                glTexCoord2fv(st + 2);
63
-                glVertex3fv(vertices + (index[1] * 3));
64
-
65
-                glNormal3fv(normals + (index[2] * 3));
66
-                glTexCoord2fv(st + 4);
67
-                glVertex3fv(vertices + (index[2] * 3));
68
-            } else {
69
-                glTexCoord2fv(st);
70
-                glVertex3fv(vertices + (index[0] * 3));
71
-                glTexCoord2fv(st + 2);
72
-                glVertex3fv(vertices + (index[1] * 3));
73
-                glTexCoord2fv(st + 4);
74
-                glVertex3fv(vertices + (index[2] * 3));
75
-            }
76
-            break;
77
-
78
-        case RenderMode::Wireframe:
79
-            glVertex3fv(vertices + (index[0] * 3));
80
-            glVertex3fv(vertices + (index[1] * 3));
81
-            glVertex3fv(vertices + (index[2] * 3));
82
-            break;
83
-
84
-        default:
85
-            glTexCoord2fv(st);
86
-            glVertex3fv(vertices + (index[0] * 3));
87
-            glTexCoord2fv(st + 2);
88
-            glVertex3fv(vertices + (index[1] * 3));
89
-            glTexCoord2fv(st + 4);
90
-            glVertex3fv(vertices + (index[2] * 3));
91
-    }
92
-
93
-    glEnd();
94
-    */
95
-}
96
-
97
-// ----------------------------------------------------------------------------
98
-
99
-StaticMesh::StaticMesh(int i, int m) : id(i), mesh(m) {
100
-    vertices = colors = normals = nullptr;
101
-}
102
-
103
-StaticMesh::~StaticMesh() {
104
-    while (!triangles.empty()) {
105
-        delete triangles.back();
106
-        triangles.pop_back();
107
-    }
108
-
109
-    delete [] vertices;
110
-    delete [] normals;
111
-    delete [] colors;
112
-}
113
-
114
-void StaticMesh::display() {
115
-    /*
116
-    if ((id != -1) && (mesh != -1)) {
117
-        getWorld().getMesh(mesh).drawSolid();
118
-        return;
119
-    }
120
-
121
-    if (!dontshow) {
122
-        //! \fixme Duh, vis tests need to be put back
123
-        //if (!isVisible(center, radius, bbox))
124
-        //   return;
125
-
126
-        //! \fixme 'AMBIENT' -- Mongoose 2002.01.08
127
-        glColor3ubv(WHITE);
128
-
129
-        if (Render::getMode() == RenderMode::Wireframe)
130
-            glColor3ubv(WHITE);
131
-
132
-        getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
133
-
134
-        for (unsigned int i = 0; i < triangles.size(); i++)
135
-            triangles.at(i)->display(vertices, colors, normals);
136
-    }
137
-    */
138
-}
139
-
140
-float StaticMesh::getRadius() {
141
-    assert((id == -1) && (mesh == -1));
142
-    return radius;
12
+void StaticMesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {
13
+    getWorld().getMesh(mesh).display(model, view, projection);
143 14
 }
144 15
 

+ 2
- 1
src/TextureManager.cpp Ver arquivo

@@ -141,7 +141,8 @@ int TextureManager::initialize() {
141 141
 
142 142
 int TextureManager::initializeSplash() {
143 143
     unsigned char* image = generateColorTexture(WHITE, 32, 32, 32);
144
-    int res = loadBufferSlot(image, 32, 32, ColorMode::RGBA, 32, TextureStorage::SYSTEM, TEXTURE_WHITE, false);
144
+    int res = loadBufferSlot(image, 32, 32, ColorMode::RGBA, 32, TextureStorage::SYSTEM, TEXTURE_WHITE,
145
+                             false);
145 146
     delete [] image;
146 147
     if (res < 0) {
147 148
         return -1;

+ 26
- 21
src/loader/LoaderTR2.cpp Ver arquivo

@@ -12,6 +12,7 @@
12 12
 #include "global.h"
13 13
 #include "Game.h"
14 14
 #include "Log.h"
15
+#include "Mesh.h"
15 16
 #include "Room.h"
16 17
 #include "SoundManager.h"
17 18
 #include "TextureManager.h"
@@ -354,6 +355,7 @@ void LoaderTR2::loadRooms() {
354 355
         }
355 356
 
356 357
         uint16_t numStaticMeshes = file.readU16();
358
+        std::vector<StaticModel*> staticModels;
357 359
         for (unsigned int s = 0; s < numStaticMeshes; s++) {
358 360
             // Absolute position in world coordinates
359 361
             int32_t x = file.read32();
@@ -363,6 +365,7 @@ void LoaderTR2::loadRooms() {
363 365
             // High two bits (0xC000) indicate steps of
364 366
             // 90 degrees (eg. (rotation >> 14) * 90)
365 367
             uint16_t rotation = file.readU16();
368
+            assert((rotation & 0x3FFF) == 0);
366 369
 
367 370
             // Constant lighting, 0xFFFF means use mesh lighting
368 371
             uint16_t intensity1 = file.readU16();
@@ -371,7 +374,9 @@ void LoaderTR2::loadRooms() {
371 374
             // Which StaticMesh item to draw
372 375
             uint16_t objectID = file.readU16();
373 376
 
374
-            // TODO store static meshes somewhere
377
+            staticModels.push_back(new StaticModel(glm::vec3(x, y, z),
378
+                                                   (rotation >> 14) * 90.0f,
379
+                                                   objectID));
375 380
         }
376 381
 
377 382
         int16_t alternateRoom = file.read16(); // TODO
@@ -386,6 +391,9 @@ void LoaderTR2::loadRooms() {
386 391
         RoomMesh* mesh = new RoomMesh(vertices, rectangles, triangles);
387 392
         Room* room = new Room(pos, boundingbox, mesh, roomFlags);
388 393
 
394
+        for (auto m : staticModels)
395
+            room->addModel(m);
396
+
389 397
         getWorld().addRoom(room);
390 398
 
391 399
         // Sanity check
@@ -501,8 +509,6 @@ void LoaderTR2::loadMeshes() {
501 509
             vertices.emplace_back(x, y, z);
502 510
         }
503 511
 
504
-        //Mesh* mesh = new Mesh();
505
-
506 512
         int16_t numNormals = mem.read16();
507 513
         if (numNormals > 0) {
508 514
             // External vertex lighting is used, with the lighting calculated
@@ -527,6 +533,7 @@ void LoaderTR2::loadMeshes() {
527 533
         }
528 534
 
529 535
         int16_t numTexturedRectangles = mem.read16();
536
+        std::vector<IndexedRectangle> texturedRectangles;
530 537
         for (int r = 0; r < numTexturedRectangles; r++) {
531 538
             uint16_t vertex1 = mem.readU16();
532 539
             uint16_t vertex2 = mem.readU16();
@@ -534,23 +541,22 @@ void LoaderTR2::loadMeshes() {
534 541
             uint16_t vertex4 = mem.readU16();
535 542
             uint16_t texture = mem.readU16();
536 543
 
537
-            //mesh->addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
538
-            //                           vertices.at(vertex3), vertices.at(vertex4),
539
-            //                           texture);
544
+            texturedRectangles.emplace_back(texture, vertex1, vertex2, vertex3, vertex4);
540 545
         }
541 546
 
542 547
         int16_t numTexturedTriangles = mem.read16();
548
+        std::vector<IndexedRectangle> texturedTriangles;
543 549
         for (int t = 0; t < numTexturedTriangles; t++) {
544 550
             uint16_t vertex1 = mem.readU16();
545 551
             uint16_t vertex2 = mem.readU16();
546 552
             uint16_t vertex3 = mem.readU16();
547 553
             uint16_t texture = mem.readU16();
548 554
 
549
-            //mesh->addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
550
-            //                          vertices.at(vertex3), texture);
555
+            texturedTriangles.emplace_back(texture, vertex1, vertex2, vertex3);
551 556
         }
552 557
 
553 558
         int16_t numColoredRectangles = mem.read16();
559
+        std::vector<IndexedColoredRectangle> coloredRectangles;
554 560
         for (int r = 0; r < numColoredRectangles; r++) {
555 561
             uint16_t vertex1 = mem.readU16();
556 562
             uint16_t vertex2 = mem.readU16();
@@ -559,17 +565,15 @@ void LoaderTR2::loadMeshes() {
559 565
             uint16_t texture = mem.readU16();
560 566
 
561 567
             int index = (texture & 0xFF00) >> 8;
562
-            float red = (palette.at(index) & 0xFF000000) >> 24,
563
-                  green = (palette.at(index) & 0x00FF0000) >> 16,
564
-                  blue = (palette.at(index) & 0x0000FF00) >> 8;
565
-
568
+            uint8_t red = (palette.at(index) & 0xFF000000) >> 24,
569
+                    green = (palette.at(index) & 0x00FF0000) >> 16,
570
+                    blue = (palette.at(index) & 0x0000FF00) >> 8;
566 571
 
567
-            //mesh->addColoredRectangle(vertices.at(vertex1), vertices.at(vertex2),
568
-            //                          vertices.at(vertex3), vertices.at(vertex4),
569
-            //                          red, green, blue);
572
+            coloredRectangles.emplace_back(red, green, blue, vertex1, vertex2, vertex3, vertex4);
570 573
         }
571 574
 
572 575
         int16_t numColoredTriangles = mem.read16();
576
+        std::vector<IndexedColoredRectangle> coloredTriangles;
573 577
         for (int t = 0; t < numColoredTriangles; t++) {
574 578
             uint16_t vertex1 = mem.readU16();
575 579
             uint16_t vertex2 = mem.readU16();
@@ -577,15 +581,16 @@ void LoaderTR2::loadMeshes() {
577 581
             uint16_t texture = mem.readU16();
578 582
 
579 583
             int index = (texture & 0xFF00) >> 8;
580
-            float red = (palette.at(index) & 0xFF000000) >> 24,
581
-                  green = (palette.at(index) & 0x00FF0000) >> 16,
582
-                  blue = (palette.at(index) & 0x0000FF00) >> 8;
584
+            uint8_t red = (palette.at(index) & 0xFF000000) >> 24,
585
+                    green = (palette.at(index) & 0x00FF0000) >> 16,
586
+                    blue = (palette.at(index) & 0x0000FF00) >> 8;
583 587
 
584
-            //mesh->addColoredTriangle(vertices.at(vertex1), vertices.at(vertex2),
585
-            //                         vertices.at(vertex3), red, green, blue);
588
+            coloredTriangles.emplace_back(red, green, blue, vertex1, vertex2, vertex3);
586 589
         }
587 590
 
588
-        //getWorld().addMesh(mesh);
591
+        Mesh* mesh = new Mesh(vertices, texturedRectangles, texturedTriangles,
592
+                              coloredRectangles, coloredTriangles);
593
+        getWorld().addMesh(mesh);
589 594
     }
590 595
 
591 596
     if (numMeshPointers > 0)

+ 1
- 1
src/system/FontSDL.cpp Ver arquivo

@@ -36,7 +36,7 @@ int FontSDL::initialize(std::string font) {
36 36
 
37 37
         // Reserve slot
38 38
         mFontTexture = getTextureManager().loadBufferSlot(nullptr, 256, 256,
39
-                TextureManager::ColorMode::RGBA, 32, TextureManager::TextureStorage::SYSTEM);
39
+                       TextureManager::ColorMode::RGBA, 32, TextureManager::TextureStorage::SYSTEM);
40 40
 
41 41
         mFontInit = true;
42 42
     }

+ 1
- 1
src/system/FontTRLE.cpp Ver arquivo

@@ -47,7 +47,7 @@ int FontTRLE::initialize(std::string font) {
47 47
     }
48 48
 
49 49
     mFontTexture = getTextureManager().loadBufferSlot(pixels, 256, 256,
50
-            TextureManager::ColorMode::BGRA, 32, TextureManager::TextureStorage::SYSTEM);
50
+                   TextureManager::ColorMode::BGRA, 32, TextureManager::TextureStorage::SYSTEM);
51 51
     delete [] pixels;
52 52
 
53 53
     // Try to load .lps file or use default glyph positions

+ 77
- 1
src/system/Window.cpp Ver arquivo

@@ -36,6 +36,7 @@ bool Window::getTextInput() {
36 36
 Shader Window::textShader;
37 37
 Shader Window::imguiShader;
38 38
 Shader Window::textureShader;
39
+Shader Window::colorShader;
39 40
 unsigned int Window::vertexArrayID = 0;
40 41
 
41 42
 int Window::initializeGL() {
@@ -45,7 +46,7 @@ int Window::initializeGL() {
45 46
     getLog() << "GLSL V.: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << Log::endl;
46 47
 
47 48
     glGenVertexArrays(1, &vertexArrayID);
48
-	glBindVertexArray(vertexArrayID);
49
+    glBindVertexArray(vertexArrayID);
49 50
 
50 51
     // Set background to black
51 52
     //glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f, BLACK[2] / 256.0f, BLACK[3] / 256.0f);
@@ -85,6 +86,12 @@ int Window::initializeGL() {
85 86
         return -10;
86 87
     textureShader.addBuffer(3);
87 88
 
89
+    if (colorShader.compile(colorShaderVertex, colorShaderFragment) < 0)
90
+        return -11;
91
+    if (colorShader.addUniform("MVP") < 0)
92
+        return -12;
93
+    colorShader.addBuffer(3);
94
+
88 95
     glEnable(GL_BLEND);
89 96
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
90 97
 
@@ -172,6 +179,40 @@ void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uv
172 179
     glDisableVertexAttribArray(1);
173 180
 }
174 181
 
182
+void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
183
+                    std::vector<unsigned short>& indices, glm::mat4 MVP) {
184
+    assert(vertices.size() == colors.size());
185
+    assert((indices.size() % 3) == 0);
186
+
187
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
188
+    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
189
+
190
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
191
+    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
192
+
193
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(2));
194
+    glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
195
+
196
+    colorShader.use();
197
+
198
+    glUniformMatrix4fv(colorShader.getUniform(0), 1, GL_FALSE, &MVP[0][0]);
199
+
200
+    glEnableVertexAttribArray(0);
201
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
202
+    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
203
+
204
+    glEnableVertexAttribArray(1);
205
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
206
+    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
207
+
208
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorShader.getBuffer(2));
209
+
210
+    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, nullptr);
211
+
212
+    glDisableVertexAttribArray(0);
213
+    glDisableVertexAttribArray(1);
214
+}
215
+
175 216
 // ----------------------------------------------------------------------------
176 217
 
177 218
 Shader::~Shader() {
@@ -401,5 +442,40 @@ void main() {
401 442
 }
402 443
 )!?!";
403 444
 
445
+// --------------------------------------
446
+
447
+const char* Window::colorShaderVertex = R"!?!(
448
+#version 330 core
449
+
450
+layout(location = 0) in vec3 vertexPosition_modelspace;
451
+layout(location = 1) in vec3 vertexColor;
452
+
453
+out vec3 Color;
454
+
455
+uniform mat4 MVP;
456
+
457
+void main() {
458
+    vec4 pos = MVP * vec4(vertexPosition_modelspace.x,
459
+                          -vertexPosition_modelspace.y,
460
+                          vertexPosition_modelspace.z,
461
+                          1);
462
+    gl_Position = vec4(-pos.x, pos.yzw);
463
+    Color = vertexColor;
464
+}
465
+)!?!";
466
+
467
+const char* Window::colorShaderFragment = R"!?!(
468
+#version 330 core
469
+
470
+in vec3 Color;
471
+
472
+out vec4 color;
473
+
474
+void main() {
475
+    color = vec4(Color, 1);
476
+}
477
+)!?!";
478
+
479
+// --------------------------------------
404 480
 // *INDENT-ON*
405 481
 

+ 1
- 1
src/utils/png.cpp Ver arquivo

@@ -207,7 +207,7 @@ int pngSave(const char* filename, unsigned char* image,
207 207
 
208 208
     int color_type;
209 209
     if (((mode == TextureManager::ColorMode::RGB)
210
-                || (mode == TextureManager::ColorMode::BGR)) && (bpp == 24)) {
210
+         || (mode == TextureManager::ColorMode::BGR)) && (bpp == 24)) {
211 211
         if (mode == TextureManager::ColorMode::BGR) {
212 212
             bgr2rgb24(image, width, height);
213 213
         }

Carregando…
Cancelar
Salvar