Sparkle 0.0.1
Loading...
Searching...
No Matches
spk_view_region.hpp
1#pragma once
2
3#include <cstddef>
4
5#include "structure/container/spk_flags.hpp"
6#include "structure/graphics/spk_color.hpp"
7#include "structure/graphics/spk_extend_2d.hpp"
8#include "structure/math/spk_matrix.hpp"
9
10namespace spk
11{
12 namespace OpenGL
13 {
25 class ViewRegion
26 {
27 public:
31 enum class ClearMode
32 {
33 Color,
34 Depth,
35 Stencil
36 };
37
46
47 private:
48 spk::Extend2D _viewport;
49 spk::Extend2D _scissor;
50
51 // Global transforms and configuration
52 inline static const spk::OpenGL::ViewRegion *_activeViewRegion = nullptr;
53 inline static spk::Matrix4x4 _screenToOpenGLMatrix = spk::Matrix4x4::identity();
54 inline static spk::Matrix4x4 _openGLToScreenMatrix = spk::Matrix4x4::identity();
55 inline static size_t _maxLayer = 1;
56
57 // New: window size in pixels (top-left origin)
58 inline static spk::Extend2D _windowSize{}; // {x=0,y=0,width=0,height=0} by default
59
60 static void _applyViewport(const spk::Extend2D &p_extend);
61 static void _applyScissor(const spk::Extend2D &p_extend);
62 static spk::Matrix4x4 _computeScreenToOpenGLMatrix(const spk::Extend2D &p_viewport, size_t p_maxLayer);
63 static spk::Matrix4x4 _computeOpenGLToScreenMatrix(const spk::Matrix4x4 &p_screenToOpenGLMatrix);
64
65 public:
66 ViewRegion() = default;
67
72 void setViewport(const spk::Extend2D &p_extend) noexcept;
77 void setScissor(const spk::Extend2D &p_extend) noexcept;
78
83 const spk::Extend2D &viewport() const noexcept;
88 const spk::Extend2D &scissor() const noexcept;
89
94 static size_t maxLayer() noexcept;
95
100 static void setMaxLayer(size_t p_maxLayer) noexcept;
101
106 static void setWindowSize(const spk::Extend2D &p_windowSize) noexcept;
107
112 static const spk::Extend2D &windowSize() noexcept;
113
118 static const spk::OpenGL::ViewRegion *activeViewRegion();
119
123 void apply() const;
129 static void clear(const Color &p_backgroundColor, const ClearMask &p_clearMask = defaultClearMask);
130
137 static spk::Vector3 convertScreenToOpenGL(const spk::Vector2Int &p_screenPosition, float p_layer = 0.0f) noexcept;
138
145 static spk::Vector2Int convertOpenGLToScreen(const spk::Vector2 &p_openGLPosition, float p_layer = 0.0f) noexcept;
146 };
147 }
148}
static spk::IMatrix< SizeX, SizeY > identity()
Definition spk_matrix.hpp:139
Manages viewport and scissor rectangles and clearing.
Definition spk_view_region.hpp:26
void setScissor(const spk::Extend2D &p_extend) noexcept
Sets the GL scissor rectangle.
Definition spk_view_region.cpp:111
static void clear(const Color &p_backgroundColor, const ClearMask &p_clearMask=defaultClearMask)
Clears framebuffer with given color and mask.
Definition spk_view_region.cpp:190
const spk::Extend2D & scissor() const noexcept
Returns current scissor settings.
Definition spk_view_region.cpp:121
static spk::Vector2Int convertOpenGLToScreen(const spk::Vector2 &p_openGLPosition, float p_layer=0.0f) noexcept
Converts an OpenGL normalized position back to screen-space pixels.
Definition spk_view_region.cpp:209
static const spk::Extend2D & windowSize() noexcept
Returns the current window size.
Definition spk_view_region.cpp:141
static size_t maxLayer() noexcept
Returns the current configured maximum layer value.
Definition spk_view_region.cpp:126
const spk::Extend2D & viewport() const noexcept
Returns current viewport settings.
Definition spk_view_region.cpp:116
ClearMode
Buffer types that can be cleared.
Definition spk_view_region.hpp:32
static void setWindowSize(const spk::Extend2D &p_windowSize) noexcept
Sets the logical window size used for Y inversion (top-left -> bottom-left).
Definition spk_view_region.cpp:136
void apply() const
Applies viewport and scissor to OpenGL state.
Definition spk_view_region.cpp:151
static const spk::OpenGL::ViewRegion * activeViewRegion()
Returns the active view region used for OpenGL operations.
Definition spk_view_region.cpp:146
spk::Flags< ClearMode > ClearMask
Mask of buffers to clear at once.
Definition spk_view_region.hpp:41
static const ClearMask defaultClearMask
Default mask clearing color, depth, and stencil.
Definition spk_view_region.hpp:45
static spk::Vector3 convertScreenToOpenGL(const spk::Vector2Int &p_screenPosition, float p_layer=0.0f) noexcept
Converts a screen-space pixel position to OpenGL normalized device coordinates.
Definition spk_view_region.cpp:200
static void setMaxLayer(size_t p_maxLayer) noexcept
Sets the maximum layer value used for depth mapping.
Definition spk_view_region.cpp:131
void setViewport(const spk::Extend2D &p_extend) noexcept
Sets the GL viewport rectangle.
Definition spk_view_region.cpp:106
RGBA color utility storing normalized floating components.
Definition spk_color.hpp:18
Axis-aligned rectangle defined by an anchor point and size.
Definition spk_extend_2d.hpp:24
Type-safe bitmask wrapper for enum flags.
Definition spk_flags.hpp:111