Browse Source

Selector ray casting

Thomas Buck 8 years ago
parent
commit
6a51c7c010
7 changed files with 43 additions and 36 deletions
  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 View File

4
 Icon

4
 Icon

5
 
5
 
6
 build*
6
 build*
7
+.idea

+ 3
- 0
ChangeLog.md View File

2
 
2
 
3
 ## OpenRaider (0.1.4) xythobuz <xythobuz@xythobuz.de>
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
     [ 20150801 ]
8
     [ 20150801 ]
6
     * Updated to current Imgui version (now using indexed rendering)
9
     * Updated to current Imgui version (now using indexed rendering)
7
     * Added mouse selection window (not yet doing anything useful)
10
     * Added mouse selection window (not yet doing anything useful)

+ 4
- 5
include/Camera.h View File

41
     static void setRoom(int r) { if (room != r) dirty = true; room = r; }
41
     static void setRoom(int r) { if (room != r) dirty = true; room = r; }
42
     static int getRoom() { return room; }
42
     static int getRoom() { return room; }
43
 
43
 
44
-    static void setShowOverlay(bool s) { showOverlay = s; }
45
-    static bool getShowOverlay() { return showOverlay; }
46
-
47
     static void setKeepInRoom(bool k) { keepInRoom = k; }
44
     static void setKeepInRoom(bool k) { keepInRoom = k; }
48
     static bool getKeepInRoom() { return keepInRoom; }
45
     static bool getKeepInRoom() { return keepInRoom; }
49
 
46
 
50
     static bool boxInFrustum(BoundingBox b);
47
     static bool boxInFrustum(BoundingBox b);
51
     static void displayFrustum(glm::mat4 MVP);
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
   private:
54
   private:
56
     static void calculateFrustumPlanes();
55
     static void calculateFrustumPlanes();
62
     static glm::mat4 projection;
61
     static glm::mat4 projection;
63
     static glm::mat4 view;
62
     static glm::mat4 view;
64
     static float rotationDeltaX, rotationDeltaY;
63
     static float rotationDeltaX, rotationDeltaY;
65
-    static bool updateViewFrustum, dirty, showOverlay, movingFaster;
64
+    static bool updateViewFrustum, dirty, movingFaster;
66
     static bool keepInRoom;
65
     static bool keepInRoom;
67
     static int room;
66
     static int room;
68
 };
67
 };

+ 4
- 17
src/Camera.cpp View File

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
 const static float maxSpeed = 3072.0f;
44
 const static float maxSpeed = 3072.0f;
44
 const static float controllerDeadZone = 0.33f;
45
 const static float controllerDeadZone = 0.33f;
45
 const static float controllerViewFactor = glm::pi<float>();
46
 const static float controllerViewFactor = glm::pi<float>();
61
 float Camera::rotationDeltaY = 0.75f;
62
 float Camera::rotationDeltaY = 0.75f;
62
 bool Camera::updateViewFrustum = true;
63
 bool Camera::updateViewFrustum = true;
63
 bool Camera::dirty = true;
64
 bool Camera::dirty = true;
64
-bool Camera::showOverlay = false;
65
 bool Camera::movingFaster = false;
65
 bool Camera::movingFaster = false;
66
 bool Camera::keepInRoom = false;
66
 bool Camera::keepInRoom = false;
67
 int Camera::room = -1;
67
 int Camera::room = -1;
216
     return updateViewFrustum;
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
 class FrustumPlane {
221
 class FrustumPlane {

+ 0
- 5
src/Render.cpp View File

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

+ 30
- 5
src/Selector.cpp View File

2
  * \file src/Selector.cpp
2
  * \file src/Selector.cpp
3
  * \brief Selector Window
3
  * \brief Selector Window
4
  *
4
  *
5
+ * http://antongerdelan.net/opengl/raycasting.html
6
+ *
5
  * \author xythobuz
7
  * \author xythobuz
6
  */
8
  */
7
 
9
 
8
 #include "imgui/imgui.h"
10
 #include "imgui/imgui.h"
9
 
11
 
10
 #include "global.h"
12
 #include "global.h"
13
+#include "Camera.h"
11
 #include "Log.h"
14
 #include "Log.h"
15
+#include "system/Window.h"
12
 #include "Selector.h"
16
 #include "Selector.h"
13
 
17
 
14
 bool Selector::visible = false;
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
 void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
23
 void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released) {
19
     if ((button == leftmouseKey) && (!released)) {
24
     if ((button == leftmouseKey) && (!released)) {
20
         lastX = x;
25
         lastX = x;
21
         lastY = y;
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
     if (!visible)
37
     if (!visible)
27
         return;
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
         ImGui::End();
41
         ImGui::End();
31
         return;
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
     if ((lastX < 0) || (lastY < 0)) {
60
     if ((lastX < 0) || (lastY < 0)) {
36
-        ImGui::Text("Last Click (World): (? ?)");
61
+        ImGui::Text("Normalized Ray: (? ? ?)");
37
     } else {
62
     } else {
38
-
63
+        ImGui::Text("Normalized Ray: (%.3f %.3f %.3f)", rayWorld.x, rayWorld.y, rayWorld.z);
39
     }
64
     }
40
 
65
 
41
     if (ImGui::Button("Hide Selector")) {
66
     if (ImGui::Button("Hide Selector")) {

+ 1
- 4
src/UI.cpp View File

186
     while (!keyboardEvents.empty()) {
186
     while (!keyboardEvents.empty()) {
187
         auto i = keyboardEvents.front();
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
             for (int n = forwardAction; n < ActionEventCount; n++) {
190
             for (int n = forwardAction; n < ActionEventCount; n++) {
191
                 auto ae = static_cast<ActionEvents>(n);
191
                 auto ae = static_cast<ActionEvents>(n);
192
                 if (RunTime::getKeyBinding(ae) == std::get<0>(i))
192
                 if (RunTime::getKeyBinding(ae) == std::get<0>(i))
269
         ImGui::End();
269
         ImGui::End();
270
     }
270
     }
271
 
271
 
272
-    if (Game::isLoaded())
273
-        Camera::displayUI();
274
-
275
     Console::display();
272
     Console::display();
276
     Menu::display();
273
     Menu::display();
277
     Selector::display();
274
     Selector::display();

Loading…
Cancel
Save