Browse Source

First bbox cache test

Thomas Buck 9 years ago
parent
commit
6f7ddbfbaf
10 changed files with 155 additions and 89 deletions
  1. 35
    0
      include/BoundingBox.h
  2. 1
    0
      include/Room.h
  3. 1
    39
      include/RoomData.h
  4. 3
    0
      include/config.h.in
  5. 90
    0
      src/BoundingBox.cpp
  6. 5
    0
      src/CMakeLists.txt
  7. 1
    1
      src/Entity.cpp
  8. 0
    42
      src/RoomData.cpp
  9. 2
    0
      src/main.cpp
  10. 17
    7
      src/utils/filesystem.cpp

+ 35
- 0
include/BoundingBox.h View File

@@ -0,0 +1,35 @@
1
+/*!
2
+ * \file include/BoundingBox.h
3
+ * \brief 3D Axis Aligned Bounding Box
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _BOUNDING_BOX_H_
9
+#define _BOUNDING_BOX_H_
10
+
11
+#include <array>
12
+#include <vector>
13
+
14
+class BoundingBox {
15
+  public:
16
+    BoundingBox(glm::vec3 min, glm::vec3 max);
17
+
18
+    bool inBox(glm::vec3 p);
19
+    bool inBoxPlane(glm::vec3 p);
20
+
21
+    glm::vec3 getCorner(int i) { return corner.at(i); }
22
+
23
+    void display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot);
24
+
25
+    static void display();
26
+
27
+  private:
28
+    std::array<glm::vec3, 8> corner;
29
+
30
+    static std::vector<glm::vec3> vertices, colorsLine, colorsPoint;
31
+    static std::vector<unsigned short> indicesLine;
32
+};
33
+
34
+#endif
35
+

+ 1
- 0
include/Room.h View File

@@ -11,6 +11,7 @@
11 11
 #include <memory>
12 12
 #include <vector>
13 13
 
14
+#include "BoundingBox.h"
14 15
 #include "Sprite.h"
15 16
 #include "RoomData.h"
16 17
 #include "RoomMesh.h"

+ 1
- 39
include/RoomData.h View File

@@ -8,45 +8,7 @@
8 8
 #ifndef _ROOM_DATA_H_
9 9
 #define _ROOM_DATA_H_
10 10
 
11
-#include <memory>
12
-#include <vector>
13
-
14
-class BoundingBox {
15
-  public:
16
-    BoundingBox(glm::vec3 min, glm::vec3 max) {
17
-        corner[0] = glm::vec3(min.x, min.y, min.z);
18
-        corner[1] = glm::vec3(max.x, min.y, min.z);
19
-        corner[2] = glm::vec3(min.x, max.y, min.z);
20
-        corner[3] = glm::vec3(min.x, min.y, max.z);
21
-        corner[4] = glm::vec3(max.x, max.y, min.z);
22
-        corner[5] = glm::vec3(min.x, max.y, max.z);
23
-        corner[6] = glm::vec3(max.x, min.y, max.z);
24
-        corner[7] = glm::vec3(max.x, max.y, max.z);
25
-    }
26
-
27
-    bool inBox(glm::vec3 p) {
28
-        return ((p.y >= corner[0].y) && (p.y <= corner[7].y)
29
-                && inBoxPlane(p));
30
-    }
31
-
32
-    bool inBoxPlane(glm::vec3 p) {
33
-        return ((p.x >= corner[0].x) && (p.x <= corner[7].x)
34
-                && (p.z >= corner[0].z) && (p.z <= corner[7].z));
35
-    }
36
-
37
-    glm::vec3 getCorner(int i) {
38
-        orAssertGreaterThanEqual(i, 0);
39
-        orAssertLessThan(i, 8);
40
-        return corner[i];
41
-    }
42
-
43
-    void display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot);
44
-
45
-  private:
46
-    glm::vec3 corner[8];
47
-};
48
-
49
-// --------------------------------------
11
+#include "BoundingBox.h"
50 12
 
51 13
 class StaticModel {
52 14
   public:

+ 3
- 0
include/config.h.in View File

@@ -35,6 +35,9 @@ extern const char* INSTALL_PREFIX;
35 35
 #cmakedefine HAVE_UNISTD_H
36 36
 #cmakedefine HAVE_GETCWD
37 37
 
38
+#cmakedefine HAVE_DIRECT_H
39
+#cmakedefine HAVE__GETCWD
40
+
38 41
 #cmakedefine HAVE_STDLIB_H
39 42
 #cmakedefine HAVE_GETENV
40 43
 

+ 90
- 0
src/BoundingBox.cpp View File

@@ -0,0 +1,90 @@
1
+/*!
2
+ * \file src/BoundingBox.cpp
3
+ * \brief 3D Axis Aligned Bounding Box
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "Camera.h"
10
+#include "World.h"
11
+#include "system/Shader.h"
12
+#include "RoomData.h"
13
+
14
+#include <glbinding/gl/gl.h>
15
+
16
+std::vector<glm::vec3> BoundingBox::vertices, BoundingBox::colorsLine, BoundingBox::colorsPoint;
17
+std::vector<unsigned short> BoundingBox::indicesLine;
18
+
19
+BoundingBox::BoundingBox(glm::vec3 min, glm::vec3 max) {
20
+    corner[0] = min;
21
+    corner[1] = glm::vec3(max.x, min.y, min.z);
22
+    corner[2] = glm::vec3(min.x, max.y, min.z);
23
+    corner[3] = glm::vec3(min.x, min.y, max.z);
24
+    corner[4] = glm::vec3(max.x, max.y, min.z);
25
+    corner[5] = glm::vec3(min.x, max.y, max.z);
26
+    corner[6] = glm::vec3(max.x, min.y, max.z);
27
+    corner[7] = max;
28
+}
29
+
30
+bool BoundingBox::inBox(glm::vec3 p) {
31
+    return ((p.y >= corner[0].y) && (p.y <= corner[7].y) && inBoxPlane(p));
32
+}
33
+
34
+bool BoundingBox::inBoxPlane(glm::vec3 p) {
35
+    return ((p.x >= corner[0].x) && (p.x <= corner[7].x) && (p.z >= corner[0].z) && (p.z <= corner[7].z));
36
+}
37
+
38
+void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot) {
39
+    auto startSize = vertices.size();
40
+
41
+    for (auto& c : corner) {
42
+        vertices.emplace_back(VP * glm::vec4(c, 1.0f));
43
+        colorsLine.emplace_back(colorLine);
44
+        colorsPoint.emplace_back(colorDot);
45
+    }
46
+
47
+    indicesLine.emplace_back(startSize);
48
+    indicesLine.emplace_back(startSize + 2);
49
+    indicesLine.emplace_back(startSize + 2);
50
+    indicesLine.emplace_back(startSize + 4);
51
+    indicesLine.emplace_back(startSize + 4);
52
+    indicesLine.emplace_back(startSize + 1);
53
+    indicesLine.emplace_back(startSize + 1);
54
+    indicesLine.emplace_back(startSize + 6);
55
+    indicesLine.emplace_back(startSize + 6);
56
+    indicesLine.emplace_back(startSize + 7);
57
+    indicesLine.emplace_back(startSize + 7);
58
+    indicesLine.emplace_back(startSize + 5);
59
+    indicesLine.emplace_back(startSize + 5);
60
+    indicesLine.emplace_back(startSize + 3);
61
+    indicesLine.emplace_back(startSize + 3);
62
+    indicesLine.emplace_back(startSize);
63
+    indicesLine.emplace_back(startSize);
64
+    indicesLine.emplace_back(startSize + 1);
65
+    indicesLine.emplace_back(startSize + 1);
66
+    indicesLine.emplace_back(startSize + 4);
67
+    indicesLine.emplace_back(startSize + 4);
68
+    indicesLine.emplace_back(startSize + 7);
69
+    indicesLine.emplace_back(startSize + 7);
70
+    indicesLine.emplace_back(startSize + 6);
71
+    indicesLine.emplace_back(startSize + 6);
72
+    indicesLine.emplace_back(startSize + 3);
73
+    indicesLine.emplace_back(startSize + 3);
74
+    indicesLine.emplace_back(startSize + 5);
75
+    indicesLine.emplace_back(startSize + 5);
76
+    indicesLine.emplace_back(startSize + 2);
77
+}
78
+
79
+void BoundingBox::display() {
80
+    if (vertices.size() > 0) {
81
+        Shader::drawGL(vertices, colorsLine, indicesLine, glm::mat4(1.0f), gl::GL_LINES);
82
+        Shader::drawGL(vertices, colorsPoint, glm::mat4(1.0f), gl::GL_POINTS);
83
+
84
+        vertices.clear();
85
+        colorsLine.clear();
86
+        colorsPoint.clear();
87
+        indicesLine.clear();
88
+    }
89
+}
90
+

+ 5
- 0
src/CMakeLists.txt View File

@@ -46,6 +46,7 @@ endif (OPENAL_FOUND)
46 46
 #################################################################
47 47
 
48 48
 # Set Source files
49
+set (SRCS ${SRCS} "BoundingBox.cpp" "../include/BoundingBox.h")
49 50
 set (SRCS ${SRCS} "Camera.cpp" "../include/Camera.h")
50 51
 set (SRCS ${SRCS} "Console.cpp" "../include/Console.h")
51 52
 set (SRCS ${SRCS} "Entity.cpp" "../include/Entity.h")
@@ -113,6 +114,10 @@ check_symbol_exists (DT_DIR "dirent.h" HAVE_DT_DIR)
113 114
 check_include_files (unistd.h HAVE_UNISTD_H)
114 115
 check_function_exists (getcwd HAVE_GETCWD)
115 116
 
117
+# _getcwd() for current working directory in windows
118
+check_include_files (direct.h HAVE_DIRECT_H)
119
+check_function_exists (_getcwd HAVE__GETCWD)
120
+
116 121
 # getenv() for reading environment variables
117 122
 check_include_files (stdlib.h HAVE_STDLIB_H)
118 123
 check_function_exists (getenv HAVE_GETENV)

+ 1
- 1
src/Entity.cpp View File

@@ -61,7 +61,7 @@ void Entity::display(glm::mat4 VP) {
61 61
         orAssertGreaterThan(cacheType, -1);
62 62
     }
63 63
 
64
-    glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
64
+    glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
65 65
     glm::mat4 rotate;
66 66
     if (cacheType == 0) {
67 67
         rotate = glm::rotate(glm::mat4(1.0f), Camera::getRotation().x, glm::vec3(0.0f, 1.0f, 0.0f));

+ 0
- 42
src/RoomData.cpp View File

@@ -16,48 +16,6 @@
16 16
 #include <glbinding/gl/gl.h>
17 17
 #include <glm/gtc/matrix_transform.hpp>
18 18
 
19
-void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot) {
20
-    std::vector<glm::vec3> verts;
21
-    std::vector<glm::vec3> cols;
22
-    std::vector<unsigned short> inds;
23
-
24
-    for (int i = 0; i < 8; i++) {
25
-        verts.push_back(corner[i]);
26
-        cols.push_back(colorLine);
27
-    }
28
-
29
-    inds.push_back(0);
30
-    inds.push_back(2);
31
-    inds.push_back(4);
32
-    inds.push_back(1);
33
-    inds.push_back(6);
34
-    inds.push_back(7);
35
-    inds.push_back(5);
36
-    inds.push_back(3);
37
-    inds.push_back(0);
38
-    inds.push_back(1);
39
-    inds.push_back(4);
40
-    inds.push_back(7);
41
-    inds.push_back(6);
42
-    inds.push_back(3);
43
-    inds.push_back(5);
44
-    inds.push_back(2);
45
-
46
-    Shader::drawGL(verts, cols, inds, VP, gl::GL_LINE_STRIP);
47
-
48
-    cols.clear();
49
-    inds.clear();
50
-
51
-    for (int i = 0; i < 8; i++) {
52
-        cols.push_back(colorDot);
53
-        inds.push_back(i);
54
-    }
55
-
56
-    Shader::drawGL(verts, cols, inds, VP, gl::GL_POINTS);
57
-}
58
-
59
-// ----------------------------------------------------------------------------
60
-
61 19
 StaticModel::StaticModel(glm::vec3 pos, float angle, int i) : id(i), cache(-1) {
62 20
     glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
63 21
     glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0.0f, 1.0f, 0.0f));

+ 2
- 0
src/main.cpp View File

@@ -9,6 +9,7 @@
9 9
 #include <memory>
10 10
 
11 11
 #include "global.h"
12
+#include "BoundingBox.h"
12 13
 #include "Camera.h"
13 14
 #include "Log.h"
14 15
 #include "Menu.h"
@@ -195,6 +196,7 @@ int main(int argc, char* argv[]) {
195 196
 
196 197
 void renderFrame() {
197 198
     Render::display();
199
+    BoundingBox::display();
198 200
     UI::display();
199 201
     Window::swapBuffers();
200 202
     RunTime::updateFPS();

+ 17
- 7
src/utils/filesystem.cpp View File

@@ -16,6 +16,10 @@
16 16
 #include <stdlib.h>
17 17
 #endif
18 18
 
19
+#if defined(HAVE_DIRECT_H) && defined(HAVE__GETCWD)
20
+#include <direct.h>
21
+#endif
22
+
19 23
 #ifdef _WIN32
20 24
 #include <windows.h>
21 25
 #include <shlobj.h>
@@ -28,6 +32,12 @@ std::string getCurrentWorkingDirectory() {
28 32
     orAssertEqual(getcwd(path, 1024), path);
29 33
     return std::string(path);
30 34
 
35
+#elif defined(HAVE_DIRECT_H) && defined(HAVE__GETCWD)
36
+
37
+    char path[1024];
38
+    orAssertEqual(_getcwd(path, 1024), path);
39
+    return std::string(path);
40
+
31 41
 #else
32 42
 
33 43
     orAssert(false);
@@ -37,13 +47,7 @@ std::string getCurrentWorkingDirectory() {
37 47
 }
38 48
 
39 49
 std::string getHomeDirectory() {
40
-#if defined(HAVE_STDLIB_H) && defined(HAVE_GETENV)
41
-
42
-    char* path = getenv("HOME");
43
-    orAssert(path != nullptr);
44
-    return path;
45
-
46
-#elif defined(_WIN32)
50
+#if defined(_WIN32)
47 51
 
48 52
     char path[MAX_PATH];
49 53
     orAssertEqual(SHGetFolderPath(nullptr, CSIDL_PROFILE, nullptr, 0, path), S_OK);
@@ -53,6 +57,12 @@ std::string getHomeDirectory() {
53 57
             path[i] = '/';
54 58
     return std::string(path);
55 59
 
60
+#elif defined(HAVE_STDLIB_H) && defined(HAVE_GETENV)
61
+
62
+    char* path = getenv("HOME");
63
+    orAssert(path != nullptr);
64
+    return path;
65
+
56 66
 #else
57 67
 
58 68
     orAssert(false);

Loading…
Cancel
Save