Elevate Engine 1
Loading...
Searching...
No Matches
OpenGLRendererAPI.cpp
Go to the documentation of this file.
1#include "eepch.h"
2#include "OpenGLRendererAPI.h"
3
5#include <GLFW/glfw3.h>
6
9
10namespace Elevate
11{
13 {
14 switch (type)
15 {
16 case DrawPrimitiveType::Points: return GL_POINTS;
17 case DrawPrimitiveType::Lines: return GL_LINES;
18 case DrawPrimitiveType::LineStrip: return GL_LINE_STRIP;
19 case DrawPrimitiveType::Triangles: return GL_TRIANGLES;
20 case DrawPrimitiveType::TriangleStrip: return GL_TRIANGLE_STRIP;
21 case DrawPrimitiveType::TriangleFan: return GL_TRIANGLE_FAN;
22 case DrawPrimitiveType::Patches: return GL_PATCHES;
23 default: return GL_TRIANGLES;
24 }
25 return GL_TRIANGLES;
26 }
27
28 void OpenGLRendererAPI::SetClearColor(const glm::vec4& color) const
29 {
30 glClearColor(color.r, color.g, color.b, color.a);
31 }
32
33 void OpenGLRendererAPI::SetViewport(int x, int y, int width, int height) const
34 {
35 glViewport(x, y, width, height);
36 }
37
39 {
40 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
41 }
42
44 {
45 glFlush();
46 }
47
49 {
50 glActiveTexture(GL_TEXTURE0);
51 glBindTexture(GL_TEXTURE_2D, 0);
52 }
53
55 {
56 if (!glfwGetCurrentContext()) {
57 EE_CORE_ERROR("No OpenGL context active!");
58 return;
59 }
60
61 if (!vao)
62 {
63 EE_CORE_ERROR("VAO or IndexBuffer is null!");
64 return;
65 }
66
67 vao->Bind();
68
69 if (vao->GetIndexBuffer())
70 {
71 if (vao->GetIndexBuffer()->GetCount() == 0)
72 {
73 EE_CORE_ERROR("IndexBuffer has 0 indices!");
74 return;
75 }
76
77 GLCheck(glDrawElements(DrawPrimitiveTypeToOpenGL(primitive), vao->GetIndexBuffer()->GetCount(), GL_UNSIGNED_INT, nullptr));
78 }
79 else
80 {
81 for (const auto& vbo : vao->GetVertexBuffers())
82 {
83 uint32_t vertexCount = vbo->GetSize() / vbo->GetLayout().GetStride();
84 GLCheck(glDrawArrays(DrawPrimitiveTypeToOpenGL(primitive), 0, vertexCount));
85 }
86 }
87 }
88
89 void OpenGLRendererAPI::SetCullingState(bool enabled) const
90 {
91 if(enabled) glEnable(GL_CULL_FACE);
92 else glDisable(GL_CULL_FACE);
93 }
95 {
96 glDepthMask(enabled ? GL_TRUE : GL_FALSE);
97 }
99 {
100 if (enabled) glEnable(GL_DEPTH_TEST);
101 else glDisable(GL_DEPTH_TEST);
102 }
103}
#define GLCheck(x)
Definition GLDebug.h:11
virtual void SetDepthWrittingState(bool enabled) const override
virtual void SetDepthTestingState(bool enabled) const override
virtual void DrawArray(const VertexArray *vao, DrawPrimitiveType primitive=DrawPrimitiveType::Triangles) const override
virtual void Clear() const override
virtual void SetViewport(int x, int y, int width, int height) const override
virtual void FlushBuffers() const override
virtual void ClearTextureBindings() const override
virtual void SetCullingState(bool enabled) const override
virtual void SetClearColor(const glm::vec4 &color) const override
GLenum DrawPrimitiveTypeToOpenGL(DrawPrimitiveType type) const
virtual void Bind() const =0
const std::vector< std::shared_ptr< VertexBuffer > > & GetVertexBuffers() const
Definition VertexArray.h:21
const std::shared_ptr< IndexBuffer > GetIndexBuffer() const
Definition VertexArray.h:22
DrawPrimitiveType
Definition RendererAPI.h:17
unsigned int GLenum