Browse Source

Clicking on Room Models will now highlight their bounding sphere

Thomas Buck 8 years ago
parent
commit
bbeaf45edc

+ 37
- 0
include/BoundingSphere.h View File

@@ -0,0 +1,37 @@
1
+/*!
2
+ * \file include/BoundingSphere.h
3
+ * \brief 3D Bounding Sphere
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _BOUNDING_SPHERE_H_
9
+#define _BOUNDING_SPHERE_H_
10
+
11
+#include <vector>
12
+
13
+class BoundingSphere {
14
+  public:
15
+    BoundingSphere(glm::vec3 p = glm::vec3(0.0f, 0.0f, 0.0f), float r = 100.0f, int res = 10) : pos(p), radius(r), resolution(res) { }
16
+
17
+    void setPosition(glm::vec3 p) { pos = p; }
18
+    glm::vec3 getPosition() { return pos; }
19
+
20
+    void setRadius(float r) { radius = r; }
21
+    float getRadius() { return radius; }
22
+
23
+    void display(glm::mat4 VP, glm::vec3 color);
24
+
25
+    static void display();
26
+
27
+  private:
28
+    glm::vec3 pos;
29
+    float radius;
30
+    int resolution;
31
+
32
+    static std::vector<glm::vec4> vertices;
33
+    static std::vector<glm::vec3> colors;
34
+};
35
+
36
+#endif
37
+

+ 3
- 4
include/Mesh.h View File

@@ -10,6 +10,7 @@
10 10
 
11 11
 #include <vector>
12 12
 
13
+#include "BoundingSphere.h"
13 14
 #include "system/Shader.h"
14 15
 
15 16
 struct IndexedRectangle {
@@ -42,8 +43,7 @@ class Mesh {
42 43
     void prepare();
43 44
     void display(glm::mat4 MVP, ShaderTexture* shaderTexture = nullptr);
44 45
 
45
-    glm::vec3 getCenter() { return center; }
46
-    float getRadius() { return radius; }
46
+    BoundingSphere& getBoundingSphere() { return sphere; }
47 47
 
48 48
   private:
49 49
     std::vector<unsigned short> indicesBuff;
@@ -56,8 +56,7 @@ class Mesh {
56 56
     std::vector<glm::vec3> colorsBuff;
57 57
     std::vector<unsigned int> colorsIndexBuff;
58 58
 
59
-    glm::vec3 center;
60
-    float radius;
59
+    BoundingSphere sphere;
61 60
 };
62 61
 
63 62
 #endif

+ 1
- 0
include/RoomData.h View File

@@ -18,6 +18,7 @@ class StaticModel {
18 18
 
19 19
     glm::vec3 getCenter();
20 20
     float getRadius();
21
+    void displayBoundingSphere(glm::mat4 VP, glm::vec3 color);
21 22
 
22 23
   private:
23 24
     void find();

+ 3
- 3
include/StaticMesh.h View File

@@ -10,7 +10,8 @@
10 10
 
11 11
 #include <memory>
12 12
 
13
-#include "RoomData.h"
13
+#include "BoundingBox.h"
14
+#include "BoundingSphere.h"
14 15
 
15 16
 class StaticMesh {
16 17
   public:
@@ -19,8 +20,7 @@ class StaticMesh {
19 20
     void display(glm::mat4 MVP);
20 21
     void displayUI();
21 22
 
22
-    glm::vec3 getCenter();
23
-    float getRadius();
23
+    BoundingSphere& getBoundingSphere();
24 24
 
25 25
     int getID() { return id; }
26 26
 

+ 1
- 1
src/BoundingBox.cpp View File

@@ -9,7 +9,7 @@
9 9
 #include "Camera.h"
10 10
 #include "World.h"
11 11
 #include "system/Shader.h"
12
-#include "RoomData.h"
12
+#include "BoundingBox.h"
13 13
 
14 14
 #include <glbinding/gl/gl.h>
15 15
 

+ 61
- 0
src/BoundingSphere.cpp View File

@@ -0,0 +1,61 @@
1
+/*!
2
+ * \file src/BoundingSphere.cpp
3
+ * \brief 3D Axis Aligned Bounding Sphere
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 "BoundingSphere.h"
13
+
14
+#include <glbinding/gl/gl.h>
15
+
16
+std::vector<glm::vec4> BoundingSphere::vertices;
17
+std::vector<glm::vec3> BoundingSphere::colors;
18
+
19
+void BoundingSphere::display(glm::mat4 VP, glm::vec3 color) {
20
+    for (int w = 0; w < resolution; w++) {
21
+        for (int h = (-resolution / 2); h < (resolution / 2); h++){
22
+            float inc1 = (w / float(resolution)) * 2.0f * glm::pi<float>();
23
+            float inc2 = ((w + 1) / float(resolution)) * 2.0f * glm::pi<float>();
24
+            float inc3 = (h / float(resolution)) * glm::pi<float>();
25
+            float inc4 = ((h + 1) / float(resolution)) * glm::pi<float>();
26
+
27
+            float x1 = glm::sin(inc1);
28
+            float y1 = glm::cos(inc1);
29
+            float x2 = glm::sin(inc2);
30
+            float y2 = glm::cos(inc2);
31
+
32
+            float radius1 = radius * glm::cos(inc3);
33
+            float radius2 = radius * glm::cos(inc4);
34
+
35
+            float z1 = radius * glm::sin(inc3);
36
+            float z2 = radius * glm::sin(inc4);
37
+
38
+            vertices.emplace_back(VP * (glm::vec4(radius1 * x1, z1, radius1 * y1, 1.0f) + glm::vec4(pos, 0.0f)));
39
+            vertices.emplace_back(VP * (glm::vec4(radius1 * x2, z1, radius1 * y2, 1.0f) + glm::vec4(pos, 0.0f)));
40
+            vertices.emplace_back(VP * (glm::vec4(radius2 * x2, z2, radius2 * y2, 1.0f) + glm::vec4(pos, 0.0f)));
41
+
42
+            vertices.emplace_back(VP * (glm::vec4(radius1 * x1, z1, radius1 * y1, 1.0f) + glm::vec4(pos, 0.0f)));
43
+            vertices.emplace_back(VP * (glm::vec4(radius2 * x2, z2, radius2 * y2, 1.0f) + glm::vec4(pos, 0.0f)));
44
+            vertices.emplace_back(VP * (glm::vec4(radius2 * x1, z2, radius2 * y1, 1.0f) + glm::vec4(pos, 0.0f)));
45
+
46
+            for (int i = 0; i < 6; i++) {
47
+                colors.emplace_back(color);
48
+            }
49
+        }
50
+
51
+    }
52
+}
53
+
54
+void BoundingSphere::display() {
55
+    if (vertices.size() > 0) {
56
+        Shader::drawGL(vertices, colors, gl::GL_POINTS);
57
+    }
58
+
59
+    vertices.clear();
60
+    colors.clear();
61
+}

+ 1
- 0
src/CMakeLists.txt View File

@@ -47,6 +47,7 @@ endif (OPENAL_FOUND)
47 47
 
48 48
 # Set Source files
49 49
 set (SRCS ${SRCS} "BoundingBox.cpp" "../include/BoundingBox.h")
50
+set (SRCS ${SRCS} "BoundingSphere.cpp" "../include/BoundingSphere.h")
50 51
 set (SRCS ${SRCS} "Camera.cpp" "../include/Camera.h")
51 52
 set (SRCS ${SRCS} "Console.cpp" "../include/Console.h")
52 53
 set (SRCS ${SRCS} "Entity.cpp" "../include/Entity.h")

+ 5
- 2
src/Mesh.cpp View File

@@ -133,8 +133,8 @@ void Mesh::prepare() {
133 133
     verticesColorBuff = std::move(vertCol);
134 134
     colorsBuff = std::move(cols);
135 135
 
136
-    center = average / float(averageCount);
137
-    radius = 0.0f;
136
+    glm::vec3 center = average / float(averageCount);
137
+    float radius = 0.0f;
138 138
     for (auto& vert : verticesBuff) {
139 139
         float dist = glm::distance(center, vert);
140 140
         if (dist > radius) radius = dist;
@@ -143,6 +143,9 @@ void Mesh::prepare() {
143 143
         float dist = glm::distance(center, vert);
144 144
         if (dist > radius) radius = dist;
145 145
     }
146
+
147
+    sphere.setPosition(center);
148
+    sphere.setRadius(radius);
146 149
 }
147 150
 
148 151
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {

+ 2
- 0
src/Render.cpp View File

@@ -11,6 +11,7 @@
11 11
 
12 12
 #include "global.h"
13 13
 #include "BoundingBox.h"
14
+#include "BoundingSphere.h"
14 15
 #include "Camera.h"
15 16
 #include "Log.h"
16 17
 #include "Menu.h"
@@ -91,6 +92,7 @@ void Render::display() {
91 92
         Camera::displayFrustum(VP);
92 93
 
93 94
     BoundingBox::display();
95
+    BoundingSphere::display();
94 96
 
95 97
     if (mode == RenderMode::Wireframe) {
96 98
         gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);

+ 7
- 2
src/RoomData.cpp View File

@@ -34,14 +34,19 @@ void StaticModel::find() {
34 34
 
35 35
 glm::vec3 StaticModel::getCenter() {
36 36
     find();
37
-    glm::vec3 center = World::getStaticMesh(cache).getCenter();
37
+    glm::vec3 center = World::getStaticMesh(cache).getBoundingSphere().getPosition();
38 38
     glm::vec4 tmp = model * glm::vec4(center, 1.0f);
39 39
     return glm::vec3(tmp) / tmp.w;
40 40
 }
41 41
 
42 42
 float StaticModel::getRadius() {
43 43
     find();
44
-    return World::getStaticMesh(cache).getRadius();
44
+    return World::getStaticMesh(cache).getBoundingSphere().getRadius();
45
+}
46
+
47
+void StaticModel::displayBoundingSphere(glm::mat4 VP, glm::vec3 color) {
48
+    find();
49
+    World::getStaticMesh(cache).getBoundingSphere().display(VP * model, color);
45 50
 }
46 51
 
47 52
 void StaticModel::display(glm::mat4 VP) {

+ 2
- 2
src/Selector.cpp View File

@@ -22,7 +22,6 @@ bool Selector::visible = false;
22 22
 
23 23
 static int lastX = -1, lastY = -1;
24 24
 static bool workToDo = false;
25
-static float grabSphere = 102.4f;
26 25
 static bool clickOnGeometry = false, clickOnRoomModels = true, clickOnRoomSprites = true;
27 26
 static bool clickOnSprites = true, clickOnMeshes = false, clickOnModels = false;
28 27
 
@@ -105,7 +104,6 @@ void Selector::display() {
105 104
         }
106 105
     }
107 106
 
108
-    ImGui::SliderFloat("Grab Sphere", &grabSphere, 0.1f, 10240.0f);
109 107
     ImGui::Checkbox("Geometry", &clickOnGeometry);
110 108
     ImGui::SameLine();
111 109
     ImGui::Checkbox("RoomModels", &clickOnRoomModels);
@@ -140,6 +138,8 @@ void Selector::display() {
140 138
         ImGui::Text("Intersect Norm: (%.2f %.2f %.2f)", lastIntersectNorm.x, lastIntersectNorm.y, lastIntersectNorm.z);
141 139
         ImGui::Text("Last Room: %lu", lastRoom);
142 140
         ImGui::Text("Last RoomModel: %lu", lastModel);
141
+
142
+        World::getRoom(lastRoom).getModel(lastModel).displayBoundingSphere(Camera::getProjectionMatrix() * Camera::getViewMatrix(), glm::vec3(1.0f, 0.0f, 0.0f));
143 143
     }
144 144
 
145 145
     ImGui::End();

+ 2
- 6
src/StaticMesh.cpp View File

@@ -13,12 +13,8 @@
13 13
 
14 14
 bool StaticMesh::showBoundingBox = false;
15 15
 
16
-glm::vec3 StaticMesh::getCenter() {
17
-    return World::getMesh(mesh).getCenter();
18
-}
19
-
20
-float StaticMesh::getRadius() {
21
-    return World::getMesh(mesh).getRadius();
16
+BoundingSphere& StaticMesh::getBoundingSphere() {
17
+    return World::getMesh(mesh).getBoundingSphere();
22 18
 }
23 19
 
24 20
 void StaticMesh::display(glm::mat4 MVP) {

Loading…
Cancel
Save