소스 검색

Can click on Room Models

Thomas Buck 9 년 전
부모
커밋
30917cecec
12개의 변경된 파일156개의 추가작업 그리고 18개의 파일을 삭제
  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 파일 보기

@@ -47,10 +47,6 @@ class Camera {
47 47
     static bool boxInFrustum(BoundingBox b);
48 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 50
   private:
55 51
     static void calculateFrustumPlanes();
56 52
 
@@ -64,6 +60,10 @@ class Camera {
64 60
     static bool updateViewFrustum, dirty, movingFaster;
65 61
     static bool keepInRoom;
66 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 69
 #endif

+ 6
- 0
include/Mesh.h 파일 보기

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

+ 4
- 1
include/Room.h 파일 보기

@@ -45,9 +45,12 @@ class Room {
45 45
     int getIndex() { return roomIndex; }
46 46
 
47 47
     void addSprite(RoomSprite* s) { sprites.emplace_back(s); }
48
-    void addModel(StaticModel* s) { models.emplace_back(s); }
49 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 54
     void addPortal(Portal* p) { portals.emplace_back(p); }
52 55
     unsigned long sizePortals() { return portals.size(); }
53 56
     Portal& getPortal(unsigned long index) { return *portals.at(index); }

+ 5
- 0
include/RoomData.h 파일 보기

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

+ 3
- 0
include/StaticMesh.h 파일 보기

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

+ 1
- 1
src/CMakeLists.txt 파일 보기

@@ -52,7 +52,7 @@ set (SRCS ${SRCS} "Console.cpp" "../include/Console.h")
52 52
 set (SRCS ${SRCS} "Entity.cpp" "../include/Entity.h")
53 53
 set (SRCS ${SRCS} "Game.cpp" "../include/Game.h")
54 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 56
 set (SRCS ${SRCS} "Menu.cpp" "../include/Menu.h")
57 57
 set (SRCS ${SRCS} "Mesh.cpp" "../include/Mesh.h")
58 58
 set (SRCS ${SRCS} "Render.cpp" "../include/Render.h")

+ 22
- 0
src/Mesh.cpp 파일 보기

@@ -10,6 +10,7 @@
10 10
 #include "Mesh.h"
11 11
 
12 12
 #include <glbinding/gl/gl.h>
13
+#include <glm/glm.hpp>
13 14
 
14 15
 Mesh::Mesh(const std::vector<glm::vec3>& vert,
15 16
            const std::vector<IndexedRectangle>& rect,
@@ -57,6 +58,9 @@ void Mesh::prepare() {
57 58
     std::vector<glm::vec2> uvBuff;
58 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 64
     int vertIndex = 0;
61 65
     for (int i = 0; i < indicesBuff.size(); i++) {
62 66
         unsigned int texture = TextureManager::getTile(texturesBuff.at(i)).getTexture();
@@ -70,6 +74,10 @@ void Mesh::prepare() {
70 74
             vert.push_back(verticesBuff.at(vertIndex + v));
71 75
             uvBuff.push_back(TextureManager::getTile(texturesBuff.at(i)).getUV(v));
72 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 83
         if (indicesBuff.at(i) == 0) {
@@ -105,6 +113,9 @@ void Mesh::prepare() {
105 113
             vertCol.push_back(verticesColorBuff.at(vertIndex + v));
106 114
             glm::vec4 c = TextureManager::getPalette(colorsIndexBuff.at(i));
107 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 121
         if (indicesColorBuff.at(i) == 0) {
@@ -121,6 +132,17 @@ void Mesh::prepare() {
121 132
     indicesColorBuff = std::move(indCol);
122 133
     verticesColorBuff = std::move(vertCol);
123 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 148
 void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {

+ 1
- 1
src/Room.cpp 파일 보기

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

+ 16
- 1
src/RoomData.cpp 파일 보기

@@ -21,7 +21,7 @@ StaticModel::StaticModel(glm::vec3 pos, float angle, int i) : id(i), cache(-1) {
21 21
     model = translate * rotate;
22 22
 }
23 23
 
24
-void StaticModel::display(glm::mat4 VP) {
24
+void StaticModel::find() {
25 25
     if (cache < 0) {
26 26
         for (int i = 0; i < World::sizeStaticMesh(); i++) {
27 27
             if (World::getStaticMesh(i).getID() == id) {
@@ -30,7 +30,22 @@ void StaticModel::display(glm::mat4 VP) {
30 30
         }
31 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 49
     World::getStaticMesh(cache).display(VP * model);
35 50
 }
36 51
 

+ 81
- 6
src/Selector.cpp 파일 보기

@@ -12,13 +12,19 @@
12 12
 #include "global.h"
13 13
 #include "Camera.h"
14 14
 #include "Log.h"
15
+#include "World.h"
15 16
 #include "system/Window.h"
16 17
 #include "Selector.h"
17 18
 
19
+#include <glm/gtx/intersect.hpp>
20
+
18 21
 bool Selector::visible = false;
19 22
 
20 23
 static int lastX = -1, lastY = -1;
21 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 29
 void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
24 30
     if ((button == leftmouseKey) && (!released)) {
@@ -43,8 +49,12 @@ void Selector::display() {
43 49
     }
44 50
 
45 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 56
     if (workToDo) {
57
+        // Calculate click ray
48 58
         glm::vec2 normalized = glm::vec2((2.0f * lastX) / Window::getSize().x - 1.0f,
49 59
                                          1.0f - (2.0f * lastY) / Window::getSize().y);
50 60
         glm::vec4 rayClip(normalized.x, normalized.y, -1.0f, 1.0f);
@@ -53,18 +63,83 @@ void Selector::display() {
53 63
         rayWorld = glm::vec3(glm::inverse(Camera::getViewMatrix()) * rayEye);
54 64
         rayWorld = glm::normalize(rayWorld);
55 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 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 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 145
     ImGui::End();

+ 8
- 0
src/StaticMesh.cpp 파일 보기

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

+ 5
- 4
src/UI.cpp 파일 보기

@@ -252,10 +252,11 @@ void UI::display() {
252 252
                          ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
253 253
                          | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
254 254
                          | ImGuiWindowFlags_AlwaysAutoResize)) {
255
-#ifdef DEBUG
256
-            ImGui::Text("%d FPS  %lu CPF", RunTime::getFPS(), RunTime::getCallCount());
257
-#else
258 255
             ImGui::Text("%d FPS", RunTime::getFPS());
256
+
257
+#ifdef DEBUG
258
+            ImGui::SameLine();
259
+            ImGui::Text("%lu CPF", RunTime::getCallCount());
259 260
 #endif
260 261
 
261 262
             ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
@@ -264,7 +265,7 @@ void UI::display() {
264 265
 
265 266
             auto window = ImGui::GetWindowSize();
266 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 270
         ImGui::End();
270 271
     }

Loading…
취소
저장