Skip to content
Snippets Groups Projects
Commit 98ab7cd9 authored by Martin Štourač's avatar Martin Štourač
Browse files

visual ray fix, began find_intersection

parent b91e7ec8
No related branches found
No related tags found
No related merge requests found
......@@ -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;
};
......
......@@ -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();
......
......@@ -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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment