Skip to content

Renderer

Renderer object for drawing a scene from a given camera.

The renderer owns GPU state and draw logic for rendering a Scene with a specified Camera. It is typically constructed and driven by the Application runtime, but can also be used directly in manual setups.

cpp
vglx::Renderer renderer({
  .framebuffer_width = window.FramebufferWidth(),
  .framebuffer_height = window.FramebufferHeight(),
  .clear_color = 0x444444
});

auto ok = renderer.Initialize();
if (!ok) {
  HandleError(ok.error());
}

// Per-frame:
renderer.Render(scene.get(), camera.get());

Construction

Constructors


Renderer()

Constructs a renderer.

GPU resources are not created until Initialize is called.

cpp
Renderer::Renderer(const Renderer::Parameters& params);
ParameterDescription
paramsInitialization parameters for constructing the renderer.

Types

Renderer::Parameters struct

Parameters for constructing a Renderer object.

ParameterDescription
framebuffer_width intCurrent framebuffer width in pixels.
framebuffer_height intCurrent framebuffer height in pixels.
clear_color ColorClear color used at the start of a frame.

Functions

Initialize() std::expected<void, std::string>

Initializes GPU state and allocates required resources.

cpp
std::expected<void, std::string> Renderer::Initialize();

operator=() Renderer&

cpp
Renderer& Renderer::operator=(Renderer& &) noexcept;
ParameterDescription

Render() void

Renders the given scene from the specified camera.

The scene is expected to be in a consistent state for rendering. If you are using the runtime path, this is handled automatically. In direct initialization flows, call tye per-frame update routine Scene::Advance prior to rendering.

cpp
void Renderer::Render(Scene* scene, Camera* camera);
ParameterDescription
scenePointer to the scene to render.
cameraPointer to the active camera.

RenderedObjectsPerFrame() size_t

Returns the number of renderable objects drawn in the last frame.

Intended for statistics overlays and debugging.

cpp
size_t Renderer::RenderedObjectsPerFrame() const;

SetClearColor() void

Sets the clear color for subsequent frames.

The color is applied at the start of each frame when the framebuffer is cleared. Typically used to define the background color of the rendering surface.

cpp
void Renderer::SetClearColor(const Color& color);
ParameterDescription
colorClear color in RGB format.

SetViewport() void

Sets the active viewport rectangle in pixels.

Adjusts the area of the framebuffer that subsequent draw calls will target. This should be called whenever the window or framebuffer size changes, or when rendering to a specific sub-region of the target surface.

When using the runtime-managed rendering path, the viewport is updated automatically. In manual initialization flows, you are responsible for calling this method whenever the framebuffer dimensions change.

cpp
void Renderer::SetViewport(int x, int y, int width, int height);
ParameterDescription
xLeft pixel of the viewport.
yBottom pixel of the viewport.
widthViewport width in pixels.
heightViewport height in pixels.

Released under the MIT License.