Engine
Frameworkcreatedbymeusableforthecreationofsimplegames.CurrentlysupportsOpenGL(Verysimple)andVulkan.
OpenGLRenderer.cpp
Go to the documentation of this file.
2 
3 #ifdef USING_OPENGL
7 #include "Engine/Mesh/Mesh.hpp"
8 #include "Engine/engine.hpp"
9 
10 namespace Engine {
11  OpenGLRenderer::OpenGLRenderer(eastl::string vertexShader, eastl::string fragmentShader) noexcept
12  {
13  this->shader = eastl::shared_ptr<OpenGLShader>(new OpenGLShader(vertexShader, fragmentShader));
14  projParam = this->shader->GetParameter("u_projection");
15  modelParam = this->shader->GetParameter("u_model");
16  viewParam = this->shader->GetParameter("u_view");
17  textureParam = this->shader->GetParameter("u_texture");
18  mainTextureColor = this->shader->GetParameter("u_mainTextColor");
19 
20  positionAttrib = this->shader->GetAttribute("a_position");
21  normalAttrib = this->shader->GetAttribute("a_normal");
22  textureAttrib = this->shader->GetAttribute("a_texture");
23 
24  ImGui_ImplGlfwGL3_Init(Engine::GetEngine().lock()->GetWindow().lock()->GetGLFWWindow().lock().get());
25  }
26 
27  OpenGLRenderer::~OpenGLRenderer() noexcept
28  {
29  ImGui_ImplGlfwGL3_Shutdown();
30  }
31 
32  void OpenGLRenderer::RendererBegin(const glm::mat4x4 & view, const glm::mat4x4 & projection)
33  {
34  ImGui_ImplGlfwGL3_NewFrame();
35  glViewport(0, 0, Engine::GetEngine().lock()->GetWindow().lock()->GetDisplayWidth(), Engine::GetEngine().lock()->GetWindow().lock()->GetDisplayHeight());
36  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
37  glEnable(GL_BLEND);
38  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
39  shader->Activate();
40 
41  viewParam.lock()->SetValue(view);
42  projParam.lock()->SetValue(projection);
43  }
44 
45  void OpenGLRenderer::Render(const glm::mat4x4& modelMatrix, eastl::shared_ptr<Model> model, const glm::vec4& mainColor)
46  {
47  if (model == nullptr)
48  return;
49 
50  modelParam.lock()->SetValue(modelMatrix);
51  mainTextureColor.lock()->SetValue(mainColor);
52 
53  eastl::vector<eastl::shared_ptr<Mesh>>& meshes = model->GetModelMeshes();
54  for (size_t i = 0, size = meshes.size(); i < size; ++i)
55  {
56  if (meshes[i] == nullptr) continue;
57 
58  if (glIsBuffer(GLuint(meshes[i]->GetVBO())) != GL_TRUE)
59  {
60  _ASSERT("The vertex buffer is not a valid buffer (VBO).");
61  }
62  if (glIsBuffer(GLuint(meshes[i]->GetEBO())) != GL_TRUE)
63  {
64  _ASSERT("The index buffer is not a valid buffer (EBO)");
65  }
66 
67  // Bind the buffers to the global state
68  glBindBuffer(GL_ARRAY_BUFFER, GLuint(meshes[i]->GetVBO()));
69  glGetError();
70 
71  // Default to VBO values, the pointer addresses are interpreted as byte-offsets.
72  const void* firstPosition = reinterpret_cast<const void*>(offsetof(Vertex, position));
73  const void* firstNormal = reinterpret_cast<const void*>(offsetof(Vertex, normal));
74  const void* firstTexture = reinterpret_cast<const void*>(offsetof(Vertex, texCoords));
75 
76  GLsizei stride = sizeof(Vertex);
77  positionAttrib.lock()->SetAttributePointer(3, GL_FLOAT, GL_FALSE, stride, firstPosition);
78  normalAttrib.lock()->SetAttributePointer(3, GL_FLOAT, GL_FALSE, stride, firstNormal);
79  textureAttrib.lock()->SetAttributePointer(2, GL_FLOAT, GL_FALSE, stride, firstTexture);
80 
81  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, GLuint(meshes[i]->GetEBO()));
82  glGetError();
83 
84  if (model->GetMeshMaterial(meshes[i])->IsDiffuseLoaded())
85  {
86  textureParam.lock()->SetValue(*model->GetMeshMaterial(meshes[i])->GetDiffuseTexture().lock());
87  }
88  glGetError();
89 
90  glDrawElements(GL_TRIANGLES, GLsizei(meshes[i]->indices.size()), GL_UNSIGNED_INT, reinterpret_cast<const void*>(0));
91  glGetError();
92 
93  glBindBuffer(GL_ARRAY_BUFFER, 0);
94  glGetError();
95  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 1);
96  glGetError();
97 
98  positionAttrib.lock()->DisableAttributePointer();
99  normalAttrib.lock()->DisableAttributePointer();
100  textureAttrib.lock()->DisableAttributePointer();
101  }
102  }
103 
104  void OpenGLRenderer::RendererEnd()
105  {
106  ImGui::Render();
107  shader->Deactivate();
108  Engine::GetEngine().lock()->GetWindow().lock()->SwapBuffers();
109  }
110 } // namespace Engine
111 #endif // USING_OPENGL
This object is used to store general data for a vertex.
Definition: Vertex.hpp:6
IMGUI_API void Render()
Definition: imgui.cpp:2769