-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.h
216 lines (195 loc) · 5.67 KB
/
renderer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
This file is part of OpenGL-StarterPack (GLSP), an open source OpenGL based framework
that facilitates and speeds up demo and project creation by offering an abstraction to
the basic objects of OpenGL as well as incluing the necessary libraries.
MIT License
Copyright (c) 2024 Antonio Espinosa Garcia
*/
#ifndef __RENDERER__
#define __RENDERER__
#include <GLSP/core.h>
#include <GLSP/utils.h>
#include <GLSP/framebuffer.h>
GLSP_NAMESPACE_BEGIN
/*
Stores window related data
*/
struct Window
{
Extent2D extent{800, 600};
Position2D position{50, 50};
const char *title;
GLFWwindow *ptr{nullptr};
bool fullscreen{false};
inline void set_fullscreen(bool op)
{
fullscreen = op;
if (!fullscreen)
{
glfwSetWindowMonitor(ptr, NULL, position.x, position.y, extent.width, extent.height, GLFW_DONT_CARE);
}
else
{
const GLFWvidmode *mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowMonitor(ptr, glfwGetPrimaryMonitor(), 0, 0, mode->width, mode->height, mode->refreshRate);
}
}
inline void should_close(bool op)
{
glfwSetWindowShouldClose(ptr, op);
}
};
/*
OpenGL context creation settings
*/
struct ContextSettings
{
int OpenGLMajor{4};
int OpenGLMinor{6};
int OpenGLProfile{GLFW_OPENGL_CORE_PROFILE};
};
struct RendererSettings
{
bool vSync{true};
int framerateCap{-1};
bool userInterface{true};
bool depthTest{true};
bool depthWrites{true};
bool blending{true};
};
/*
Core class. Implements all basic render functionality as the render loop and OpenGL context creation.
Should be inherited if user wants more complex functionality.
*/
class Renderer
{
protected:
const ContextSettings m_context{};
RendererSettings m_settings{};
Window m_window{};
struct Time
{
double delta{0.0};
double last{0.0};
double current{0.0};
int framerate{0};
};
Time m_time{};
utils::EventDispatcher m_cleanupQueue;
void create_context();
void tick();
void cleanup();
/*
Override function in order to initiate desired funcitonality. Call parent function if want to use events functionality.
*/
virtual void init();
/*
Override function in order to customize update.
*/
virtual void update();
/*
Override function in order to customize render funcitonality.
*/
virtual void draw();
/*
Setup GLFW window callbacks here
*/
virtual void setup_window_callbacks();
public:
Renderer(const char *title) { m_window.title = title; }
Renderer(Window &window) { m_window = window; }
Renderer(Window &window, ContextSettings &contextSettings, RendererSettings &settings) : m_window(window), m_context(contextSettings), m_settings(settings) {}
void run();
#pragma region GETTERS & SETTERS
inline RendererSettings get_settings() const
{
return m_settings;
}
inline void set_settings(RendererSettings &s)
{
m_settings = s;
}
inline Time get_time() const
{
return m_time;
}
inline void set_v_sync(bool op)
{
glfwSwapInterval(op);
m_settings.vSync = op;
}
#pragma endregion
/*
Use as callback
*/
inline virtual void resize(Extent2D extent, Position2D origin = {0, 0})
{
m_window.extent = extent;
Renderer::resize_viewport(extent, origin);
}
inline static void resize_viewport(Extent2D extent, Position2D origin = {0, 0})
{
GL_CHECK(glViewport(origin.x, origin.y, extent.width, extent.height));
}
inline static void enable_depth_test(bool op)
{
op ? glEnable(GL_DEPTH_TEST) : glDisable(GL_DEPTH_TEST);
}
inline static void enable_depth_writes(bool op)
{
GL_CHECK(glDepthMask(op));
}
inline static void set_depth_func(DepthFuncType func)
{
GL_CHECK(glDepthFunc(func));
}
inline static void enable_blend(bool op)
{
op ? glEnable(GL_BLEND) : glDisable(GL_BLEND);
}
inline static void set_blend_func(BlendFuncType src, BlendFuncType dst)
{
GL_CHECK(glBlendFunc(src, dst));
}
inline static void set_blend_func_separate(BlendFuncType src, BlendFuncType dst, BlendFuncType srcA = ONE, BlendFuncType dstA = ONE)
{
GL_CHECK(glBlendFuncSeparate(src, dst, srcA, dstA));
}
inline static void set_blend_func_separate(Framebuffer *fbo, BlendFuncType src, BlendFuncType dst, BlendFuncType srcA = ONE, BlendFuncType dstA = ONE)
{
GL_CHECK(glBlendFuncSeparatei(fbo->get_id(), src, dst, srcA, dstA));
}
inline static void set_blend_op(BlendOperationType op)
{
glBlendEquation(ADD);
}
inline static void enable_face_cull(bool op)
{
glCullFace(op);
}
inline static void set_polygon_mode(unsigned int face = GL_FRONT_AND_BACK, unsigned int mode = GL_FILL)
{
GL_CHECK(glPolygonMode(face, mode));
}
#pragma region USER INTERFACE
inline bool user_interface_wants_to_handle_input()
{
ImGuiIO &io = ImGui::GetIO();
if (io.WantCaptureMouse || io.WantCaptureKeyboard)
return true;
else
return false;
}
/*
Override if custom init functionality needed. Predetermined graphic user interface backend is IMGUI.
*/
virtual void init_user_interface();
/*
Override to add IMGUI windows and widgets.Call ImGui::NewFrame at start and ImGui::Render at the end. Don't forget to call parent function for IMGUI frame set up
*/
virtual void setup_user_interface_frame();
virtual void upload_user_interface_render_data();
#pragma endregion
};
GLSP_NAMESPACE_END
#endif