Skip to content

Commit

Permalink
partial rendering example (#119)
Browse files Browse the repository at this point in the history
* partial rendering example

* doc for partial
  • Loading branch information
koide3 authored Jun 21, 2024
1 parent 4d6fb69 commit 16a8d4d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 4 deletions.
26 changes: 26 additions & 0 deletions docs/misc.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,32 @@ std::vector<Eigen::Vector3f> points
glk::save_ply_binary("model.ply", points.data(), points.size());
```
## Partial point cloud rendering
The rendering cost of large static point cloud can be mitigated with the partial rendering mode.
In this mode, only a part of static point clouds are rendered every frame and accumulated over time.
Although this causes flickering, it can drastically increase the rendering speed.
1. Enable the partial rendering mode for the viewer.
`viewer->enable_partial_rendering()`.
2. Create a point cloud buffer and set the points rendering budget.
```cpp
auto cloud_buffer = std::make_shared<glk::PointCloudBuffer>(...);
int points_rendering_budget = 512;
cloud_buffer->enable_partial_rendering(points_rendering_budget);
```
3. Mark the drawable as a static object.
```cpp
viewer->update_drawable("points", cloud_buffer, guik::Rainbow().static_object());
```
4. Mark other objects as dynamic.
```cpp
viewer->update_cube("cube", guik::FlatBlue().dynamic_object());
```

See also [ext_light_viewer_partial_rendering.cpp](https://github.com/koide3/iridescence/blob/master/src/example/ext_light_viewer_partial_rendering.cpp).

![partial_rendering](https://github.com/koide3/iridescence/assets/31344317/1a54035f-3faf-4910-8a3a-97d0cfb71a1e)

## Viewer menu

Expand Down
4 changes: 0 additions & 4 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ markdown_extensions:
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.superfences
- pymdownx.emoji:
emoji_index: !!python/name:materialx.emoji.twemoji
emoji_generator: !!python/name:materialx.emoji.to_svg
- fontawesome_markdown

copyright: Copyright &copy; 2021 - 2022 Kenji Koide
extra:
Expand Down
65 changes: 65 additions & 0 deletions src/example/ext_light_viewer_partial_rendering.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <iostream>
#include <glk/path.hpp>
#include <glk/pointcloud_buffer.hpp>
#include <glk/primitives/primitives.hpp>
#include <glk/io/ply_io.hpp>
#include <guik/viewer/light_viewer.hpp>

int main(int argc, char** argv) {
auto viewer = guik::viewer();
viewer->disable_vsync();
viewer->show_info_window();

// Enable thepartial rendering mode. In this mode, only a part of point clouds are rendered in each frame.
// Although this may cause flickering, it can drastically reduce the time for rendering static point clouds.
// Note:
// 1. In the partial rendering mode, every drawable must be marked as static or dynamic object (e.g., guik::FlatOrange().static_object()).
// 2. For static point clouds, enable partial rendering by calling enable_partial_rendering() with the points rendering budget.
const double clear_thresh = 1e-6;
viewer->enable_partial_rendering(clear_thresh);

auto ply = glk::load_ply(glk::get_data_path() + "/models/bunny.ply");
auto cloud_buffer = std::make_shared<glk::PointCloudBuffer>(ply->vertices);

// Set the number of points to be rendered in each frame.
int points_rendering_budget = 512;

viewer->register_ui_callback("ui_callback", [&] {
ImGui::DragInt("points_rendering_budget", &points_rendering_budget, 1, 1, 8192 * 5);
if (ImGui::Button("Enable Partial Rendering")) {
// Enable partial rendering with the specified budget.
cloud_buffer->enable_partial_rendering(points_rendering_budget);
}
});

// Draw many bunnies as high rendering load objects.
for (int i = -5; i <= 5; i++) {
for (int j = -5; j <= 5; j++) {
for (int k = 0; k < 5; k++) {
auto setting = guik::Rainbow()
// Setting transformation
.rotate(M_PI_2, Eigen::Vector3f::UnitX())
.scale(5.0f)
.translate(i, j, k)
// Mark as static object
.static_object();

viewer->update_drawable("bunny_" + std::to_string(i) + "_" + std::to_string(j) + "_" + std::to_string(k), cloud_buffer, setting);
}
}
}

double t = 0.0f;
while (t += ImGui::GetIO().DeltaTime, viewer->spin_once()) {
auto setting = guik::FlatBlue()
// Setting transformation
.translate(std::cos(t) * 10.0f, std::sin(t) * 10.0f, 0.0f)
.scale(1.0f, 1.0f, 20.0f)
// Mark as dynamic object
.dymamic_object();

viewer->update_cube("cube", setting);
}

return 0;
}

0 comments on commit 16a8d4d

Please sign in to comment.