Brenta Engine 1.1
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;
36
37void gl::load_opengl(bool gl_blending, bool gl_cull_face, bool gl_multisample,
38 bool gl_depth_test)
39{
40 GLADloadproc loadproc = (GLADloadproc) screen::get_proc_address();
41 if (!gladLoadGLLoader(loadproc))
42 {
43 ERROR("Failed to initialize GLAD");
44 exit(-1);
45 }
46
47 int SCR_WIDTH = screen::get_width();
48 int SCR_HEIGHT = screen::get_height();
49
50 glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); /* Set viewport */
51 glEnable(GL_DEPTH_TEST); /* Enable depth testing */
52 INFO("Enabled GL_DEPTH_TEST");
53
54 /* Enable blending for transparency */
55 if (gl_blending)
56 {
57 glEnable(GL_BLEND);
58 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
59 INFO("Enabled GL_BLEND (transparency)");
60 }
61
62 /* Enable face culling, draw only visible triangles
63 * based on their orientation (defined clockwise or counterclockwise) */
64 if (gl_cull_face)
65 {
66 glEnable(GL_CULL_FACE);
67 INFO("Enabled GL_CULL_FACE (draw only visible triangles)");
68 }
69
70 /* Enable multisampling
71 * Only works for a multisample buffer. */
72 if (gl_multisample)
73 {
74 glEnable(GL_MULTISAMPLE);
75 INFO("Enabled GL_MULTISAMPLE");
76 }
77
78 GLenum errcode = gl::check_error();
79 if (!errcode)
80 INFO("OpenGl loaded");
81}
82
83void gl::set_poligon_mode(GLboolean enable)
84{
85 if (enable)
86 {
87 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
88 INFO("Enabled GL_POLYGON_MODE (wireframe)");
89 }
90 else
91 {
92 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
93 INFO("Disabled GL_POLYGON_MODE (fill)");
94 }
95}
96
97void gl::set_viewport(int x, int y, int SCR_WIDTH, int SCR_HEIGHT)
98{
99 glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); /* Set viewport */
100}
101
102void gl::set_color(float r, float g, float b, float a)
103{
104 glClearColor(r, g, b, a); /* Set clear color */
105}
106
107void gl::draw_arrays(GLenum mode, int first, int count)
108{
109 glDrawArrays(mode, first, count);
110}
111
112void gl::draw_elements(GLenum mode, int count, GLenum type, const void *indices)
113{
114 glDrawElements(mode, count, type, indices);
115}
116
118{
119 /* Clear color and depth buffer */
120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
121}
122
123void gl::bind_vertex_array(unsigned int n)
124{
125 glBindVertexArray(n);
126}
127
128GLenum gl::check_error_(const char *file, int line)
129{
130 GLenum errorCode;
131 while ((errorCode = glGetError()) != GL_NO_ERROR)
132 {
133 std::string error;
134 switch (errorCode)
135 {
136 case GL_INVALID_ENUM:
137 error = "INVALID_ENUM";
138 break;
139 case GL_INVALID_VALUE:
140 error = "INVALID_VALUE";
141 break;
142 case GL_INVALID_OPERATION:
143 error = "INVALID_OPERATION";
144 break;
145 case GL_STACK_OVERFLOW:
146 error = "STACK_OVERFLOW";
147 break;
148 case GL_STACK_UNDERFLOW:
149 error = "STACK_UNDERFLOW";
150 break;
151 case GL_OUT_OF_MEMORY:
152 error = "OUT_OF_MEMORY";
153 break;
154 case GL_INVALID_FRAMEBUFFER_OPERATION:
155 error = "INVALID_FRAMEBUFFER_OPERATION";
156 break;
157 default:
158 error = "UNKNOWN";
159 break;
160 }
161
162 error += " | " + std::string(file) + " (" + std::to_string(line) + ")";
163 ERROR(error);
164 }
165 return errorCode;
166}
static void set_viewport(int x, int y, int SCR_WIDTH, int SCR_HEIGHT)
Set Viewport.
Definition gl_helper.cpp:97
static void set_color(float r, float g, float b, float a)
Set Clear Color.
static void clear()
Clear.
static void set_poligon_mode(GLboolean enable)
Set Poligon Mode.
Definition gl_helper.cpp:83
static void draw_elements(GLenum mode, int count, GLenum type, const void *indices)
Draw Elements.
static void load_opengl(bool gl_blending=true, bool gl_cull_face=true, bool gl_multisample=true, bool gl_depth_test=true)
Load OpenGL.
Definition gl_helper.cpp:37
static void bind_vertex_array(unsigned int n)
Enable Depth Test.
static GLenum check_error_(const char *file, int line)
Check OpenGL error.
static void draw_arrays(GLenum mode, int first, int count)
Draw Arrays.
static GLFWglproc get_proc_address()
Get the OpenGL function pointer.
Definition screen.cpp:100
static int get_width()
Get the width of the window.
Definition screen.cpp:105
static int get_height()
Get the height of the window.
Definition screen.cpp:110