Graphic

High-level 2D graphic handling utilities.

Overview

Typical use cases:

  • Handling 2D graphics rendering, instance data management, and batching

Header

<RaeptorCogs/Graphic.hpp>

Metadata

Author

Estorc

Version

v1.0

Copyright

Copyright (c) 2025 Estorc MIT License.

Enums

Enumerations

Enum

Description

RaeptorCogs::ComputeInstanceDataMode

Compute instance data mode enumeration.

RaeptorCogs::GraphicFlags

Graphic flags enumeration.

RaeptorCogs::RenderableGraphicFlags

RenderableGraphicFlags enumeration.

RaeptorCogs::TransformFlags

Transform flags enumeration.

enum class RaeptorCogs::ComputeInstanceDataMode

Compute instance data mode enumeration.

Defines modes for computing instance data.

Values:

enumerator NONE

No instance data computation

enumerator FORCE_REBUILD

Force rebuild of instance data

enumerator REBUILD_TEXTURE

Rebuild texture atlas if needed

enum class RaeptorCogs::GraphicFlags : uint32_t

Graphic flags enumeration.

Defines flags for graphic state management.

Values:

enumerator NONE

No flags set

enumerator DATA_DIRTY

Flag to indicate if the graphic’s instance data needs to be rebuilt

enumerator INHERIT_READ_MASK

Flag to indicate if the graphic should inherit read mask from parent

enumerator NO_BATCHING

Flag to disable batching for this graphic (disables batch processing for this graphic - used by text)

enum class RaeptorCogs::RenderableGraphicFlags : uint32_t

RenderableGraphicFlags enumeration.

Defines flags for renderable graphic state management.

Values:

enumerator NONE

No flags set

enumerator GLOBAL_COLOR_DIRTY

Flag to indicate if the graphic’s instance data needs to be rebuilt

enumerator IS_VISIBLE

Flag to indicate if the graphic is visible

enum class RaeptorCogs::TransformFlags : uint32_t

Transform flags enumeration.

Defines flags for transformable graphic state management.

Values:

enumerator NONE

No flags set

enumerator LOCAL_MATRIX_DIRTY

Flag to indicate if the local transformation matrix needs to be rebuilt

enumerator GLOBAL_MATRIX_DIRTY

Flag to indicate if the global transformation matrix needs to be rebuilt

Classes

Classes

Class

Description

RaeptorCogs::Graphic2D

Base Graphic2D class.

RaeptorCogs::RenderableGraphic2D

No description.

RaeptorCogs::TransformableGraphic2D

TransformableGraphic2D class.

class Graphic2D : public RaeptorCogs::RegisterNode<Graphic2D, Node>, public RaeptorCogs::FlagSet<GraphicFlags>

Base Graphic2D class.

Provides an interface for 2D graphic objects.

Typical use cases:

  • Creating and managing 2D graphics in a rendering engine.

Subclassed by RaeptorCogs::RegisterNode< RenderableGraphic2D, Graphic2D >

Public Functions

inline Graphic2D()

Default constructor for Graphic2D.

virtual ~Graphic2D()

Virtual destructor for Graphic2D.

virtual bool computeInstanceData(GAPI::Common::InstanceAllocator &instanceAllocator, ComputeInstanceDataMode mode = ComputeInstanceDataMode::NONE)

Compute instance data for the graphic.

Note

This is how GPU side instance data is computed. Override this method in derived classes to provide specific instance data computation.

Parameters:
  • instanceAllocator – Reference to the instance allocator.

  • mode – Mode for computing instance data.

Returns:

true if updates were made, false otherwise.

virtual void bind() const = 0

Bind the graphic for rendering.

Note

Override this method in derived classes to provide specific binding behavior.

virtual uint32_t getID() const = 0

Get the unique ID of the graphic.

Returns:

Unique ID of the graphic.

inline virtual uint32_t getProgramID() const

Get the program ID used for rendering.

Returns:

Program ID.

virtual void setRenderer(Singletons::Renderer *renderer)

Set the renderer for the graphic.

See also

GAPI::Singletons::Renderer

Note

This method is called when the graphic is added to a renderer.

Parameters:

renderer – Pointer to the renderer.

virtual void setZIndex(float z)

Set the z-index of the graphic.

Note

Higher z-index values are rendered on top of lower values.

Parameters:

z – Z-index value.

void setReadingMaskID(int index, bool inheritFromParent = false)

Set the reading mask ID.

Note

If inheritFromParent is true, the reading mask will only be applied if no reading mask was already set.

Parameters:
  • index – Reading mask index.

  • inheritFromParent – Whether to inherit the reading mask from the parent graphic.

void setWritingMaskID(int index)

Set the writing mask ID.

Parameters:

index – Writing mask index.

inline void setBatchHandlerCursor(size_t cursor)

Set the batch handler cursor index.

Note

This index indicates the position of this graphic in the batch processing.

Parameters:

cursor – Cursor index.

virtual bool isVisible() const = 0

Check if the graphic is visible.

Note

Override this method in derived classes to provide specific visibility behavior.

Returns:

true if the graphic is visible, false otherwise.

