diff --git a/src/gfx/include/gfx/aabb.hpp b/src/gfx/include/gfx/aabb.hpp index ac43f938930849979ecf92336ab21be9c6e41818..654bb9a3b02214c1e4d38be7e509e81224dee754 100644 --- a/src/gfx/include/gfx/aabb.hpp +++ b/src/gfx/include/gfx/aabb.hpp @@ -17,6 +17,7 @@ class AABB public: AABB(std::vector<float> const &_vertices); + std::pair<glm::vec3, glm::vec3> const &getLH() const { return LH; } std::vector<float> getVertices() const; std::vector<unsigned int> getIndices() const; }; diff --git a/src/studio/include/studio/simulator.hpp b/src/studio/include/studio/simulator.hpp index 48ae10148b9db44f1f81dc84032d7eff6bbb821b..09a8a8f62ad145ba902abfc38bd370ad42ca57c4 100644 --- a/src/studio/include/studio/simulator.hpp +++ b/src/studio/include/studio/simulator.hpp @@ -53,7 +53,8 @@ struct Simulator : public osi::Simulator void create_scene(); void process_input(); - void add_ray(camera_ptr camera, glm::vec3 rayDirection); + void find_intersection(glm::vec3 rayPosition, glm::vec3 rayDirection); + void add_ray(glm::vec3 rayPosition, glm::vec3 rayDirection); void select_object(); void process_mouse(); diff --git a/src/studio/src/simulator.cpp b/src/studio/src/simulator.cpp index 4157040583d58001a87050dd637c701cd17334a4..1846a17b1b7a3486ea3b9a6edee84d1e259a3763 100644 --- a/src/studio/src/simulator.cpp +++ b/src/studio/src/simulator.cpp @@ -315,12 +315,32 @@ void Simulator::process_input() ASSUMPTION(glGetError() == GL_NO_ERROR); } -void Simulator::add_ray(camera_ptr camera, glm::vec3 rayDirection) +void Simulator::find_intersection(glm::vec3 rayPosition, glm::vec3 rayDirection) +{ + node_ptr nearest_node = nullptr; + float nearest_t = 0; + for (auto &node : scene) + { + for (auto &object : node->getObjects()) + { + auto obj = dynamic_pointer_cast<Object>(object); + if (!obj) + continue; + + auto LH = obj->getAABB().getLH(); + // float t = intersect(rayPosition, rayDirection, LH); + // if (t < nearest_t) + // nearest_t = t; + } + } +} + +void Simulator::add_ray(glm::vec3 rayPosition, glm::vec3 rayDirection) { object_ptr ray = std::make_shared<Object>(Object{{0, 0, 0, rayDirection.x, rayDirection.y, rayDirection.z}, {0, 1}, {}, glm::vec3(0.5f, 1, 0.31f), "basic"}); ray->setDrawMode(GL_LINES); - frame_ptr ray_frame = std::make_shared<Frame>(camera->getFrame()->getPosition()); + frame_ptr ray_frame = std::make_shared<Frame>(rayPosition); scene.emplace_back(std::make_shared<Node>(ray_frame)); scene.back()->addObject(std::move(ray)); } @@ -330,14 +350,6 @@ void Simulator::select_object() if (!mouse().just_released().contains("MouseLeft")) return; - /* ATTEMPT 1 */ - // https://antongerdelan.net/opengl/raycasting.html - float x_ = mouse().pos().x - (window().size().x / 2) - 1.0f; - float y_ = 1.0f - mouse().pos().y - (window().size().y / 2); - float z_ = 1.0f; - glm::vec3 ray_ndc = glm::vec3(x_, y_, z_); - glm::vec4 ray_clip = glm::vec4(ray_ndc.x, ray_ndc.y, -1.0f, 1.0f); - camera_ptr camera = active_cameras[0]; glm::mat4 view; @@ -350,24 +362,32 @@ void Simulator::select_object() 0.1f, 100.0f); - glm::vec4 ray_eye = glm::inverse(projection) * ray_clip; - ray_eye.z = -1.0f; - ray_eye.w = 0.0f; - - glm::vec3 ray_wor = (glm::inverse(view) * ray_eye); - ray_wor = glm::normalize(ray_wor); - - /* ATTEMPT 2 */ - // WHITEBOARD - float u = static_cast<float>(mouse().pos().x - (window().size().x / 2.0f)); - float v = static_cast<float>((window().size().y - mouse().pos().y) - (window().size().y / 2.0f)); - glm::vec2 uv = glm::normalize(glm::vec2(u, v)); - - glm::vec3 camPos = camera->getFrame()->getPosition(); - glm::vec3 P = camPos + 0.1f * -camera->getFrame()->getRotationAxisZ() + uv.x * camera->getFrame()->getRotationAxisX() + uv.y * camera->getFrame()->getRotationAxisY(); - glm::vec3 w = P - camPos; - w = glm::normalize(w); - w.z = 1.0f; + /* ATTEMPT 1 */ + // https://antongerdelan.net/opengl/raycasting.html + // float x_ = mouse().pos().x - (window().size().x / 2) - 1.0f; + // float y_ = 1.0f - mouse().pos().y - (window().size().y / 2); + // float z_ = 1.0f; + // glm::vec3 ray_ndc = glm::vec3(x_, y_, z_); + // glm::vec4 ray_clip = glm::vec4(ray_ndc.x, ray_ndc.y, -1.0f, 1.0f); + + // glm::vec4 ray_eye = glm::inverse(projection) * ray_clip; + // ray_eye.z = -1.0f; + // ray_eye.w = 0.0f; + + // glm::vec3 ray_wor = (glm::inverse(view) * ray_eye); + // ray_wor = glm::normalize(ray_wor); + + // /* ATTEMPT 2 */ + // // WHITEBOARD + // float u = static_cast<float>(mouse().pos().x - (window().size().x / 2.0f)); + // float v = static_cast<float>((window().size().y - mouse().pos().y) - (window().size().y / 2.0f)); + // glm::vec2 uv = glm::normalize(glm::vec2(u, v)); + + // glm::vec3 camPos = camera->getFrame()->getPosition(); + // glm::vec3 P = camPos + 0.1f * -camera->getFrame()->getRotationAxisZ() + uv.x * camera->getFrame()->getRotationAxisX() + uv.y * camera->getFrame()->getRotationAxisY(); + // glm::vec3 w = P - camPos; + // w = glm::normalize(w); + // w.z = 1.0f; // std::cout << "P point is: " << P.x << ", " << P.y << ", " << P.z << std::endl; // std::cout << "camPos is: " << camPos.x << ", " << camPos.y << ", " << camPos.z << std::endl; @@ -392,7 +412,6 @@ void Simulator::select_object() glm::vec4 cameraSpaceNear = glm::vec4(screenSpaceX * nearPlane, screenSpaceY * nearPlane, -nearPlane, 1); glm::vec4 cameraSpaceFar = glm::vec4(screenSpaceX * farPlane, screenSpaceY * farPlane, -farPlane, 1); - //Unproject the 2D window into 3D to see where in 3D we're actually clicking glm::mat4 tmpView = glm::mat4(view); glm::mat4 invView = glm::inverse(tmpView); @@ -407,12 +426,10 @@ void Simulator::select_object() rayDirection = camera->getFrame()->getRotation() * rayDirection; - add_ray(camera, rayDirection); + add_ray(rayPosition, rayDirection); rayDirection = glm::normalize(rayDirection); std::cout << "rayDirection vector is: " << rayDirection.x << ", " << rayDirection.y << ", " << rayDirection.z << std::endl << std::endl; - - } void Simulator::process_mouse()