Skip to content

Commit 5e56acd

Browse files
committed
feat: animate query point position
1 parent 21c31f5 commit 5e56acd

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

src/gui/mainwindow.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ void MainWindow::run()
126126
process_glfw_window_inputs();
127127
ImGui_ImplGlfwGL3_NewFrame();
128128

129+
// Animation query point.
130+
if (m_animate_query_point)
131+
animate_query_point();
132+
129133
// Run query.
130134
if (m_closest_point_query)
131135
find_closest_point();
@@ -148,7 +152,13 @@ void MainWindow::release()
148152

149153
void MainWindow::load_scene(const std::string& path)
150154
{
155+
// Stop the running animation.
156+
m_animate_query_point = false;
157+
158+
// Load the scene.
151159
m_scene.reset(core::load_scene_from_file(path));
160+
161+
// Prepare closest point queries.
152162
core::ClosestPointQuery* query = new core::ClosestPointQuery(m_scene->get_mesh());
153163
m_closest_point_query.reset(query);
154164
}
@@ -157,6 +167,7 @@ void MainWindow::load_scene(const std::string& path)
157167
MainWindow::MainWindow()
158168
: m_query_point_max_serach_radius(10.0f)
159169
, m_query_point_pos(-1.2f, 1.6f, 2.8f)
170+
, m_animate_query_point(false)
160171
{}
161172

162173
// Singleton instance.
@@ -189,9 +200,20 @@ void MainWindow::imgui_draw()
189200
{
190201
ImGui::Text("Position");
191202
ImGui::DragFloat3("", glm::value_ptr(m_query_point_pos), 0.1f);
203+
192204
ImGui::Text("Maximum search radius");
193205
ImGui::DragFloat("", &m_query_point_max_serach_radius, 0.1f, 0.0f);
194206
m_query_point_max_serach_radius = std::max(m_query_point_max_serach_radius, 0.0f);
207+
208+
if (!m_animate_query_point && ImGui::Button("Animate query point"))
209+
{
210+
m_animate_query_point = true;
211+
}
212+
else if (m_animate_query_point && ImGui::Button("Stop animation"))
213+
{
214+
m_animate_query_point = false;
215+
}
216+
195217
ImGui::TreePop();
196218
}
197219

@@ -300,7 +322,7 @@ void MainWindow::find_closest_point()
300322
auto timer_start = std::chrono::high_resolution_clock::now();
301323

302324
// Run the query.
303-
bool found = run && m_closest_point_query->get_closest_point(
325+
m_closest_point_found = run && m_closest_point_query->get_closest_point(
304326
m_query_point_pos,
305327
m_query_point_max_serach_radius,
306328
m_closest_point_pos);
@@ -316,7 +338,7 @@ void MainWindow::find_closest_point()
316338
{ 0.8, 0.8, 0.8, 1.0 }
317339
});
318340
// Add the closest result point (in green).
319-
if (found)
341+
if (m_closest_point_found)
320342
points.push_back({
321343
m_closest_point_pos,
322344
{ 0.1, 1.0, 0.1, 1.0 }
@@ -325,6 +347,18 @@ void MainWindow::find_closest_point()
325347
m_scene_points.set_points(points);
326348
}
327349

350+
void MainWindow::animate_query_point()
351+
{
352+
// Try to move the query point arround the model with some wiggle.
353+
const glm::vec3 towards_model = m_closest_point_pos - m_query_point_pos;
354+
const glm::vec3 somewhere_else = glm::cross(towards_model, glm::vec3(0.0f, 1.0f, 0.0f));
355+
356+
const float wiggle = std::sin(m_time_since_startup) * 0.5f;
357+
358+
m_query_point_pos += towards_model * wiggle * m_frame_delta_time;
359+
m_query_point_pos += somewhere_else * m_frame_delta_time;
360+
}
361+
328362
// GLFW Window callbacks.
329363
void MainWindow::glfw_framebuffer_size_callback(GLFWwindow* window, int width, int height)
330364
{

src/gui/mainwindow.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace core { class ClosestPointQuery; }
1515
namespace core { class Scene; }
1616

1717
namespace gui
18-
{
18+
{
1919

2020
/**
2121
* @brief GUI Singleton.
@@ -71,6 +71,8 @@ class MainWindow
7171
float m_query_point_max_serach_radius;
7272
glm::vec3 m_closest_point_pos;
7373
std::int64_t m_closest_point_query_time; // milliseconds
74+
bool m_closest_point_found;
75+
bool m_animate_query_point;
7476

7577
OrbitCamera m_camera;
7678

@@ -82,6 +84,7 @@ class MainWindow
8284
void opengl_draw();
8385

8486
void find_closest_point();
87+
void animate_query_point();
8588

8689
// GLFW Window callbacks.
8790
static void glfw_framebuffer_size_callback(GLFWwindow* window, int width, int height);
@@ -92,4 +95,4 @@ class MainWindow
9295
static void glfw_mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
9396
};
9497

95-
} // namespace gui
98+
} // namespace gui

0 commit comments

Comments
 (0)