diff --git a/Core/Application/Application.cpp b/Core/Application/Application.cpp index dc7792b..660a045 100644 --- a/Core/Application/Application.cpp +++ b/Core/Application/Application.cpp @@ -1,5 +1,4 @@ #include "Application.h" -#include "SDL_scancode.h" #include #include @@ -10,7 +9,9 @@ namespace CGL::Core bool g_isTestMode{ false }; Application::Application(std::string_view name, i32 argc, char** argv) - : m_name(name), m_isRunning(true), m_window(nullptr) + : m_isRunning(true) + , m_name(name) + , m_window(nullptr) { // Parse command line arguments for (int i = 1; i < argc; ++i) @@ -56,7 +57,9 @@ namespace CGL::Core { switch (e.window.event) { - case SDL_WINDOWEVENT_RESIZED: OnResize(e.window.data1, e.window.data2); break; + case SDL_WINDOWEVENT_RESIZED: + OnResize(e.window.data1, e.window.data2); + break; } } } @@ -80,21 +83,18 @@ namespace CGL::Core } } } - OnShutdown(); } - bool Application::OnInit() + bool Application::OnInit() { - // Create SDL window + // Create SDL window u32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI; - #if defined(CGL_RHI_OPENGL) flags |= SDL_WINDOW_OPENGL; #elif defined(CGL_RHI_VULKAN) flags |= SDL_WINDOW_VULKAN; #elif defined(CGL_RHI_METAL) - flags |= SDL_WINDOW_METAL; #endif diff --git a/Core/Application/Application.h b/Core/Application/Application.h index 71768d3..e6ec913 100644 --- a/Core/Application/Application.h +++ b/Core/Application/Application.h @@ -1,7 +1,7 @@ #pragma once +#include #include #include -#include struct SDL_Window; diff --git a/Core/External/SimpleMath.h b/Core/External/SimpleMath.h index 5df676e..21a165a 100644 --- a/Core/External/SimpleMath.h +++ b/Core/External/SimpleMath.h @@ -14,6 +14,12 @@ #include #endif +#if defined(_MSC_VER) + #define CGL_CDECL __cdecl +#else + #define CGL_CDECL +#endif + #include #include #include @@ -1017,7 +1023,7 @@ namespace DirectX float Dot(const Quaternion& Q) const noexcept; void RotateTowards(const Quaternion& target, float maxAngle) noexcept; - void __cdecl RotateTowards(const Quaternion& target, float maxAngle, Quaternion& result) const noexcept; + void CGL_CDECL RotateTowards(const Quaternion& target, float maxAngle, Quaternion& result) const noexcept; // Computes rotation about y-axis (y), then x-axis (x), then z-axis (z) Vector3 ToEuler() const noexcept; @@ -1043,11 +1049,10 @@ namespace DirectX static void Concatenate(const Quaternion& q1, const Quaternion& q2, Quaternion& result) noexcept; static Quaternion Concatenate(const Quaternion& q1, const Quaternion& q2) noexcept; - static void __cdecl FromToRotation(const Vector3& fromDir, const Vector3& toDir, - Quaternion& result) noexcept; + static void CGL_CDECL FromToRotation(const Vector3& fromDir, const Vector3& toDir, Quaternion& result) noexcept; static Quaternion FromToRotation(const Vector3& fromDir, const Vector3& toDir) noexcept; - static void __cdecl LookRotation(const Vector3& forward, const Vector3& up, Quaternion& result) noexcept; + static void CGL_CDECL LookRotation(const Vector3& forward, const Vector3& up, Quaternion& result) noexcept; static Quaternion LookRotation(const Vector3& forward, const Vector3& up) noexcept; static float Angle(const Quaternion& q1, const Quaternion& q2) noexcept; @@ -1254,15 +1259,16 @@ namespace DirectX : x(ix), y(iy), width(iw), height(ih), minDepth(iminz), maxDepth(imaxz) { } - explicit Viewport(const RECT& rct) noexcept - : x(float(rct.left)) - , y(float(rct.top)) - , width(float(rct.right - rct.left)) - , height(float(rct.bottom - rct.top)) - , minDepth(0.f) - , maxDepth(1.f) + +#ifdef CGL_PLATFORM_WINDOWS + explicit Viewport(const RECT& rct) noexcept : + x(float(rct.left)), y(float(rct.top)), + width(float(rct.right - rct.left)), + height(float(rct.bottom - rct.top)), + minDepth(0.f), maxDepth(1.f) { } +#endif #if defined(__d3d11_h__) || defined(__d3d11_x_h__) // Direct3D 11 interop @@ -1314,8 +1320,9 @@ namespace DirectX #endif // Assignment operators - Viewport& operator=(const RECT& rct) noexcept; - +#ifdef CGL_PLATFORM_WINDOWS + Viewport& operator= (const RECT& rct) noexcept; +#endif // Viewport operations float AspectRatio() const noexcept; diff --git a/Core/External/SimpleMath.inl b/Core/External/SimpleMath.inl index 83cf820..ad26ffa 100644 --- a/Core/External/SimpleMath.inl +++ b/Core/External/SimpleMath.inl @@ -3797,7 +3797,8 @@ inline bool Viewport::operator!=(const Viewport& vp) const noexcept // Assignment operators //------------------------------------------------------------------------------ -inline Viewport& Viewport::operator=(const RECT& rct) noexcept +#ifdef CGL_PLATFORM_WINDOWS +inline Viewport& Viewport::operator= (const RECT& rct) noexcept { x = float(rct.left); y = float(rct.top); @@ -3807,6 +3808,7 @@ inline Viewport& Viewport::operator=(const RECT& rct) noexcept maxDepth = 1.f; return *this; } +#endif #if defined(__d3d11_h__) || defined(__d3d11_x_h__) inline Viewport& Viewport::operator=(const D3D11_VIEWPORT& vp) noexcept diff --git a/Core/Graphics/Buffer.h b/Core/Graphics/Buffer.h index aa5a819..619ed0e 100644 --- a/Core/Graphics/Buffer.h +++ b/Core/Graphics/Buffer.h @@ -13,8 +13,9 @@ #endif #if defined(CGL_RHI_OPENGL) - #include #include + #include + #include #endif #if defined(CGL_RHI_METAL) @@ -49,6 +50,8 @@ namespace CGL::Graphics #elif defined(CGL_RHI_OPENGL) using VertexBuffer = OPENGLVertexBuffer; using IndexBuffer = OPENGLIndexBuffer; + template + using ConstantBuffer = OPENGLConstantBuffer; #elif defined(CGL_RHI_METAL) using VertexBuffer = METALVertexBuffer; using IndexBuffer = METALIndexBuffer; diff --git a/Core/Graphics/RHI/OpenGL/OPENGLConstantBuffer.h b/Core/Graphics/RHI/OpenGL/OPENGLConstantBuffer.h new file mode 100644 index 0000000..52f20ce --- /dev/null +++ b/Core/Graphics/RHI/OpenGL/OPENGLConstantBuffer.h @@ -0,0 +1,18 @@ +#pragma once +#include + +namespace CGL::Graphics +{ + template + struct OPENGLConstantBuffer + { + using value_type = T; + + OPENGLConstantBuffer() + { + // Ensure 16-byte alignment + static_assert((sizeof(T) % 16) == 0, "Constant buffer size must be 16-byte aligned."); + } + GLuint Buffer; + }; +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/Graphics/RHI/OpenGL/OPENGLIndexBuffer.h b/Core/Graphics/RHI/OpenGL/OPENGLIndexBuffer.h new file mode 100644 index 0000000..26d6ee9 --- /dev/null +++ b/Core/Graphics/RHI/OpenGL/OPENGLIndexBuffer.h @@ -0,0 +1,11 @@ +#pragma once +#include + +namespace CGL::Graphics +{ + struct OPENGLIndexBuffer + { + u32 Stride; + GLuint EBO; + }; +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/Graphics/RHI/OpenGL/OPENGLPixelShader.h b/Core/Graphics/RHI/OpenGL/OPENGLPixelShader.h new file mode 100644 index 0000000..3d9e09a --- /dev/null +++ b/Core/Graphics/RHI/OpenGL/OPENGLPixelShader.h @@ -0,0 +1,10 @@ +#pragma once +#include + +namespace CGL::Graphics +{ + struct OPENGLPixelShader + { + GLuint FragmentShader; + }; +} // namespace CGL::Graphics diff --git a/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp b/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp index b341166..c3f69b7 100644 --- a/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp +++ b/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp @@ -3,11 +3,34 @@ namespace CGL::Graphics { -#ifdef CGL_RHI_OPENGL +#if defined(CGL_RHI_OPENGL) + + namespace Mapping + { + static constexpr std::array PrimitiveTopology = + { + GL_TRIANGLES, + GL_LINES, + GL_POINTS, + GL_TRIANGLE_STRIP, + GL_LINE_STRIP + }; + + static constexpr std::array BufferUsage = + { + GL_STATIC_DRAW, + GL_DYNAMIC_DRAW, + GL_STREAM_DRAW, + GL_STREAM_READ + }; + + static_assert(PrimitiveTopology.size() == size_t(PrimitiveType::COUNT)); + static_assert(BufferUsage.size() == size_t(BufferUsage::COUNT)); + } + void Renderer::Constructor_OPENGL(SDL_Window* window) { this->m_impl = new OPENGLRendererImpl(window); - CGL_LOG(Renderer, Info, "OpenGL Renderer Initialized"); } @@ -21,7 +44,6 @@ namespace CGL::Graphics OPENGLRendererImpl* Renderer::GetImpl() const { - assert(GetAPI() == RHIType::OpenGL); return static_cast(m_impl); } @@ -41,5 +63,164 @@ namespace CGL::Graphics glViewport(0, 0, width, height); } -#endif // CGL_RHI_OPENGL -} // namespace CGL::Graphics + void Renderer::SetPrimitiveTopology_OPENGL(PrimitiveType topology) + { + assert(GetImpl()); + GetImpl()->SetPrimitive(Mapping::PrimitiveTopology[size_t(topology)]); + } + + void Renderer::SetVertexShader_OPENGL([[maybe_unused]] const VertexShader& shader) + { + // This function is currently empty because shader creation and compilation are handled + // by separate functions. The SetVertexShader function will be implemented to switch to + // an active vertex shader for each frame as needed. + assert(GetImpl()); + } + + void Renderer::SetPixelShader_OPENGL( [[maybe_unused]] const PixelShader& shader) + { + // This function is currently empty because shader creation and compilation are handled + // by separate functions. The SetPixelShader function will be implemented to switch to + // an active pixel shader for each frame as needed. + assert(GetImpl() ); + } + + void Renderer::SetVertexBuffer_OPENGL(const VertexBuffer& buffer) + { + assert(GetImpl()); + glBindVertexArray(buffer.VAO); + } + + void Renderer::SetIndexBuffer_OPENGL([[maybe_unused]] const IndexBuffer& buffer) + { + // This function is currently empty. The implementation will involve binding the index buffer + // object. As more Samples are implemented this function will be implemented asap. + assert(GetImpl()); + } + + ShaderCompileResult Renderer::CompileVertexShader_OPENGL(const ShaderSource& source, VertexShader* outShader) + { + assert(GetImpl() && outShader); + + CompileConfig cfg{}; + cfg.EntryPoint = "main"; +#ifdef CGL_BUILD_DEBUG + cfg.Debug = true; + cfg.Optimize = false; +#endif + + ShaderCompileResult result = ShaderCompiler::Compile(source, cfg, outShader->VertexShader); + if (result.Status != ShaderCompileStatus::Failure) + { + assert(outShader->VertexShader); + } + + return result; + } + + ShaderCompileResult Renderer::CompilePixelShader_OPENGL(const ShaderSource& source, PixelShader* outShader) + { + assert(GetImpl() && outShader); + + CompileConfig cfg{}; + cfg.EntryPoint = "main"; +#ifdef CGL_BUILD_DEBUG + cfg.Debug = true; + cfg.Optimize = false; +#endif + + ShaderCompileResult result = ShaderCompiler::Compile(source, cfg, outShader->FragmentShader); + if (result.Status != ShaderCompileStatus::Failure) + { + assert(outShader->FragmentShader); + } + + return result; + } + + void Renderer::LinkShaders_OPENGL(Material* material) + { + material->m_id = glCreateProgram(); + glAttachShader(material->m_id, material->GetVertexShader()->Shader.VertexShader); + glAttachShader(material->m_id, material->GetPixelShader()->Shader.FragmentShader); + glLinkProgram(material->m_id); + + GLint success; + char infoLog[512]; + glGetProgramiv(material->m_id, GL_LINK_STATUS, &success); + if (!success) + { + glGetProgramInfoLog(material->m_id, 512, nullptr, infoLog); + CGL_LOG(Renderer, Error, "Failed to link shaders : " + std::string(infoLog)); + } + else + { + material->m_vs->State = ShaderState::Compiled; + material->m_ps->State = ShaderState::Compiled; + glDeleteShader(material->GetVertexShader()->Shader.VertexShader); + glDeleteShader(material->GetPixelShader()->Shader.FragmentShader); + } + } + + VertexBuffer Renderer::CreateVertexBuffer_OPENGL(const BufferSource& source) + { + assert(source.Type == BufferType::Vertex); + assert(GetImpl()); + + VertexBuffer vb; + vb.Stride = source.TypeSize * source.Count; + glGenVertexArrays(1, &vb.VAO); + glGenBuffers(1, &vb.VBO); + glBindVertexArray(vb.VAO); + + glBindBuffer(GL_ARRAY_BUFFER, vb.VBO); + glBufferData(GL_ARRAY_BUFFER, vb.Stride, source.Data, Mapping::BufferUsage[size_t(source.Usage)]); + + // Position Attribute + glVertexAttribPointer(0, sizeof(VertexTypes::PositionColor::Position) / sizeof(float), GL_FLOAT, GL_FALSE, source.TypeSize, (void*)offsetof(Graphics::VertexTypes::PositionColor,Graphics::VertexTypes::PositionColor::Position)); + glEnableVertexAttribArray(0); + + // Color Attribute + glVertexAttribPointer(1, sizeof(VertexTypes::PositionColor::Color)/sizeof(float), GL_FLOAT, GL_FALSE, source.TypeSize, (void*)offsetof(Graphics::VertexTypes::PositionColor, Graphics::VertexTypes::PositionColor::Color)); + glEnableVertexAttribArray(1); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); + + return vb; + } + + IndexBuffer Renderer::CreateIndexBuffer_OPENGL( [[maybe_unused]] const BufferSource& source) + { + assert(source.Type == BufferType::Index); + assert(GetImpl()); + + IndexBuffer ib; + ib.Stride = source.TypeSize * source.Count; + glGenBuffers(1, &ib.EBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib.EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, ib.Stride, source.Data, Mapping::BufferUsage[size_t(source.Usage)]); + + return ib; + } + + void Renderer::CreateConstantBuffer_OPENGL([[maybe_unused]] const BufferSource& source, [[maybe_unused]] GLuint& buffer){} + + void Renderer::SetConstantBufferData_OPENGL([[maybe_unused]] GLuint* buffer, [[maybe_unused]] const void* data, [[maybe_unused]] size_t size){} + + void Renderer::SetConstantBuffer_OPENGL([[maybe_unused]] ShaderType type, [[maybe_unused]] u32 startSlot, [[maybe_unused]] const GLuint& buffer){} + + void Renderer::Draw_OPENGL(u32 vertexCount, u32 startVertex) + { + assert(GetImpl()); + glDrawArrays(GetImpl()->GetPrimitive(), startVertex, vertexCount); + } + + void Renderer::DrawIndexed_OPENGL([[maybe_unused]] u32 indexCount, [[maybe_unused]] u32 startIndex, [[maybe_unused]] u32 baseVertex) + { + assert(GetImpl()); + glDrawElementsBaseVertex(GetImpl()->GetPrimitive(), indexCount, GL_UNSIGNED_INT, (void*)(startIndex * sizeof(GLuint)), baseVertex); + } + +#endif // CGL_RHI_OPENGL +} \ No newline at end of file diff --git a/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.cpp b/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.cpp index c39b14e..1f1df74 100644 --- a/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.cpp +++ b/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.cpp @@ -5,30 +5,39 @@ namespace CGL::Graphics CGL_DEFINE_LOG_CATEGORY(OPENGLRendererImpl); OPENGLRendererImpl::OPENGLRendererImpl(SDL_Window* window) - : gWindow(window) + : m_gWindow(window) + , m_primitive(GL_ZERO) { - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - gContext = SDL_GL_CreateContext(gWindow); + m_gContext = SDL_GL_CreateContext(m_gWindow); glewExperimental = GL_TRUE; - - glewError = glewInit(); - + m_glewError = glewInit(); + glClearColor(0.0f, 1.0f, 1.0f, 1.0f); - SDL_GL_SetSwapInterval(1); + SDL_GL_SetSwapInterval(1); + + SDL_GetWindowSize(window, &m_width, &m_height); + glViewport(0, 0, m_width, m_height); + glEnable(GL_DEPTH_TEST); } OPENGLRendererImpl::~OPENGLRendererImpl() { - SDL_GL_DeleteContext(gContext); + SDL_GL_DeleteContext(m_gContext); + } + + void OPENGLRendererImpl::SetPrimitive(GLenum primitive) + { + m_primitive = primitive; } void OPENGLRendererImpl::SwapWindow() { - SDL_GL_SwapWindow(gWindow); + SDL_GL_SwapWindow(m_gWindow); } -} // namespace CGL::Graphics +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.h b/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.h index 33f4c96..54b667b 100644 --- a/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.h +++ b/Core/Graphics/RHI/OpenGL/OPENGLRendererImpl.h @@ -1,9 +1,8 @@ #pragma once -#include "Core/Logging/Log.h" -#include "SDL2/SDL_opengl.h" -#include #include -#include +#include +#include "SDL2/SDL_opengl.h" +#include "Core/Logging/Log.h" namespace CGL::Graphics { @@ -14,12 +13,18 @@ namespace CGL::Graphics public: explicit OPENGLRendererImpl(SDL_Window* window); ~OPENGLRendererImpl(); - + + void SetPrimitive(GLenum primitive); + inline GLenum GetPrimitive() { return m_primitive;} + void SwapWindow(); - private: - SDL_Window* gWindow; - SDL_GLContext gContext; - GLenum glewError; + SDL_Window* m_gWindow; + SDL_GLContext m_gContext; + i32 m_width; + i32 m_height; + + GLenum m_glewError; + GLenum m_primitive; }; -} // namespace CGL::Graphics +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/Graphics/RHI/OpenGL/OPENGLVertexBuffer.h b/Core/Graphics/RHI/OpenGL/OPENGLVertexBuffer.h new file mode 100644 index 0000000..a83f061 --- /dev/null +++ b/Core/Graphics/RHI/OpenGL/OPENGLVertexBuffer.h @@ -0,0 +1,14 @@ +#pragma once +#include +#include + +namespace CGL::Graphics +{ + struct OPENGLVertexBuffer + { + u32 Stride; + u32 Offset; + GLuint VBO; + GLuint VAO; + }; +} // namespace CGL::Graphics diff --git a/Core/Graphics/RHI/OpenGL/OPENGLVertexShader.h b/Core/Graphics/RHI/OpenGL/OPENGLVertexShader.h new file mode 100644 index 0000000..9d8f63b --- /dev/null +++ b/Core/Graphics/RHI/OpenGL/OPENGLVertexShader.h @@ -0,0 +1,10 @@ +#pragma once +#include + +namespace CGL::Graphics +{ + struct OPENGLVertexShader + { + GLuint VertexShader; + }; +} // namespace CGL::Graphics diff --git a/Core/Graphics/Renderer.cpp b/Core/Graphics/Renderer.cpp index 123add8..76a36ee 100644 --- a/Core/Graphics/Renderer.cpp +++ b/Core/Graphics/Renderer.cpp @@ -1,6 +1,6 @@ #include "Renderer.h" #include "Core/Graphics/Types.h" -#include +#include namespace CGL::Graphics { @@ -100,14 +100,18 @@ namespace CGL::Graphics void Renderer::SetVertexShader(const VertexShader& shader) { #if defined(CGL_RHI_DX11) - SetVertexShader_D3D11(shader); + SetVertexShader_D3D11(shader); +#elif defined(CGL_RHI_OPENGL) + SetVertexShader_OPENGL(shader); #endif } void Renderer::SetPixelShader(const PixelShader& shader) { #if defined(CGL_RHI_DX11) - SetPixelShader_D3D11(shader); + SetPixelShader_D3D11(shader); +#elif defined(CGL_RHI_OPENGL) + SetPixelShader_OPENGL(shader); #endif } @@ -120,21 +124,29 @@ namespace CGL::Graphics assert(vs->State == ShaderState::Compiled); assert(ps->State == ShaderState::Compiled); - SetVertexShader(vs->Shader); - SetPixelShader(ps->Shader); - } + SetVertexShader(vs->Shader); + SetPixelShader(ps->Shader); + +#if defined(CGL_RHI_OPENGL) + glUseProgram(material.GetMaterialID()); +#endif + } void Renderer::SetVertexBuffer(const VertexBuffer& buffer) { #if defined(CGL_RHI_DX11) - SetVertexBuffer_D3D11(buffer); + SetVertexBuffer_D3D11(buffer); +#elif defined(CGL_RHI_OPENGL) + SetVertexBuffer_OPENGL(buffer); #endif } void Renderer::SetIndexBuffer(const IndexBuffer& buffer) { #if defined(CGL_RHI_DX11) - SetIndexBuffer_D3D11(buffer); + SetIndexBuffer_D3D11(buffer); +#elif defined(CGL_RHI_OPENGL) + SetIndexBuffer_OPENGL(buffer); #endif } @@ -143,9 +155,10 @@ namespace CGL::Graphics assert(outShader); #if defined(CGL_RHI_DX11) - ShaderCompileResult result = CompileVertexShader_D3D11(source, outShader); + ShaderCompileResult result = CompileVertexShader_D3D11(source, outShader); +#elif defined(CGL_RHI_OPENGL) + ShaderCompileResult result = CompileVertexShader_OPENGL(source, outShader); #endif - ShaderCompiler::ReportResult(result, source.Name.data()); return result.Status == ShaderCompileStatus::Success || result.Status == ShaderCompileStatus::HasWarnings; } @@ -155,9 +168,10 @@ namespace CGL::Graphics assert(outShader); #if defined(CGL_RHI_DX11) - ShaderCompileResult result = CompilePixelShader_D3D11(source, outShader); + ShaderCompileResult result = CompilePixelShader_D3D11(source, outShader); +#elif defined(CGL_RHI_OPENGL) + ShaderCompileResult result = CompilePixelShader_OPENGL(source, outShader); #endif - ShaderCompiler::ReportResult(result, source.Name.data()); return result.Status == ShaderCompileStatus::Success || result.Status == ShaderCompileStatus::HasWarnings; } @@ -165,14 +179,18 @@ namespace CGL::Graphics VertexBuffer Renderer::CreateVertexBuffer(const BufferSource& source) { #if defined(CGL_RHI_DX11) - return CreateVertexBuffer_D3D11(source); + return CreateVertexBuffer_D3D11(source); +#elif defined(CGL_RHI_OPENGL) + return CreateVertexBuffer_OPENGL(source); #endif } IndexBuffer Renderer::CreateIndexBuffer(const BufferSource& source) { #if defined(CGL_RHI_DX11) - return CreateIndexBuffer_D3D11(source); + return CreateIndexBuffer_D3D11(source); +#elif defined(CGL_RHI_OPENGL) + return CreateIndexBuffer_OPENGL(source); #endif } @@ -197,21 +215,28 @@ namespace CGL::Graphics material->m_ps->State = ShaderState::Compiled; } - // TODO: Add other shader types - return result; - } - - void Renderer::Draw(u32 vertexCount, u32 startVertex) - { +#if defined(CGL_RHI_OPENGL) + LinkShaders_OPENGL(material); +#endif + // TODO: Add other shader types + return result; + } + + void Renderer::Draw(u32 vertexCount, u32 startVertex) + { #if defined(CGL_RHI_DX11) - Draw_D3D11(vertexCount, startVertex); + Draw_D3D11(vertexCount, startVertex); +#elif defined(CGL_RHI_OPENGL) + Draw_OPENGL(vertexCount, startVertex); #endif } void Renderer::DrawIndexed(u32 indexCount, u32 startIndex, u32 baseVertex) { #if defined(CGL_RHI_DX11) - DrawIndexed_D3D11(indexCount, startIndex, baseVertex); + DrawIndexed_D3D11(indexCount, startIndex, baseVertex); +#elif defined(CGL_RHI_OPENGL) + DrawIndexed_OPENGL(indexCount, startIndex, baseVertex); #endif } -} // namespace CGL::Graphics +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/Graphics/Renderer.h b/Core/Graphics/Renderer.h index 12843e6..e5f4863 100644 --- a/Core/Graphics/Renderer.h +++ b/Core/Graphics/Renderer.h @@ -84,13 +84,30 @@ namespace CGL::Graphics void Draw_D3D11(u32 vertexCount, u32 startVertex = 0); void DrawIndexed_D3D11(u32 indexCount, u32 startIndex = 0, u32 baseVertex = 0); D3D11RendererImpl* GetImpl() const; + #elif defined(CGL_RHI_OPENGL) void Constructor_OPENGL(SDL_Window* window); void Destructor_OPENGL(); void BeginFrame_OPENGL(); void EndFrame_OPENGL(); void Resize_OPENGL(u32 width, u32 height); + void SetPrimitiveTopology_OPENGL(PrimitiveType topology); + void SetVertexShader_OPENGL(const VertexShader& shader); + void SetPixelShader_OPENGL(const PixelShader& shader); + void SetVertexBuffer_OPENGL(const VertexBuffer& buffer); + void SetIndexBuffer_OPENGL(const IndexBuffer& buffer); + ShaderCompileResult CompileVertexShader_OPENGL(const ShaderSource& source, VertexShader* outShader); + ShaderCompileResult CompilePixelShader_OPENGL(const ShaderSource& source, PixelShader* outShader); + void LinkShaders_OPENGL(Material* material); + void CreateConstantBuffer_OPENGL(const BufferSource& source, GLuint& outBuffer); + void SetConstantBufferData_OPENGL(GLuint* buffer, const void* data, size_t size); + void SetConstantBuffer_OPENGL(ShaderType type, u32 startSlot, const GLuint& buffer); + VertexBuffer CreateVertexBuffer_OPENGL(const BufferSource& source); + IndexBuffer CreateIndexBuffer_OPENGL(const BufferSource& source); + void Draw_OPENGL(u32 vertexCount, u32 startVertex = 0); + void DrawIndexed_OPENGL(u32 indexCount, u32 startIndex = 0, u32 baseVertex = 0); OPENGLRendererImpl* GetImpl() const; + #elif defined(CGL_RHI_METAL) void Constructor_METAL(SDL_Window* window); void Destructor_METAL(); @@ -109,4 +126,4 @@ namespace CGL::Graphics } // namespace CGL::Graphics // Contains template bodies -#include "Renderer.inl" +#include "Renderer.inl" \ No newline at end of file diff --git a/Core/Graphics/Renderer.inl b/Core/Graphics/Renderer.inl index 851f080..e081fdf 100644 --- a/Core/Graphics/Renderer.inl +++ b/Core/Graphics/Renderer.inl @@ -6,7 +6,9 @@ namespace CGL::Graphics void Renderer::CreateContantBuffer(const BufferSource& source, ConstantBuffer& outBuffer) { #if defined(CGL_RHI_DX11) - CreateContantBuffer_D3D11(source, outBuffer.Buffer); + CreateContantBuffer_D3D11(source, outBuffer.Buffer); +#elif defined(CGL_RHI_OPENGL) + CreateConstantBuffer_OPENGL(source, outBuffer.Buffer); #endif } @@ -14,14 +16,19 @@ namespace CGL::Graphics void Renderer::SetConstantBufferData(const ConstantBuffer& buffer, const T& data) { #if defined(CGL_RHI_DX11) - SetConstantBufferData_D3D11(buffer.Buffer.Get(), static_cast(&data), sizeof(T)); -#endif - } - template - void Renderer::SetContantBuffer(ShaderType shaderType, u32 startSlot, const ConstantBuffer& buffer) - { + SetConstantBufferData_D3D11(buffer.Buffer.Get(), static_cast(&data), sizeof(T)); +#elif defined(CGL_RHI_OPENGL) + SetConstantBufferData_OPENGL(buffer.Buffer.Get(), static_cast(&data), sizeof(T)); +#endif + } + + template + void Renderer::SetContantBuffer(ShaderType shaderType, u32 startSlot, const ConstantBuffer& buffer) + { #if defined(CGL_RHI_DX11) - SetContantBuffer_D3D11(shaderType, startSlot, buffer.Buffer); -#endif - } -} // namespace CGL::Graphics + SetContantBuffer_D3D11(shaderType, startSlot, buffer.Buffer); +#elif defined(CGL_RHI_OPENGL) + SetConstantBuffer_OPENGL(shaderType, startSlot, buffer.Buffer); +#endif + } +} \ No newline at end of file diff --git a/Core/Graphics/Shader/Shader.h b/Core/Graphics/Shader/Shader.h index 93ab27b..30b6184 100644 --- a/Core/Graphics/Shader/Shader.h +++ b/Core/Graphics/Shader/Shader.h @@ -10,6 +10,7 @@ #if defined(CGL_RHI_OPENGL) #include + #include #endif #if defined(CGL_RHI_METAL) @@ -40,4 +41,4 @@ namespace CGL::Graphics #else #error Unsupported shader RHI #endif -} // namespace CGL::Graphics +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/Graphics/Shader/ShaderCompiler.cpp b/Core/Graphics/Shader/ShaderCompiler.cpp index 30f40f1..ec6bd6b 100644 --- a/Core/Graphics/Shader/ShaderCompiler.cpp +++ b/Core/Graphics/Shader/ShaderCompiler.cpp @@ -29,15 +29,14 @@ namespace CGL::Graphics } #if defined(CGL_RHI_D3D) - ShaderCompileResult ShaderCompiler::Compile(const ShaderSource& shader, const CompileConfig& config, - ComPtr& outBlob) + ShaderCompileResult ShaderCompiler::Compile(const ShaderSource& shader, const CompileConfig& config, ComPtr& outBlob) { ShaderCompileResult result{}; if (shader.SourceData.empty()) { - result.Status = ShaderCompileStatus::Failure; - result.Message = "Shader file path is empty"; + result.Status = ShaderCompileStatus::Failure; + result.Message = "Shader file content is empty"; return result; } @@ -59,9 +58,19 @@ namespace CGL::Graphics ID3DBlob* errorBlob = nullptr; ID3DBlob* shaderBlob = nullptr; - HRESULT hr = D3DCompile(shader.SourceData.c_str(), shader.SourceData.length(), shader.Name.c_str(), - config.Defines.data(), D3D_COMPILE_STANDARD_FILE_INCLUDE, config.EntryPoint.data(), - config.Target.data(), compileFlags, 0, &shaderBlob, &errorBlob); + HRESULT hr = D3DCompile( + shader.SourceData.c_str(), + shader.SourceData.length(), + shader.Name.c_str(), + config.Defines.data(), + D3D_COMPILE_STANDARD_FILE_INCLUDE, + config.EntryPoint.data(), + config.Target.data(), + compileFlags, + 0, + &shaderBlob, + &errorBlob + ); if (FAILED(hr) || shaderBlob == nullptr) { @@ -91,6 +100,55 @@ namespace CGL::Graphics return result; } } +#elif defined(CGL_RHI_OPENGL) + ShaderCompileResult ShaderCompiler::Compile(const ShaderSource& shader, [[maybe_unused]] const CompileConfig& config, GLuint& outShader) + { + ShaderCompileResult result{}; + + if (shader.SourceData.empty()) + { + result.Status = ShaderCompileStatus::Failure; + result.Message = "Shader file content is empty"; + return result; + } + switch (shader.Type) + { + case Graphics::ShaderType::Vertex: + { + outShader = glCreateShader(GL_VERTEX_SHADER); + break; + } + case Graphics::ShaderType::Pixel: + { + outShader = glCreateShader(GL_FRAGMENT_SHADER); + break; + } + default: + std::unreachable(); + break; + } + + auto src = shader.SourceData.c_str(); + + glShaderSource(outShader, 1, &src, nullptr); + glCompileShader(outShader); + + GLint success; + glGetShaderiv(outShader, GL_COMPILE_STATUS, &success); + if (!success) { + char infoLog[512]; + glGetShaderInfoLog(outShader, 512, nullptr, infoLog); + + result.Message = std::string(infoLog); + result.Status = ShaderCompileStatus::Failure; + return result; + } + else + { + result.Status = ShaderCompileStatus::Success; + return result; + } + } #endif -} // namespace CGL::Graphics +} \ No newline at end of file diff --git a/Core/Graphics/Shader/ShaderCompiler.h b/Core/Graphics/Shader/ShaderCompiler.h index bdb9b8d..d2be1c8 100644 --- a/Core/Graphics/Shader/ShaderCompiler.h +++ b/Core/Graphics/Shader/ShaderCompiler.h @@ -8,6 +8,10 @@ #include #endif +#if defined(CGL_RHI_OPENGL) +#include +#endif + namespace CGL::Graphics { CGL_DECLARE_LOG_CATEGORY(ShaderCompiler); @@ -30,8 +34,9 @@ namespace CGL::Graphics static void ReportResult(const ShaderCompileResult& result, const std::string& source); #if defined(CGL_RHI_D3D) - static ShaderCompileResult Compile(const ShaderSource& shader, const CompileConfig& config, - ComPtr& outBlob); + static ShaderCompileResult Compile(const ShaderSource& shader, const CompileConfig& config, ComPtr& outBlob); +#elif defined(CGL_RHI_OPENGL) + static ShaderCompileResult Compile(const ShaderSource& shader, const CompileConfig& config, GLuint& outBlob); #endif }; -} // namespace CGL::Graphics +} // namespace CGL::Graphics \ No newline at end of file diff --git a/Core/xmake.lua b/Core/xmake.lua index 10244cc..e238639 100644 --- a/Core/xmake.lua +++ b/Core/xmake.lua @@ -65,7 +65,11 @@ target("VisualizerCore") add_files("Graphics/RHI/OpenGL/**.cpp") add_headerfiles("Graphics/RHI/OpenGL/**.h") add_packages("glew") - add_links("GL") + if is_os("windows") then + add_links("opengl32") + elseif is_os("linux") then + add_links("GL") + end elseif rhi == "VULKAN" then add_files("Graphics/RHI/Vulkan/**.cpp") add_headerfiles("Graphics/RHI/Vulkan/**.h") diff --git a/Samples/BlankApp/xmake.lua b/Samples/BlankApp/xmake.lua index e9a3c8e..c83405f 100644 --- a/Samples/BlankApp/xmake.lua +++ b/Samples/BlankApp/xmake.lua @@ -7,7 +7,7 @@ target("BlankApp") set_kind("binary") set_group("Samples") - add_packages("libsdl") + add_packages("libsdl", "directxmath") add_includedirs("..", "$(projectdir)") add_files("**.cpp") @@ -37,7 +37,17 @@ target("BlankApp") end) if has_config("rhi") then - add_links("VisualizerCore" .. "_" .. string.upper(get_config("rhi"))) + local rhi = string.upper(get_config("rhi")) + add_links("VisualizerCore" .. "_" .. rhi) + + if rhi == "OPENGL" then + add_packages("glew") + if is_plat("windows") then + add_links("opengl32") + elseif is_plat("linux") then + add_links("GL") + end + end end add_tests("compile_pass", {build_should_pass = true}) diff --git a/Samples/HelloTriangle/Assets/HelloTrianglePS.hlsl b/Samples/HelloTriangle/Assets/DirectX/HelloTrianglePS.hlsl similarity index 100% rename from Samples/HelloTriangle/Assets/HelloTrianglePS.hlsl rename to Samples/HelloTriangle/Assets/DirectX/HelloTrianglePS.hlsl diff --git a/Samples/HelloTriangle/Assets/HelloTriangleVS.hlsl b/Samples/HelloTriangle/Assets/DirectX/HelloTriangleVS.hlsl similarity index 100% rename from Samples/HelloTriangle/Assets/HelloTriangleVS.hlsl rename to Samples/HelloTriangle/Assets/DirectX/HelloTriangleVS.hlsl diff --git a/Samples/HelloTriangle/Assets/OpenGL/HelloTrianglePS.frag b/Samples/HelloTriangle/Assets/OpenGL/HelloTrianglePS.frag new file mode 100644 index 0000000..fa06508 --- /dev/null +++ b/Samples/HelloTriangle/Assets/OpenGL/HelloTrianglePS.frag @@ -0,0 +1,8 @@ +#version 460 core +in vec3 Color; +out vec4 FragColor; + +void main() +{ + FragColor = vec4(Color,1.0f); +} \ No newline at end of file diff --git a/Samples/HelloTriangle/Assets/OpenGL/HelloTriangleVS.vert b/Samples/HelloTriangle/Assets/OpenGL/HelloTriangleVS.vert new file mode 100644 index 0000000..e8e5b17 --- /dev/null +++ b/Samples/HelloTriangle/Assets/OpenGL/HelloTriangleVS.vert @@ -0,0 +1,10 @@ +#version 460 core +layout (location = 0) in vec3 Position; +layout (location = 1) in vec3 aColor; +out vec3 Color; + +void main() +{ + gl_Position = vec4(Position, 1.0); + Color = aColor; +} \ No newline at end of file diff --git a/Samples/HelloTriangle/HelloTriangle.cpp b/Samples/HelloTriangle/HelloTriangle.cpp index fce949e..7a66c38 100644 --- a/Samples/HelloTriangle/HelloTriangle.cpp +++ b/Samples/HelloTriangle/HelloTriangle.cpp @@ -5,15 +5,29 @@ namespace CGL { CGL_DEFINE_LOG_CATEGORY(HelloTriangle); - static constexpr byte s_vertexShader[] = { -#include "HelloTriangleVS.hlsl.h" + static constexpr byte s_vertexShader[] = + { + #if defined(CGL_RHI_DX11) + #include "HelloTriangleVS.hlsl.h" + #elif defined(CGL_RHI_OPENGL) + #include "HelloTriangleVS.vert.h" + #elif defined(CGL_RHI_METAL) + #include "HelloTriangleVS.metal.h" + #endif }; - static constexpr byte s_pixelShader[] = { -#include "HelloTrianglePS.hlsl.h" + static constexpr byte s_pixelShader[] = + { + #if defined(CGL_RHI_DX11) + #include "HelloTrianglePS.hlsl.h" + #elif defined(CGL_RHI_OPENGL) + #include "HelloTrianglePS.frag.h" + #elif defined(CGL_RHI_METAL) + #include "HelloTrianglePS.metal.h" + #endif }; - HelloTriangle::HelloTriangle(i32 argc, char** argv) + HelloTriangle::HelloTriangle(i32 argc, char** argv) : Super("[CGL] Hello Triangle Sample", argc, argv) { CGL_LOG(HelloTriangle, Trace, "Created HelloTriangle App"); @@ -39,8 +53,8 @@ namespace CGL // Define pixel shader source Graphics::ShaderSource psSrc; psSrc.SourceData = Core::DataToString(s_pixelShader, sizeof(s_pixelShader)); - psSrc.Type = Graphics::ShaderType::Pixel; - psSrc.Name = "HelloTrianglePS"; + psSrc.Type = Graphics::ShaderType::Pixel; + psSrc.Name = "HelloTrianglePS"; // Compile material m_material.AddSource(vsSrc); @@ -50,21 +64,25 @@ namespace CGL CGL_LOG(HelloTriangle, Error, "Failed to compile material for triangle"); } - // Define triangle vertices - constexpr std::array vertices = { - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(0.0f, 0.5f, 0.0f), - .Color = SM::Vector4(1.0f, 0.0f, 0.0f, 1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(0.5f, -0.5f, 0.0f), - .Color = SM::Vector4(0.0f, 1.0f, 0.0f, 1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(-0.5f, -0.5f, 0.0f), - .Color = SM::Vector4(0.0f, 0.0f, 1.0f, 1.0f), - }, - }; + // Define triangle vertices + constexpr std::array vertices = + { + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3( 0.0f, 0.5f, 0.0f ), + .Color = SM::Vector4( 1.0f, 0.0f, 0.0f, 1.0f ), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3( 0.5f, -0.5f, 0.0f ), + .Color = SM::Vector4( 0.0f, 1.0f, 0.0f, 1.0f ), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3( -0.5f, -0.5f, 0.0f ), + .Color = SM::Vector4( 0.0f, 0.0f, 1.0f, 1.0f ), + }, + }; Graphics::BufferSource vbs; vbs.Data = (void*)vertices.data(); @@ -100,4 +118,4 @@ namespace CGL Super::OnShutdown(); } -} // namespace CGL +} \ No newline at end of file diff --git a/Samples/HelloTriangle/xmake.lua b/Samples/HelloTriangle/xmake.lua index 8915df5..54b1dfd 100644 --- a/Samples/HelloTriangle/xmake.lua +++ b/Samples/HelloTriangle/xmake.lua @@ -7,7 +7,7 @@ target("HelloTriangle") set_kind("binary") set_group("Samples") - add_packages("libsdl") + add_packages("libsdl", "directxmath") add_includedirs("..", "$(projectdir)") add_files("**.cpp") @@ -41,12 +41,23 @@ target("HelloTriangle") local rhi = string.upper(get_config("rhi")) add_links("VisualizerCore" .. "_" .. rhi) + if rhi == "OPENGL" then + add_packages("glew") + if is_plat("windows") then + add_links("opengl32") + elseif is_plat("linux") then + add_links("GL") + end + end + if rhi == "DX11" or rhi == "DX12" then add_rules("utils.bin2c", { extensions = { ".hlsl" } }) - add_files("Assets/**.hlsl") + add_files("Assets/DirectX/**.hlsl") elseif rhi == "VULKAN" or rhi == "OPENGL" then add_rules("utils.bin2c", { extensions = { ".vert", ".frag" } }) - add_files("Assets/**.vert", "Assets/**.frag") + if rhi == "OPENGL" then + add_files("Assets/OpenGL/**.vert", "Assets/OpenGL/**.frag") + end elseif rhi == "METAL" then add_rules("utils.bin2c", { extensions = { ".metal" } }) add_files("Assets/**.metal") diff --git a/Samples/SpinningCube/Assets/SpinningCubePS.hlsl b/Samples/SpinningCube/Assets/DirectX/SpinningCubePS.hlsl similarity index 100% rename from Samples/SpinningCube/Assets/SpinningCubePS.hlsl rename to Samples/SpinningCube/Assets/DirectX/SpinningCubePS.hlsl diff --git a/Samples/SpinningCube/Assets/SpinningCubeVS.hlsl b/Samples/SpinningCube/Assets/DirectX/SpinningCubeVS.hlsl similarity index 100% rename from Samples/SpinningCube/Assets/SpinningCubeVS.hlsl rename to Samples/SpinningCube/Assets/DirectX/SpinningCubeVS.hlsl diff --git a/Samples/SpinningCube/Assets/OpenGL/SpinningCubePS.frag b/Samples/SpinningCube/Assets/OpenGL/SpinningCubePS.frag new file mode 100644 index 0000000..0adc8a7 --- /dev/null +++ b/Samples/SpinningCube/Assets/OpenGL/SpinningCubePS.frag @@ -0,0 +1,9 @@ +#version 460 core + +in vec3 fragColor; +out vec4 outColor; + +void main() +{ + outColor = vec4(fragColor, 1.0); +} diff --git a/Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert b/Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert new file mode 100644 index 0000000..5f36660 --- /dev/null +++ b/Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert @@ -0,0 +1,22 @@ +#version 460 core + +// Uniform buffers +uniform mat4 WorldMatrix; +uniform mat4 ViewMatrix; +uniform mat4 ProjMatrix; + +layout(location = 0) in vec3 inPosition; +layout(location = 1) in vec3 inColor; + +out vec3 fragColor; + +void main() +{ + vec4 position = vec4(inPosition, 1.0); + position = WorldMatrix * position; + position = ViewMatrix * position; + position = ProjMatrix * position; + + gl_Position = position; + fragColor = inColor; +} diff --git a/Samples/SpinningCube/SpinningCube.cpp b/Samples/SpinningCube/SpinningCube.cpp index edc0c68..43132f1 100644 --- a/Samples/SpinningCube/SpinningCube.cpp +++ b/Samples/SpinningCube/SpinningCube.cpp @@ -4,15 +4,29 @@ namespace CGL { CGL_DEFINE_LOG_CATEGORY(SpinningCube); - static constexpr byte s_vertexShader[] = { -#include "SpinningCubeVS.hlsl.h" + static constexpr byte s_vertexShader[] = + { + #if defined(CGL_RHI_DX11) + #include "SpinningCubeVS.hlsl.h" + #elif defined(CGL_RHI_OPENGL) + #include "SpinningCubeVS.vert.h" + #elif defined(CGL_RHI_METAL) + #include "SpinningCubeVS.metal.h" + #endif }; - static constexpr byte s_pixelShader[] = { -#include "SpinningCubePS.hlsl.h" + static constexpr byte s_pixelShader[] = + { + #if defined(CGL_RHI_DX11) + #include "SpinningCubePS.hlsl.h" + #elif defined(CGL_RHI_OPENGL) + #include "SpinningCubePS.frag.h" + #elif defined(CGL_RHI_METAL) + #include "SpinningCubePS.metal.h" + #endif }; - SpinningCube::SpinningCube(i32 argc, char** argv) + SpinningCube::SpinningCube(i32 argc, char** argv) : Super("[CGL] Spinning Cube Sample", argc, argv) { CGL_LOG(SpinningCube, Trace, "Created SpinningCube"); @@ -52,39 +66,48 @@ namespace CGL } // Define cube vertices - constexpr std::array vertices = { - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(-1.0f, 1.0f, -1.0f), - .Color = SM::Vector4(1.0f, 0.0f,0.0f, 1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(1.0f, 1.0f, -1.0f), - .Color = SM::Vector4(0.0f, 1.0f,0.0f,1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(1.0f, 1.0f, 1.0f), - .Color = SM::Vector4(0.0f, 0.0f,1.0f,1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(-1.0f, 1.0f, 1.0f), - .Color = SM::Vector4(1.0f,1.0f,0.0f,1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(-1.0f, -1.0f, -1.0f), - .Color = SM::Vector4(1.0f, 0.0f, 1.0f, 1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(1.0f, -1.0f, -1.0f), - .Color = SM::Vector4(0.0f, 1.0f,1.0f,1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(1.0f, -1.0f, 1.0f), - .Color = SM::Vector4(1.0f, 0.5f,0.0f,1.0f), - }, - Graphics::VertexTypes::PositionColor{ - .Position = SM::Vector3(-1.0f, -1.0f, 1.0f), - .Color = SM::Vector4(0.5f, 0.0f,0.5f,1.0f), - }, + constexpr std::array vertices = + { + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(-1.0f, 1.0f, -1.0f), + .Color = SM::Vector4(1.0f, 0.0f, 0.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(1.0f, 1.0f, -1.0f), + .Color = SM::Vector4(0.0f, 1.0f, 0.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(1.0f, 1.0f, 1.0f), + .Color = SM::Vector4(0.0f, 0.0f, 1.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(-1.0f, 1.0f, 1.0f), + .Color = SM::Vector4(1.0f, 1.0f, 0.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(-1.0f, -1.0f, -1.0f), + .Color = SM::Vector4(1.0f, 0.0f, 1.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(1.0f, -1.0f, -1.0f), + .Color = SM::Vector4(0.0f, 1.0f, 1.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(1.0f, -1.0f, 1.0f), + .Color = SM::Vector4(1.0f, 0.5f, 0.0f, 1.0f), + }, + Graphics::VertexTypes::PositionColor + { + .Position = SM::Vector3(-1.0f, -1.0f, 1.0f), + .Color = SM::Vector4(0.5f, 0.0f, 0.5f, 1.0f), + }, }; Graphics::BufferSource vbs; @@ -95,26 +118,33 @@ namespace CGL m_vertexBuffer = GetRenderer()->CreateVertexBuffer(vbs); // Define cube indices - constexpr std::array indices = { - 3, 1, 0, 2, 1, 3, + constexpr std::array indices = + { + 3,1,0, + 2,1,3, - 0, 5, 4, 1, 5, 0, + 0,5,4, + 1,5,0, - 3, 4, 7, 0, 4, 3, + 3,4,7, + 0,4,3, - 1, 6, 5, 2, 6, 1, + 1,6,5, + 2,6,1, - 2, 7, 6, 3, 7, 2, + 2,7,6, + 3,7,2, - 6, 4, 5, 7, 4, 6, + 6,4,5, + 7,4,6, }; Graphics::BufferSource ibs; - ibs.Data = (void*)indices.data(); - ibs.Type = Graphics::BufferType::Index; - ibs.TypeSize = sizeof(decltype(indices)::value_type); - ibs.Count = u32(indices.size()); - m_indexBuffer = GetRenderer()->CreateIndexBuffer(ibs); + ibs.Data = (void*)indices.data(); + ibs.Type = Graphics::BufferType::Index; + ibs.TypeSize = sizeof(decltype(indices)::value_type); + ibs.Count = u32(indices.size()); + m_indexBuffer = GetRenderer()->CreateIndexBuffer(ibs); // Create constant buffer Graphics::BufferSource cbs; @@ -125,7 +155,7 @@ namespace CGL // Create camera m_camera.InitAsPerspective(90, f32(GetRenderer()->GetWidth()), f32(GetRenderer()->GetHeight())); - m_camera.SetPosition({ 0.0f, 1.5f, -2.5f }); + m_camera.SetPosition({0.0f, 1.5f, -2.5f}); CGL_LOG(SpinningCube, Info, "Initialized SpinningCube"); return true; } @@ -134,9 +164,12 @@ namespace CGL void SpinningCube::OnUpdate([[maybe_unused]] const SDL_Event& e) { time += 0.0001f; - FrameData data{ .World = SM::Matrix::CreateRotationY(time), - .View = m_camera.GetViewMatrix().Transpose(), - .Projection = m_camera.GetProjectionMatrix().Transpose() }; + FrameData data + { + .World = SM::Matrix::CreateRotationY(time), + .View = m_camera.GetViewMatrix().Transpose(), + .Projection = m_camera.GetProjectionMatrix().Transpose() + }; GetRenderer()->SetConstantBufferData(m_constantBuffer, data); } @@ -163,4 +196,4 @@ namespace CGL CGL_LOG(SpinningCube, Info, "Shutting down SpinningCube"); Super::OnShutdown(); } -} // namespace CGL +} \ No newline at end of file diff --git a/Samples/SpinningCube/xmake.lua b/Samples/SpinningCube/xmake.lua index a357d3d..068535a 100644 --- a/Samples/SpinningCube/xmake.lua +++ b/Samples/SpinningCube/xmake.lua @@ -7,7 +7,7 @@ target("SpinningCube") set_kind("binary") set_group("Samples") - add_packages("libsdl") + add_packages("libsdl", "directxmath") add_includedirs("..", "$(projectdir)") add_files("**.cpp") @@ -41,12 +41,23 @@ target("SpinningCube") local rhi = string.upper(get_config("rhi")) add_links("VisualizerCore" .. "_" .. rhi) + if rhi == "OPENGL" then + add_packages("glew") + if is_plat("windows") then + add_links("opengl32") + elseif is_plat("linux") then + add_links("GL") + end + end + if rhi == "DX11" or rhi == "DX12" then add_rules("utils.bin2c", { extensions = { ".hlsl" } }) - add_files("Assets/**.hlsl") + add_files("Assets/DirectX/**.hlsl") elseif rhi == "VULKAN" or rhi == "OPENGL" then add_rules("utils.bin2c", { extensions = { ".vert", ".frag" } }) - add_files("Assets/**.vert", "Assets/**.frag") + if rhi == "OPENGL" then + add_files("Assets/OpenGL/**.vert", "Assets/OpenGL/**.frag") + end elseif rhi == "METAL" then add_rules("utils.bin2c", { extensions = { ".metal" } }) add_files("Assets/**.metal") diff --git a/Scripts/Templates/XmakeTemplate b/Scripts/Templates/XmakeTemplate index 3857f03..93af6e1 100644 --- a/Scripts/Templates/XmakeTemplate +++ b/Scripts/Templates/XmakeTemplate @@ -41,6 +41,15 @@ target("%PROJECT_NAME%") local rhi = string.upper(get_config("rhi")) add_links("VisualizerCore" .. "_" .. rhi) + if rhi == "OPENGL" then + add_packages("glew") + if is_plat("windows") then + add_links("opengl32") + elseif is_plat("linux") then + add_links("GL") + end + end + if rhi == "DX11" or rhi == "DX12" then add_rules("utils.bin2c", { extensions = { ".hlsl" } }) add_files("Assets/**.hlsl")