Browse Source

Draw room bounding boxes in wireframe mode

Thomas Buck 9 years ago
parent
commit
826c05f5b6
11 changed files with 125 additions and 38 deletions
  1. 3
    0
      ChangeLog.md
  2. 2
    2
      include/Room.h
  3. 15
    12
      include/RoomData.h
  4. 3
    0
      include/system/Window.h
  5. 8
    8
      src/Camera.cpp
  6. 6
    6
      src/Mesh.cpp
  7. 3
    0
      src/Room.cpp
  8. 33
    0
      src/RoomData.cpp
  9. 3
    3
      src/RoomMesh.cpp
  10. 33
    0
      src/system/Window.cpp
  11. 16
    7
      src/system/WindowSDL.cpp

+ 3
- 0
ChangeLog.md View File

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141228 ]
6
+    * Room Bounding Boxes are now visualized in Wireframe mode (again)
7
+
5 8
     [ 20141227 ]
6 9
     * Added rudimentary SDL2 Game Controller support.
7 10
     * Camera now using Quaternion for rotation.

+ 2
- 2
include/Room.h View File

@@ -25,8 +25,8 @@ class Room {
25 25
   public:
26 26
     Room(glm::vec3 _pos, BoundingBox* _bbox, RoomMesh* _mesh, unsigned int f,
27 27
          int a, int x, int z)
28
-       : pos(_pos), bbox(_bbox), mesh(_mesh), flags(f), alternateRoom(a),
29
-         numXSectors(x), numZSectors(z) { }
28
+        : pos(_pos), bbox(_bbox), mesh(_mesh), flags(f), alternateRoom(a),
29
+          numXSectors(x), numZSectors(z) { }
30 30
 
31 31
     void prepare() { mesh->prepare(); }
32 32
     void display(glm::mat4 view, glm::mat4 projection);

+ 15
- 12
include/RoomData.h View File

@@ -14,23 +14,25 @@
14 14
 
15 15
 class BoundingBox {
16 16
   public:
17
-    BoundingBox(glm::vec3 min, glm::vec3 max) : a(min), b(max) {
18
-        corner[0] = glm::vec3(a.x, a.y, a.z);
19
-        corner[1] = glm::vec3(b.x, a.y, a.z);
20
-        corner[2] = glm::vec3(a.x, b.y, a.z);
21
-        corner[3] = glm::vec3(a.x, a.y, b.z);
22
-        corner[4] = glm::vec3(b.x, b.y, a.z);
23
-        corner[5] = glm::vec3(a.x, b.y, b.z);
24
-        corner[6] = glm::vec3(b.x, a.y, b.z);
25
-        corner[7] = glm::vec3(b.x, b.y, b.z);
17
+    BoundingBox(glm::vec3 min, glm::vec3 max) {
18
+        corner[0] = glm::vec3(min.x, min.y, min.z);
19
+        corner[1] = glm::vec3(max.x, min.y, min.z);
20
+        corner[2] = glm::vec3(min.x, max.y, min.z);
21
+        corner[3] = glm::vec3(min.x, min.y, max.z);
22
+        corner[4] = glm::vec3(max.x, max.y, min.z);
23
+        corner[5] = glm::vec3(min.x, max.y, max.z);
24
+        corner[6] = glm::vec3(max.x, min.y, max.z);
25
+        corner[7] = glm::vec3(max.x, max.y, max.z);
26 26
     }
27 27
 
28 28
     bool inBox(glm::vec3 p) {
29
-        return ((p.y >= a.y) && (p.y <= b.y) && inBoxPlane(p));
29
+        return ((p.y >= corner[0].y) && (p.y <= corner[7].y)
30
+                && inBoxPlane(p));
30 31
     }
31 32
 
32 33
     bool inBoxPlane(glm::vec3 p) {
33
-        return ((p.x >= a.x) && (p.x <= b.x) && (p.z >= a.z) && (p.z <= b.z));
34
+        return ((p.x >= corner[0].x) && (p.x <= corner[7].x)
35
+                && (p.z >= corner[0].z) && (p.z <= corner[7].z));
34 36
     }
35 37
 
36 38
     glm::vec3 getCorner(int i) {
@@ -38,8 +40,9 @@ class BoundingBox {
38 40
         return corner[i];
39 41
     }
40 42
 
43
+    void display(glm::mat4 VP, glm::vec3 color);
44
+
41 45
   private:
42
-    glm::vec3 a, b;
43 46
     glm::vec3 corner[8];
44 47
 };
45 48
 

+ 3
- 0
include/system/Window.h View File

@@ -71,6 +71,9 @@ class Window {
71 71
     static void drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
72 72
                        std::vector<unsigned short>& indices, glm::mat4 MVP);
73 73
 
74
+    static void drawLinesGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
75
+                            std::vector<unsigned short>& indices, glm::mat4 MVP);
76
+
74 77
   protected:
75 78
     bool mInit;
76 79
     bool mFullscreen;

+ 8
- 8
src/Camera.cpp View File

@@ -6,7 +6,6 @@
6 6
  * \author xythobuz
7 7
  */
8 8
 
9
-#include <limits>
10 9
 #include <glm/gtc/epsilon.hpp>
11 10
 #include <glm/gtc/matrix_transform.hpp>
12 11
 #include <glm/gtx/quaternion.hpp>
@@ -37,6 +36,7 @@ const static float nearDist = 0.1f;
37 36
 const static float farDist = 75000.0f;
38 37
 const static float maxSpeed = 2048.0f;
39 38
 const static float controllerViewFactor = 384.0f;
39
+const static float controllerDeadZone = 0.1f;
40 40
 
41 41
 const static glm::vec3 rightUnit(1.0f, 0.0f, 0.0f);
42 42
 const static glm::vec3 upUnit(0.0f, 1.0f, 0.0f);
@@ -92,7 +92,7 @@ void Camera::handleMouseMotion(int x, int y) {
92 92
 }
93 93
 
94 94
 void Camera::handleControllerAxis(float value, KeyboardButton axis) {
95
-    if (glm::epsilonEqual(value, 0.0f, 0.01f))
95
+    if (glm::epsilonEqual(value, 0.0f, controllerDeadZone))
96 96
         value = 0.0f;
97 97
 
98 98
     if (axis == leftXAxis) {
@@ -134,10 +134,10 @@ bool Camera::update() {
134 134
     float dT = getRunTime().getLastFrameTime();
135 135
     pos += quaternion * posSpeed * dT;
136 136
 
137
-    if (glm::epsilonNotEqual(rotSpeed.x, 0.0f, 0.01f))
137
+    if (glm::epsilonNotEqual(rotSpeed.x, 0.0f, controllerDeadZone))
138 138
         quaternion = glm::quat(upUnit * rotationDeltaX * rotSpeed.x * dT) * quaternion;
139 139
 
140
-    if (glm::epsilonNotEqual(rotSpeed.y, 0.0f, 0.01f))
140
+    if (glm::epsilonNotEqual(rotSpeed.y, 0.0f, controllerDeadZone))
141 141
         quaternion = glm::quat(quaternion * -rightUnit * rotationDeltaY * rotSpeed.y * dT) * quaternion;
142 142
 
143 143
     glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
@@ -190,13 +190,13 @@ void Camera::calculateFrustumPlanes() {
190 190
 
191 191
     // Calculate frustum corners to display them
192 192
     glm::mat4 inverse = glm::inverse(combo);
193
-    frustumVertices[NTL] = glm::vec3( 1.0f,  1.0f, 0.0f);
193
+    frustumVertices[NTL] = glm::vec3(1.0f,  1.0f, 0.0f);
194 194
     frustumVertices[NTR] = glm::vec3(-1.0f,  1.0f, 0.0f);
195
-    frustumVertices[NBL] = glm::vec3( 1.0f, -1.0f, 0.0f);
195
+    frustumVertices[NBL] = glm::vec3(1.0f, -1.0f, 0.0f);
196 196
     frustumVertices[NBR] = glm::vec3(-1.0f, -1.0f, 0.0f);
197
-    frustumVertices[FTL] = glm::vec3( 1.0f,  1.0f, 1.0f);
197
+    frustumVertices[FTL] = glm::vec3(1.0f,  1.0f, 1.0f);
198 198
     frustumVertices[FTR] = glm::vec3(-1.0f,  1.0f, 1.0f);
199
-    frustumVertices[FBL] = glm::vec3( 1.0f, -1.0f, 1.0f);
199
+    frustumVertices[FBL] = glm::vec3(1.0f, -1.0f, 1.0f);
200 200
     frustumVertices[FBR] = glm::vec3(-1.0f, -1.0f, 1.0f);
201 201
     for (int i = 0; i < 8; i++) {
202 202
         glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);

+ 6
- 6
src/Mesh.cpp View File

@@ -83,9 +83,9 @@ void Mesh::prepare() {
83 83
     assert((ind.size() % 3) == 0);
84 84
     assert(vert.size() == tex.size());
85 85
     assert(vert.size() == uvs.size());
86
-    indices = ind;
87
-    vertices = vert;
88
-    textures = tex;
86
+    indices = std::move(ind);
87
+    vertices = std::move(vert);
88
+    textures = std::move(tex);
89 89
 
90 90
     std::vector<unsigned short> indC;
91 91
     std::vector<glm::vec3> vertC;
@@ -115,9 +115,9 @@ void Mesh::prepare() {
115 115
     }
116 116
     assert((indC.size() % 3) == 0);
117 117
     assert(vertC.size() == col.size());
118
-    indicesColor = indC;
119
-    verticesColor = vertC;
120
-    colors = col;
118
+    indicesColor = std::move(indC);
119
+    verticesColor = std::move(vertC);
120
+    colors = std::move(col);
121 121
 }
122 122
 
123 123
 void Mesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {

+ 3
- 0
src/Room.cpp View File

@@ -23,6 +23,9 @@ void Room::display(glm::mat4 view, glm::mat4 projection) {
23 23
     for (auto& m : models) {
24 24
         m->display(view, projection);
25 25
     }
26
+
27
+    if (Render::getMode() == RenderMode::Wireframe)
28
+        bbox->display(projection * view, glm::vec3(0.0f, 1.0f, 0.0f));
26 29
 }
27 30
 
28 31
 bool Room::isWall(unsigned long sector) {

+ 33
- 0
src/RoomData.cpp View File

@@ -10,8 +10,41 @@
10 10
 #include "global.h"
11 11
 #include "SkeletalModel.h"
12 12
 #include "World.h"
13
+#include "system/Window.h"
13 14
 #include "RoomData.h"
14 15
 
16
+void BoundingBox::display(glm::mat4 VP, glm::vec3 color) {
17
+    std::vector<glm::vec3> verts;
18
+    std::vector<glm::vec3> cols;
19
+    std::vector<unsigned short> inds;
20
+
21
+    for (int i = 0; i < 8; i++) {
22
+        verts.push_back(corner[i]);
23
+        cols.push_back(color);
24
+    }
25
+
26
+    inds.push_back(0);
27
+    inds.push_back(2);
28
+    inds.push_back(4);
29
+    inds.push_back(1);
30
+    inds.push_back(6);
31
+    inds.push_back(7);
32
+    inds.push_back(5);
33
+    inds.push_back(3);
34
+    inds.push_back(0);
35
+    inds.push_back(1);
36
+    inds.push_back(4);
37
+    inds.push_back(7);
38
+    inds.push_back(6);
39
+    inds.push_back(3);
40
+    inds.push_back(5);
41
+    inds.push_back(2);
42
+
43
+    Window::drawLinesGL(verts, cols, inds, VP);
44
+}
45
+
46
+// ----------------------------------------------------------------------------
47
+
15 48
 void StaticModel::display(glm::mat4 view, glm::mat4 projection) {
16 49
     if (cache < 0) {
17 50
         for (int i = 0; i < getWorld().sizeStaticMesh(); i++) {

+ 3
- 3
src/RoomMesh.cpp View File

@@ -67,9 +67,9 @@ void RoomMesh::prepare() {
67 67
 
68 68
     assert((ind.size() % 3) == 0);
69 69
 
70
-    indices = ind;
71
-    vertices = vert;
72
-    textures = tex;
70
+    indices = std::move(ind);
71
+    vertices = std::move(vert);
72
+    textures = std::move(tex);
73 73
 }
74 74
 
75 75
 void RoomMesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {

+ 33
- 0
src/system/Window.cpp View File

@@ -213,6 +213,39 @@ void Window::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& co
213 213
     glDisableVertexAttribArray(1);
214 214
 }
215 215
 
216
+void Window::drawLinesGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
217
+                         std::vector<unsigned short>& indices, glm::mat4 MVP) {
218
+    assert(vertices.size() == colors.size());
219
+
220
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
221
+    glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
222
+
223
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
224
+    glBufferData(GL_ARRAY_BUFFER, colors.size() * sizeof(glm::vec3), &colors[0], GL_STATIC_DRAW);
225
+
226
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(2));
227
+    glBufferData(GL_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
228
+
229
+    colorShader.use();
230
+
231
+    glUniformMatrix4fv(colorShader.getUniform(0), 1, GL_FALSE, &MVP[0][0]);
232
+
233
+    glEnableVertexAttribArray(0);
234
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(0));
235
+    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
236
+
237
+    glEnableVertexAttribArray(1);
238
+    glBindBuffer(GL_ARRAY_BUFFER, colorShader.getBuffer(1));
239
+    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
240
+
241
+    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, colorShader.getBuffer(2));
242
+
243
+    glDrawElements(GL_LINE_STRIP, indices.size(), GL_UNSIGNED_SHORT, nullptr);
244
+
245
+    glDisableVertexAttribArray(0);
246
+    glDisableVertexAttribArray(1);
247
+}
248
+
216 249
 // ----------------------------------------------------------------------------
217 250
 
218 251
 Shader::~Shader() {

+ 16
- 7
src/system/WindowSDL.cpp View File

@@ -125,13 +125,22 @@ int WindowSDL::initialize() {
125 125
         return 0;
126 126
     }
127 127
 
128
-    SDL_GameControllerAddMapping("341a0000000000000208000000000000,"
129
-                                 "USB GAMEPAD 8116,"
130
-                                 "a:b0,x:b2,start:b7,back:b6,leftstick:b8,rightstick:b9,"
131
-                                 "leftshoulder:b4,rightshoulder:b5,"
132
-                                 "dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,"
133
-                                 "leftx:a0,lefty:a1,rightx:a3,righty:a2,"
134
-                                 "lefttrigger:,b:b1,y:b3,lefttrigger:a4,righttrigger:a4");
128
+    //! \todo Provide a way for user-defined controller mappings
129
+    /*
130
+        SDL_GameControllerAddMapping("341a0000000000000208000000000000,"
131
+                                     "USB GAMEPAD 8116,"
132
+                                     "a:b0,x:b2,start:b7,back:b6,leftstick:b8,rightstick:b9,"
133
+                                     "leftshoulder:b4,rightshoulder:b5,"
134
+                                     "dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,"
135
+                                     "leftx:a0,lefty:a1,rightx:a3,righty:a2,"
136
+                                     "lefttrigger:,b:b1,y:b3,lefttrigger:a4,righttrigger:a4");
137
+        SDL_GameControllerAddMapping("4c050000000000006802000000000000,"
138
+                                     "PLAYSTATION(R)3 Controller,"
139
+                                     "a:b14,b:b13,y:b12,x:b15,start:b3,guide:b16,back:b0,"
140
+                                     "leftstick:b1,rightstick:b2,leftshoulder:b10,"
141
+                                     "rightshoulder:b11,dpup:b4,dpleft:b7,dpdown:b6,dpright:b5,"
142
+                                     "leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b8,righttrigger:b9");
143
+    */
135 144
 
136 145
     for (int i = 0; i < SDL_NumJoysticks(); i++) {
137 146
         if (SDL_IsGameController(i)) {

Loading…
Cancel
Save