LiangliangNan / Easy3D

A lightweight, easy-to-use, and efficient C++ library for processing and rendering 3D data
GNU General Public License v3.0
1.35k stars 243 forks source link

[Question]: Can you render to a texture and save as a file #185

Closed jacquesvaneeden closed 3 months ago

jacquesvaneeden commented 3 months ago

Is it possible to render to a file without having a visible viewer. The viewer.Snapshot works great but, in my case, I don't want to have a visible viewer just the final rendered image file

LiangliangNan commented 3 months ago

Thanks for asking. Indeed the current viewer can be adapted for such a purpose (by simply hiding it).

I just added offscreen rendering. Below is an example showing how to use it. Let me know if you encounter any issuing when using it.

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

using namespace easy3d;

int main(int argc, char** argv) {
    // initialize Easy3D.
    initialize();

    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 success = os.render("D:\\tmp\\image.png");
    return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
jacquesvaneeden commented 3 months ago

Thank you very much

jacquesvaneeden commented 3 months ago

May I suggest you add the rest of the parameters for the snapshot function. It is useful to render to a transparent background

 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);
 }
LiangliangNan commented 3 months ago

Done!