virtual bool isOpaque() const = 0

Check if the graphic is opaque.

Note

Override this method in derived classes to provide specific opacity behavior.

Returns:

true if the graphic is opaque, false otherwise.

float getZIndex() const

Get the z-index of the graphic.

Note

Higher z-index values are rendered on top of lower values.

Returns:

Z-index value.

int getWritingMaskID() const

Get the writing mask ID.

Note

A value of 0 indicates no writing mask is applied.

Returns:

Writing mask index.

int getReadingMaskID() const

Get the reading mask ID.

Returns:

Reading mask index.

inline virtual Texture getTexture() const

Get the texture associated with the graphic.

Note

Override this method in derived classes to provide specific texture retrieval behavior.

Returns:

Texture object.

inline size_t getBatchHandlerCursor() const

Get the batch handler cursor index.

Note

This index indicates the position of this graphic in the batch processing.

Returns:

Cursor index.

inline size_t getRenderListCount() const

Get the number of render lists this graphic belongs to.

Returns:

Number of render lists.

inline std::vector<GAPI::Common::RenderList*> &getRenderLists()

Get the render lists this graphic belongs to.

Returns:

Vector of render list pointers.

GAPI::Common::GraphicBatchHandler &getBatchHandler()

Get the batch handler associated with this graphic.

Returns:

Reference to the GraphicBatchHandler.

Singletons::Renderer *getRenderer() const

Get the renderer associated with this graphic.

Returns:

Pointer to the Renderer.

inline BatchKey buildRendererKey()

Build the renderer key for batching.

Note

The renderer key is used for ordering and batching graphics during rendering.

Returns:

BatchKey object.

void setDataDirty(bool dirty)

Set the data dirty flag.

Note

When data is dirty, instance data needs to be recomputed.

Parameters:

dirty – Boolean indicating if the data is dirty.

bool isDataDirty() const

Check if the data is dirty.

Note

When data is dirty, instance data needs to be recomputed.

Returns:

true if the data is dirty, false otherwise.

virtual void setParent(Node *parent) override

Set the parent node of the graphic.

Note

This method overrides the base class implementation to handle graphic-specific parent setting.

Parameters:

parent – Pointer to the parent node.

void updatePositionInRenderLists()

Update the graphic’s position in the render lists.

Note

Called when z-index or mask IDs change.

Private Members

Singletons::Renderer *renderer = nullptr

Pointer to the renderer.

Used for rendering operations.

size_t batchHandlerCursor = 0

Cursor index in the batch handler.

Indicates the position of this graphic in the batch processing.

std::vector<GAPI::Common::RenderList*> renderLists

List of render lists this graphic belongs to.

float zIndex = 0.0f

Z-index of the graphic.

Determines the rendering order of the graphic.

int readingMaskIndex = 0

Reading mask index.

Used for masking operations during rendering.

int writingMaskIndex = 0

Writing mask index.

Used for masking operations during rendering.

class RenderableGraphic2D : public RaeptorCogs::RegisterNode<RenderableGraphic2D, Graphic2D>, public RaeptorCogs::FlagSet<RenderableGraphicFlags>

Subclassed by RaeptorCogs::RegisterNode< TransformableGraphic2D, RenderableGraphic2D >

Public Functions

inline RenderableGraphic2D()

Default constructor for RenderableGraphic2D.

Initializes the graphic as visible and marks the global color as dirty.

~RenderableGraphic2D() = default

Destructor for RenderableGraphic2D.

void setColor(const glm::vec3 &color)

Set the color of the graphic.

Note

Setting the color marks the data as dirty.

Parameters:

color – Color vector (RGB).

template<typename ShapeType, typename ...Args>
inline void setShape(Args&&... args)

Set the shape of the graphic.

Note

Setting the shape marks the data as dirty.

Template Parameters:

ShapeType – Type of the shape (must derive from Shape).

Parameters:

args – Arguments to construct the shape.

Shape &getShape() const

Get the shape of the graphic.

Returns:

Reference to the shape.

glm::vec3 getColor() const

Get the color of the graphic.

Returns:

Color vector (RGB).

void rebuildGlobalColor()

Rebuild the global color of the graphic.

Note

The global color is computed by combining the graphic’s color with its parent’s global color.

glm::vec3 getGlobalColor()

Get the global color of the graphic.

Returns:

Global color vector (RGB).

void setVisibility(bool visible)

Set the visibility of the graphic.

Parameters:

visible – Boolean indicating if the graphic should be visible.

virtual bool isVisible() const override

Check if the graphic is visible.

Returns:

True if visible, false otherwise.

void setGlobalColorDirty(bool dirty)

Set the global color dirty flag.

Parameters:

dirty – Boolean indicating if the global color is dirty.

bool isGlobalColorDirty() const

Check if the global color is dirty.

Returns:

True if the global color is dirty, false otherwise.

virtual void setParent(Node *parent) override

Set the parent node of the graphic.

Note

This method overrides the base class implementation to handle graphic-specific parent setting.

Parameters:

parent – Pointer to the parent node.

Private Members

