diff --git a/Core/Graphics/Mesh.cpp b/Core/Graphics/Mesh.cpp index 042c714..c38eeda 100644 --- a/Core/Graphics/Mesh.cpp +++ b/Core/Graphics/Mesh.cpp @@ -1,6 +1,15 @@ #include "Mesh.h" #define TINYOBJLOADER_IMPLEMENTATION -#include +// tinyobjloader uses NULL instead of nullptr for pointers, +// which can cause issues on Linux and macOS. The code below +// temporarily redefines NULL as nullptr to ensure compatibility. +#if defined(__linux__) || defined(__APPLE__) + #define NULL nullptr + #include + #undef NULL +#else + #include +#endif namespace CGL::Graphics { diff --git a/Core/Graphics/Mesh.h b/Core/Graphics/Mesh.h index ad736b4..ac09b3a 100644 --- a/Core/Graphics/Mesh.h +++ b/Core/Graphics/Mesh.h @@ -10,8 +10,8 @@ namespace CGL::Graphics struct MeshData { - VertexBuffer VertexBuffer; - IndexBuffer IndexBuffer; + VertexBuffer VBuffer; + IndexBuffer IBuffer; }; diff --git a/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp b/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp index 038eb69..1976023 100644 --- a/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp +++ b/Core/Graphics/RHI/OpenGL/OPENGLRenderer.cpp @@ -185,6 +185,19 @@ namespace CGL::Graphics { OPENGLEnableVertexAttributes(); } + else if (source.VertexType == typeid(VertexTypes::PositionTexture)) + { + OPENGLEnableVertexAttributes(); + } + else if (source.VertexType == typeid(VertexTypes::PositionColorTexture)) + { + OPENGLEnableVertexAttributes(); + } + else if (source.VertexType == typeid(VertexTypes::PositionNormalTexture)) + { + OPENGLEnableVertexAttributes(); + } + glBindVertexArray(0); return vb; diff --git a/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp b/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp index 03707e3..644bc33 100644 --- a/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp +++ b/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.cpp @@ -38,4 +38,93 @@ namespace CGL::Graphics (void*)offsetof(Graphics::VertexTypes::PositionColor, Color) ); } + + template <> + void OPENGLEnableVertexAttributes() { + glEnableVertexAttribArray(0); // Position Attribute + glVertexAttribPointer( + 0, + sizeof(Graphics::VertexTypes::PositionTexture::Position) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionTexture), + (void*)offsetof(Graphics::VertexTypes::PositionTexture, Position) + ); + + glEnableVertexAttribArray(1); // Texture Attribute + glVertexAttribPointer( + 1, + sizeof(Graphics::VertexTypes::PositionTexture::Texture) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionTexture), + (void*)offsetof(Graphics::VertexTypes::PositionTexture, Texture) + ); + } + + template <> + void OPENGLEnableVertexAttributes() { + glEnableVertexAttribArray(0); // Position Attribute + glVertexAttribPointer( + 0, + sizeof(Graphics::VertexTypes::PositionColorTexture::Position) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionColorTexture), + (void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Position) + ); + + glEnableVertexAttribArray(1); // Color Attribute + glVertexAttribPointer( + 1, + sizeof(Graphics::VertexTypes::PositionColorTexture::Color) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionColorTexture), + (void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Color) + ); + + glEnableVertexAttribArray(2); // Texture Attribute + glVertexAttribPointer( + 2, + sizeof(Graphics::VertexTypes::PositionColorTexture::Texture) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionColorTexture), + (void*)offsetof(Graphics::VertexTypes::PositionColorTexture, Texture) + ); + } + + template <> + void OPENGLEnableVertexAttributes() { + glEnableVertexAttribArray(0); // Position Attribute + glVertexAttribPointer( + 0, + sizeof(Graphics::VertexTypes::PositionNormalTexture::Position) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionNormalTexture), + (void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Position) + ); + + glEnableVertexAttribArray(1); // Normal Attribute + glVertexAttribPointer( + 1, + sizeof(Graphics::VertexTypes::PositionNormalTexture::Normal) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionNormalTexture), + (void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Normal) + ); + + glEnableVertexAttribArray(2); // Texture Attribute + glVertexAttribPointer( + 2, + sizeof(Graphics::VertexTypes::PositionNormalTexture::Texture) / sizeof(float), + GL_FLOAT, + GL_FALSE, + sizeof(Graphics::VertexTypes::PositionNormalTexture), + (void*)offsetof(Graphics::VertexTypes::PositionNormalTexture, Texture) + ); + } } \ No newline at end of file diff --git a/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h b/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h index c0908ca..d54829a 100644 --- a/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h +++ b/Core/Graphics/RHI/OpenGL/OPENGLVertexAttributes.h @@ -19,4 +19,7 @@ namespace CGL::Graphics template <> void OPENGLEnableVertexAttributes(); + + template <> + void OPENGLEnableVertexAttributes(); } \ No newline at end of file diff --git a/Samples/ModelLoading/Assets/OpenGL/ModelLoadingPS.frag b/Samples/ModelLoading/Assets/OpenGL/ModelLoadingPS.frag new file mode 100644 index 0000000..41128df --- /dev/null +++ b/Samples/ModelLoading/Assets/OpenGL/ModelLoadingPS.frag @@ -0,0 +1,11 @@ +#version 460 core + +in vec3 vColor; +in vec3 vNormal; + +out vec4 outColor; + +void main() +{ + outColor = vec4(vNormal, 1.0); +} diff --git a/Samples/ModelLoading/Assets/OpenGL/ModelLoadingVS.vert b/Samples/ModelLoading/Assets/OpenGL/ModelLoadingVS.vert new file mode 100644 index 0000000..c3d38ec --- /dev/null +++ b/Samples/ModelLoading/Assets/OpenGL/ModelLoadingVS.vert @@ -0,0 +1,27 @@ +#version 460 core + +layout(std140, binding = 0) uniform Matrices +{ + mat4 WorldMatrix; + mat4 ViewMatrix; + mat4 ProjMatrix; +}; + +layout(location = 0) in vec3 inPosition; +layout(location = 1) in vec3 inNormal; +layout(location = 2) in vec3 inColor; + +out vec3 vColor; +out vec3 vNormal; + +void main() +{ + vec4 position = vec4(inPosition, 1.0); + position = WorldMatrix * position; + position = ViewMatrix * position; + position = ProjMatrix * position; + + gl_Position = position; + vNormal = inNormal; + vColor = inColor; +} \ No newline at end of file diff --git a/Samples/ModelLoading/ModelLoading.cpp b/Samples/ModelLoading/ModelLoading.cpp index cfeea17..77b8a4b 100644 --- a/Samples/ModelLoading/ModelLoading.cpp +++ b/Samples/ModelLoading/ModelLoading.cpp @@ -1,6 +1,12 @@ #include "ModelLoading.h" #include +#if defined(CGL_RHI_OPENGL) || defined(CGL_RHI_VULKAN) + #define CGL_UPLOAD_MATRIX(mat) mat +#else + #define CGL_UPLOAD_MATRIX(mat) mat.Transpose() +#endif + namespace CGL { CGL_DEFINE_LOG_CATEGORY(ModelLoading); @@ -8,14 +14,18 @@ namespace CGL static constexpr byte s_vertexShader[] = { #if defined(CGL_RHI_DX11) - #include "ModelLoadingVS.hlsl.h" + #include "ModelLoadingVS.hlsl.h" +#elif defined(CGL_RHI_OPENGL) + #include "ModelLoadingVS.vert.h" #endif }; static constexpr byte s_pixelShader[] = { #if defined(CGL_RHI_DX11) - #include "ModelLoadingPS.hlsl.h" + #include "ModelLoadingPS.hlsl.h" +#elif defined(CGL_RHI_OPENGL) + #include "ModelLoadingPS.frag.h" #endif }; @@ -94,7 +104,8 @@ namespace CGL vbs.Type = Graphics::BufferType::Vertex; vbs.TypeSize = sizeof(decltype(vb)::value_type); vbs.Count = u32(vb.size()); - meshData.VertexBuffer = GetRenderer()->CreateVertexBuffer(vbs); + vbs.VertexType = typeid(decltype(vb)::value_type); + meshData.VBuffer = GetRenderer()->CreateVertexBuffer(vbs); // Create index buffer Graphics::BufferSource ibs; @@ -102,7 +113,7 @@ namespace CGL ibs.Type = Graphics::BufferType::Index; ibs.TypeSize = sizeof(u32); ibs.Count = u32(indices.size()); - meshData.IndexBuffer = GetRenderer()->CreateIndexBuffer(ibs); + meshData.IBuffer = GetRenderer()->CreateIndexBuffer(ibs); } // Create constant buffer @@ -122,14 +133,14 @@ namespace CGL } f32 time = 0.0f; - void ModelLoading::OnUpdate([[maybe_unused]] const SDL_Event& e) + void ModelLoading::OnUpdate([[maybe_unused]] const SDL_Event& e, f32 deltaTime) { - time += 0.0001f; + time += deltaTime; FrameData data { - .World = SM::Matrix::CreateRotationY(time), - .View = m_camera.GetViewMatrix().Transpose(), - .Projection = m_camera.GetProjectionMatrix().Transpose() + .World = CGL_UPLOAD_MATRIX(SM::Matrix::CreateRotationY(time).Transpose()), + .View = CGL_UPLOAD_MATRIX(m_camera.GetViewMatrix()), + .Projection = CGL_UPLOAD_MATRIX(m_camera.GetProjectionMatrix()) }; GetRenderer()->SetConstantBufferData(m_constantBuffer, data); @@ -144,10 +155,10 @@ namespace CGL for (auto& subMesh : m_mesh.SubMeshes) { auto& meshData = subMesh.GetMeshData(); - GetRenderer()->SetVertexBuffer(meshData.VertexBuffer); - GetRenderer()->SetIndexBuffer(meshData.IndexBuffer); + GetRenderer()->SetVertexBuffer(meshData.VBuffer); + GetRenderer()->SetIndexBuffer(meshData.IBuffer); - GetRenderer()->DrawIndexed(meshData.IndexBuffer.IndicesCount); + GetRenderer()->DrawIndexed(meshData.IBuffer.IndicesCount); } } diff --git a/Samples/ModelLoading/ModelLoading.h b/Samples/ModelLoading/ModelLoading.h index 421e622..f7fb4e4 100644 --- a/Samples/ModelLoading/ModelLoading.h +++ b/Samples/ModelLoading/ModelLoading.h @@ -26,7 +26,7 @@ namespace CGL private: bool OnInit() override final; - void OnUpdate(const SDL_Event& e) override final; + void OnUpdate(const SDL_Event& e, f32 deltaTime) override final; void OnRender() override final; void OnResize(u32 width, u32 height) override final; void OnShutdown() override final; diff --git a/Samples/ModelLoading/xmake.lua b/Samples/ModelLoading/xmake.lua index 29ba85f..2258416 100644 --- a/Samples/ModelLoading/xmake.lua +++ b/Samples/ModelLoading/xmake.lua @@ -8,7 +8,7 @@ target("ModelLoading") set_group("Samples") add_rules("RHICompat", "CopyOBJ") - add_packages("libsdl", "tinyobjloader") + add_packages("libsdl", "directxmath", "tinyobjloader") add_includedirs("..", "$(projectdir)") add_files("**.cpp") @@ -35,7 +35,9 @@ target("ModelLoading") add_files("Assets/**.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/OpenGL/SpinningCubeVS.vert b/Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert index 7f397b5..16a1061 100644 --- a/Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert +++ b/Samples/SpinningCube/Assets/OpenGL/SpinningCubeVS.vert @@ -15,6 +15,7 @@ out vec3 fragColor; void main() { vec4 position = vec4(inPosition, 1.0); + position = WorldMatrix * position; position = ViewMatrix * position; position = ProjMatrix * position;