11 OpenGLRenderer::OpenGLRenderer(eastl::string vertexShader, eastl::string fragmentShader) noexcept
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");
20 positionAttrib = this->shader->GetAttribute(
"a_position");
21 normalAttrib = this->shader->GetAttribute(
"a_normal");
22 textureAttrib = this->shader->GetAttribute(
"a_texture");
24 ImGui_ImplGlfwGL3_Init(Engine::GetEngine().lock()->GetWindow().lock()->GetGLFWWindow().lock().
get());
27 OpenGLRenderer::~OpenGLRenderer() noexcept
29 ImGui_ImplGlfwGL3_Shutdown();
32 void OpenGLRenderer::RendererBegin(
const glm::mat4x4 & view,
const glm::mat4x4 & projection)
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);
38 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
41 viewParam.lock()->SetValue(view);
42 projParam.lock()->SetValue(projection);
45 void OpenGLRenderer::Render(
const glm::mat4x4& modelMatrix, eastl::shared_ptr<Model> model,
const glm::vec4& mainColor)
50 modelParam.lock()->SetValue(modelMatrix);
51 mainTextureColor.lock()->SetValue(mainColor);
53 eastl::vector<eastl::shared_ptr<Mesh>>& meshes = model->GetModelMeshes();
54 for (
size_t i = 0, size = meshes.size(); i < size; ++i)
56 if (meshes[i] ==
nullptr)
continue;
58 if (glIsBuffer(GLuint(meshes[i]->GetVBO())) != GL_TRUE)
60 _ASSERT(
"The vertex buffer is not a valid buffer (VBO).");
62 if (glIsBuffer(GLuint(meshes[i]->GetEBO())) != GL_TRUE)
64 _ASSERT(
"The index buffer is not a valid buffer (EBO)");
68 glBindBuffer(GL_ARRAY_BUFFER, GLuint(meshes[i]->GetVBO()));
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));
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);
81 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, GLuint(meshes[i]->GetEBO()));
84 if (model->GetMeshMaterial(meshes[i])->IsDiffuseLoaded())
86 textureParam.lock()->SetValue(*model->GetMeshMaterial(meshes[i])->GetDiffuseTexture().lock());
90 glDrawElements(GL_TRIANGLES, GLsizei(meshes[i]->indices.size()), GL_UNSIGNED_INT, reinterpret_cast<const void*>(0));
93 glBindBuffer(GL_ARRAY_BUFFER, 0);
95 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 1);
98 positionAttrib.lock()->DisableAttributePointer();
99 normalAttrib.lock()->DisableAttributePointer();
100 textureAttrib.lock()->DisableAttributePointer();
104 void OpenGLRenderer::RendererEnd()
107 shader->Deactivate();
108 Engine::GetEngine().lock()->GetWindow().lock()->SwapBuffers();
111 #endif // USING_OPENGL
This object is used to store general data for a vertex.