Browse Source

Can click on Room Models

Thomas Buck 9 years ago
parent
commit
30917cecec
12 changed files with 156 additions and 18 deletions
  1. 4
    4
      include/Camera.h
  2. 6
    0
      include/Mesh.h
  3. 4
    1
      include/Room.h
  4. 5
    0
      include/RoomData.h
  5. 3
    0
      include/StaticMesh.h
  6. 1
    1
      src/CMakeLists.txt
  7. 22
    0
      src/Mesh.cpp
  8. 1
    1
      src/Room.cpp
  9. 16
    1
      src/RoomData.cpp
  10. 81
    6
      src/Selector.cpp
  11. 8
    0
      src/StaticMesh.cpp
  12. 5
    4
      src/UI.cpp

+ 4
- 4
include/Camera.h View File

47
     static bool boxInFrustum(BoundingBox b);
47
     static bool boxInFrustum(BoundingBox b);
48
     static void displayFrustum(glm::mat4 MVP);
48
     static void displayFrustum(glm::mat4 MVP);
49
 
49
 
50
-    static const float fov;
51
-    static const float nearDist;
52
-    static const float farDist;
53
-
54
   private:
50
   private:
55
     static void calculateFrustumPlanes();
51
     static void calculateFrustumPlanes();
56
 
52
 
64
     static bool updateViewFrustum, dirty, movingFaster;
60
     static bool updateViewFrustum, dirty, movingFaster;
65
     static bool keepInRoom;
61
     static bool keepInRoom;
66
     static int room;
62
     static int room;
63
+
64
+    static const float fov;
65
+    static const float nearDist;
66
+    static const float farDist;
67
 };
67
 };
68
 
68
 
69
 #endif
69
 #endif

+ 6
- 0
include/Mesh.h View File

42
     void prepare();
42
     void prepare();
43
     void display(glm::mat4 MVP, ShaderTexture* shaderTexture = nullptr);
43
     void display(glm::mat4 MVP, ShaderTexture* shaderTexture = nullptr);
44
 
44
 
45
+    glm::vec3 getCenter() { return center; }
46
+    float getRadius() { return radius; }
47
+
45
   private:
48
   private:
46
     std::vector<unsigned short> indicesBuff;
49
     std::vector<unsigned short> indicesBuff;
47
     std::vector<glm::vec3> verticesBuff;
50
     std::vector<glm::vec3> verticesBuff;
52
     std::vector<glm::vec3> verticesColorBuff;
55
     std::vector<glm::vec3> verticesColorBuff;
53
     std::vector<glm::vec3> colorsBuff;
56
     std::vector<glm::vec3> colorsBuff;
54
     std::vector<unsigned int> colorsIndexBuff;
57
     std::vector<unsigned int> colorsIndexBuff;
58
+
59
+    glm::vec3 center;
60
+    float radius;
55
 };
61
 };
56
 
62
 
57
 #endif
63
 #endif

+ 4
- 1
include/Room.h View File

45
     int getIndex() { return roomIndex; }
45
     int getIndex() { return roomIndex; }
46
 
46
 
47
     void addSprite(RoomSprite* s) { sprites.emplace_back(s); }
47
     void addSprite(RoomSprite* s) { sprites.emplace_back(s); }
48
-    void addModel(StaticModel* s) { models.emplace_back(s); }
49
     void addSector(Sector* s) { sectors.emplace_back(s); }
48
     void addSector(Sector* s) { sectors.emplace_back(s); }
50
 
49
 
50
+    void addModel(StaticModel* s) { models.emplace_back(s); }
51
+    unsigned long sizeModels() { return models.size(); }
52
+    StaticModel& getModel(unsigned long index) { return *models.at(index); }
53
+
51
     void addPortal(Portal* p) { portals.emplace_back(p); }
54
     void addPortal(Portal* p) { portals.emplace_back(p); }
52
     unsigned long sizePortals() { return portals.size(); }
55
     unsigned long sizePortals() { return portals.size(); }
53
     Portal& getPortal(unsigned long index) { return *portals.at(index); }
56
     Portal& getPortal(unsigned long index) { return *portals.at(index); }

