Selaa lähdekoodia

Selector ray casting

Thomas Buck 9 vuotta sitten
vanhempi
commit
6a51c7c010
7 muutettua tiedostoa jossa 43 lisäystä ja 36 poistoa
  1. 1
    0
      .gitignore
  2. 3
    0
      ChangeLog.md
  3. 4
    5
      include/Camera.h
  4. 4
    17
      src/Camera.cpp
  5. 0
    5
      src/Render.cpp
  6. 30
    5
      src/Selector.cpp
  7. 1
    4
      src/UI.cpp

+ 1
- 0
.gitignore Näytä tiedosto

@@ -4,3 +4,4 @@
4 4
 Icon

5 5
 
6 6
 build*
7
+.idea

+ 3
- 0
ChangeLog.md Näytä tiedosto

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.4) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20150803 ]
6
+    * Started working on ray casting for mouse object selection
7
+
5 8
     [ 20150801 ]
6 9
     * Updated to current Imgui version (now using indexed rendering)
7 10
     * Added mouse selection window (not yet doing anything useful)

+ 4
- 5
include/Camera.h Näytä tiedosto

@@ -41,16 +41,15 @@ class Camera {
41 41
     static void setRoom(int r) { if (room != r) dirty = true; room = r; }
42 42
     static int getRoom() { return room; }
43 43
 
44
-    static void setShowOverlay(bool s) { showOverlay = s; }
45
-    static bool getShowOverlay() { return showOverlay; }
46
-
47 44
     static void setKeepInRoom(bool k) { keepInRoom = k; }
48 45
     static bool getKeepInRoom() { return keepInRoom; }
49 46
 
50 47
     static bool boxInFrustum(BoundingBox b);
51 48
     static void displayFrustum(glm::mat4 MVP);
52 49
 
53
-    static void displayUI();
50
+    static const float fov;
51
+    static const float nearDist;
52
+    static const float farDist;
54 53
 
55 54
   private:
56 55
     static void calculateFrustumPlanes();
@@ -62,7 +61,7 @@ class Camera {
62 61
     static glm::mat4 projection;
63 62
     static glm::mat4 view;
64 63
     static float rotationDeltaX, rotationDeltaY;
65
-    static bool updateViewFrustum, dirty, showOverlay, movingFaster;
64
+    static bool updateViewFrustum, dirty, movingFaster;
66 65
     static bool keepInRoom;
67 66
     static int room;
68 67
 };

+ 4
- 17
src/Camera.cpp Näytä tiedosto

@@ -37,9 +37,10 @@ static bool equal(glm::vec3 a, float b) {
37 37
 
38 38
 // ----------------------------------------------------------------------------
39 39
 
40
-const static float fov = 45.0f;
41
-const static float nearDist = 0.1f;
42
-const static float farDist = 75000.0f;
40
+const float Camera::fov = 45.0f;
41
+const float Camera::nearDist = 0.1f;
42
+const float Camera::farDist = 75000.0f;
43
+
43 44
 const static float maxSpeed = 3072.0f;
44 45
 const static float controllerDeadZone = 0.33f;
45 46
 const static float controllerViewFactor = glm::pi<float>();
@@ -61,7 +62,6 @@ float Camera::rotationDeltaX = 0.75f;
61 62
 float Camera::rotationDeltaY = 0.75f;
62 63
 bool Camera::updateViewFrustum = true;
63 64
 bool Camera::dirty = true;
64
-bool Camera::showOverlay = false;
65 65
 bool Camera::movingFaster = false;
66 66
 bool Camera::keepInRoom = false;
67 67
 int Camera::room = -1;
@@ -216,19 +216,6 @@ bool Camera::update() {
216 216
     return updateViewFrustum;
217 217
 }
218 218
 
219
-void Camera::displayUI() {
220
-    if (!showOverlay)
221
-        return;
222
-
223
-    if (ImGui::Begin("Camera Look-At Overlay", &showOverlay, ImVec2(0, 0), -1.0f,
224
-                     ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
225
-                     | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
226
-                     | ImGuiWindowFlags_AlwaysAutoResize)) {
227
-        // TODO
228
-    }
229
-    ImGui::End();
230
-}
231
-
232 219
 // ----------------------------------------------------------------------------
233 220
 
234 221
 class FrustumPlane {

+ 0
- 5
src/Render.cpp Näytä tiedosto

@@ -274,11 +274,6 @@ void Render::displayUI() {
274 274
         ImGui::SameLine();
275 275
         ImGui::Checkbox("Show Frustum##camera", &displayViewFrustum);
276 276
         ImGui::SameLine();
277
-        bool showOverlay = Camera::getShowOverlay();
278
-        if (ImGui::Checkbox("Overlay##camera", &showOverlay)) {
279
-            Camera::setShowOverlay(showOverlay);
280
-        }
281
-        ImGui::SameLine();
282 277
         bool keepInRoom = Camera::getKeepInRoom();
283 278
         if (ImGui::Checkbox("Keep in Room##camera", &keepInRoom)) {
284 279
             Camera::setKeepInRoom(keepInRoom);

+ 30
- 5
src/Selector.cpp Näytä tiedosto

@@ -2,23 +2,34 @@
2 2
  * \file src/Selector.cpp
3 3
  * \brief Selector Window
4 4
  *
5
+ * http://antongerdelan.net/opengl/raycasting.html
6
+ *
5 7
  * \author xythobuz
6 8
  */
7 9
 
8 10
 #include "imgui/imgui.h"
9 11
 
10 12
 #include "global.h"
13
+#include "Camera.h"
11 14
 #include "Log.h"
15
+#include "system/Window.h"
12 16
 #include "Selector.h"
13 17
 
14 18
 bool Selector::visible = false;
15 19
 
16
-static unsigned int lastX = -1, lastY = -1;
20
+static int lastX = -1, lastY = -1;
21
+static bool workToDo = false;
17 22
 
18 23
 void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
19 24
     if ((button == leftmouseKey) && (!released)) {
20 25
         lastX = x;
21 26
         lastY = y;
27
+
28
+        if (workToDo) {
29
+            Log::get(LOG_DEBUG) << "Selector missed mouse click event!" << Log::endl;
30
+        }
31
+
32
+        workToDo = true;
22 33
     }
23 34
 }
24 35
 
@@ -26,16 +37,30 @@ void Selector::display() {
26 37
     if (!visible)
27 38
         return;
28 39
 
29
-    if (!ImGui::Begin("Object Selector", &visible, ImVec2(300, 300))) {
40
+    if (!ImGui::Begin("Object Selector", &visible, ImVec2(500, 200))) {
30 41
         ImGui::End();
31 42
         return;
32 43
     }
33 44
 
34
-    ImGui::Text("Last Click (Screen): (%d %d)", lastX, lastY);
45
+    static glm::vec3 rayWorld;
46
+
47
+    if (workToDo) {
48
+        glm::vec2 normalized = glm::vec2((2.0f * lastX) / Window::getSize().x - 1.0f,
49
+                                         1.0f - (2.0f * lastY) / Window::getSize().y);
50
+        glm::vec4 rayClip(normalized.x, normalized.y, -1.0f, 1.0f);
51
+        glm::vec4 rayEye(glm::inverse(Camera::getProjectionMatrix()) * rayClip);
52
+        rayEye = glm::vec4(rayEye.x, rayEye.y, -1.0f, 0.0f);
53
+        rayWorld = glm::vec3(glm::inverse(Camera::getViewMatrix()) * rayEye);
54
+        rayWorld = glm::normalize(rayWorld);
55
+        workToDo = false;
56
+    }
57
+
58
+    ImGui::Text("Screenspace: (%d %d)", lastX, lastY);
59
+    ImGui::Text("Camera: (%.2f %.2f %.2f)", Camera::getPosition().x, Camera::getPosition().y, Camera::getPosition().z);
35 60
     if ((lastX < 0) || (lastY < 0)) {
36
-        ImGui::Text("Last Click (World): (? ?)");
61
+        ImGui::Text("Normalized Ray: (? ? ?)");
37 62
     } else {
38
-
63
+        ImGui::Text("Normalized Ray: (%.3f %.3f %.3f)", rayWorld.x, rayWorld.y, rayWorld.z);
39 64
     }
40 65
 
41 66
     if (ImGui::Button("Hide Selector")) {

+ 1
- 4
src/UI.cpp Näytä tiedosto

@@ -186,7 +186,7 @@ void UI::eventsFinished() {
186 186
     while (!keyboardEvents.empty()) {
187 187
         auto i = keyboardEvents.front();
188 188
 
189
-        if (!(visible || Console::isVisible() || Menu::isVisible() || Selector::isVisible())) {
189
+        if (!(visible || Console::isVisible() || Menu::isVisible() /* || Selector::isVisible() */ )) {
190 190
             for (int n = forwardAction; n < ActionEventCount; n++) {
191 191
                 auto ae = static_cast<ActionEvents>(n);
192 192
                 if (RunTime::getKeyBinding(ae) == std::get<0>(i))
@@ -269,9 +269,6 @@ void UI::display() {
269 269
         ImGui::End();
270 270
     }
271 271
 
272
-    if (Game::isLoaded())
273
-        Camera::displayUI();
274
-
275 272
     Console::display();
276 273
     Menu::display();
277 274
     Selector::display();

Loading…
Peruuta
Tallenna