Skip to content

Commit

Permalink
improving 2D grid view, and auto radius
Browse files Browse the repository at this point in the history
  • Loading branch information
wkjarosz committed Dec 27, 2023
1 parent 88f4c65 commit b542ca5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
1 change: 1 addition & 0 deletions include/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class SampleViewer
void draw_text(const int2 &pos, const std::string &text, const float4 &col, ImFont *font = nullptr,
int align = TextAlign_RIGHT | TextAlign_BOTTOM) const;
void draw_points(const float4x4 &mvp, const float4x4 &smash, const float3 &color);
void draw_grid(const float4x4 &mat, int2 size, float alpha) const;
void draw_trigrid(Shader *shader, const float4x4 &mvp, float alpha, const int2x3 &count);
void draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int plotIndex);
int2 get_draw_range() const;
Expand Down
5 changes: 5 additions & 0 deletions include/arcball.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ struct Arcball
return linalg::rotation_matrix(quat());
}

Mat44f inv_matrix() const
{
return linalg::rotation_matrix(linalg::qconj(quat()));
}

private:
/// Whether or not this Arcball is currently active.
bool m_active;
Expand Down
45 changes: 22 additions & 23 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ static int g_dismissed_version = 0;

static bool g_show_modal = false;

static const auto rot90 = linalg::rotation_matrix(linalg::rotation_quat({0.f, 0.f, 1.f}, float(M_PI_2)));

static const vector<pair<string, string>> g_help_strings = {
{"h", "Show this help window"},
{"Left click+drag", "Rotate the camera"},
Expand Down Expand Up @@ -96,18 +94,6 @@ static float4x4 layout_2d_matrix(int num_dims, int2 dims)
return mul(translation_matrix(float3{offset * cell_spacing, 1}), scaling_matrix(float3{float2{cell_size}, 1}));
}

static void draw_grid(Shader *shader, const float4x4 &mat, int2 size, float alpha)
{
shader->set_uniform("mvp", mat);
shader->set_uniform("size", size);
shader->set_uniform("alpha", alpha);
shader->begin();
CHK(glDepthMask(GL_FALSE));
shader->draw_array(Shader::PrimitiveType::TriangleFan, 0, 4);
CHK(glDepthMask(GL_TRUE));
shader->end();
}

SampleViewer::SampleViewer()
{
m_custom_line_counts.fill(1);
Expand Down Expand Up @@ -875,7 +861,8 @@ void SampleViewer::draw_editor()
ImGui::SliderFloat("Radius", &m_radius, 0.f, 1.f, "%4.3f");
ImGui::SameLine();
{
ImGui::ToggleButton(ICON_FA_COMPRESS, &m_scale_radius_with_points);
if (ImGui::ToggleButton(ICON_FA_COMPRESS, &m_scale_radius_with_points))
m_radius *= m_scale_radius_with_points ? std::sqrt(m_point_count) : 1.f / std::sqrt(m_point_count);
tooltip("Automatically scale radius with number of points");
}

Expand Down Expand Up @@ -1260,19 +1247,19 @@ void SampleViewer::draw_2D_points_and_grid(const float4x4 &mvp, int2 dims, int p
m_2d_point_shader->end();
}

auto mat = mul(mvp, mul(pos, m_camera[CAMERA_2D].arcball.matrix()));
auto mat = mul(mvp, mul(translation_matrix(float3{0, 0, -1.001}), pos));

if (m_show_bbox)
draw_grid(m_grid_shader, mat, int2{1}, 1.f);
draw_grid(mat, int2{1}, 1.f);

if (m_show_coarse_grid)
draw_grid(m_grid_shader, mat, int2{m_samplers[m_sampler]->coarseGridRes(m_point_count)}, 0.6f);
draw_grid(mat, int2{m_samplers[m_sampler]->coarseGridRes(m_point_count)}, 0.6f);

if (m_show_fine_grid)
draw_grid(m_grid_shader, mat, int2{m_point_count}, 0.2f);
draw_grid(mat, int2{m_point_count}, 0.2f);

if (m_show_custom_grid)
draw_grid(m_grid_shader, mat, int2{m_custom_line_counts[dims.x], m_custom_line_counts[dims.y]}, 1.f);
draw_grid(mat, int2{m_custom_line_counts[dims.x], m_custom_line_counts[dims.y]}, 1.f);
}

void SampleViewer::draw_scene()
Expand Down Expand Up @@ -1477,6 +1464,18 @@ void SampleViewer::draw_points(const float4x4 &mvp, const float4x4 &smash, const
m_3d_point_shader->end();
}

void SampleViewer::draw_grid(const float4x4 &mat, int2 size, float alpha) const
{
m_grid_shader->set_uniform("mvp", mat);
m_grid_shader->set_uniform("size", size);
m_grid_shader->set_uniform("alpha", alpha);
m_grid_shader->begin();
CHK(glDepthMask(GL_FALSE));
m_grid_shader->draw_array(Shader::PrimitiveType::TriangleFan, 0, 4);
CHK(glDepthMask(GL_TRUE));
m_grid_shader->end();
}

/*!
Draw grids along the XY, XZ, and ZY planes.
Expand All @@ -1499,7 +1498,7 @@ void SampleViewer::draw_trigrid(Shader *shader, const float4x4 &mvp, float alpha
{
for (int axis = CAMERA_XY; axis < CAMERA_CURRENT; ++axis)
if (m_camera[CAMERA_CURRENT].camera_type == axis || m_camera[CAMERA_CURRENT].camera_type == CAMERA_CURRENT)
draw_grid(shader, mul(mvp, m_camera[axis].arcball.matrix()), counts[axis], alpha);
draw_grid(mul(mvp, m_camera[axis].arcball.inv_matrix()), counts[axis], alpha);
}

void SampleViewer::draw_text(const int2 &pos, const string &text, const float4 &color, ImFont *font, int align) const
Expand Down Expand Up @@ -1540,11 +1539,11 @@ string SampleViewer::export_XYZ_points(const string &format)
for (int axis = CAMERA_XY; axis < CAMERA_CURRENT; ++axis)
{
if (format == "eps")
out += draw_grids_eps(mul(mvp, m_camera[axis].arcball.matrix()), m_point_count,
out += draw_grids_eps(mul(mvp, m_camera[axis].arcball.inv_matrix()), m_point_count,
m_samplers[m_sampler]->coarseGridRes(m_point_count), m_show_fine_grid,
m_show_coarse_grid, m_show_bbox);
else
out += draw_grids_svg(mul(mvp, m_camera[axis].arcball.matrix()), m_point_count,
out += draw_grids_svg(mul(mvp, m_camera[axis].arcball.inv_matrix()), m_point_count,
m_samplers[m_sampler]->coarseGridRes(m_point_count), m_show_fine_grid,
m_show_coarse_grid, m_show_bbox);
}
Expand Down

0 comments on commit b542ca5

Please sign in to comment.