+ 5
- 0
include/RoomData.h View File

16
     void display(glm::mat4 VP);
16
     void display(glm::mat4 VP);
17
     void displayUI();
17
     void displayUI();
18
 
18
 
19
+    glm::vec3 getCenter();
20
+    float getRadius();
21
+
19
   private:
22
   private:
23
+    void find();
24
+
20
     int id;
25
     int id;
21
     int cache;
26
     int cache;
22
     glm::mat4 model;
27
     glm::mat4 model;

+ 3
- 0
include/StaticMesh.h View File

19
     void display(glm::mat4 MVP);
19
     void display(glm::mat4 MVP);
20
     void displayUI();
20
     void displayUI();
21
 
21
 
22
+    glm::vec3 getCenter();
23
+    float getRadius();
24
+
22
     int getID() { return id; }
25
     int getID() { return id; }
23
 
26
 
24
     static void setShowBoundingBox(bool s) { showBoundingBox = s; }
27
     static void setShowBoundingBox(bool s) { showBoundingBox = s; }

+ 1
- 1
src/CMakeLists.txt View File

52
 set (SRCS ${SRCS} "Entity.cpp" "../include/Entity.h")
52
 set (SRCS ${SRCS} "Entity.cpp" "../include/Entity.h")
53
 set (SRCS ${SRCS} "Game.cpp" "../include/Game.h")
53
 set (SRCS ${SRCS} "Game.cpp" "../include/Game.h")
54
 set (SRCS ${SRCS} "Log.cpp" "../include/Log.h")
54
 set (SRCS ${SRCS} "Log.cpp" "../include/Log.h")
55
-set (SRCS ${SRCS} "main.cpp")
55
+set (SRCS ${SRCS} "main.cpp" "../include/global.h")
56
 set (SRCS ${SRCS} "Menu.cpp" "../include/Menu.h")
56
 set (SRCS ${SRCS} "Menu.cpp" "../include/Menu.h")
57
 set (SRCS ${SRCS} "Mesh.cpp" "../include/Mesh.h")
57
 set (SRCS ${SRCS} "Mesh.cpp" "../include/Mesh.h")
58
 set (SRCS ${SRCS} "Render.cpp" "../include/Render.h")
58
 set (SRCS ${SRCS} "Render.cpp" "../include/Render.h")

+ 22
- 0
src/Mesh.cpp View File

10
 #include "Mesh.h"
10
 #include "Mesh.h"
11
 
11
 
12
 #include <glbinding/gl/gl.h>
12
 #include <glbinding/gl/gl.h>
13
+#include <glm/glm.hpp>
13
 
14
 
