VoxelEngine
 
Loading...
Searching...
No Matches
RenderContext.h
1#pragma once
2
3#include <optional>
4
5#define GLM_ENABLE_EXPERIMENTAL
6#include <glm/glm.hpp>
7#include <glm/gtc/matrix_transform.hpp>
8
9#include <LWGL/buffer/IBuffer.h>
10
11#include "RenderPass.h"
12// #include <glad/glad.h>
13
14namespace gl {
15 class FBO;
16} // namespace gl
17
18namespace engine {
19
20 class Camera;
21 class Material;
22
23 // Represents OpenGL render state that can be batched
24 // struct RenderState {
25 // std::optional<GLenum> depthFunc = std::nullopt;
26
27 // // For future: blend mode, cull face, etc.
28 // // std::optional<GLenum> blendSrc, blendDst;
29 // // std::optional<GLenum> cullFace;
30
31 // bool operator==(const RenderState& other) const { return depthFunc == other.depthFunc; }
32
33 // bool operator<(const RenderState& other) const {
34 // // Compare depthFunc for ordering (nullopt < GL_LESS < GL_LEQUAL < ...)
35 // if (!depthFunc.has_value() && !other.depthFunc.has_value())
36 // return false;
37 // if (!depthFunc.has_value())
38 // return true;
39 // if (!other.depthFunc.has_value())
40 // return false;
41 // return depthFunc.value() < other.depthFunc.value();
42 // }
43 // };
44
46 gl::IBuffer* attributes;
47 const Material* material;
48 const Camera* camera = nullptr;
49 const gl::FBO* fbo = nullptr;
50 RenderPass::ID passMask = RenderPass::Scene;
51
52 struct {
53 std::optional<glm::mat4> projection;
54 std::optional<glm::mat4> view;
55 glm::mat4 model;
56 } matrices;
57
58
59 void setModelMatrix(const glm::vec3& position) {
60 matrices.model = glm::translate(glm::mat4(1.0f), position);
61 }
62
63 void setModelMatrix(const glm::vec3& position, float angle, const glm::vec3& axis) {
64 matrices.model = glm::translate(glm::rotate(glm::mat4(1.0f), angle, axis), position);
65 }
66 };
67
68 // Minimal context for batching - only contains per-drawcall data
70 gl::IBuffer* attributes;
71 glm::mat4 model;
72 };
73
74 // Groups multiple draw calls that share material, FBO, camera, etc.
75 // Only model matrices and attributes differ between draw calls.
76 //
77 // Performance notes:
78 // - Vector is heap-allocated, so stack overflow risk is minimal
79 // - Use reserve() when you know the approximate count to avoid reallocations
80 // - Best for batching 10+ draw calls with shared state
81 // - For very large batches (1000+), consider splitting into multiple groups
83 const Material* material;
84 const Camera* camera = nullptr;
85 const gl::FBO* fbo = nullptr;
86 RenderPass::ID passMask = RenderPass::Scene;
87
88 struct {
89 std::optional<glm::mat4> projection;
90 std::optional<glm::mat4> view;
91 } matrices;
92
93 std::vector<MinimalRenderContext> drawCalls;
94
95 // Reserve space to avoid reallocations when building large groups
96 void reserve(size_t count) { drawCalls.reserve(count); }
97
98 // Add a draw call (helper for convenience)
99 void addDrawCall(gl::IBuffer* attributes, const glm::mat4& model) {
100 drawCalls.push_back({attributes, model});
101 }
102 };
103
104} // namespace engine
Definition Camera.h:29
Definition Material.h:7
Definition RenderContext.h:82
Definition RenderContext.h:69
Definition RenderContext.h:45