Skip to content

Commit

Permalink
enhance offscreen
Browse files Browse the repository at this point in the history
  • Loading branch information
LiangliangNan committed Jun 13, 2024
1 parent 7f02118 commit 1a6c9cf
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 10 deletions.
2 changes: 1 addition & 1 deletion easy3d/util/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ namespace easy3d {
#define EASY3D_VERSION_NR 1020503

/// Easy3D release date, in the format YYYYMMDD.
#define EASY3D_RELEASE_DATE 20240611
#define EASY3D_RELEASE_DATE 20240613


#endif // EASY3D_UTIL_VERSION_H
4 changes: 2 additions & 2 deletions easy3d/viewer/offscreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace easy3d {
}


bool OffScreen::render(const std::string &image_file) const {
return snapshot(image_file);
bool OffScreen::render(const std::string &file_name, float scaling, int samples, int back_ground, bool expand) const {
return snapshot(file_name, scaling, samples, back_ground, expand);
}


Expand Down
18 changes: 15 additions & 3 deletions easy3d/viewer/offscreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace easy3d {
* LOG(ERROR) << "failed to load model";
* return EXIT_FAILURE;
* }
* ... when necessary, access the camera and modify the view here.
* bool success = os.render("image.png");
* \endcode
*/
Expand All @@ -64,11 +65,22 @@ namespace easy3d {
//@}

/**
* @brief Render the current scene into an image file.
* @param image_file The file name of the image to which the scene will be saved.
* @brief Render the current scene into an image file. Supported image format: png, jpg, bmp, and tga.
* @details This function renders the scene into a framebuffer and takes a snapshot of the framebuffer.
* It allows the snapshot image to have a dimension different from the offscreen renderer, and it
* has no limit on the image size (if memory allows).
* @param file_name The image file name.
* @param scaling The scaling factor that determines the size of the image (default to 1.0, same size as
* the offscreen renderer), i.e.,
* image_width = width() * scaling;
* image_height = height() * scaling;
* @param samples The required number of samples for antialiased rendering (which can be different from
* that of the default framebuffer). The default value is 4.
* @param back_ground Determines the background color. 0: current color; 1: white; 2: transparent.
* @param expand Expand the frustum to ensure the image aspect ratio.
* @return true on success and false otherwise.
*/
bool render(const std::string& image_file) const;
bool render(const std::string& file_name, float scaling = 1.0f, int samples = 4, int back_ground = 1, bool expand = true) const;


/// @name Other properties
Expand Down
8 changes: 4 additions & 4 deletions easy3d/viewer/viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,19 +336,19 @@ namespace easy3d {
/**
* @brief Take a snapshot of the screen and save it to an image file. Supported image format: png, jpg, bmp, and tga.
* @details This function renders the scene into a framebuffer and takes a snapshot of the framebuffer.
* It allow the snapshot image to have a dimension different from the viewer and it has no limit on the
* It allows the snapshot image to have a dimension different from the viewer, and it has no limit on the
* image size (if memory allows).
* @param file_name The image file name.
* @param scaling The scaling factor that determines the size of the image (default to 1.0, using the viewer size), i.e.,
* image_width = viewer_width * scaling;
* image_height = viewer_height * scaling;
* @param samples The required number of samples for antialiased rendering (can be different from the default framebuffer).
* The default value is 0 (no antialiasing).
* @param samples The required number of samples for antialiased rendering (which can be different from
* that of the default framebuffer). The default value is 4.
* @param back_ground Determines the background color. 0: current color; 1: white; 2: transparent.
* @param expand Expand the frustum to ensure the image aspect ratio.
* @return true on success and false otherwise.
*/
bool snapshot(const std::string& file_name, float scaling = 1.0f, int samples = 0, int back_ground = 1, bool expand = true) const;
bool snapshot(const std::string& file_name, float scaling = 1.0f, int samples = 4, int back_ground = 1, bool expand = true) const;

/**
* @brief Query the XYZ coordinates of the surface point under the cursor.
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ add_executable(Tests
visualization_viewer_imgui/viewer.h
visualization_viewer_imgui/viewer.cpp
visualization_multi_view/main.cpp
visualization_offscreen/offscreen.cpp
visualization_camera_interpolation/main.cpp
visualization_camera_interpolation/viewer.h
visualization_camera_interpolation/viewer.cpp
Expand Down
3 changes: 3 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ int test_kdtree();
int test_point_cloud_algorithms();
int test_surface_mesh_algorithms();

int offscreen();
int test_viewer_imgui(int duration);
int test_composite_view(int duration);
int test_real_camera();
Expand Down Expand Up @@ -103,6 +104,8 @@ int main(int argc, char* argv[]) {
result += test_point_cloud_algorithms();
result += test_surface_mesh_algorithms();

result += offscreen();

const int duration = 1500; // in millisecond
result += test_viewer_imgui(duration);
result += test_composite_view(duration);
Expand Down
56 changes: 56 additions & 0 deletions tests/visualization_offscreen/offscreen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/********************************************************************
* Copyright (C) 2015 Liangliang Nan <[email protected]>
* https://3d.bk.tudelft.nl/liangliang/
*
* This file is part of Easy3D. If it is useful in your research/work,
* I would be grateful if you show your appreciation by citing it:
* ------------------------------------------------------------------
* Liangliang Nan.
* Easy3D: a lightweight, easy-to-use, and efficient C++ library
* for processing and rendering 3D data.
* Journal of Open Source Software, 6(64), 3255, 2021.
* ------------------------------------------------------------------
*
* Easy3D is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* Easy3D is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/

#include <easy3d/viewer/offscreen.h>
#include <easy3d/util/resource.h>


using namespace easy3d;


int offscreen() {
const std::string file_name = resource::directory() + "/data/bunny.ply";
OffScreen os;
if (!os.add_model(file_name)) {
LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
return EXIT_FAILURE;
}

bool failed(false);
if (!os.render("bunny-0.png"))
failed = true;
if (!os.render("bunny-1.png", 2.0f, 0))
failed = true;
if (!os.render("bunny-2.png", 2.0f, 4, 0))
failed = true;
if (!os.render("bunny-3.png", 2.0f, 4, 1))
failed = true;
if (!os.render("bunny-4.png", 2.0f, 4, 2))
failed = true;

LOG_IF(failed, ERROR) << "offscreen renderer failed to render the scene";
return failed ? EXIT_FAILURE : EXIT_SUCCESS;
}

0 comments on commit 1a6c9cf

Please sign in to comment.