diff --git a/CMakeLists.txt b/CMakeLists.txt index 9f19b5a..5ef6e1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -72,11 +72,11 @@ 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" -"src/graphics/orthographic_camera.cpp" +"src/renderer/render_context.cpp" +"src/renderer/vertex_buffer.cpp" +"src/renderer/orthographic_camera.cpp" +"src/renderer/texture.cpp" +"src/renderer/frame_buffer.cpp" "src/gui/gui.cpp" "src/gui/imgui_ext.cpp" "src/gui/panel.cpp" diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index 454be94..499abfe 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -7,6 +7,8 @@ Build System: ✔ Modify .sh scripts to recognize the noeditor flag @done (1/25/2022, 3:59:23 PM) Core: + ☐ Create log rotation system so that log files do not grow to GB in size... @critical + - If file is X MBs, check for file with .old ext and erase if exists, .old to current file and create new log file ☐ Figure out how to represent Unique Entities and entity instances - and how this will work with UUIDs @critical ✔ Implement generic serialization system @done(22-06-29 18:44) ✔ JSON serializable base class @done(22-06-29 17:41) diff --git a/src/graphics/renderer/frame_buffer.h b/src/graphics/renderer/frame_buffer.h deleted file mode 100644 index 7ba47ad..0000000 --- a/src/graphics/renderer/frame_buffer.h +++ /dev/null @@ -1,21 +0,0 @@ -/****************************************************************************** -* 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 deleted file mode 100644 index 4744ebf..0000000 --- a/src/graphics/renderer/render_context.h +++ /dev/null @@ -1,29 +0,0 @@ -/****************************************************************************** -* 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/graphics.h b/src/renderer/OLD/graphics.h similarity index 100% rename from src/graphics/graphics.h rename to src/renderer/OLD/graphics.h diff --git a/src/graphics/opengl/defaultShaders.h b/src/renderer/OLD/opengl/defaultShaders.h similarity index 100% rename from src/graphics/opengl/defaultShaders.h rename to src/renderer/OLD/opengl/defaultShaders.h diff --git a/src/graphics/opengl/glGraphics.cpp b/src/renderer/OLD/opengl/glGraphics.cpp similarity index 100% rename from src/graphics/opengl/glGraphics.cpp rename to src/renderer/OLD/opengl/glGraphics.cpp diff --git a/src/graphics/opengl/glGraphics.h b/src/renderer/OLD/opengl/glGraphics.h similarity index 100% rename from src/graphics/opengl/glGraphics.h rename to src/renderer/OLD/opengl/glGraphics.h diff --git a/src/graphics/opengl/glShader.cpp b/src/renderer/OLD/opengl/glShader.cpp similarity index 100% rename from src/graphics/opengl/glShader.cpp rename to src/renderer/OLD/opengl/glShader.cpp diff --git a/src/graphics/opengl/glShader.h b/src/renderer/OLD/opengl/glShader.h similarity index 100% rename from src/graphics/opengl/glShader.h rename to src/renderer/OLD/opengl/glShader.h diff --git a/src/graphics/opengl/glText.cpp b/src/renderer/OLD/opengl/glText.cpp similarity index 100% rename from src/graphics/opengl/glText.cpp rename to src/renderer/OLD/opengl/glText.cpp diff --git a/src/graphics/opengl/glText.h b/src/renderer/OLD/opengl/glText.h similarity index 100% rename from src/graphics/opengl/glText.h rename to src/renderer/OLD/opengl/glText.h diff --git a/src/renderer/frame_buffer.cpp b/src/renderer/frame_buffer.cpp new file mode 100644 index 0000000..2260e5e --- /dev/null +++ b/src/renderer/frame_buffer.cpp @@ -0,0 +1,94 @@ +/****************************************************************************** +* 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 +******************************************************************************/ + +#include "frame_buffer.h" +#include "texture.h" +#include +#include + +namespace lunarium +{ + FrameBuffer::FrameBuffer() + : mGLID(0), mTexture(nullptr), mRenderBufferID(0) + { + + } + + FrameBuffer* FrameBuffer::Create(int width, int height) + { + FrameBuffer* fb = new FrameBuffer; + fb->Resize(width, height); + return fb; + } + + void FrameBuffer::Destroy(FrameBuffer** ppBuf) + { + (*ppBuf)->Unbind(); + glDeleteFramebuffers(1, &(*ppBuf)->mGLID); + (*ppBuf) = nullptr; + } + + + void FrameBuffer::Resize(int width, int height) + { + if (mGLID != 0) + { + glDeleteFramebuffers(1, &mGLID); + } + + glGenFramebuffers(1, &mGLID); + Bind(); + + if (mTexture) + { + Texture::Destroy(&mTexture); + } + + mTexture = Texture::Create(nullptr, width, height); + + // Attach texture + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexture->GetGLID(), 0); + + // Render Buffer for depth/stencil testing + if (mRenderBufferID) + { + glDeleteRenderbuffers(1, &mRenderBufferID); + } + glGenRenderbuffers(1, &mRenderBufferID); + glBindRenderbuffer(GL_RENDERBUFFER, mRenderBufferID); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + + // Atach the render buffer + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, mRenderBufferID); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + { + Logger::Error(LogCategory::GRAPHICS, "Failed to create FrameBuffer object"); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + + Unbind(); + } + + void FrameBuffer::Bind() + { + glBindFramebuffer(GL_FRAMEBUFFER, mGLID); + } + + void FrameBuffer::Unbind() + { + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + + Texture* FrameBuffer::GetTexture() + { + return mTexture; + } + +} \ No newline at end of file diff --git a/src/renderer/frame_buffer.h b/src/renderer/frame_buffer.h new file mode 100644 index 0000000..5cba14d --- /dev/null +++ b/src/renderer/frame_buffer.h @@ -0,0 +1,41 @@ +/****************************************************************************** +* 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_ + +#include + +namespace lunarium +{ + class Texture; + + class FrameBuffer + { + public: + static FrameBuffer* Create(int width, int height); + static void Destroy(FrameBuffer** ppBuf); + + void Resize(int width, int height); + void Bind(); + void Unbind(); + + Texture* GetTexture(); + + private: + u32 mGLID; + Texture* mTexture; + u32 mRenderBufferID; + + private: + FrameBuffer(); + }; +} + + +#endif // LUNARIUM_FRAME_BUFFER_H_ \ No newline at end of file diff --git a/src/graphics/orthographic_camera.cpp b/src/renderer/orthographic_camera.cpp similarity index 100% rename from src/graphics/orthographic_camera.cpp rename to src/renderer/orthographic_camera.cpp diff --git a/src/graphics/orthographic_camera.h b/src/renderer/orthographic_camera.h similarity index 100% rename from src/graphics/orthographic_camera.h rename to src/renderer/orthographic_camera.h diff --git a/src/renderer/render_context.cpp b/src/renderer/render_context.cpp new file mode 100644 index 0000000..1023e3b --- /dev/null +++ b/src/renderer/render_context.cpp @@ -0,0 +1,142 @@ +/****************************************************************************** +* File - render_context.cpp +* 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 +******************************************************************************/ + +#include "render_context.h" +#include +#include + +namespace lunarium +{ + RenderContext::RenderContext() + : mIsInit(false), mpWindow(nullptr) + { + + } + + OpRes RenderContext::Initialize(Window* pWindow, bool enableDebugMessages = true) + { + if (!pWindow->IsInit()) + { + return OpRes::Fail("Can not initialize Render Context. The Window must be initialized first!"); + } + + if (mIsInit) + { + return OpRes::Fail("Can not initialize Render Context. It is already initialized."); + } + + mpWindow = pWindow; + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + if (enableDebugMessages) + { + InitDebugMsgSystem(); + } + + // TODO: Init render subsystems + + // TODO: Init text rendering + // OpRes res = mText.Initialize(); + // if (Failed(res)) + // { + // Logger::Warn(LogCategory::GRAPHICS, "Could not initialized the text renderer - %s", res.Description); + // } + + // Load the default internal font + // const char* font = "OpenSans-Regular.ttf"; + // // GenerateFontFileAt(font); + // mDefaultFont = mText.LoadFont(OpenFontData, OpenDataSize, font); + // if (mDefaultFont < 0) + // { + // Logger::Warn(LogCategory::GRAPHICS, "Unable to load the default font: %s", font); + // } + // else + // { + // Logger::Info(LogCategory::GRAPHICS, "Successfully created default font: %s", font); + // } + + return OpRes::OK(); + } + + void RenderContext::Shutdown() + { + + } + + void RenderContext::SwapBuffers() + { + mpWindow->SwapBuffers(); + } + + //////////////////////////////////////////////////////////// + // DEBUG MESSAGE CALLBACK + //////////////////////////////////////////////////////////// + std::map RenderContext::mDebugMsgTypes; + std::map RenderContext::mDebugMsgSources; + std::map RenderContext::mDebugMsgSeverity; + + void GLAPIENTRY MessageCallback(GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, const GLchar* message, const void* userParam) + { + + if (type == GL_DEBUG_TYPE_ERROR) + { + Logger::GIError(LogCategory::GRAPHICS, + "%s (type: %s, source: %s, severity: %s), message: %s", + (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), + RenderContext::mDebugMsgSources[source].c_str(), + RenderContext::mDebugMsgTypes[type].c_str(), + RenderContext::mDebugMsgSeverity[severity].c_str(), + message); + } + else + { + Logger::GIDebug(LogCategory::GRAPHICS, + "%s (type: %s, source: %s, severity: %s), message: %s", + (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""), + RenderContext::mDebugMsgSources[source].c_str(), + RenderContext::mDebugMsgTypes[type].c_str(), + RenderContext::mDebugMsgSeverity[severity].c_str(), + message); + } + } + + void RenderContext::InitDebugMsgSystem() + { + // DEBUG MESSAGE TYPES + mDebugMsgTypes[GL_DEBUG_TYPE_ERROR] = "ERROR"; + mDebugMsgTypes[GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR] = "DEPRECATED_BEHAVIOR"; + mDebugMsgTypes[GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR] = "UNDEFINED_BEHAVIOR"; + mDebugMsgTypes[GL_DEBUG_TYPE_PORTABILITY] = "PORTABILITY"; + mDebugMsgTypes[GL_DEBUG_TYPE_PERFORMANCE] = "PERFORMANCE"; + mDebugMsgTypes[GL_DEBUG_TYPE_OTHER] = "OTHER"; + mDebugMsgTypes[GL_DEBUG_TYPE_MARKER] = "MARKER"; + mDebugMsgTypes[GL_DEBUG_TYPE_PUSH_GROUP] = "PUSH_GROUP"; + mDebugMsgTypes[GL_DEBUG_TYPE_POP_GROUP] = "POP_GROUP"; + + // DEBUG MESSAGE SOURCES + mDebugMsgSources[GL_DEBUG_SOURCE_API] = "API"; + mDebugMsgSources[GL_DEBUG_SOURCE_WINDOW_SYSTEM] = "WINDOW_SYSTEM"; + mDebugMsgSources[GL_DEBUG_SOURCE_SHADER_COMPILER] = "SHADER_COMPILER"; + mDebugMsgSources[GL_DEBUG_SOURCE_THIRD_PARTY] = "THIRD_PARTY"; + mDebugMsgSources[GL_DEBUG_SOURCE_APPLICATION] = "APPLICATION"; + mDebugMsgSources[GL_DEBUG_SOURCE_OTHER] = "OTHER"; + + // DEBUG MESSAGE SEVERITY + mDebugMsgSeverity[GL_DEBUG_SEVERITY_HIGH] = "HIGH"; + mDebugMsgSeverity[GL_DEBUG_SEVERITY_MEDIUM] = "MEDIUM"; + mDebugMsgSeverity[GL_DEBUG_SEVERITY_LOW] = "LOW"; + mDebugMsgSeverity[GL_DEBUG_SEVERITY_NOTIFICATION] = "NOTIFICATION"; + + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageCallback(MessageCallback, 0); + } + +} \ No newline at end of file diff --git a/src/renderer/render_context.h b/src/renderer/render_context.h new file mode 100644 index 0000000..c107962 --- /dev/null +++ b/src/renderer/render_context.h @@ -0,0 +1,54 @@ +/****************************************************************************** +* 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_ + +#include +#include +#include +#include +#include + +namespace lunarium +{ + class Window; + + // NOTE: + // This class will probably not be exposed outside of the Core + // The Core will expose Renderer2D instead + class RenderContext + { + public: + RenderContext(); + OpRes Initialize(Window* pWindow, bool enableDebugMessages = true); + void Shutdown(); + void SwapBuffers(); + + + private: + bool mIsInit; + Window* mpWindow; + + // TODO: Manage window framebuffer? + + private: // HELPERS + + void InitDebugMsgSystem(); + + private: // Debug Message System + + friend void GLAPIENTRY MessageCallback(GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, const GLchar* message, const void* userParam); + static std::map mDebugMsgTypes; + static std::map mDebugMsgSources; + static std::map mDebugMsgSeverity; + }; +} + +#endif // LUNARIUM_RENDER_CONTEXT_H_ \ No newline at end of file diff --git a/src/graphics/renderer/renderer2D.h b/src/renderer/renderer2D.h similarity index 100% rename from src/graphics/renderer/renderer2D.h rename to src/renderer/renderer2D.h diff --git a/src/graphics/shaders/DefaultImage.frag b/src/renderer/shaders/DefaultImage.frag similarity index 100% rename from src/graphics/shaders/DefaultImage.frag rename to src/renderer/shaders/DefaultImage.frag diff --git a/src/graphics/shaders/DefaultImage.vert b/src/renderer/shaders/DefaultImage.vert similarity index 100% rename from src/graphics/shaders/DefaultImage.vert rename to src/renderer/shaders/DefaultImage.vert diff --git a/src/renderer/texture.cpp b/src/renderer/texture.cpp new file mode 100644 index 0000000..5150e5a --- /dev/null +++ b/src/renderer/texture.cpp @@ -0,0 +1,101 @@ +/****************************************************************************** +* File - texture.h +* Author - Joey Pollack +* Date - 2022/07/14 (y/m/d) +* Mod Date - 2022/07/14 (y/m/d) +* Description - OpenGL texture 2D +******************************************************************************/ + +#include "texture.h" +#include + +namespace lunarium +{ + Texture::Texture() + : mGLID(0), mWidth(0), mHeight(0), mFormat(TextureFormat::RGBA) + { + + } + + Texture* Texture::Create(u8* data, u32 width, u32 height, TextureFormat format) + { + u32 formats[2] = { GL_RGB, GL_RGBA }; + Texture* t = new Texture(); + + t->mFormat = format; + t->mWidth = width; + t->mHeight = height; + + glGenTextures(1, &t->mGLID); + t->Bind(); + + glTexImage2D(GL_TEXTURE_2D, 0, formats[format], width, height, 0, formats[format], GL_UNSIGNED_BYTE, data); + + glGenerateMipmap(GL_TEXTURE_2D); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + t->Unbind(); + return t; + } + + void Texture::Destroy(Texture** ppTex) + { + (*ppTex)->Unbind(); + glDeleteTextures(1, &(*ppTex)->mGLID); + (*ppTex) = nullptr; + } + + + void Texture::Bind() const + { + glBindTexture(GL_TEXTURE_2D, mGLID); + } + + void Texture::Unbind() const + { + glBindTexture(GL_TEXTURE_2D, 0); + } + + + u32 Texture::GetGLID() const + { + return mGLID; + } + + u64 Texture::GetGLID64() const + { + return (u64) mGLID; + } + + TextureFormat Texture::GetFormat() const + { + return mFormat; + } + + u32 Texture::GetWidth() const + { + return mWidth; + } + + u32 Texture::GetHeight() const + { + return mHeight; + } + + + u8* Texture::GetPixels() const + { + u32 format_size[2] = { 3, 4 }; + u8* buffer = new u8[mWidth * mHeight * format_size[mFormat]]; + + Bind(); + glGetTexImage(GL_TEXTURE_2D, 0, mFormat, GL_UNSIGNED_BYTE, (void*)buffer); + Unbind(); + + return buffer; + } + +} \ No newline at end of file diff --git a/src/renderer/texture.h b/src/renderer/texture.h new file mode 100644 index 0000000..b3b0e38 --- /dev/null +++ b/src/renderer/texture.h @@ -0,0 +1,50 @@ +/****************************************************************************** +* File - texture.h +* Author - Joey Pollack +* Date - 2022/07/14 (y/m/d) +* Mod Date - 2022/07/14 (y/m/d) +* Description - OpenGL texture 2D +******************************************************************************/ + +#ifndef LUNARIUM_TEXTURE_H_ +#define LUNARIUM_TEXTURE_H_ + +#include + +namespace lunarium +{ + enum TextureFormat + { + RGB, + RGBA, + }; + + class Texture + { + public: + static Texture* Create(u8* data = nullptr, u32 width = 1, u32 height = 1, TextureFormat format = TextureFormat::RGBA); + static void Destroy(Texture** ppTex); + + void Bind() const; + void Unbind() const; + + u32 GetGLID() const; + u64 GetGLID64() const; + TextureFormat GetFormat() const; + u32 GetWidth() const; + u32 GetHeight() const; + + u8* GetPixels() const; + + private: + TextureFormat mFormat; + u32 mWidth; + u32 mHeight; + u32 mGLID; + + private: + Texture(); + }; +} + +#endif // LUNARIUM_TEXTURE_H_ \ No newline at end of file diff --git a/src/graphics/renderer/vertex_buffer.cpp b/src/renderer/vertex_buffer.cpp similarity index 100% rename from src/graphics/renderer/vertex_buffer.cpp rename to src/renderer/vertex_buffer.cpp diff --git a/src/graphics/renderer/vertex_buffer.h b/src/renderer/vertex_buffer.h similarity index 100% rename from src/graphics/renderer/vertex_buffer.h rename to src/renderer/vertex_buffer.h diff --git a/src/utils/logger.h b/src/utils/logger.h index 06dd065..c9bb0bf 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -133,7 +133,7 @@ namespace lunarium // //////////////////////////////////////////////////////////////////////// // ======================================================================== - // MAIN LOGGERCLASS + // MAIN LOGGER CLASS // ======================================================================== // //////////////////////////////////////////////////////////////////////// class Logger