Brenta Engine 1.0
Loading...
Searching...
No Matches
gl_helper.cpp
1/*
2 * MIT License
3 *
4 * Copyright (c) 2024 Giovanni Santini
5
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 */
26
27#include "gl_helper.hpp"
28
29#include "engine_logger.hpp"
30#include "screen.hpp"
31#include "text.hpp"
32
33#include <iostream>
34
35using namespace Brenta;
36using namespace Brenta::Utils;
37
38void GL::LoadOpenGL(bool gl_blending, bool gl_cull_face, bool gl_multisample,
39 bool gl_depth_test)
40{
41 GLADloadproc loadproc = (GLADloadproc) Screen::GetProcAddress();
42 if (!gladLoadGLLoader(loadproc))
43 {
44 ERROR("Failed to initialize GLAD");
45 exit(-1);
46 }
47
48 int SCR_WIDTH = Screen::GetWidth();
49 int SCR_HEIGHT = Screen::GetHeight();
50
51 glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); /* Set viewport */
52 glEnable(GL_DEPTH_TEST); /* Enable depth testing */
53 INFO("Enabled GL_DEPTH_TEST");
54
55 /* Enable blending for transparency */
56 if (gl_blending)
57 {
58 glEnable(GL_BLEND);
59 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
60 INFO("Enabled GL_BLEND (transparency)");
61 }
62
63 /* Enable face culling, draw only visible triangles
64 * based on their orientation (defined clockwise or counterclockwise) */
65 if (gl_cull_face)
66 {
67 glEnable(GL_CULL_FACE);
68 INFO("Enabled GL_CULL_FACE (draw only visible triangles)");
69 }
70
71 /* Enable multisampling
72 * Only works for a multisample buffer. */
73 if (gl_multisample)
74 {
75 glEnable(GL_MULTISAMPLE);
76 INFO("Enabled GL_MULTISAMPLE");
77 }
78
79 GLenum errcode = GL::glCheckError();
80 if (!errcode)
81 INFO("OpenGl loaded");
82}
83
84void GL::SetPoligonMode(GLboolean enable)
85{
86 if (enable)
87 {
88 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
89 INFO("Enabled GL_POLYGON_MODE (wireframe)");
90 }
91 else
92 {
93 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
94 INFO("Disabled GL_POLYGON_MODE (fill)");
95 }
96}
97
98void GL::SetViewport(int x, int y, int SCR_WIDTH, int SCR_HEIGHT)
99{
100 glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); /* Set viewport */
101 INFO("Set viewport: y = ", SCR_WIDTH, "x = ", SCR_HEIGHT);
102}
103
104void GL::SetColor(float r, float g, float b, float a)
105{
106 glClearColor(r, g, b, a); /* Set clear color */
107}
108
109void GL::DrawArrays(GLenum mode, int first, int count)
110{
111 glDrawArrays(mode, first, count);
112}
113
114void GL::DrawElements(GLenum mode, int count, GLenum type, const void *indices)
115{
116 glDrawElements(mode, count, type, indices);
117}
118
120{
121 /* Clear color and depth buffer */
122 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123}
124
125void GL::BindVertexArray(unsigned int n)
126{
127 glBindVertexArray(n);
128}
129
130GLenum GL::glCheckError_(const char *file, int line)
131{
132 GLenum errorCode;
133 while ((errorCode = glGetError()) != GL_NO_ERROR)
134 {
135 std::string error;
136 switch (errorCode)
137 {
138 case GL_INVALID_ENUM:
139 error = "INVALID_ENUM";
140 break;
141 case GL_INVALID_VALUE:
142 error = "INVALID_VALUE";
143 break;
144 case GL_INVALID_OPERATION:
145 error = "INVALID_OPERATION";
146 break;
147 case GL_STACK_OVERFLOW:
148 error = "STACK_OVERFLOW";
149 break;
150 case GL_STACK_UNDERFLOW:
151 error = "STACK_UNDERFLOW";
152 break;
153 case GL_OUT_OF_MEMORY:
154 error = "OUT_OF_MEMORY";
155 break;
156 case GL_INVALID_FRAMEBUFFER_OPERATION:
157 error = "INVALID_FRAMEBUFFER_OPERATION";
158 break;
159 }
160
161 error += " | " + std::string(file) + " (" + std::to_string(line) + ")";
162 ERROR(error);
163 }
164 return errorCode;
165}
static void LoadOpenGL(bool gl_blending=true, bool gl_cull_face=true, bool gl_multisample=true, bool gl_depth_test=true)
Load OpenGL.
Definition gl_helper.cpp:38
static void DrawElements(GLenum mode, int count, GLenum type, const void *indices)
Draw Elements.
static GLenum glCheckError_(const char *file, int line)
Check OpenGL error.
static void DrawArrays(GLenum mode, int first, int count)
Draw Arrays.
static void SetColor(float r, float g, float b, float a)
Set Clear Color.
static void Clear()
Clear.
static void BindVertexArray(unsigned int n)
Enable Depth Test.
static void SetViewport(int x, int y, int SCR_WIDTH, int SCR_HEIGHT)
Set Viewport.
Definition gl_helper.cpp:98
static void SetPoligonMode(GLboolean enable)
Set Poligon Mode.
Definition gl_helper.cpp:84
static int GetHeight()
Get the height of the window.
Definition screen.cpp:110
static GLFWglproc GetProcAddress()
Get the OpenGL function pointer.
Definition screen.cpp:100
static int GetWidth()
Get the width of the window.
Definition screen.cpp:105