瀏覽代碼

Camera movement, first Mesh drawing

Thomas Buck 10 年之前
父節點
當前提交
5a95f1c49f
共有 21 個文件被更改,包括 445 次插入1137 次删除
  1. 4
    0
      ChangeLog.md
  2. 4
    0
      include/Camera.h
  3. 1
    0
      include/Game.h
  4. 16
    170
      include/Mesh.h
  5. 26
    24
      include/Room.h
  6. 2
    4
      include/RoomData.h
  7. 10
    10
      include/TextureManager.h
  8. 28
    0
      include/loader/LoaderTR2.h
  9. 9
    0
      include/system/Window.h
  10. 42
    0
      src/Camera.cpp
  11. 49
    41
      src/Game.cpp
  12. 77
    626
      src/Mesh.cpp
  13. 9
    2
      src/Render.cpp
  14. 14
    71
      src/Room.cpp
  15. 4
    25
      src/RoomData.cpp
  16. 2
    0
      src/SkeletalModel.cpp
  17. 2
    0
      src/SoundManager.cpp
  18. 4
    28
      src/TextureManager.cpp
  19. 64
    123
      src/loader/LoaderTR2.cpp
  20. 0
    13
      src/main.cpp
  21. 78
    0
      src/system/Window.cpp

+ 4
- 0
ChangeLog.md 查看文件

@@ -2,6 +2,10 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141216 ]
6
+    * Allow navigation with a free-floating Camera
7
+    * Started drawing the Room Mesh structures
8
+
5 9
     [ 20141215 ]
6 10
     * Rewrote GL code for the Font implementations, ImGUI and the Main Menu
7 11
 

+ 4
- 0
include/Camera.h 查看文件

@@ -13,6 +13,10 @@
13 13
 
14 14
 class Camera {
15 15
   public:
16
+
17
+    static void reset();
18
+
19
+    static void handleAction(ActionEvents action, bool isFinished);
16 20
     static void handleMouseMotion(int x, int y);
17 21
     static glm::mat4 getViewMatrix();
18 22
 

+ 1
- 0
include/Game.h 查看文件

@@ -53,3 +53,4 @@ class Game {
53 53
 Game& getGame();
54 54
 
55 55
 #endif
56
+

+ 16
- 170
include/Mesh.h 查看文件

@@ -10,183 +10,29 @@
10 10
 #ifndef _MESH_H_
11 11
 #define _MESH_H_
12 12
 
13
+#include <vector>
14
+#include <glm/mat4x4.hpp>
15
+#include <glm/vec2.hpp>
13 16
 #include <glm/vec3.hpp>
14 17
 
15
-/*!
16
- * \brief OpenGL Mesh
17
- */
18
+#include "loader/LoaderTR2.h"
19
+
18 20
 class Mesh {
19 21
   public:
22
+    Mesh(const std::vector<RoomVertexTR2>& vertices,
23
+         const std::vector<RoomRectangleTR2>& rectangles,
24
+         const std::vector<RoomTriangleTR2>& triangles);
20 25
 
21
-    struct rectangle_t {
22
-        glm::vec3 a, b, c, d;
23
-        uint16_t texture;
24
-        float red, green, blue;
25
-
26
-        rectangle_t(glm::vec3 _a, glm::vec3 _b, glm::vec3 _c, glm::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) { }
29
-    };
30
-
31
-    Mesh();
32
-    ~Mesh();
33
-
34
-    void drawAlpha();
35
-    void drawSolid();
36
-
37
-    void addTexturedRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d, uint16_t textile);
38
-    void addTexturedTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, uint16_t textile);
39
-
40
-    void addColoredRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d, float re, float gr,
41
-                             float bl);
42
-    void addColoredTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, float re, float gr, float bl);
43
-
44
-    void addNormal(glm::vec3 n);
45
-
46
-    std::vector<rectangle_t> texturedRectangles;
47
-    std::vector<rectangle_t> coloredRectangles;
48
-    std::vector<rectangle_t> texturedTriangles;
49
-    std::vector<rectangle_t> coloredTriangles;
50
-    std::vector<glm::vec3> normals;
51
-
52
-
53
-    // Old API
54
-
55
-    typedef enum {
56
-        MeshModeSolid,
57
-        MeshModeWireframe,
58
-        MeshModeTexture,
59
-        MeshModeMultiTexture
60
-    } MeshMode;
61
-
62
-    typedef enum {
63
-        fMesh_UseVertexArray = (1 << 0)
64
-    } MeshFlags;
65
-
66
-    typedef struct {
67
-        int texture;
68
-#ifdef MULTITEXTURE
69
-        int bumpmap;
70
-#endif
71
-
72
-        unsigned int cnum_triangles;
73
-        unsigned int cnum_alpha_triangles;
74
-
75
-        unsigned int num_texcoors;
76
-        float** texcoors; // 2D
77
-
78
-        unsigned int num_texcoors2;
79
-        float** texcoors2; // 2D
80
-
81
-        //! Arrays of triangle indices sorted by texture
82
-        unsigned int num_triangles;
83
-        unsigned int* triangles; //!< ABCABCABC...
84
-
85
-        //! Arrays of alpha triangle indices sorted by texture
86
-        unsigned int num_alpha_triangles;
87
-        unsigned int* alpha_triangles; //!< ABCABCABC...
88
-    } tris_t;
89
-
90
-    typedef struct {
91
-
92
-        int texture;
93
-#ifdef MULTITEXTURE
94
-        int bumpmap;
95
-#endif
96
-
97
-        unsigned int cnum_quads;
98
-        unsigned int cnum_alpha_quads;
99
-
100
-        unsigned int num_texcoors;
101
-        float** texcoors; // 2D
102
-
103
-        unsigned int num_texcoors2;
104
-        float** texcoors2; // 2D
105
-
106
-        //! Arrays of rectangle indices sorted by texture
107
-        unsigned int num_quads;
108
-        unsigned int* quads; //!< ABCDABCDABCD...
109
-
110
-        //! Arrays of alpha rectangle indices sorted by texture
111
-        unsigned int num_alpha_quads;
112
-        unsigned int* alpha_quads; //!< ABCDABCDABCD...
26
+    void prepare();
113 27
 
114
-    } rect_t;
28
+    void display(glm::mat4 model, glm::mat4 view, glm::mat4 projection);
115 29
 
116
-    void drawAlphaOld();
117
-    void drawSolidOld();
118
-
119
-    void allocateColors(unsigned int n);
120
-
121
-    void allocateNormals(unsigned int n);
122
-
123
-    void allocateRectangles(unsigned int n);
124
-
125
-    void allocateTriangles(unsigned int n);
126
-
127
-    void allocateVertices(unsigned int n);
128
-
129
-    void bufferColorArray(unsigned int colorCount, float* colors);
130
-
131
-    void bufferNormalArray(unsigned int normalCount, float* normals);
132
-
133
-    void bufferTriangles(unsigned int count,
134
-                         unsigned int* indices, float* texCoords,
135
-                         int* textures, unsigned int* flags);
136
-
137
-    void bufferVertexArray(unsigned int vertexCount, float* vertices);
138
-
139
-    void setColor(unsigned int index, float r, float g, float b, float a);
140
-
141
-    void setColor(unsigned int index, float rgba[4]);
142
-
143
-    void setNormal(unsigned int index, float i, float j, float k);
144
-
145
-    void setVertex(unsigned int index, float x, float y, float z);
146
-
147
-#if 0
148
-    void sortFacesByTexture();
149
-
150
-    void addFace(int textureIndex, int textureIndexB, unsigned int flags,
151
-                 unsigned int vertexIndexCount, float* vertexIndices);
152
-
153
-    void addTexTiledFace(int textureIndex, int textureIndexB,
154
-                         unsigned int flags, unsigned int indexCount,
155
-                         float* vertexIndices, float* texcoords);
156
-
157
-    void bufferTexcoords(unsigned int texcoordCount, float* texcoords);
158
-
159
-    void duplicateArraysForTexTiledTexcoords();
160
-#endif
161
-
162
-    unsigned int mFlags;
163
-
164
-    MeshMode mMode;
165
-
166
-    unsigned int mNumVertices;
167
-    float** mVertices; //!< XYZ
168
-
169
-    unsigned int mNumNormals;
170
-    float** mNormals; //!< IJK
171
-
172
-    unsigned int mNumColors;
173
-    float** mColors; //!< RGBA
174
-
175
-    unsigned int mNumTris;
176
-    tris_t* mTris;
177
-
178
-    unsigned int mNumQuads;
179
-    rect_t* mQuads;
180
-
181
-    unsigned int mTriangleCount;
182
-    int* mTriangleTextures;
183
-    unsigned int* mTriangleIndices;
184
-    unsigned int* mTriangleFlags;
185
-    float* mTriangleTexCoordArray;
186
-
187
-    float* mVertexArray;
188
-    float* mNormalArray;
189
-    float* mColorArray;
30
+  private:
31
+    std::vector<unsigned short> indices;
32
+    std::vector<glm::vec3> vertices;
33
+    std::vector<glm::vec2> uvs;
34
+    std::vector<unsigned int> textures;
190 35
 };
191 36
 
192 37
 #endif
38
+

+ 26
- 24
include/Room.h 查看文件

@@ -8,22 +8,26 @@
8 8
 #ifndef _ROOM_H_
9 9
 #define _ROOM_H_
10 10
 
11
+#include <memory>
11 12
 #include <vector>
13
+#include <glm/mat4x4.hpp>
14
+#include <glm/vec3.hpp>
15
+
12 16
 #include "Mesh.h"
13 17
 #include "Sprite.h"
14 18
 #include "RoomData.h"
15 19
 
16
-typedef enum {
20
+enum RoomFlags {
17 21
     RoomFlagUnderWater = (1 << 0)
18
-} RoomFlags;
22
+};
19 23
 
