From e74ba8594b5d7f0835f5763a46573837fd211003 Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Wed, 13 Jul 2022 19:09:42 -0400 Subject: [PATCH] Renderer outline started VertexBuffer class implemented --- .gitignore | 3 +- CMakeLists.txt | 1 + src/graphics/renderer/frame_buffer.h | 21 ++++ src/graphics/renderer/render_context.h | 29 ++++++ src/graphics/renderer/renderer2D.h | 21 ++++ src/graphics/renderer/vertex_buffer.cpp | 125 ++++++++++++++++++++++++ src/graphics/renderer/vertex_buffer.h | 53 ++++++++++ 7 files changed, 252 insertions(+), 1 deletion(-) create mode 100644 src/graphics/renderer/frame_buffer.h create mode 100644 src/graphics/renderer/render_context.h create mode 100644 src/graphics/renderer/renderer2D.h create mode 100644 src/graphics/renderer/vertex_buffer.cpp create mode 100644 src/graphics/renderer/vertex_buffer.h diff --git a/.gitignore b/.gitignore index 4ba396e..9742238 100644 --- a/.gitignore +++ b/.gitignore @@ -66,4 +66,5 @@ build/ ui_files/ ######################## TEST FILE IGNORES -test*/ \ No newline at end of file +test*/ +/ui assets diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b1d9df..9f19b5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,6 +72,7 @@ set(LUNARIUM_SRC "src/utils/uuid.cpp" "src/platform/window.cpp" "src/platform/terminal.cpp" +"src/graphics/renderer/vertex_buffer.cpp" "src/graphics/opengl/glGraphics.cpp" "src/graphics/opengl/glText.cpp" "src/graphics/opengl/glShader.cpp" diff --git a/src/graphics/renderer/frame_buffer.h b/src/graphics/renderer/frame_buffer.h new file mode 100644 index 0000000..7ba47ad --- /dev/null +++ b/src/graphics/renderer/frame_buffer.h @@ -0,0 +1,21 @@ +/****************************************************************************** +* File - frame_buffer.h +* Author - Joey Pollack +* Date - 2022/07/13 (y/m/d) +* Mod Date - 2022/07/13 (y/m/d) +* Description - OpenGL Frame buffer object +******************************************************************************/ + +#ifndef LUNARIUM_FRAME_BUFFER_H_ +#define LUNARIUM_FRAME_BUFFER_H_ + +namespace lunarium +{ + class FrameBuffer + { + + }; +} + + +#endif // LUNARIUM_FRAME_BUFFER_H_ \ No newline at end of file diff --git a/src/graphics/renderer/render_context.h b/src/graphics/renderer/render_context.h new file mode 100644 index 0000000..4744ebf --- /dev/null +++ b/src/graphics/renderer/render_context.h @@ -0,0 +1,29 @@ +/****************************************************************************** +* File - render_context.h +* Author - Joey Pollack +* Date - 2022/07/13 (y/m/d) +* Mod Date - 2022/07/13 (y/m/d) +* Description - General setup/shutdown for the render system +******************************************************************************/ + +#ifndef LUNARIUM_RENDER_CONTEXT_H_ +#define LUNARIUM_RENDER_CONTEXT_H_ + +namespace lunarium +{ + class Window; + + // NOTE: + // This class will probably not be exposed outside of the Core + // The Core will expose Renderer2D instead + class RenderContext + { + + + private: + + Window* mpWindow; + }; +} + +#endif // LUNARIUM_RENDER_CONTEXT_H_ \ No newline at end of file diff --git a/src/graphics/renderer/renderer2D.h b/src/graphics/renderer/renderer2D.h new file mode 100644 index 0000000..ffde329 --- /dev/null +++ b/src/graphics/renderer/renderer2D.h @@ -0,0 +1,21 @@ +/****************************************************************************** +* File - renderer2D.h +* Author - Joey Pollack +* Date - 2022/07/13 (y/m/d) +* Mod Date - 2022/07/13 (y/m/d) +* Description - The main 2D render class +******************************************************************************/ + +#ifndef LUNARIUM_RENDERER_2D_H_ +#define LUNARIUM_RENDERER_2D_H_ + +namespace lunarium +{ + class Renderer2D + { + + }; +} + + +#endif // LUNARIUM_RENDERER_2D_H_ \ No newline at end of file diff --git a/src/graphics/renderer/vertex_buffer.cpp b/src/graphics/renderer/vertex_buffer.cpp new file mode 100644 index 0000000..0776510 --- /dev/null +++ b/src/graphics/renderer/vertex_buffer.cpp @@ -0,0 +1,125 @@ +/****************************************************************************** +* File - vertex_buffer.cpp +* Author - Joey Pollack +* Date - 2022/07/13 (y/m/d) +* Mod Date - 2022/07/13 (y/m/d) +* Description - OpenGL vertex buffer object +******************************************************************************/ + +#include "vertex_buffer.h" +#include + +#include + +namespace lunarium +{ + VertexBuffer::VertexBuffer(LayoutType ltype, const void* vertices, u32 size) + : mLayoutType(ltype), mSize(size), mIndex(0), mIsStatic(true) + { + glGenVertexArrays(1, &mVAO); + glGenBuffers(1, &mVBO); + + Bind(); + + glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW); + + InitLayout(); + } + + VertexBuffer::VertexBuffer(LayoutType ltype, u32 size) + : mLayoutType(ltype), mSize(size), mIndex(0), mIsStatic(false) + { + glGenVertexArrays(1, &mVAO); + glGenBuffers(1, &mVBO); + + Bind(); + + glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW); + + InitLayout(); + } + + void VertexBuffer::Bind() + { + glBindVertexArray(mVAO); + glBindBuffer(GL_ARRAY_BUFFER, mVBO); + } + + void VertexBuffer::Unbind() + { + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } + + bool VertexBuffer::PushVertices(const void* vertices, u32 size) + { + if (mIsStatic) + { + Logger::Warn(LogCategory::GRAPHICS, "Cannot push vertices into a static buffer!"); + return false; + } + + if (mIndex + size >= mSize) + { + // Vertices do not fit into the buffer + return false; + } + + // Add verts to the buffer + Bind(); + glBufferSubData(GL_ARRAY_BUFFER, mIndex, size, vertices); + Unbind(); + + mIndex += size; + return true; + } + + ///////////////////////////////////////////////////////////////////// + // HELPER METHODS + ///////////////////////////////////////////////////////////////////// + + void VertexBuffer::InitLayout() + { + + switch (mLayoutType) + { + case LayoutType::LINE: + { + Logger::Error(LogCategory::GRAPHICS, "Vertex layout type LINE not supported yet!"); + } + break; + + case LayoutType::QUAD: + { + // position + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)0); + glEnableVertexAttribArray(0); + + // Texture coords + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + + // Color + glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)(5 * sizeof(float))); + glEnableVertexAttribArray(2); + + // Texture sampler index + glVertexAttribPointer(0, 1, GL_FLOAT, GL_FALSE, 10 * sizeof(GLfloat), (GLvoid*)(9 * sizeof(float))); + glEnableVertexAttribArray(2); + + } + break; + + case LayoutType::SPRITE: + { + Logger::Error(LogCategory::GRAPHICS, "Vertex layout type SPRITE not supported yet!"); + } + break; + + default: + Logger::Warn(LogCategory::GRAPHICS, "UNKNOWN Vertex Layout Type: %d", mLayoutType); + + } + } + +} \ No newline at end of file diff --git a/src/graphics/renderer/vertex_buffer.h b/src/graphics/renderer/vertex_buffer.h new file mode 100644 index 0000000..f43b3c9 --- /dev/null +++ b/src/graphics/renderer/vertex_buffer.h @@ -0,0 +1,53 @@ +/****************************************************************************** +* File - vertex_buffer.h +* Author - Joey Pollack +* Date - 2022/07/13 (y/m/d) +* Mod Date - 2022/07/13 (y/m/d) +* Description - OpenGL vertex buffer object +******************************************************************************/ + +#ifndef LUNARIUM_VERTEX_BUFFER_H_ +#define LUNARIUM_VERTEX_BUFFER_H_ + +#include + +namespace lunarium +{ + // For now this class will also contain the VAO for the buffer + class VertexBuffer + { + public: + enum LayoutType + { + LINE, + QUAD, + SPRITE, + }; + + public: + + VertexBuffer(LayoutType ltype, const void* vertices, u32 size); + VertexBuffer(LayoutType ltype, u32 size); + + void Bind(); + void Unbind(); + + // Returns false if the data does not fit in the buffer + bool PushVertices(const void* vertices, u32 size); + + + private: + LayoutType mLayoutType; + bool mIsStatic; + u32 mVAO; + u32 mVBO; + u32 mSize; + u32 mIndex; // The next spot to push data into for a dynamic buffer + + private: // HELPER METHODS + + void InitLayout(); + }; +} + +#endif // LUNARIUM_VERTEX_BUFFER_H_ \ No newline at end of file