glm::vec3 color = glm::vec3(1.0f, 1.0f, 1.0f)

Color of the graphic.

Used for tinting the graphic.

glm::vec3 globalColor = glm::vec3(1.0f, 1.0f, 1.0f)

Global color of the graphic.

Computed by combining the graphic’s color with its parent’s global color.

std::shared_ptr<Shape> shape = std::make_shared<Quad>()

Shape of the graphic.

Defines the geometry of the graphic.

class TransformableGraphic2D : public RaeptorCogs::RegisterNode<TransformableGraphic2D, RenderableGraphic2D>, public RaeptorCogs::FlagSet<TransformFlags>

TransformableGraphic2D class.

Provides functionality for 2D graphics that can be transformed.

Typical use cases:

  • Managing position, size, scale, rotation, and anchor point of 2D graphics

See also

Graphic2D

Subclassed by RaeptorCogs::Glyph, RaeptorCogs::Sprite2D, RaeptorCogs::Text2D

Public Functions

inline TransformableGraphic2D()

Default constructor for TransformableGraphic2D.

Initializes the local and global matrices as dirty.

~TransformableGraphic2D() = default

Destructor for TransformableGraphic2D.

void rebuildLocalMatrix()

Rebuild the local transformation matrix.

Note

This method recalculates the local matrix based on position, size, scale, rotation, and anchor point.

void rebuildGlobalMatrix()

Rebuild the global transformation matrix.

Note

This method recalculates the global matrix by combining the local matrix with the parent’s global matrix.

glm::vec2 getPosition() const

Get the position of the graphic.

Returns:

position of the graphic

glm::vec2 getSize() const

Get the size of the graphic.

Returns:

size of the graphic

glm::vec2 getScale() const

Get the scale of the graphic.

Returns:

scale of the graphic

float getRotation() const

Get the rotation of the graphic.

Returns:

rotation angle in radians

glm::vec2 getAnchor() const

Get the anchor point of the graphic.

Returns:

anchor point of the graphic

void setPosition(const glm::vec2 &pos)

Set the position of the graphic.

Note

Setting the position marks the local and global matrices as dirty.

Parameters:

pos – New position vector

void setSize(const glm::vec2 &size)

Set the size of the graphic.

Note

Setting the size marks the local and global matrices as dirty.

Parameters:

size – New size vector

void setScale(const glm::vec2 &scale)

Set the scale of the graphic.

Note

Setting the scale marks the local and global matrices as dirty.

Parameters:

scale – New scale vector

void setRotation(float angle)

Set the rotation of the graphic.

Note

Setting the rotation marks the local and global matrices as dirty.

Parameters:

angle – New rotation angle in radians

void setAnchor(const glm::vec2 &anchor)

Set the anchor point of the graphic.

Note

Setting the anchor point marks the local and global matrices as dirty.

Parameters:

anchor – New anchor point vector

virtual void setZIndex(float z) override

Set the z-index of the graphic.

Note

Setting the z-index marks the local matrix as dirty.

Parameters:

z – Z-index value.

virtual void setParent(Node *parent) override

Set the parent node of the graphic.

Note

This method overrides the base class implementation to handle graphic-specific parent setting.

Parameters:

parent – Pointer to the parent node.

glm::mat4 getModelMatrix()

Get the model matrix of the graphic.

Note

The model matrix represents the graphic’s transformation in world space.

Returns:

Model matrix (global transformation matrix).

glm::mat4 &getLocalMatrix()

Get the local transformation matrix.

Returns:

Reference to the local transformation matrix.

glm::mat4 &getGlobalMatrix()

Get the global transformation matrix.

Returns:

Reference to the global transformation matrix.

void setLocalMatrixDirty(bool dirty)

Set the local matrix dirty flag.

Parameters:

dirty – New state of the local matrix dirty flag.

void setGlobalMatrixDirty(bool dirty)

Set the global matrix dirty flag.

Parameters:

dirty – New state of the global matrix dirty flag.

bool isLocalMatrixDirty() const

Check if the local matrix is dirty.

Returns:

true if the local matrix is dirty, false otherwise.

bool isGlobalMatrixDirty() const

Check if the global matrix is dirty.

Returns:

true if the global matrix is dirty, false otherwise.

Private Members

glm::mat4 localMatrix = glm::mat4(1.0f)

Local transformation matrix.

Used to represent the graphic transform in its local space.

glm::mat4 globalMatrix = glm::mat4(1.0f)

Global transformation matrix.

Used to represent the graphic transform in world space.

glm::vec2 position = glm::vec2(0.0f, 0.0f)

Position of the graphic.

Defines the position in 2D space.

glm::vec2 size = glm::vec2(1.0f, 1.0f)

Size of the graphic.

Defines the width and height.

glm::vec2 scale = glm::vec2(1.0f, 1.0f)

Scale of the graphic.

Defines the scaling factors along x and y axes.

float rotation = 0.0f

Rotation of the graphic.

Defines the rotation angle in radians.

glm::vec2 anchor = glm::vec2(0.0f, 0.0f)

Anchor point of the graphic.

Defines the pivot point for transformations.