14
 Mesh::Mesh(const std::vector<glm::vec3>& vert,
15
 Mesh::Mesh(const std::vector<glm::vec3>& vert,
15
            const std::vector<IndexedRectangle>& rect,
16
            const std::vector<IndexedRectangle>& rect,
57
     std::vector<glm::vec2> uvBuff;
58
     std::vector<glm::vec2> uvBuff;
58
     std::vector<unsigned int> tex;
59
     std::vector<unsigned int> tex;
59
 
60
 
61
+    glm::vec3 average(0.0f, 0.0f, 0.0f);
62
+    unsigned long averageCount = 0;
63
+
60
     int vertIndex = 0;
64
     int vertIndex = 0;
61
     for (int i = 0; i < indicesBuff.size(); i++) {
65
     for (int i = 0; i < indicesBuff.size(); i++) {
62
         unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
66
         unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
70
             vert.push_back(verticesBuff.at(vertIndex + v));
74
             vert.push_back(verticesBuff.at(vertIndex + v));
71
             uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
75
             uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
72
             tex.push_back(texture);
76
             tex.push_back(texture);
77
+
78
+            // Calculate quick-n-dirty center point of mesh
79
+            average += verticesBuff.at(vertIndex + v);
80
+            averageCount++;
73
         }
81
         }
74
 
82
 
75
         if (indicesBuff.at(i) == 0) {
83
         if (indicesBuff.at(i) == 0) {
105
             vertCol.push_back(verticesColorBuff.at(vertIndex + v));
113
             vertCol.push_back(verticesColorBuff.at(vertIndex + v));
106
             glm::vec4 c = TextureManager::getPalette(colorsIndexBuff.at(i));
114
             glm::vec4 c = TextureManager::getPalette(colorsIndexBuff.at(i));
107
             cols.push_back(glm::vec3(c.x, c.y, c.z));
115
             cols.push_back(glm::vec3(c.x, c.y, c.z));
116
+
117
+            average += verticesColorBuff.at(vertIndex + v);
118
+            averageCount++;
108
         }
119
         }
109
 
120
 
110
         if (indicesColorBuff.at(i) == 0) {
121
         if (indicesColorBuff.at(i) == 0) {
121
     indicesColorBuff = std::move(indCol);
132
     indicesColorBuff = std::move(indCol);
122
     verticesColorBuff = std::move(vertCol);
133
     verticesColorBuff = std::move(vertCol);
123
     colorsBuff = std::move(cols);
134
     colorsBuff = std::move(cols);
135
+
136
+    center = average / float(averageCount);
137
+    radius = 0.0f;
138
+    for (auto& vert : verticesBuff) {
139
+        float dist = glm::distance(center, vert);
140
+        if (dist > radius) radius = dist;
141
+    }
142
+    for (auto& vert : verticesColorBuff) {
143
+        float dist = glm::distance(center, vert);
144
+        if (dist > radius) radius = dist;
145
+    }
124
 }
146
 }
125
 
147
 
126
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
148
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {

+ 1
- 1
src/Room.cpp View File

15
 #include <glm/gtx/intersect.hpp>
15
 #include <glm/gtx/intersect.hpp>
16
 
16
 
17
 bool Room::showBoundingBox = false;
17
 bool Room::showBoundingBox = false;
18
-bool Room::showRoomModels = false;
18
+bool Room::showRoomModels = true;
19
 bool Room::showRoomSprites = true;
19
 bool Room::showRoomSprites = true;
20
 bool Room::showRoomGeometry = true;
20
 bool Room::showRoomGeometry = true;
21
 
21
 

+ 16
- 1
src/RoomData.cpp View File

21
     model = translate * rotate;
21
     model = translate * rotate;
22
 }
22
 }
23
 
23
 
24
-void StaticModel::display(glm::mat4 VP) {
24
+void StaticModel::find() {
25
     if (cache < 0) {
25
     if (cache < 0) {
26
         for (int i = 0; i < World::sizeStaticMesh(); i++) {
26
         for (int i = 0; i < World::sizeStaticMesh(); i++) {
27
             if (World::getStaticMesh(i).getID() == id) {
27
             if (World::getStaticMesh(i).getID() == id) {
30
         }
30
         }
31
         orAssertGreaterThanEqual(cache, 0);
31
         orAssertGreaterThanEqual(cache, 0);
32
     }
32
     }
33
+}
34
+
35
+glm::vec3 StaticModel::getCenter() {
36
+    find();
37
+    glm::vec3 center = World::getStaticMesh(cache).getCenter();
38
+    glm::vec4 tmp = model * glm::vec4(center, 1.0f);
39
+    return glm::vec3(tmp) / tmp.w;
40
+}
33
 
41
 
42
+float StaticModel::getRadius() {
43
+    find();
44
+    return World::getStaticMesh(cache).getRadius();
45
+}
46
+
47
+void StaticModel::display(glm::mat4 VP) {
48
+    find();
34
     World::getStaticMesh(cache).display(VP * model);
49
     World::getStaticMesh(cache).display(VP * model);
35
 }
50
 }
36
 
51
 

+ 81
- 6
src/Selector.cpp View File

12
 #include "global.h"
12
 #include "global.h"
13
 #include "Camera.h"
13
 #include "Camera.h"
14
 #include "Log.h"
14
 #include "Log.h"
15
+#include "World.h"
15
 #include "system/Window.h"
16
 #include "system/Window.h"
16
 #include "Selector.h"
17
 #include "Selector.h"
17
 
18
 
19
+#include <glm/gtx/intersect.hpp>
20
+
18
 bool Selector::visible = false;
21
 bool Selector::visible = false;
19
 
22
 
20
 static int lastX = -1, lastY = -1;
23
 static int lastX = -1, lastY = -1;
21
 static bool workToDo = false;
24
 static bool workToDo = false;
25
+static float grabSphere = 102.4f;
26
+static bool clickOnGeometry = false, clickOnRoomModels = true, clickOnRoomSprites = true;
27
+static bool clickOnSprites = true, clickOnMeshes = false, clickOnModels = false;
22
 
28
 
23
 void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
29
 void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
24
     if ((button == leftmouseKey) && (!released)) {
30
     if ((button == leftmouseKey) && (!released)) {
43
     }
49
     }
44
 
50
 
45
     static glm::vec3 rayWorld;
51
     static glm::vec3 rayWorld;
52
+    static glm::vec3 lastIntersectPos, lastIntersectNorm;
53
+    static unsigned long lastRoom, lastModel;
54
+    static bool foundSomething = false;
46
 
55
 
47
     if (workToDo) {
56
     if (workToDo) {
57
+        // Calculate click ray
48
         glm::vec2 normalized = glm::vec2((2.0f * lastX) / Window::getSize().x - 1.0f,
58
         glm::vec2 normalized = glm::vec2((2.0f * lastX) / Window::getSize().x - 1.0f,
49
                                          1.0f - (2.0f * lastY) / Window::getSize().y);
59
                                          1.0f - (2.0f * lastY) / Window::getSize().y);
50
         glm::vec4 rayClip(normalized.x, normalized.y, -1.0f, 1.0f);
60
         glm::vec4 rayClip(normalized.x, normalized.y, -1.0f, 1.0f);
53
         rayWorld = glm::vec3(glm::inverse(Camera::getViewMatrix()) * rayEye);
63
         rayWorld = glm::vec3(glm::inverse(Camera::getViewMatrix()) * rayEye);
54
         rayWorld = glm::normalize(rayWorld);
64
         rayWorld = glm::normalize(rayWorld);
55
         workToDo = false;
65
         workToDo = false;
66
+
67
+        // Check for any intersections with object bounding spheres
68
+        if (clickOnModels) {
69
+
70
+        }
71
+
72
+        if (clickOnMeshes) {
73
+
74
+        }
75
+
76
+        if (clickOnSprites) {
77
+
78
+        }
79
+
80
+        if (clickOnRoomModels) {
81
+            for (unsigned long i = 0; i < World::sizeRoom(); i++) {
82
+                Room& r = World::getRoom(i);
83
+                for (unsigned long j = 0; j < r.sizeModels(); j++) {
84
+                    StaticModel& sm = r.getModel(j);
85
+                    glm::vec3 pos, norm;
86
+                    if (glm::intersectRaySphere(Camera::getPosition(), rayWorld, sm.getCenter(), sm.getRadius(),
87
+                                                pos, norm)) {
88
+                        //! \fixme This is not enough. Should be depth sorted?!
89
+                        lastRoom = i;
90
+                        lastModel = j;
91
+                        lastIntersectPos = pos;
92
+                        lastIntersectNorm = norm;
93
+                        foundSomething = true;
94
+                    }
95
+                }
96
+            }
97
+        }
98
+
99
+        if (clickOnRoomSprites) {
100
+
101
+        }
102
+
103
+        if (clickOnGeometry) {
104
+
105
+        }
106
+    }
107
+
108
+    ImGui::SliderFloat("Grab Sphere", &grabSphere, 0.1f, 10240.0f);
109
+    ImGui::Checkbox("Geometry", &clickOnGeometry);
110
+    ImGui::SameLine();
111
+    ImGui::Checkbox("RoomModels", &clickOnRoomModels);
112
+    ImGui::SameLine();
113
+    ImGui::Checkbox("RoomSprites", &clickOnRoomSprites);
114
+    ImGui::Checkbox("Sprites", &clickOnSprites);
115
+    ImGui::SameLine();
116
+    ImGui::Checkbox("Meshes", &clickOnMeshes);
117
+    ImGui::SameLine();
118
+    ImGui::Checkbox("Models", &clickOnModels);
119
+    ImGui::SameLine();
120
+    if (ImGui::Button("Hide Selector")) {
121
+        visible = false;
56
     }
122
     }
123
+    ImGui::Separator();
124
+
125
+    // Not yet implemented!
126
+    clickOnModels = false;
127
+    clickOnMeshes = false;
128
+    clickOnSprites = false;
129
+    clickOnRoomSprites = false;
130
+    clickOnGeometry = false;
57
 
131
 
58
-    ImGui::Text("Screenspace: (%d %d)", lastX, lastY);
59
     ImGui::Text("Camera: (%.2f %.2f %.2f)", Camera::getPosition().x, Camera::getPosition().y, Camera::getPosition().z);
132
     ImGui::Text("Camera: (%.2f %.2f %.2f)", Camera::getPosition().x, Camera::getPosition().y, Camera::getPosition().z);
60
-    if ((lastX < 0) || (lastY < 0)) {
61
-        ImGui::Text("Normalized Ray: (? ? ?)");
62
-    } else {
133
+    ImGui::Text("Last click: (%d %d)", lastX, lastY);
134
+    if ((lastX >= 0) && (lastY >= 0)) {
63
         ImGui::Text("Normalized Ray: (%.3f %.3f %.3f)", rayWorld.x, rayWorld.y, rayWorld.z);
135
         ImGui::Text("Normalized Ray: (%.3f %.3f %.3f)", rayWorld.x, rayWorld.y, rayWorld.z);
64
     }
136
     }
65
 
137
 
66
-    if (ImGui::Button("Hide Selector")) {
67
-        visible = false;
138
+    if (foundSomething) {
139
+        ImGui::Text("Intersect Pos: (%.2f %.2f %.2f)", lastIntersectPos.x, lastIntersectPos.y, lastIntersectPos.z);
140
+        ImGui::Text("Intersect Norm: (%.2f %.2f %.2f)", lastIntersectNorm.x, lastIntersectNorm.y, lastIntersectNorm.z);
141
+        ImGui::Text("Last Room: %lu", lastRoom);
142
+        ImGui::Text("Last RoomModel: %lu", lastModel);
68
     }
143
     }
69
 
144
 
70
     ImGui::End();
145
     ImGui::End();

+ 8
- 0
src/StaticMesh.cpp View File

13
 
13
 
14
 bool StaticMesh::showBoundingBox = false;
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();
22
+}
23
+
16
 void StaticMesh::display(glm::mat4 MVP) {
24
 void StaticMesh::display(glm::mat4 MVP) {
17
     World::getMesh(mesh).display(MVP);
25
     World::getMesh(mesh).display(MVP);
18
 
26
 

+ 5
- 4
src/UI.cpp View File

252
                          ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
252
                          ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
253
                          | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
253
                          | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
254
                          | ImGuiWindowFlags_AlwaysAutoResize)) {
254
                          | ImGuiWindowFlags_AlwaysAutoResize)) {
255
-#ifdef DEBUG
256
-            ImGui::Text("%d FPS  %lu CPF", RunTime::getFPS(), RunTime::getCallCount());
257
-#else
258
             ImGui::Text("%d FPS", RunTime::getFPS());
255
             ImGui::Text("%d FPS", RunTime::getFPS());
256
+
257
+#ifdef DEBUG
258
+            ImGui::SameLine();
259
+            ImGui::Text("%lu CPF", RunTime::getCallCount());
259
 #endif
260
 #endif
260
 
261
 
261
             ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
262
             ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
264
 
265
 
265
             auto window = ImGui::GetWindowSize();
266
             auto window = ImGui::GetWindowSize();
266
             auto screen = Window::getSize();
267
             auto screen = Window::getSize();
267
-            //ImGui::SetWindowPos(ImVec2(10, screen.y - window.y - 10));
268
+            ImGui::SetWindowPos(ImVec2(10, screen.y - window.y - 10));
268
         }
269
         }
269
         ImGui::End();
270
         ImGui::End();
270
     }
271
     }

Loading…
Cancel
Save