Euphoria
init.cc
Go to the documentation of this file.
1 #include "render/init.h"
2 
3 #include <iostream>
4 
5 #include "log/log.h"
6 #include "assert/assert.h"
7 
8 #include "render/gl.h"
9 
10 
11 namespace eu::render
12 {
14  : is_ok(true)
15  {
16  const int glad_result = gladLoadGLLoader(loader);
17  if(glad_result == 0)
18  {
19  LOG_ERROR("Failed to init glad, error: {0}", glad_result);
20  is_ok = false;
21  }
22 
23  {
24  const std::string gl_vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
25  const std::string gl_renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
26  const std::string gl_version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
27  const std::string gl_shading_language_version = reinterpret_cast<const char*>(glGetString(GL_SHADING_LANGUAGE_VERSION));
28 
29  LOG_INFO("Vendor: {0}", gl_vendor);
30  LOG_INFO("Renderer: {0}", gl_renderer);
31  LOG_INFO("Version OpenGL: {0}", gl_version);
32  LOG_INFO("Version GLSL: {0}", gl_shading_language_version);
33  }
34 
35  int attribute_count = 0;
36  glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &attribute_count);
37  LOG_INFO("Max shader attribute count: {0}", attribute_count);
38 
39  // todo(Gustav): move this to a better place
40  glEnable(GL_DEPTH_TEST);
41  glEnable(GL_STENCIL_TEST);
42  glEnable(GL_SCISSOR_TEST); // need scissor test for the viewport clearing
43 
44  if(blend_hack == Init::BlendHack::enable_hack)
45  {
46  LOG_INFO("Enabled blend hack");
47  // need to be enabled for shitty 2d rendering to work
48  // todo(Gustav): fix a proper blending/backface culling render stack
49  glDisable(GL_CULL_FACE);
50  // glEnable(GL_CULL_FACE);
51  glEnable(GL_BLEND);
52  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
53  }
54  }
55 
56 
57  Init::~Init() = default;
58 
59 
60  mat4f
61  Init::get_ortho_projection(float width, float height) const
62  {
63  ASSERT(is_ok);
64  return mat4f::create_ortho(0.0f, width, 0.0f, height, -1.0f, 1.0f);
65  }
66 
67 
68  void
69  Init::use_2d() const
70  {
71  ASSERT(is_ok);
72  glDisable(GL_DEPTH_TEST);
73  }
74 
75 
76  void
77  Init::clear_screen(const Rgb& color) const
78  {
79  ASSERT(is_ok);
80  glClearColor(color.r, color.g, color.b, 1.0f);
81  // glClearStencil(0xFF);
82  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
83  }
84 
85 }
#define ASSERT(x)
Definition: assert.h:29
#define LOG_INFO(...)
Definition: log.h:7
#define LOG_ERROR(...)
Definition: log.h:9
void *(*)(const char *name) LoaderFunction
Definition: init.h:9
Definition: rgb.h:62
Definition: mat4.h:14
static mat4f create_ortho(float l, float r, float b, float t, float n, float f)
Definition: mat4.cc:392
void clear_screen(const Rgb &color) const
Definition: init.cc:77
mat4f get_ortho_projection(float width, float height) const
Definition: init.cc:61
void use_2d() const
Definition: init.cc:69
Init(LoaderFunction loader, BlendHack blend_hack=BlendHack::no_hack)
Definition: init.cc:13
bool is_ok
Definition: init.h:20