Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_204_Offscreen

Rendering a model straight to an image file with no window, using the OffScreen renderer.

Overview

Sometimes you want a picture of a scene without any user interaction — for batch processing, automated thumbnails, or headless servers with no display. OffScreen provides the same scene management and rendering pipeline as Viewer but draws into an offscreen framebuffer and writes the result to an image file instead of presenting it in a window. This tutorial loads the Stanford bunny, gives it a flat color, and saves a PNG.

The technique

Offscreen rendering means the GPU draws into a framebuffer object (a texture-backed render target) rather than the default on-screen framebuffer. The geometry, camera, and shading are identical to interactive rendering; only the destination differs. After the draw, the pixels are read back from the GPU and encoded to an image file. OffScreen subclasses Viewer (privately) so it reuses add_model(), the camera, and the draw path, but it never opens a window or runs an event loop — render() performs a single capture. Optional supersampling (the samples argument) and scaling let you trade speed for image quality.

Easy3D API

Low-level rendering with RenderDevice

Applications that need explicit control over geometry, transforms, viewports, or readback can skip the scene graph and submit backend-independent draw commands directly: build a graphics::Geometry and graphics::DrawCommand and pass them to graphics::RenderDevice::render(), which returns a FrameImage. No backend-selection macro or native graphics handle appears in client code. The second half of this tutorial draws a triangle in framebuffer pixel coordinates this way. The OffScreen object keeps the configured backend (and, on OpenGL, a current context) alive while the device is used.

Running it

This example is non-interactive: it prints "surface rendering saved to 'offscreen.png'" and "triangle rendered with ..." and exits, leaving offscreen.png and render_device.png in the working directory. No window appears.

1
27#include <easy3d/viewer/offscreen.h>
28#include <easy3d/core/model.h>
29#include <easy3d/fileio/image_io.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/renderer/drawable_triangles.h>
32#include <easy3d/renderer/render_device.h>
33#include <easy3d/util/initializer.h>
34#include <easy3d/util/resource.h>
35
36#include <cstdlib>
37#include <iostream>
38#include <memory>
39#include <vector>
40
92int main(int argc, char **argv) {
93 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
95
96 // Create a headless renderer with an 800 x 600 pixel canvas (no window is opened).
97 easy3d::OffScreen renderer(800, 600);
98
99 // Load the bunny mesh; add_model() returns nullptr if the file is missing or invalid.
100 const std::string filename = easy3d::resource::directory() + "/data/bunny.ply";
101 auto *mesh = renderer.add_model(filename);
102 if (!mesh) {
103 std::cerr << "model not found or failed to load: " << filename << std::endl;
104 return EXIT_FAILURE;
105 }
106
107 // Give the surface a flat light-blue material so the saved image has a uniform color.
108 auto *faces = mesh->renderer()->get_triangles_drawable("faces", true);
109 if (faces)
110 faces->set_uniform_coloring(easy3d::vec4(0.7f, 0.85f, 1, 1));
111
112 // Render one frame offscreen and write it to disk; render() returns true on success.
113 if (!renderer.render("offscreen.png")) {
114 std::cerr << "offscreen rendering failed" << std::endl;
115 return EXIT_FAILURE;
116 }
117 std::cout << "surface rendering saved to 'offscreen.png'" << std::endl;
118
119 // ----------------------------------------------------------------------------------------------------------------
120
121 // Low-level path: submit a draw command straight to a RenderDevice. The OffScreen object above
122 // already provides the backend (and context); the device renders a 128 x 128 triangle given in
123 // framebuffer pixel coordinates and returns the pixels as a FrameImage (top row first).
124 auto device = easy3d::graphics::create_device();
125
126 std::vector<easy3d::graphics::Vertex> vertices(3);
127 vertices[0].position = easy3d::vec3(24, 104, 0);
128 vertices[1].position = easy3d::vec3(104, 104, 0);
129 vertices[2].position = easy3d::vec3(64, 24, 0);
130 auto geometry = std::make_shared<const easy3d::graphics::Geometry>(
131 vertices, std::vector<std::uint32_t>{0, 1, 2}, easy3d::graphics::Topology::triangles);
132
134 command.geometry = geometry;
135 command.model_view_projection = easy3d::graphics::screen_space_projection(128, 128);
136 command.color = easy3d::vec4(0.2f, 0.7f, 1.0f, 1.0f);
137 command.object_id = 7;
138
140 options.background = easy3d::vec4(0, 0, 0, 1);
141 options.samples = 1;
142 const auto frame = device->render(128, 128, {command}, options);
143 if (!easy3d::ImageIO::save("offscreen-render_device.png", frame.rgba, frame.width, frame.height, 4, false)) {
144 std::cerr << "failed to save render_device.png" << std::endl;
145 return EXIT_FAILURE;
146 }
147 std::cout << "triangle rendered with " << device->name() << "; rendering saved to 'offscreen-render_device.png'" << std::endl;
148 return EXIT_SUCCESS;
149}
static bool save(const std::string &file_name, const std::vector< unsigned char > &data, int width, int height, int channels, bool flip_vertically=false)
Write image data into a file.
Definition image_io.cpp:82
Render an Easy3D scene into an image without showing a window.
Definition offscreen.h:50
std::string directory()
Get the selected resource directory.
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
Vec< 4, float > vec4
A 4D point/vector of float type.
Definition types.h:46
Complete backend-independent description of a draw operation.
Definition render_device.h:219
Common frame-rendering parameters.
Definition render_device.h:458
See also
OffScreen, RenderDevice, Geometry, DrawCommand, FrameImage, Viewer, TrianglesDrawable, Tutorial_201_Viewer_default