20 24
 class Room {
21 25
   public:
22
-    Room(float p[3] = nullptr, unsigned int f = 0, unsigned int x = 0, unsigned int z = 0);
23
-    ~Room();
26
+    Room(glm::vec3 _pos, BoundingBox* _bbox, Mesh* _mesh, unsigned int f)
27
+        : pos(_pos), bbox(_bbox), mesh(_mesh), flags(f) { }
24 28
 
25
-    BoundingBox& getBoundingBox();
26
-    void display(bool alpha);
29
+    void prepare();
30
+    void display(glm::mat4 view, glm::mat4 projection);
27 31
 
28 32
     bool isWall(unsigned long sector);
29 33
     long getSector(float x, float z, float* floor, float* ceiling);
@@ -32,18 +36,16 @@ class Room {
32 36
     int getAdjoiningRoom(float x, float y, float z,
33 37
                          float x2, float y2, float z2);
34 38
 
35
-    Mesh& getMesh() { return mesh; }
39
+    BoundingBox& getBoundingBox() { return *bbox; }
40
+    Mesh& getMesh() { return *mesh; }
36 41
 
37
-    unsigned int getNumXSectors();
38
-    unsigned int getNumZSectors();
39
-    void getPos(float p[3]);
42
+    void setNumXSectors(unsigned int n) { numXSectors = n; }
43
+    unsigned int getNumXSectors() { return numXSectors; }
40 44
 
41
-    void setNumXSectors(unsigned int n);
42
-    void setNumZSectors(unsigned int n);
43
-    void setPos(float p[3]);
45
+    void setNumZSectors(unsigned int n) { numZSectors = n; }
46
+    unsigned int getNumZSectors() { return numZSectors; }
44 47
 
45
-    void setFlags(unsigned int f);
46
-    unsigned int getFlags();
48
+    unsigned int getFlags() { return flags; }
47 49
 
48 50
     unsigned long sizeAdjacentRooms();
49 51
     long getAdjacentRoom(unsigned long index);
@@ -70,20 +72,20 @@ class Room {
70 72
     void addSprite(Sprite* s);
71 73
 
72 74
   private:
75
+    glm::vec3 pos;
76
+    std::unique_ptr<BoundingBox> bbox;
77
+    std::unique_ptr<Mesh> mesh;
78
+
73 79
     unsigned int flags;
74 80
     unsigned int numXSectors;
75 81
     unsigned int numZSectors;
76
-    float pos[3];
77
-
78
-    BoundingBox bbox;
79
-    Mesh mesh;
80 82
 
81 83
     std::vector<long> adjacentRooms;
82
-    std::vector<Sprite*> sprites;
83
-    std::vector<StaticModel*> models;
84
-    std::vector<Portal*> portals;
85
-    std::vector<Sector*> sectors;
86
-    std::vector<Light*> lights;
84
+    std::vector<std::unique_ptr<Sprite>> sprites;
85
+    std::vector<std::unique_ptr<StaticModel>> models;
86
+    std::vector<std::unique_ptr<Portal>> portals;
87
+    std::vector<std::unique_ptr<Sector>> sectors;
88
+    std::vector<std::unique_ptr<Light>> lights;
87 89
 
88 90
     // Was used for "depth sorting" render list, but never assigned...?!
89 91
     //float dist; // Distance to near plane, move to room?

+ 2
- 4
include/RoomData.h 查看文件

@@ -14,15 +14,13 @@
14 14
 
15 15
 class BoundingBox {
16 16
   public:
17
-    BoundingBox();
18
-    void getBoundingBox(float box[2][3]);
19
-    void setBoundingBox(float min[3], float max[3]);
17
+    BoundingBox(glm::vec3 min, glm::vec3 max);
20 18
     void display(bool points, const unsigned char c1[4], const unsigned char c2[4]);
21 19
     bool inBox(float x, float y, float z);
22 20
     bool inBoxPlane(float x, float z);
23 21
 
24 22
   private:
25
-    float a[3], b[3];
23
+    glm::vec3 a, b;
26 24
 };
27 25
 
28 26
 class Light {

+ 10
- 10
include/TextureManager.h 查看文件

@@ -11,6 +11,7 @@
11 11
 
12 12
 #include <cstdint>
13 13
 #include <vector>
14
+#include <glm/vec2.hpp>
14 15
 
15 16
 // These are loaded into TextureStorage::SYSTEM by initialize()!
16 17
 #define TEXTURE_WHITE 0
@@ -18,7 +19,8 @@
18 19
 
19 20
 class TextureTileVertex {
20 21
   public:
21
-    TextureTileVertex(uint8_t xc, uint8_t xp, uint8_t yc, uint8_t yp);
22
+    TextureTileVertex(uint8_t xc, uint8_t xp, uint8_t yc, uint8_t yp)
23
+        : xCoordinate(xc), xPixel(xp), yCoordinate(yc), yPixel(yp) { }
22 24
 
23 25
     uint8_t xCoordinate, xPixel;
24 26
     uint8_t yCoordinate, yPixel;
@@ -26,18 +28,16 @@ class TextureTileVertex {
26 28
 
27 29
 class TextureTile {
28 30
   public:
29
-    TextureTile(uint16_t a, uint16_t t) : attribute(a), texture(t) { }
30
-    ~TextureTile();
31
+    TextureTile(unsigned int a, unsigned int t) : attribute(a), texture(t) { }
31 32
 
32
-    void add(TextureTileVertex* t);
33
-
34
-    void displayTriangle(float a[3], float b[3], float c[3]);
35
-    void displayRectangle(float a[3], float b[3], float c[3], float d[3]);
33
+    unsigned int getTexture() { return texture; }
34
+    glm::vec2 getUV(unsigned int i) { return glm::vec2(vertices.at(i).xPixel, vertices.at(i).yPixel); }
35
+    void add(TextureTileVertex t) { vertices.push_back(t); }
36 36
 
37 37
   private:
38
-    uint16_t attribute;
39
-    uint16_t texture;
40
-    std::vector<TextureTileVertex*> vertices;
38
+    unsigned int attribute;
39
+    unsigned int texture;
40
+    std::vector<TextureTileVertex> vertices;
41 41
 };
42 42
 
43 43
 /*!

+ 28
- 0
include/loader/LoaderTR2.h 查看文件

@@ -45,5 +45,33 @@ class LoaderTR2 : public Loader {
45 45
     std::array<uint32_t, 256> palette;
46 46
 };
47 47
 
48
+struct RoomVertexTR2 {
49
+    int16_t x, y, z; // Vertex coordinates, relative to x/zOffset
50
+    int16_t light1, light2; // Almost always equal
51
+
52
+    // Set of flags for special rendering effects
53
+    // 0x8000 - Something to do with water surface?
54
+    // 0x4000 - Underwater lighting modulation/movement if seen from above
55
+    // 0x2000 - Water/Quicksand surface movement
56
+    // 0x0010 - Normal?
57
+    uint16_t attributes;
58
+};
59
+
60
+struct RoomRectangleTR2 {
61
+    uint16_t v1, v2, v3, v4; // Vertex list indices
62
+    uint16_t texture; // Index into object-texture list
63
+
64
+    RoomRectangleTR2(uint16_t _v1, uint16_t _v2, uint16_t _v3, uint16_t _v4, uint16_t t)
65
+        : v1(_v1), v2(_v2), v3(_v3), v4(_v4), texture(t) { }
66
+};
67
+
68
+struct RoomTriangleTR2 {
69
+    uint16_t v1, v2, v3; // Vertex list indices
70
+    uint16_t texture; // Index into object-texture list
71
+
72
+    RoomTriangleTR2(uint16_t _v1, uint16_t _v2, uint16_t _v3, uint16_t t)
73
+        : v1(_v1), v2(_v2), v3(_v3), texture(t) { }
74
+};
75
+
48 76
 #endif
49 77
 

+ 9
- 0
include/system/Window.h 查看文件

@@ -9,7 +9,9 @@
9 9
 #define _WINDOW_H_
10 10
 
11 11
 #include <vector>
12
+#include <glm/mat4x4.hpp>
12 13
 #include <glm/vec2.hpp>
14
+#include <glm/vec3.hpp>
13 15
 #include <glm/vec4.hpp>
14 16
 
15 17
 class Shader {
@@ -63,6 +65,9 @@ class Window {
63 65
     static void drawTextGL(std::vector<glm::vec2>& vertices, std::vector<glm::vec2>& uvs,
64 66
                            glm::vec4 color, unsigned int texture);
65 67
 
68
+    static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
69
+                       std::vector<unsigned short>& indices, glm::mat4 MVP, unsigned int texture);
70
+
66 71
   protected:
67 72
     bool mInit;
68 73
     bool mFullscreen;
@@ -80,6 +85,10 @@ class Window {
80 85
     static const char* imguiShaderVertex;
81 86
     static const char* imguiShaderFragment;
82 87
 
88
+    static Shader textureShader;
89
+    static const char* textureShaderVertex;
90
+    static const char* textureShaderFragment;
91
+
83 92
     static unsigned int vertexArrayID;
84 93
 
85 94
     friend class UI;

+ 42
- 0
src/Camera.cpp 查看文件

@@ -17,6 +17,48 @@ float Camera::thetaY = 0.0f;
17 17
 float Camera::rotationDeltaX = 0.75f;
18 18
 float Camera::rotationDeltaY = 0.75f;
19 19
 
20
+void Camera::reset() {
21
+    pos = glm::vec3(0.0f, 0.0f, 0.0f);
22
+    thetaX = glm::pi<float>();
23
+    thetaY = 0.0f;
24
+}
25
+
26
+void Camera::handleAction(ActionEvents action, bool isFinished) {
27
+    if (isFinished)
28
+        return;
29
+
30
+    const static float step = 256.0f;
31
+
32
+    glm::vec3 dir(
33
+        glm::cos(thetaY) * glm::sin(thetaX),
34
+        glm::sin(thetaY),
35
+        glm::cos(thetaY) * glm::cos(thetaX)
36
+    );
37
+    glm::vec3 right(
38
+        glm::sin(thetaX - glm::pi<float>() / 2.0f),
39
+        0.0f,
40
+        glm::cos(thetaX - glm::pi<float>() / 2.0f)
41
+    );
42
+    glm::vec3 up = glm::cross(right, dir);
43
+
44
+    if (action == forwardAction) {
45
+        pos += dir * step;
46
+    } else if (action == backwardAction) {
47
+        pos -= dir * step;
48
+    } else if (action == leftAction) {
49
+        pos -= right * step;
50
+    } else if (action == rightAction) {
51
+        pos += right * step;
52
+    } else if (action == jumpAction) {
53
+        pos += up * step;
54
+    } else if (action == crouchAction) {
55
+        pos -= up * step;
56
+    } else if (action == useAction) {
57
+    } else if (action == holsterAction) {
58
+    } else if (action == walkAction) {
59
+    }
60
+}
61
+
20 62
 void Camera::handleMouseMotion(int x, int y) {
21 63
     while (x > 0) {
22 64
         if (thetaX < (glm::pi<float>() / 2.0f)) {

+ 49
- 41
src/Game.cpp 查看文件

@@ -7,6 +7,7 @@
7 7
 
8 8
 #include <algorithm>
9 9
 #include <map>
10
+#include <sstream>
10 11
 #include <cstdlib>
11 12
 #include <cstring>
12 13
 
@@ -16,9 +17,12 @@
16 17
 #include "loader/Loader.h"
17 18
 #include "Log.h"
18 19
 #include "Render.h"
20
+#include "RunTime.h"
19 21
 #include "SoundManager.h"
20 22
 #include "StaticMesh.h"
23
+#include "system/Font.h"
21 24
 #include "system/Sound.h"
25
+#include "system/Window.h"
22 26
 #include "TextureManager.h"
23 27
 #include "UI.h"
24 28
 #include "World.h"
@@ -41,6 +45,24 @@ int Game::initialize() {
41 45
 
42 46
 void Game::display() {
43 47
     Render::display();
48
+
49
+    if (getRunTime().getShowFPS()) {
50
+        std::ostringstream s;
51
+        s << getRunTime().getFPS() << "FPS";
52
+        Font::drawText(10, getWindow().getHeight() - 25, 0.6f, BLUE, s.str());
53
+
54
+        s.str("");
55
+        s << "X: " << Camera::getPosition().x << " (" << Camera::getRadianPitch() << ")";
56
+        Font::drawText(10, getWindow().getHeight() - 70, 0.6f, BLUE, s.str());
57
+
58
+        s.str("");
59
+        s << "Y: " << Camera::getPosition().y << " (" << Camera::getRadianYaw() << ")";
60
+        Font::drawText(10, getWindow().getHeight() - 55, 0.6f, BLUE, s.str());
61
+
62
+        s.str("");
63
+        s << "Z: " << Camera::getPosition().z;
64
+        Font::drawText(10, getWindow().getHeight() - 40, 0.6f, BLUE, s.str());
65
+    }
44 66
 }
45 67
 
46 68
 void Game::destroy() {
@@ -48,10 +70,10 @@ void Game::destroy() {
48 70
     mLara = -1;
49 71
     Render::setMode(RenderMode::LoadScreen);
50 72
 
51
-    getWorld().destroy();
52
-    Sound::clear(); // Remove all previously loaded sounds
73
+    Camera::reset();
53 74
     SoundManager::clear();
54 75
     getTextureManager().clear();
76
+    getWorld().destroy();
55 77
 }
56 78
 
57 79
 bool Game::isLoaded() {
@@ -64,65 +86,51 @@ int Game::loadLevel(const char* level) {
64 86
     getLog() << "Loading " << levelName << Log::endl;
65 87
     auto loader = Loader::createLoader(level);
66 88
     if (loader) {
67
-        // First Loader test
68
-        getLog() << "Trying to load using new loader..." << Log::endl;
69 89
         int error = loader->load(level);
70 90
         if (error != 0) {
71
-            getLog() << "Error while trying new loader (" << error << ")..." << Log::endl;
91
+            getLog() << "Error loading level (" << error << ")..." << Log::endl;
72 92
             destroy();
73 93
             return -2;
74
-        } else {
75
-            SoundManager::prepareSources();
94
+        }
95
+
96
+        for (int i = 0; i < getWorld().sizeRoom(); i++) {
97
+            getWorld().getRoom(i).prepare();
98
+        }
76 99
 
77
-            if (mLara == -1) {
78
-                getLog() << "Can't find Lara entity in level?!" << Log::endl;
79
-            } else {
80
-                mLoaded = true;
81
-                //Render::setMode(RenderMode::Texture);
82
-            }
100
+        SoundManager::prepareSources();
83 101
 
102
+        if (mLara == -1) {
103
+            getLog() << "Can't find Lara entity in level?!" << Log::endl;
84 104
             UI::setVisible(true);
85
-            return 0;
105
+        } else {
106
+            mLoaded = true;
107
+            Render::setMode(RenderMode::Texture);
86 108
         }
87 109
     } else {
88 110
         getLog() << "No suitable loader for this level!" << Log::endl;
89 111
         return -1;
90 112
     }
113
+
114
+    return 0;
91 115
 }
92 116
 
93 117
 void Game::handleAction(ActionEvents action, bool isFinished) {
94
-    if (mLoaded && (!isFinished)) {
95
-        if (action == forwardAction) {
96
-            getLara().move('f');
97
-        } else if (action == backwardAction) {
98
-            getLara().move('b');
99
-        } else if (action == leftAction) {
100
-            getLara().move('l');
101
-        } else if (action == rightAction) {
102
-            getLara().move('r');
103
-        } else if (action == jumpAction) {
104
-
105
-        } else if (action == crouchAction) {
106
-
107
-        } else if (action == useAction) {
108
-
109
-        } else if (action == holsterAction) {
118
+    if (isFinished || (!mLoaded))
119
+        return;
110 120
 
111
-        } else if (action == walkAction) {
112
-
113
-        }
114
-    }
121
+    Camera::handleAction(action, isFinished);
115 122
 }
116 123
 
117 124
 void Game::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) {
118
-    if (mLoaded) {
119
-        Camera::handleMouseMotion(xrel, yrel);
125
+    if (!mLoaded)
126
+        return;
120 127
 
121
-        /*
122
-        float angles[3] = { 0.0f, getCamera().getRadianYaw(), getCamera().getRadianPitch() };
123
-        getLara().setAngles(angles);
124
-        */
125
-    }
128
+    Camera::handleMouseMotion(xrel, yrel);
129
+
130
+    /* TODO
131
+    float angles[3] = { 0.0f, getCamera().getRadianYaw(), getCamera().getRadianPitch() };
132
+    getLara().setAngles(angles);
133
+    */
126 134
 }
127 135
 
128 136
 Entity& Game::getLara() {

+ 77
- 626
src/Mesh.cpp 查看文件

@@ -5,648 +5,99 @@
5 5
  * \author Mongoose
6 6
  */
7 7
 
8
+#include <map>
8 9
 #include <stdlib.h>
9 10
 
10 11
 #include "global.h"
11 12
 #include "TextureManager.h"
13
+#include "system/Window.h"
12 14
 #include "Mesh.h"
13 15
 
14
-void Mesh::addTexturedRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d,
15
-                                uint16_t textile) {
16
-    texturedRectangles.emplace_back(a, b, c, d, textile);
17
-}
18
-
19
-void Mesh::addTexturedTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, uint16_t textile) {
20
-    texturedTriangles.emplace_back(a, b, c, glm::vec3(), textile);
21
-}
22
-
23
-void Mesh::addColoredRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d, float re,
24
-                               float gr, float bl) {
25
-    coloredRectangles.emplace_back(a, b, c, d, -1, re, gr, bl);
26
-}
27
-
28
-void Mesh::addColoredTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, float re, float gr, float bl) {
29
-    coloredTriangles.emplace_back(a, b, c, glm::vec3(), -1, re, gr, bl);
30
-}
31
-
32
-void Mesh::addNormal(glm::vec3 n) {
33
-    normals.emplace_back(n);
34
-}
35
-
36
-void Mesh::drawAlpha() {
37
-    if ((texturedRectangles.size() == 0)
38
-        && (texturedTriangles.size() == 0)
39
-        && (coloredRectangles.size() == 0)
40
-        && (coloredTriangles.size() == 0)
41
-        && (normals.size() == 0)) {
42
-        drawAlphaOld();
43
-        return;
44
-    }
45
-
46
-    // TODO
47
-}
48
-
49
-void Mesh::drawSolid() {
50
-    if ((texturedRectangles.size() == 0)
51
-        && (texturedTriangles.size() == 0)
52
-        && (coloredRectangles.size() == 0)
53
-        && (coloredTriangles.size() == 0)
54
-        && (normals.size() == 0)) {
55
-        drawSolidOld();
56
-        return;
57
-    }
58
-
59
-    for (auto& q : texturedRectangles) {
60
-        if (mMode == MeshModeWireframe) {
61
-            getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
62
-            /*
63
-            glBegin(GL_QUADS);
64
-            glTexCoord2f(0.0f, 0.0f);
65
-            glVertex3f(q.a.x, q.a.y, q.a.z);
66
-            glTexCoord2f(1.0f, 0.0f);
67
-            glVertex3f(q.b.x, q.b.y, q.b.z);
68
-            glTexCoord2f(1.0f, 1.0f);
69
-            glVertex3f(q.c.x, q.c.y, q.c.z);
70
-            glTexCoord2f(0.0f, 1.0f);
71
-            glVertex3f(q.d.x, q.d.y, q.d.z);
72
-            glEnd();
73
-            */
74
-        } else if (mMode == MeshModeSolid) {
75
-            // TODO
76
-        } else if (mMode == MeshModeTexture) {
77
-            //getTextureManager().getTile(q.texture).displayRectangle(q.a, q.b, q.c, q.d);
78
-        }
79
-    }
80
-
81
-    for (auto& t : texturedTriangles) {
82
-        if (mMode == MeshModeWireframe) {
83
-            getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
84
-            /*
85
-            glBegin(GL_TRIANGLES);
86
-            glTexCoord2f(0.0f, 0.0f);
87
-            glVertex3f(t.a.x, t.a.y, t.a.z);
88
-            glTexCoord2f(1.0f, 0.0f);
89
-            glVertex3f(t.b.x, t.b.y, t.b.z);
90
-            glTexCoord2f(1.0f, 1.0f);
91
-            glVertex3f(t.c.x, t.c.y, t.c.z);
92
-            glEnd();
93
-            */
94
-        } else if (mMode == MeshModeSolid) {
95
-            // TODO
96
-        } else if (mMode == MeshModeTexture) {
97
-            //getTextureManager().getTile(t.texture).displayTriangle(t.a, t.b, t.c);
98
-        }
99
-    }
100
-
101
-    for (auto& q : coloredRectangles) {
102
-        if (mMode == MeshModeWireframe) {
103
-            getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
104
-            /*
105
-            glBegin(GL_QUADS);
106
-            glTexCoord2f(0.0f, 0.0f);
107
-            glVertex3f(q.a.x, q.a.y, q.a.z);
108
-            glTexCoord2f(1.0f, 0.0f);
109
-            glVertex3f(q.b.x, q.b.y, q.b.z);
110
-            glTexCoord2f(1.0f, 1.0f);
111
-            glVertex3f(q.c.x, q.c.y, q.c.z);
112
-            glTexCoord2f(0.0f, 1.0f);
113
-            glVertex3f(q.d.x, q.d.y, q.d.z);
114
-            glEnd();
115
-            */
116
-        } else if (mMode == MeshModeSolid) {
117
-            // TODO
118
-        } else if (mMode == MeshModeTexture) {
119
-            // TODO
120
-        }
121
-    }
122
-
123
-    for (auto& t : coloredTriangles) {
124
-        if (mMode == MeshModeWireframe) {
125
-            getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
126
-            /*
127
-            glBegin(GL_TRIANGLES);
128
-            glTexCoord2f(0.0f, 0.0f);
129
-            glVertex3f(t.a.x, t.a.y, t.a.z);
130
-            glTexCoord2f(1.0f, 0.0f);
131
-            glVertex3f(t.b.x, t.b.y, t.b.z);
132
-            glTexCoord2f(1.0f, 1.0f);
133
-            glVertex3f(t.c.x, t.c.y, t.c.z);
134
-            glEnd();
135
-            */
136
-        } else if (mMode == MeshModeSolid) {
137
-            // TODO
138
-        } else if (mMode == MeshModeTexture) {
139
-            // TODO
140
-        }
141
-    }
142
-}
143
-
144
-
145
-////////////////////////////////////////////////////////////
146
-// Constructors
147
-////////////////////////////////////////////////////////////
148
-
149
-Mesh::Mesh() {
150
-    mNumVertices = 0;
151
-    mVertices = 0x0;
152
-
153
-    mNumNormals = 0;
154
-    mNormals = 0x0;
155
-
156
-    mNumColors = 0;
157
-    mColors = 0x0;
158
-
159
-    mNumTris = 0;
160
-    mTris = 0x0;
161
-
162
-    mNumQuads = 0;
163
-    mQuads = 0x0;
164
-
165
-    mVertexArray = 0x0;
166
-    mNormalArray = 0x0;
167
-    mColorArray = 0x0;
168
-
169
-    mTriangleCount = 0;
170
-    mTriangleTextures = 0x0;
171
-    mTriangleIndices = 0x0;
172
-    mTriangleFlags = 0x0;
173
-    mTriangleTexCoordArray = 0x0;
174
-
175
-    mFlags = 0;
176
-    mMode = MeshModeTexture;
177
-}
178
-
179
-
180
-Mesh::~Mesh() {
181
-    for (unsigned int i = 0; i < mNumVertices; i++)
182
-        delete [] mVertices[i];
183
-    delete [] mVertices;
184
-    mVertices = nullptr;
185
-
186
-    for (unsigned int i = 0; i < mNumNormals; i++)
187
-        delete [] mNormals[i];
188
-    delete [] mNormals;
189
-    mNormals = nullptr;
190
-
191
-    for (unsigned int i = 0; i < mNumColors; i++)
192
-        delete [] mColors[i];
193
-    delete [] mColors;
194
-    mColors = nullptr;
195
-
196
-    if (mTris) {
197
-        for (unsigned int i = 0; i < mNumTris; ++i) {
198
-            delete [] mTris[i].triangles;
199
-            delete [] mTris[i].alpha_triangles;
200
-            delete [] mTris[i].texcoors;
201
-            delete [] mTris[i].texcoors2;
202
-        }
203
-
204
-        delete [] mTris;
205
-        mTris = nullptr;
206
-    }
207
-
208
-    if (mQuads) {
209
-        for (unsigned int i = 0; i < mNumQuads; ++i) {
210
-            delete [] mQuads[i].quads;
211
-            delete [] mQuads[i].alpha_quads;
212
-            delete [] mQuads[i].texcoors;
213
-            delete [] mQuads[i].texcoors2;
214
-        }
215
-
216
-        delete [] mQuads;
217
-        mQuads = nullptr;
218
-    }
219
-
220
-    delete [] mVertexArray;
221
-    mVertexArray = nullptr;
222
-
223
-    delete [] mNormalArray;
224
-    mNormalArray = nullptr;
225
-
226
-    delete [] mColorArray;
227
-    mColorArray = nullptr;
228
-
229
-    delete [] mTriangleTextures;
230
-    mTriangleTextures = nullptr;
231
-
232
-    delete [] mTriangleIndices;
233
-    mTriangleIndices = nullptr;
234
-
235
-    delete [] mTriangleFlags;
236
-    mTriangleFlags = nullptr;
237
-
238
-    delete [] mTriangleTexCoordArray;
239
-    mTriangleTexCoordArray = nullptr;
240
-}
241
-
242
-
243
-////////////////////////////////////////////////////////////
244
-// Public Accessors
245
-////////////////////////////////////////////////////////////
246
-
247
-void Mesh::drawAlphaOld() {
248
-    unsigned int i, j, k, index;
249
-
250
-    /*
251
-
252
-    // Render quadralaterals
253
-    for (mQuads ? i = 0 : i = mNumQuads; i < mNumQuads; ++i) {
254
-        switch (mMode) {
255
-            case MeshModeWireframe:
256
-                glColor3f(0.0, 0.0, 1.0);
257
-                getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
258
-                break;
259
-            case MeshModeSolid:
260
-                // Bind WHITE texture for solid colors
261
-                getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
262
-                break;
263
-            case MeshModeTexture:
264
-            case MeshModeMultiTexture:
265
-                // Bind texture id for textures
266
-                getTextureManager().bindTextureId(mQuads[i].texture);
267
-                break;
268
-        }
269
-
270
-        glBegin(GL_QUADS);
271
-
272
-        for (j = 0; j < mQuads[i].num_alpha_quads; ++j) {
273
-            for (k = 0; k < 4; ++k) {
274
-                index = mQuads[i].alpha_quads[j * 4 + k];
275
-
276
-                glTexCoord2fv(mQuads[i].texcoors2[j * 4 + k]);
277
-                glColor4fv(mColors[index]);
278
-                glVertex3fv(mVertices[index]);
279
-            }
280
-        }
281
-
282
-        glEnd();
283
-    }
284
-
285
-    // Render triangles
286
-    for (mTris ? i = 0 : i = mNumTris; i < mNumTris; ++i) {
287
-        switch (mMode) {
288
-            case MeshModeWireframe:
289
-                glColor3f(0.0, 1.0, 0.0);
290
-                getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
291
-                break;
292
-            case MeshModeSolid:
293
-                // Bind WHITE texture for solid colors
294
-                getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
295
-                break;
296
-            case MeshModeTexture:
297
-            case MeshModeMultiTexture:
298
-                // Bind texture id for textures
299
-                getTextureManager().bindTextureId(mTris[i].texture);
300
-                break;
301
-        }
302
-
303
-        glBegin(GL_TRIANGLES);
304
-
305
-        for (j = 0; j < mTris[i].num_alpha_triangles; ++j) {
306
-            for (k = 0; k < 3; ++k) {
307
-                index = mTris[i].alpha_triangles[j * 3 + k];
308
-
309
-                glTexCoord2fv(mTris[i].texcoors2[j * 3 + k]);
310
-                glColor4fv(mColors[index]);
311
-                glVertex3fv(mVertices[index]);
312
-            }
313
-        }
314
-
315
-        glEnd();
316
-    }
317
-
318
-    */
319
-}
320
-
321
-
322
-void Mesh::drawSolidOld() {
323
-    unsigned int i, j, k, index;
324
-
325
-    /*
326
-
327
-
328
-    // Render quadralaterals
329
-    for (mQuads ? i = 0 : i = mNumQuads; i < mNumQuads; ++i) {
330
-        switch (mMode) {
331
-            case MeshModeSolid:
332
-                glColor3f(0.0, 0.0, 0.0);
333
-                break;
334
-            case MeshModeWireframe:
335
-                // Bind WHITE texture for solid colors
336
-                getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
337
-                break;
338
-#ifdef MULTITEXTURE
339
-            case MeshModeMultiTexture:
340
-                glActiveTextureARB(GL_TEXTURE0_ARB);
341
-                glEnable(GL_TEXTURE_2D);
342
-                getTextureManager().bindTextureId(mQuads[i].texture);
343
-
344
-                glActiveTextureARB(GL_TEXTURE1_ARB);
345
-                glEnable(GL_TEXTURE_2D);
346
-                getTextureManager().bindTextureId(mQuads[i].bumpmap);
347
-                break;
348
-#else
349
-            case MeshModeMultiTexture:
350
-#endif
351
-            case MeshModeTexture:
352
-                // Bind texture id for textures
353
-                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
354
-                getTextureManager().bindTextureId(mQuads[i].texture);
355
-                break;
356
-        }
357
-
358
-        glBegin(GL_QUADS);
359
-
360
-        for (j = 0; j < mQuads[i].num_quads; ++j) {
361
-            for (k = 0; k < 4; ++k) {
362
-                index = mQuads[i].quads[j * 4 + k];
363
-
364
-                glColor4fv(mColors[index]);
365
-
366
-#ifdef MULTITEXTURE
367
-                if (mMode == MeshModeMultiTexture) {
368
-                    glMultiTexCoord2fvARB(GL_TEXTURE0_ARB,
369
-                                          mQuads[i].texcoors[j * 4 + k]);
370
-                    glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,
371
-                                          mQuads[i].texcoors[j * 4 + k]);
372
-                } else
373
-#endif
374
-                    glTexCoord2fv(mQuads[i].texcoors[j * 4 + k]);
375
-
376
-                glVertex3fv(mVertices[index]);
16
+Mesh::Mesh(const std::vector<RoomVertexTR2>& vert,
17
+     const std::vector<RoomRectangleTR2>& rect,
18
+     const std::vector<RoomTriangleTR2>& tri) {
19
+    for (auto& t : rect) {
20
+        indices.push_back(0);
21
+        vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
22
+        vertices.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
23
+        vertices.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
24
+        vertices.push_back(glm::vec3(vert.at(t.v4).x, vert.at(t.v4).y, vert.at(t.v4).z));
25
+        textures.push_back(t.texture);
26
+    }
27
+
28
+    for (auto& t : tri) {
29
+        indices.push_back(1);
30
+        vertices.push_back(glm::vec3(vert.at(t.v1).x, vert.at(t.v1).y, vert.at(t.v1).z));
31
+        vertices.push_back(glm::vec3(vert.at(t.v2).x, vert.at(t.v2).y, vert.at(t.v2).z));
32
+        vertices.push_back(glm::vec3(vert.at(t.v3).x, vert.at(t.v3).y, vert.at(t.v3).z));
33
+        textures.push_back(t.texture);
34
+    }
35
+}
36
+
37
+struct PackedVertex {
38
+    glm::vec3 pos;
39
+    glm::vec2 uv;
40
+    unsigned int tex;
41
+
42
+    PackedVertex(glm::vec3 p, glm::vec2 u, unsigned int t) : pos(p), uv(u), tex(t) { }
43
+    bool operator<(const PackedVertex& v) const { return memcmp(this, &v, sizeof(PackedVertex)) > 0; }
44
+};
45
+
46
+static bool findSimilarVertex(PackedVertex& v,
47
+                              std::map<PackedVertex, unsigned short> m,
48
+                              unsigned short& s) {
49
+    auto it = m.find(v);
50
+    if (it == m.end())
51
+        return false;
52
+    else {
53
+        s = it->second;
54
+        return true;
55
+    }
56
+}
57
+
58
+void Mesh::prepare() {
59
+    std::vector<unsigned short> ind;
60
+    std::vector<glm::vec3> vert;
61
+    std::vector<unsigned int> tex;
62
+
63
+    std::map<PackedVertex, unsigned short> vertexMap;
64
+
65
+    int vertIndex = 0;
66
+    for (int i = 0; i < indices.size(); i++) {
67
+        unsigned int texture = getTextureManager().getTile(textures.at(i)).getTexture();
68
+        for (int v = 0; v < ((indices.at(i) == 0) ? 4 : 3); v++) {
69
+            glm::vec2 uv = getTextureManager().getTile(textures.at(i)).getUV(v);
70
+            PackedVertex p(vertices.at(vertIndex + v), uv, texture);
71
+            unsigned short s;
72
+            if (findSimilarVertex(p, vertexMap, s)) {
73
+                ind.push_back(s); // Vertex already cached
74
+            } else {
75
+                vertexMap[p] = vert.size();
76
+                ind.push_back(vert.size());
77
+                vert.push_back(p.pos);
78
+                uvs.push_back(p.uv);
79
+                tex.push_back(p.tex);
377 80
             }
378 81
         }
379 82
 
380
-        glEnd();
381
-    }
382
-
383
-    // Render triangles
384
-    for (mTris ? i = 0 : i = mNumTris; i < mNumTris; ++i) {
385
-        switch (mMode) {
386
-            case MeshModeSolid:
387
-                glColor3f(1.0, 0.0, 0.0);
388
-                break;
389
-            case MeshModeWireframe:
390
-                // Bind WHITE texture for solid colors
391
-                getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
392
-                break;
393
-#ifdef MULTITEXTURE
394
-            case MeshModeMultiTexture:
395
-                glActiveTextureARB(GL_TEXTURE0_ARB);
396
-                glEnable(GL_TEXTURE_2D);
397
-                getTextureManager().bindTextureId(mTris[i].texture);
398
-
399
-                glActiveTextureARB(GL_TEXTURE1_ARB);
400
-                glEnable(GL_TEXTURE_2D);
401
-                getTextureManager().bindTextureId(mTris[i].bumpmap);
402
-                break;
403
-#else
404
-            case MeshModeMultiTexture:
405
-#endif
406
-            case MeshModeTexture:
407
-                // Bind texture id for textures
408
-                glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
409
-                getTextureManager().bindTextureId(mTris[i].texture);
410
-                break;
411
-        }
412
-
413
-        glBegin(GL_TRIANGLES);
414
-
415
-        for (j = 0; j < mTris[i].num_triangles; ++j) {
416
-            for (k = 0; k < 3; ++k) {
417
-                index = mTris[i].triangles[j * 3 + k];
418
-
419
-#ifdef MULTITEXTURE
420
-                if (mMode == MeshModeMultiTexture) {
421
-                    glMultiTexCoord2fvARB(GL_TEXTURE0_ARB,
422
-                                          mTris[i].texcoors[j * 3 + k]);
423
-                    glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,
424
-                                          mTris[i].texcoors[j * 3 + k]);
425
-                } else
426
-#endif
427
-                    glTexCoord2fv(mTris[i].texcoors[j * 3 + k]);
428
-
429
-                glColor4fv(mColors[index]);
430
-                glVertex3fv(mVertices[index]);
431
-            }
83
+        if (indices.at(i) == 0) {
84
+            ind.push_back(ind.at(ind.size() - 1));
85
+            ind.push_back(ind.at(ind.size() - 3));
432 86
         }
433 87
 
434
-        glEnd();
435
-    }
436
-
437
-#ifdef MULTITEXTURE
438
-    if (mMode == MeshModeMultiTexture) {
439
-        glDisable(GL_TEXTURE_2D);
440
-        glActiveTextureARB(GL_TEXTURE0_ARB);
441
-    }
442
-#endif
443
-
444
-    */
445
-}
446
-
447
-
448
-////////////////////////////////////////////////////////////
449
-// Public Mutators
450
-////////////////////////////////////////////////////////////
451
-
452
-void Mesh::allocateColors(unsigned int n) {
453
-    if (mColors) {
454
-        for (unsigned int i = 0; i < mNumColors; i++)
455
-            delete [] mColors[i];
456
-        delete [] mColors;
457
-        mNumColors = 0;
458
-    }
459
-
460
-    if (!n) {
461
-        return;
462
-    }
463
-
464
-    mNumColors = n;
465
-    mColors = new float *[mNumColors];
466
-    for (unsigned int i = 0; i < mNumColors; i++)
467
-        mColors[i] = new float[4];
468
-}
469
-
470
-
471
-void Mesh::allocateNormals(unsigned int n) {
472
-    if (mNormals) {
473
-        for (unsigned int i = 0; i < mNumNormals; i++)
474
-            delete [] mNormals[i];
475
-        delete [] mNormals;
476
-        mNumNormals = 0;
477
-    }
478
-
479
-    if (!n) {
480
-        return;
481
-    }
482
-
483
-    mNumNormals = n;
484
-    mNormals = new float *[mNumNormals];
485
-    for (unsigned int i = 0; i < mNumNormals; i++)
486
-        mNormals[i] = new float[3];
487
-}
488
-
489
-
490
-void Mesh::allocateRectangles(unsigned int n) {
491
-    if (mQuads) {
492
-        mNumQuads = 0;
493
-        delete [] mQuads;
494
-    }
495
-
496
-    if (!n) {
497
-        return;
498
-    }
499
-
500
-    mNumQuads = n;
501
-    mQuads = new rect_t[mNumQuads];
502
-}
503
-
504
-
505
-void Mesh::allocateTriangles(unsigned int n) {
506
-    if (mTris) {
507
-        mNumTris = 0;
508
-        delete [] mTris;
509
-    }
510
-
511
-    if (!n) {
512
-        return;
513
-    }
514
-
515
-    mNumTris = n;
516
-    mTris = new tris_t[mNumTris];
517
-}
518
-
519
-
520
-void Mesh::allocateVertices(unsigned int n) {
521
-    if (mVertices) {
522
-        for (unsigned int i = 0; i < mNumVertices; i++)
523
-            delete [] mVertices[i];
524
-        delete [] mVertices;
525
-        mNumVertices = 0;
526
-    }
527
-
528
-    if (!n) {
529
-        return;
530
-    }
531
-
532
-    mNumVertices = n;
533
-    mVertices = new float *[mNumVertices];
534
-    for (unsigned int i = 0; i < mNumVertices; i++)
535
-        mVertices[i] = new float[3];
536
-}
537
-
538
-
539
-void Mesh::bufferColorArray(unsigned int colorCount, float* colors) {
540
-    if (mColors) {
541
-        for (unsigned int i = 0; i < mNumColors; i++)
542
-            delete [] mColors[i];
543
-        delete [] mColors;
544
-        mNumColors = 0;
545
-    }
546
-
547
-    if (!colorCount) {
548
-        return;
549
-    }
550
-
551
-    mNumColors = colorCount;
552
-    mColorArray = colors;
553
-}
554
-
555
-
556
-void Mesh::bufferNormalArray(unsigned int normalCount, float* normals) {
557
-    if (mNormals) {
558
-        for (unsigned int i = 0; i < mNumNormals; i++)
559
-            delete [] mNormals[i];
560
-        delete [] mNormals;
561
-        mNumNormals = 0;
562
-    }
563
-
564
-    if (!normalCount) {
565
-        return;
566
-    }
567
-
568
-    mNumNormals = normalCount;
569
-    mNormalArray = normals;
570
-}
571
-
572
-
573
-void Mesh::bufferTriangles(unsigned int count,
574
-                           unsigned int* indices, float* texCoords,
575
-                           int* textures, unsigned int* flags) {
576
-
577
-    mTriangleCount = count;
578
-    mTriangleTextures = textures;
579
-    mTriangleIndices = indices;
580
-    mTriangleFlags = flags;
581
-    mTriangleTexCoordArray = texCoords;
582
-
583
-    //! \fixme sortTrianglesByTexture();
584
-}
585
-
586
-
587
-void Mesh::bufferVertexArray(unsigned int vertexCount, float* vertices) {
588
-    if (mVertices) {
589
-        for (unsigned int i = 0; i < mNumVertices; i++)
590
-            delete [] mVertices[i];
591
-        delete [] mVertices;
592
-        mNumVertices = 0;
593
-    }
594
-
595
-    if (!vertexCount) {
596
-        return;
88
+        vertIndex += (indices.at(i) == 0) ? 4 : 3;
597 89
     }
598 90
 
599
-    mNumVertices = vertexCount;
600
-    mVertexArray = vertices;
601
-    mFlags |= fMesh_UseVertexArray;
602
-}
603
-
604
-
605
-void Mesh::setColor(unsigned int index,
606
-                    float r, float g, float b, float a) {
607
-    assert(index < mNumColors);
608
-
609
-    mColors[index][0] = r;
610
-    mColors[index][1] = g;
611
-    mColors[index][2] = b;
612
-    mColors[index][3] = a;
91
+    indices = ind;
92
+    vertices = vert;
93
+    textures = tex;
613 94
 }
614 95
 
96
+void Mesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {
97
+    glm::mat4 MVP = projection * view * model;
615 98
 
616
-void Mesh::setColor(unsigned int index, float rgba[4]) {
617
-    assert(index < mNumColors);
99
+    // TODO handle different textures!
618 100
 
619
-    mColors[index][0] = rgba[0];
620
-    mColors[index][1] = rgba[1];
621
-    mColors[index][2] = rgba[2];
622
-    mColors[index][3] = rgba[3];
101
+    Window::drawGL(vertices, uvs, indices, MVP, textures.at(0));
623 102
 }
624 103
 
625
-
626
-void Mesh::setNormal(unsigned int index, float i, float j, float k) {
627
-    assert(index < mNumNormals);
628
-
629
-    mNormals[index][0] = i;
630
-    mNormals[index][1] = j;
631
-    mNormals[index][2] = k;
632
-}
633
-
634
-
635
-void Mesh::setVertex(unsigned int index, float x, float y, float z) {
636
-    assert(index < mNumVertices);
637
-
638
-    mVertices[index][0] = x;
639
-    mVertices[index][1] = y;
640
-    mVertices[index][2] = z;
641
-}
642
-
643
-
644
-////////////////////////////////////////////////////////////
645
-// Private Accessors
646
-////////////////////////////////////////////////////////////
647
-
648
-
649
-////////////////////////////////////////////////////////////
650
-// Private Mutators
651
-////////////////////////////////////////////////////////////
652
-

+ 9
- 2
src/Render.cpp 查看文件

@@ -8,6 +8,7 @@
8 8
 
9 9
 #include <algorithm>
10 10
 #include <sstream>
11
+#include <glm/gtc/matrix_transform.hpp>
11 12
 
12 13
 #include <stdlib.h>
13 14
 #include <math.h>
@@ -64,9 +65,15 @@ void Render::display() {
64 65
         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
65 66
     }
66 67
 
67
-    // TODO Setup matrices
68
+    glm::mat4 view = Camera::getViewMatrix();
69
+    glm::mat4 projection = glm::perspective(45.0f, // Field of View
70
+                                            (float)getWindow().getWidth() / (float)getWindow().getHeight(),
71
+                                            0.1f, // Min Distance
72
+                                            100000.0f); // Max Distance
68 73
 
69
-    // TODO Render world
74
+    // Just draw all rooms, as a test
75
+    for (int i = 0; i < getWorld().sizeRoom(); i++)
76
+        getWorld().getRoom(i).display(view, projection);
70 77
 
71 78
     if (mode == RenderMode::Wireframe) {
72 79
         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

+ 14
- 71
src/Room.cpp 查看文件

@@ -6,6 +6,7 @@
6 6
  */
7 7
 
8 8
 #include <algorithm>
9
+#include <glm/gtc/matrix_transform.hpp>
9 10
 #include <glm/gtx/intersect.hpp>
10 11
 
11 12
 #include "global.h"
@@ -15,48 +16,17 @@
15 16
 #include "Room.h"
16 17
 #include "TextureManager.h"
17 18
 
18
-Room::Room(float p[3], unsigned int f, unsigned int x, unsigned int z)
19
-    : flags(f), numXSectors(x), numZSectors(z) {
20
-    if (p == nullptr) {
21
-        pos[0] = 0.0f;
22
-        pos[1] = 0.0f;
23
-        pos[2] = 0.0f;
24
-    } else {
25
-        pos[0] = p[0];
26
-        pos[1] = p[0];
27
-        pos[2] = p[0];
28
-    }
29
-}
30
-
31
-void Room::setNumXSectors(unsigned int n) {
32
-    numXSectors = n;
19
+void Room::display(glm::mat4 view, glm::mat4 projection) {
20
+    glm::mat4 model = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
21
+    mesh->display(model, view, projection);
33 22
 }
34 23
 
35
-void Room::setNumZSectors(unsigned int n) {
36
-    numZSectors = n;
37
-}
38
-
39
-void Room::setPos(float p[3]) {
40
-    for (int i = 0; i < 3; i++)
41
-        pos[i] = p[i];
42
-}
43
-
44
-#define EMPTY_VECTOR(x)     \
45
-while (!x.empty()) {        \
46
-    delete x[x.size() - 1]; \
47
-    x.pop_back();           \
48
-}
49
-
50
-Room::~Room() {
51
-    EMPTY_VECTOR(sprites);
52
-    EMPTY_VECTOR(models);
53
-    EMPTY_VECTOR(portals);
54
-    EMPTY_VECTOR(sectors);
55
-    EMPTY_VECTOR(lights);
24
+void Room::prepare() {
25
+    mesh->prepare();
56 26
 }
57 27
 
28
+/*
58 29
 void Room::display(bool alpha) {
59
-    /*
60 30
     glPushMatrix();
61 31
     //LightingSetup();
62 32
 
@@ -80,9 +50,7 @@ void Room::display(bool alpha) {
80 50
         }
81 51
 
82 52
         glLineWidth(1.0);
83
-    }
84 53
 
85
-    if (Render::getMode() == RenderMode::Wireframe && (!alpha)) {
86 54
         bbox.display(true, RED, GREEN);
87 55
     }
88 56
 
@@ -120,8 +88,8 @@ void Room::display(bool alpha) {
120 88
         for (unsigned int i = 0; i < sizeSprites(); i++)
121 89
             getSprite(i).display();
122 90
     }
123
-    */
124 91
 }
92
+*/
125 93
 
126 94
 bool Room::isWall(unsigned long sector) {
127 95
     assert(sector < sectors.size());
@@ -183,27 +151,6 @@ int Room::getAdjoiningRoom(float x, float y, float z,
183 151
     return -1;
184 152
 }
185 153
 
186
-void Room::setFlags(unsigned int f) {
187
-    flags = f;
188
-}
189
-
190
-unsigned int Room::getFlags() {
191
-    return flags;
192
-}
193
-
194
-unsigned int Room::getNumXSectors() {
195
-    return numXSectors;
196
-}
197
-
198
-unsigned int Room::getNumZSectors() {
199
-    return numZSectors;
200
-}
201
-
202
-void Room::getPos(float p[3]) {
203
-    for (unsigned int i = 0; i < 3; i++)
204
-        p[i] = pos[i];
205
-}
206
-
207 154
 unsigned long Room::sizeAdjacentRooms() {
208 155
     return adjacentRooms.size();
209 156
 }
@@ -214,7 +161,7 @@ long Room::getAdjacentRoom(unsigned long index) {
214 161
 }
215 162
 
216 163
 void Room::addAdjacentRoom(long r) {
217
-    adjacentRooms.push_back(r);
164
+    adjacentRooms.emplace_back(r);
218 165
 }
219 166
 
220 167
 unsigned long Room::sizePortals() {
@@ -227,7 +174,7 @@ Portal& Room::getPortal(unsigned long index) {
227 174
 }
228 175
 
229 176
 void Room::addPortal(Portal* p) {
230
-    portals.push_back(p);
177
+    portals.emplace_back(p);
231 178
 }
232 179
 
233 180
 unsigned long Room::sizeSectors() {
@@ -240,7 +187,7 @@ Sector& Room::getSector(unsigned long index) {
240 187
 }
241 188
 
242 189
 void Room::addSector(Sector* s) {
243
-    sectors.push_back(s);
190
+    sectors.emplace_back(s);
244 191
 }
245 192
 
246 193
 unsigned long Room::sizeModels() {
@@ -253,7 +200,7 @@ StaticModel& Room::getModel(unsigned long index) {
253 200
 }
254 201
 
255 202
 void Room::addModel(StaticModel* s) {
256
-    models.push_back(s);
203
+    models.emplace_back(s);
257 204
 }
258 205
 
259 206
 unsigned long Room::sizeLights() {
@@ -266,7 +213,7 @@ Light& Room::getLight(unsigned long index) {
266 213
 }
267 214
 
268 215
 void Room::addLight(Light* l) {
269
-    lights.push_back(l);
216
+    lights.emplace_back(l);
270 217
 }
271 218
 
272 219
 unsigned long Room::sizeSprites() {
@@ -279,10 +226,6 @@ Sprite& Room::getSprite(unsigned long index) {
279 226
 }
280 227
 
281 228
 void Room::addSprite(Sprite* s) {
282
-    sprites.push_back(s);
283
-}
284
-
285
-BoundingBox& Room::getBoundingBox() {
286
-    return bbox;
229
+    sprites.emplace_back(s);
287 230
 }
288 231
 

+ 4
- 25
src/RoomData.cpp 查看文件

@@ -10,36 +10,15 @@
10 10
 #include "World.h"
11 11
 #include "RoomData.h"
12 12
 
13
-BoundingBox::BoundingBox() {
14
-    a[0] = a[1] = a[2] = 0;
15
-    b[0] = b[1] = b[2] = 0;
16
-}
17
-
18
-void BoundingBox::getBoundingBox(float box[2][3]) {
19
-    box[0][0] = a[0];
20
-    box[1][0] = b[0];
21
-    box[0][1] = a[1];
22
-    box[1][1] = b[1];
23
-    box[0][2] = a[2];
24
-    box[1][2] = b[2];
25
-}
26
-
27
-void BoundingBox::setBoundingBox(float min[3], float max[3]) {
28
-    a[0] = min[0];
29
-    b[0] = max[0];
30
-    a[1] = min[1];
31
-    b[1] = max[1];
32
-    a[2] = min[2];
33
-    b[2] = max[2];
34
-}
13
+BoundingBox::BoundingBox(glm::vec3 min, glm::vec3 max) : a(min), b(max) { }
35 14
 
36 15
 bool BoundingBox::inBox(float x, float y, float z) {
37
-    return ((y > a[1]) && (y < b[1]) && inBoxPlane(x, z));
16
+    return ((y > a.y) && (y < b.y) && inBoxPlane(x, z));
38 17
 }
39 18
 
40 19
 bool BoundingBox::inBoxPlane(float x, float z) {
41
-    return ((x > a[0]) && (x < b[0])
42
-            && (z > a[2]) && (z < b[2]));
20
+    return ((x > a.x) && (x < b.x)
21
+            && (z > a.z) && (z < b.z));
43 22
 }
44 23
 
45 24
 void BoundingBox::display(bool points, const unsigned char c1[4], const unsigned char c2[4]) {

+ 2
- 0
src/SkeletalModel.cpp 查看文件

@@ -21,10 +21,12 @@ BoneTag::BoneTag(int m, float o[3], float r[3], char f) {
21 21
 }
22 22
 
23 23
 void BoneTag::display() {
24
+    /*
24 25
     if (getWorld().sizeMesh() > 0)
25 26
         getWorld().getMesh(mesh).drawSolid(); // TODO ?
26 27
     else
27 28
         getWorld().getStaticMesh(mesh).display();
29
+    */
28 30
 }
29 31
 
30 32
 void BoneTag::getOffset(float o[3]) {

+ 2
- 0
src/SoundManager.cpp 查看文件

@@ -19,6 +19,8 @@ void SoundManager::clear() {
19 19
     soundMap.clear();
20 20
     soundDetails.clear();
21 21
     sampleIndices.clear();
22
+
23
+    Sound::clear();
22 24
 }
23 25
 
24 26
 int SoundManager::prepareSources() {

+ 4
- 28
src/TextureManager.cpp 查看文件

@@ -24,40 +24,21 @@
24 24
 #include "utils/png.h"
25 25
 #endif
26 26
 
27
-TextureTileVertex::TextureTileVertex(uint8_t xc, uint8_t xp, uint8_t yc, uint8_t yp)
28
-    : xCoordinate(xc), xPixel(xp), yCoordinate(yc), yPixel(yp) { }
29
-
30
-// ----------------------------------------------------------------------------
31
-
32
-TextureTile::~TextureTile() {
33
-    while (!vertices.empty()) {
34
-        delete vertices.at(vertices.size() - 1);
35
-        vertices.pop_back();
36
-    }
37
-}
38
-
39
-void TextureTile::add(TextureTileVertex* t) {
40
-    vertices.push_back(t);
41
-}
42
-
27
+/*
43 28
 void TextureTile::displayRectangle(float a[3], float b[3], float c[3], float d[3]) {
44 29
     //! \fixme TR Rosetta Stone says this, but looks strange?
45
-    /*
46 30
     if (attribute == 0) {
47 31
         // Ignore transparency
48 32
         glDisable(GL_BLEND);
49 33
     }
50
-    */
51 34
 
52 35
     float xmin = 256.0f, xmax = 0.0f;
53 36
     float ymin = 256.0f, ymax = 0.0f;
54 37
     for (int i = 0; i < 4; i++) {
55 38
         if (vertices.at(i)->xCoordinate == 255) {
56
-            if (vertices.at(i)->xPixel > xmax)
57
-                xmax = vertices.at(i)->xPixel;
39
+            xmax = vertices.at(i)->xPixel;
58 40
         } else {
59
-            if (vertices.at(i)->xPixel < xmin)
60
-                xmin = vertices.at(i)->xPixel;
41
+            xmin = vertices.at(i)->xPixel;
61 42
         }
62 43
 
63 44
         if (vertices.at(i)->yCoordinate == 255) {
@@ -67,7 +48,6 @@ void TextureTile::displayRectangle(float a[3], float b[3], float c[3], float d[3
67 48
         }
68 49
     }
69 50
 
70
-    /*
71 51
     glBegin(GL_QUADS);
72 52
     glTexCoord2f(xmin / 256.0f, ymin / 256.0f);
73 53
     glVertex3f(a.x, a.y, a.z);
@@ -78,12 +58,8 @@ void TextureTile::displayRectangle(float a[3], float b[3], float c[3], float d[3
78 58
     glTexCoord2f(xmin / 256.0f, ymax / 256.0f);
79 59
     glVertex3f(d.x, d.y, d.z);
80 60
     glEnd();
81
-    */
82
-}
83
-
84
-void TextureTile::displayTriangle(float a[3], float b[3], float c[3]) {
85
-
86 61
 }
62
+*/
87 63
 
88 64
 // ----------------------------------------------------------------------------
89 65
 

+ 64
- 123
src/loader/LoaderTR2.cpp 查看文件

@@ -131,7 +131,7 @@ void LoaderTR2::loadTextures() {
131 131
             assert((xCoordinate == 1) || (xCoordinate == 255) || (xCoordinate == 0));
132 132
             assert((yCoordinate == 1) || (yCoordinate == 255) || (yCoordinate == 0));
133 133
 
134
-            t->add(new TextureTileVertex(xCoordinate, xPixel, yCoordinate, yPixel));
134
+            t->add(TextureTileVertex(xCoordinate, xPixel, yCoordinate, yPixel));
135 135
         }
136 136
 
137 137
         getTextureManager().addTile(t);
@@ -187,72 +187,56 @@ void LoaderTR2::loadRooms() {
187 187
         int32_t yBottom = file.read32(); // lowest point == largest y value
188 188
         int32_t yTop = file.read32(); // highest point == smallest y value
189 189
 
190
+        glm::vec3 pos(xOffset, 0.0f, zOffset);
191
+
190 192
         // Number of data words (2 bytes) to follow
191 193
         uint32_t dataToFollow = file.readU32();
192 194
 
193
-        uint16_t numVertices = file.readU16();
194
-        std::vector<glm::vec3> vertices;
195
-        float bbox[2][3] = {
196
-            { 0.0f, 0.0f, 0.0f },
197
-            { 0.0f, 0.0f, 0.0f }
195
+        glm::vec3 bbox[2] = {
196
+            glm::vec3(0.0f, 0.0f, 0.0f),
197
+            glm::vec3(0.0f, 0.0f, 0.0f)
198 198
         };
199
-        for (unsigned int v = 0; v < numVertices; v++) {
200
-            // Vertex coordinates, relative to x/zOffset
201
-            int32_t x = file.read16() + xOffset;
202
-            int16_t y = file.read16();
203
-            int32_t z = file.read16() + zOffset;
204
-
205
-            int16_t light1 = file.read16();
206
-
207
-            // Set of flags for special rendering effects
208
-            // 0x8000 - Something to do with water surface?
209
-            // 0x4000 - Underwater lighting modulation/movement if seen from above
210
-            // 0x2000 - Water/Quicksand surface movement
211
-            // 0x0010 - Normal?
212
-            uint16_t attributes = file.readU16();
213
-
214
-            int16_t light2 = file.read16(); // Almost always equal to light1
215
-
216
-            vertices.emplace_back(x, y, z);
217 199
 
200
+        uint16_t numVertices = file.readU16();
201
+        std::vector<RoomVertexTR2> vertices;
202
+        for (unsigned int v = 0; v < numVertices; v++) {
203
+            RoomVertexTR2 vert;
204
+            vert.x = file.read16();
205
+            vert.y = file.read16();
206
+            vert.z = file.read16();
207
+            vert.light1 = file.read16();
208
+            vert.attributes = file.readU16();
209
+            vert.light2 = file.read16();
210
+            vertices.push_back(vert);
211
+
212
+            // Fill bounding box
218 213
             if (v == 0) {
219 214
                 for (int i = 0; i < 2; i++) {
220
-                    bbox[i][0] = x;
221
-                    bbox[i][1] = y;
222
-                    bbox[i][2] = z;
215
+                    bbox[i].x = vert.x;
216
+                    bbox[i].y = vert.y;
217
+                    bbox[i].z = vert.z;
223 218
                 }
224 219
             } else {
225
-                if (x < bbox[0][0])
226
-                    bbox[0][0] = x;
227
-                if (x > bbox[1][0])
228
-                    bbox[1][0] = x;
229
-
230
-                if (y < bbox[0][1])
231
-                    bbox[0][1] = y;
232
-                if (y > bbox[1][1])
233
-                    bbox[1][1] = y;
234
-
235
-                if (z < bbox[0][2])
236
-                    bbox[0][2] = z;
237
-                if (z > bbox[1][2])
238
-                    bbox[1][2] = z;
220
+                if (vert.x < bbox[0].x)
221
+                    bbox[0].x = vert.x;
222
+                if (vert.x > bbox[1].x)
223
+                    bbox[1].x = vert.x;
224
+                if (vert.y < bbox[0].y)
225
+                    bbox[0].y = vert.y;
226
+                if (vert.y > bbox[1].y)
227
+                    bbox[1].y = vert.y;
228
+                if (vert.z < bbox[0].z)
229
+                    bbox[0].z = vert.z;
230
+                if (vert.z > bbox[1].z)
231
+                    bbox[1].z = vert.z;
239 232
             }
240 233
         }
241 234
 
242
-        float pos[3] {
243
-            static_cast<float>(xOffset),
244
-            0.0f,
245
-            static_cast<float>(zOffset)
246
-        };
247
-        Room* room = new Room(pos);
248
-
249
-        bbox[0][0] += pos[0];
250
-        bbox[1][0] += pos[0];
251
-        bbox[0][2] += pos[2];
252
-        bbox[1][2] += pos[2];
253
-        room->getBoundingBox().setBoundingBox(bbox[0], bbox[1]);
235
+        bbox[0] += pos;
236
+        bbox[1] += pos;
254 237
 
255 238
         uint16_t numRectangles = file.readU16();
239
+        std::vector<RoomRectangleTR2> rectangles;
256 240
         for (unsigned int r = 0; r < numRectangles; r++) {
257 241
             // Indices into the vertex list read just before
258 242
             uint16_t vertex1 = file.readU16();
@@ -263,12 +247,11 @@ void LoaderTR2::loadRooms() {
263 247
             // Index into the object-texture list
264 248
             uint16_t texture = file.readU16();
265 249
 
266
-            room->getMesh().addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
267
-                                                 vertices.at(vertex3), vertices.at(vertex4),
268
-                                                 texture);
250
+            rectangles.emplace_back(vertex1, vertex2, vertex3, vertex4, texture);
269 251
         }
270 252
 
271 253
         uint16_t numTriangles = file.readU16();
254
+        std::vector<RoomTriangleTR2> triangles;
272 255
         for (unsigned int t = 0; t < numTriangles; t++) {
273 256
             // Indices into the room vertex list
274 257
             uint16_t vertex1 = file.readU16();
@@ -278,8 +261,7 @@ void LoaderTR2::loadRooms() {
278 261
             // Index into the object-texture list
279 262
             uint16_t texture = file.readU16();
280 263
 
281
-            room->getMesh().addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
282
-                                                vertices.at(vertex3), texture);
264
+            triangles.emplace_back(vertex1, vertex2, vertex3, texture);
283 265
         }
284 266
 
285 267
         uint16_t numSprites = file.readU16();
@@ -290,8 +272,7 @@ void LoaderTR2::loadRooms() {
290 272
             // TODO store sprites somewhere
291 273
         }
292 274
 
293
-        glm::mat4 transform = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
294
-        room->addAdjacentRoom(i); // Always set room itself as first
275
+        //room->addAdjacentRoom(i); // Always set room itself as first
295 276
 
296 277
         uint16_t numPortals = file.readU16();
297 278
         for (unsigned int p = 0; p < numPortals; p++) {
@@ -320,57 +301,13 @@ void LoaderTR2::loadRooms() {
320 301
             int16_t yCorner4 = file.read16();
321 302
             int16_t zCorner4 = file.read16();
322 303
 
323
-            glm::vec4 vertices[4] = {
324
-                glm::vec4(
325
-                    static_cast<float>(xCorner1),
326
-                    static_cast<float>(yCorner1),
327
-                    static_cast<float>(zCorner1),
328
-                    1.0f
329
-                ), glm::vec4(
330
-                    static_cast<float>(xCorner2),
331
-                    static_cast<float>(yCorner2),
332
-                    static_cast<float>(zCorner2),
333
-                    1.0f
334
-                ), glm::vec4(
335
-                    static_cast<float>(xCorner3),
336
-                    static_cast<float>(yCorner3),
337
-                    static_cast<float>(zCorner3),
338
-                    1.0f
339
-                ), glm::vec4(
340
-                    static_cast<float>(xCorner4),
341
-                    static_cast<float>(yCorner4),
342
-                    static_cast<float>(zCorner4),
343
-                    0.0f
344
-                )
345
-            };
346
-
347
-            // Portals have relative coordinates
348
-            vertices[0] = transform * vertices[0];
349
-            vertices[1] = transform * vertices[1];
350
-            vertices[2] = transform * vertices[2];
351
-            vertices[3] = transform * vertices[3];
352
-
353
-            float normals[3] = {
354
-                static_cast<float>(xNormal),
355
-                static_cast<float>(yNormal),
356
-                static_cast<float>(zNormal)
357
-            };
358
-
359
-            glm::vec3 verts[4] = {
360
-                glm::vec3(vertices[0]),
361
-                glm::vec3(vertices[1]),
362
-                glm::vec3(vertices[2]),
363
-                glm::vec3(vertices[3])
364
-            };
365
-
366
-            room->addPortal(new Portal(verts, normals, adjoiningRoom));
367
-            room->addAdjacentRoom(adjoiningRoom);
304
+            // TODO store portals
368 305
         }
369 306
 
370 307
         uint16_t numZSectors = file.readU16();
371 308
         uint16_t numXSectors = file.readU16();
372
-        room->setNumXSectors(numXSectors);
373
-        room->setNumZSectors(numZSectors);
309
+        //room->setNumXSectors(numXSectors);
310
+        //room->setNumZSectors(numZSectors);
374 311
         for (unsigned int s = 0; s < (numZSectors * numXSectors); s++) {
375 312
             // Sectors are 1024*1024 world coordinates. Floor and Ceiling are
376 313
             // signed numbers of 256 units of height.
@@ -393,7 +330,7 @@ void LoaderTR2::loadRooms() {
393 330
                 wall = true;
394 331
             }
395 332
 
396
-            room->addSector(new Sector(floor * 256.0f, ceiling * 256.0f, wall));
333
+            //room->addSector(new Sector(floor * 256.0f, ceiling * 256.0f, wall));
397 334
         }
398 335
 
399 336
         int16_t intensity1 = file.read16();
@@ -440,14 +377,18 @@ void LoaderTR2::loadRooms() {
440 377
         int16_t alternateRoom = file.read16(); // TODO
441 378
 
442 379
         uint16_t flags = file.readU16();
443
-        int roomFlags = 0;
380
+        unsigned int roomFlags = 0;
444 381
         if (flags & 0x0001) {
445 382
             roomFlags |= RoomFlagUnderWater;
446 383
         }
447
-        room->setFlags(room->getFlags() | roomFlags);
384
+
385
+        BoundingBox* boundingbox = new BoundingBox(bbox[0], bbox[1]);
386
+        Mesh* mesh = new Mesh(vertices, rectangles, triangles);
387
+        Room* room = new Room(pos, boundingbox, mesh, roomFlags);
448 388
 
449 389
         getWorld().addRoom(room);
450 390
 
391
+        // Sanity check
451 392
         if ((numPortals == 0) && (numVertices == 0)
452 393
             && (numRectangles == 0) && (numTriangles == 0))
453 394
             getLog() << "LoaderTR2: Room " << i << " seems invalid: " << numPortals << "p "
@@ -560,7 +501,7 @@ void LoaderTR2::loadMeshes() {
560 501
             vertices.emplace_back(x, y, z);
561 502
         }
562 503
 
563
-        Mesh* mesh = new Mesh();
504
+        //Mesh* mesh = new Mesh();
564 505
 
565 506
         int16_t numNormals = mem.read16();
566 507
         if (numNormals > 0) {
@@ -574,7 +515,7 @@ void LoaderTR2::loadMeshes() {
574 515
                 int16_t y = mem.read16();
575 516
                 int16_t z = mem.read16();
576 517
 
577
-                mesh->addNormal(glm::vec3(x, y, z));
518
+                //mesh->addNormal(glm::vec3(x, y, z));
578 519
             }
579 520
         } else if (numNormals < 0) {
580 521
             // Internal vertex lighting is used,
@@ -593,9 +534,9 @@ void LoaderTR2::loadMeshes() {
593 534
             uint16_t vertex4 = mem.readU16();
594 535
             uint16_t texture = mem.readU16();
595 536
 
596
-            mesh->addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
597
-                                       vertices.at(vertex3), vertices.at(vertex4),
598
-                                       texture);
537
+            //mesh->addTexturedRectangle(vertices.at(vertex1), vertices.at(vertex2),
538
+            //                           vertices.at(vertex3), vertices.at(vertex4),
539
+            //                           texture);
599 540
         }
600 541
 
601 542
         int16_t numTexturedTriangles = mem.read16();
@@ -605,8 +546,8 @@ void LoaderTR2::loadMeshes() {
605 546
             uint16_t vertex3 = mem.readU16();
606 547
             uint16_t texture = mem.readU16();
607 548
 
608
-            mesh->addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
609
-                                      vertices.at(vertex3), texture);
549
+            //mesh->addTexturedTriangle(vertices.at(vertex1), vertices.at(vertex2),
550
+            //                          vertices.at(vertex3), texture);
610 551
         }
611 552
 
612 553
         int16_t numColoredRectangles = mem.read16();
@@ -623,9 +564,9 @@ void LoaderTR2::loadMeshes() {
623 564
                   blue = (palette.at(index) & 0x0000FF00) >> 8;
624 565
 
625 566
 
626
-            mesh->addColoredRectangle(vertices.at(vertex1), vertices.at(vertex2),
627
-                                      vertices.at(vertex3), vertices.at(vertex4),
628
-                                      red, green, blue);
567
+            //mesh->addColoredRectangle(vertices.at(vertex1), vertices.at(vertex2),
568
+            //                          vertices.at(vertex3), vertices.at(vertex4),
569
+            //                          red, green, blue);
629 570
         }
630 571
 
631 572
         int16_t numColoredTriangles = mem.read16();
@@ -640,11 +581,11 @@ void LoaderTR2::loadMeshes() {
640 581
                   green = (palette.at(index) & 0x00FF0000) >> 16,
641 582
                   blue = (palette.at(index) & 0x0000FF00) >> 8;
642 583
 
643
-            mesh->addColoredTriangle(vertices.at(vertex1), vertices.at(vertex2),
644
-                                     vertices.at(vertex3), red, green, blue);
584
+            //mesh->addColoredTriangle(vertices.at(vertex1), vertices.at(vertex2),
585
+            //                         vertices.at(vertex3), red, green, blue);
645 586
         }
646 587
 
647
-        getWorld().addMesh(mesh);
588
+        //getWorld().addMesh(mesh);
648 589
     }
649 590
 
650 591
     if (numMeshPointers > 0)

+ 0
- 13
src/main.cpp 查看文件

@@ -7,7 +7,6 @@
7 7
 
8 8
 #include <iostream>
9 9
 #include <memory>
10
-#include <sstream>
11 10
 
12 11
 #include "global.h"
13 12
 #include "Exception.h"
@@ -204,19 +203,7 @@ void renderFrame() {
204 203
     getGame().display();
205 204
     getMenu().display();
206 205
     UI::display();
207
-
208
-    /*
209
-    if (getRunTime().getShowFPS()) {
210
-        std::ostringstream s;
211
-        s << getRunTime().getFPS() << "FPS";
212
-        getWindow().glEnter2D();
213
-        Font::drawText(10, getWindow().getHeight() - 25, 0.6f, BLUE, s.str());
214
-        getWindow().glExit2D();
215
-    }
216
-    */
217
-
218 206
     getWindow().swapBuffersGL();
219
-
220 207
     getRunTime().updateFPS();
221 208
 }
222 209
 

+ 78
- 0
src/system/Window.cpp 查看文件

@@ -35,6 +35,7 @@ bool Window::getTextInput() {
35 35
 
36 36
 Shader Window::textShader;
37 37
 Shader Window::imguiShader;
38
+Shader Window::textureShader;
38 39
 unsigned int Window::vertexArrayID = 0;
39 40
 
40 41
 int Window::initializeGL() {
@@ -75,6 +76,14 @@ int Window::initializeGL() {
75 76
         return -7;
76 77
     imguiShader.addBuffer(3);
77 78
 
79
+    if (textureShader.compile(textureShaderVertex, textureShaderFragment) < 0)
80
+        return -8;
81
+    if (textureShader.addUniform("MVP") < 0)
82
+        return -9;
83
+    if (textureShader.addUniform("textureSampler") < 0)
84
+        return -10;
85
+    textureShader.addBuffer(3);
86
+
78 87
     glEnable(GL_BLEND);
79 88
     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
80 89
 
@@ -125,6 +134,43 @@ void Window::drawTextGL(std::vector<glm::vec2>& vertices, std::vector<glm::vec2>
125 134
     glDisableVertexAttribArray(1);
126 135
 }
127 136
 
137
+void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
138
+                    std::vector<unsigned short>& indices, glm::mat4 MVP, unsigned int texture) {
139
+    assert(vertices.size() == uvs.size());
140
+    assert((indices.size() % 3) == 0);
141
+
142
+    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(0));
143
+    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
144
+
145
+    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(1));
146
+    glBufferData(GL_ARRAY_BUFFER, uvs.size() * sizeof(glm::vec2), &uvs[0], GL_STATIC_DRAW);
147
+
148
+    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(2));
149
+    glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
150
+
151
+    textureShader.use();
152
+
153
+    glUniformMatrix4fv(textureShader.getUniform(0), 1, GL_FALSE, &MVP[0][0]);
154
+
155
+    getTextureManager().bindTextureId(texture, TextureManager::TextureStorage::GAME, 0);
156
+    glUniform1i(textureShader.getUniform(1), 0);
157
+
158
+    glEnableVertexAttribArray(0);
159
+    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(0));
160
+    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
161
+
162
+    glEnableVertexAttribArray(1);
163
+    glBindBuffer(GL_ARRAY_BUFFER, textureShader.getBuffer(1));
164
+    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
165
+
166
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, textureShader.getBuffer(2));
167
+
168
+    glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, nullptr);
169
+
170
+    glDisableVertexAttribArray(0);
171
+    glDisableVertexAttribArray(1);
172
+}
173
+
128 174
 // ----------------------------------------------------------------------------
129 175
 
130 176
 Shader::~Shader() {
@@ -318,5 +364,37 @@ void main() {
318 364
 }
319 365
 )!?!";
320 366
 
367
+// --------------------------------------
368
+
369
+const char* Window::textureShaderVertex = R"!?!(
370
+#version 330 core
371
+
372
+layout(location = 0) in vec3 vertexPosition_modelspace;
373
+layout(location = 1) in vec2 vertexUV;
374
+
375
+out vec2 UV;
376
+
377
+uniform mat4 MVP;
378
+
379
+void main() {
380
+    gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
381
+    UV = vertexUV;
382
+}
383
+)!?!";
384
+
385
+const char* Window::textureShaderFragment = R"!?!(
386
+#version 330 core
387
+
388
+in vec2 UV;
389
+
390
+out vec4 color;
391
+
392
+uniform sampler2D textureSampler;
393
+
394
+void main() {
395
+    color = texture(textureSampler, UV);
396
+}
397
+)!?!";
398
+
321 399
 // *INDENT-ON*
322 400
 

Loading…
取消
儲存