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
- OffScreen: construct it with a pixel width/height; add_model() loads geometry; render() draws the scene and saves it to the given filename, returning true on success.
- Renderer / TrianglesDrawable: get_triangles_drawable("faces") fetches the surface drawable; set_uniform_coloring() assigns a constant material color.
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.
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>
92int main(
int argc,
char **argv) {
101 auto *mesh = renderer.add_model(filename);
103 std::cerr <<
"model not found or failed to load: " << filename << std::endl;
108 auto *faces = mesh->renderer()->get_triangles_drawable(
"faces",
true);
110 faces->set_uniform_coloring(
easy3d::vec4(0.7f, 0.85f, 1, 1));
113 if (!renderer.render(
"offscreen.png")) {
114 std::cerr <<
"offscreen rendering failed" << std::endl;
117 std::cout <<
"surface rendering saved to 'offscreen.png'" << std::endl;
124 auto device = easy3d::graphics::create_device();
126 std::vector<easy3d::graphics::Vertex> vertices(3);
130 auto geometry = std::make_shared<const easy3d::graphics::Geometry>(
131 vertices, std::vector<std::uint32_t>{0, 1, 2}, easy3d::graphics::Topology::triangles);
134 command.geometry = geometry;
135 command.model_view_projection = easy3d::graphics::screen_space_projection(128, 128);
137 command.object_id = 7;
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;
147 std::cout <<
"triangle rendered with " << device->name() <<
"; rendering saved to 'offscreen-render_device.png'" << std::endl;
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