Texture and FrameBuffer classes implemented
parent
e74ba8594b
commit
ec492b119f
@ -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_
|
||||
@ -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_
|
||||
@ -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 <utils/logger.h>
|
||||
#include <glad/gl.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <core/common_defs.h>
|
||||
|
||||
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_
|
||||
@ -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 <platform/window.h>
|
||||
#include <utils/logger.h>
|
||||
|
||||
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<int, std::string> RenderContext::mDebugMsgTypes;
|
||||
std::map<int, std::string> RenderContext::mDebugMsgSources;
|
||||
std::map<int, std::string> 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);
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <core/common_defs.h>
|
||||
#include <utils/op_res.h>
|
||||
#include <glad/gl.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
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<int, std::string> mDebugMsgTypes;
|
||||
static std::map<int, std::string> mDebugMsgSources;
|
||||
static std::map<int, std::string> mDebugMsgSeverity;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // LUNARIUM_RENDER_CONTEXT_H_
|
||||
@ -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 <glad/gl.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@ -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 <core/common_defs.h>
|
||||
|
||||
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_
|
||||
Loading…
Reference in New Issue