From 621dae4c4047113e52ac46d81dab0c466bc828bb Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 28 Oct 2022 13:32:13 +0800 Subject: [PATCH 1/7] All Vec3 and Vec4s in materials are now automatically set to full 1.0 vectors --- SHADE_Application/src/Scenes/SBTestScene.cpp | 3 +-- .../MiddleEnd/Interface/SHGraphicsSystem.cpp | 1 - .../MiddleEnd/Interface/SHGraphicsSystem.h | 1 + .../MiddleEnd/Interface/SHMaterial.cpp | 18 ++++++++++++++++++ .../Graphics/MiddleEnd/Interface/SHMaterial.h | 6 ++++++ .../MiddleEnd/Interface/SHMaterial.hpp | 10 ++++++++-- 6 files changed, 34 insertions(+), 5 deletions(-) diff --git a/SHADE_Application/src/Scenes/SBTestScene.cpp b/SHADE_Application/src/Scenes/SBTestScene.cpp index f80ec19e..8cbeb43a 100644 --- a/SHADE_Application/src/Scenes/SBTestScene.cpp +++ b/SHADE_Application/src/Scenes/SBTestScene.cpp @@ -124,8 +124,7 @@ namespace Sandbox auto& floorCollider = *SHComponentManager::GetComponent_s(floor); floorRenderable.SetMesh(CUBE_MESH); - floorRenderable.SetMaterial(customMat); - floorRenderable.GetModifiableMaterial()->SetProperty("data.color", SHVec4(1.0f, 1.0f, 1.0f, 1.0f)); + floorRenderable.SetMaterial(graphicsSystem->GetDefaultMaterialInstance()); floorTransform.SetWorldScale({ 7.5f, 0.5f, 7.5 }); floorTransform.SetWorldPosition({ 0.0f, -3.0f, -5.0f }); diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp index 2b8e97bc..3330a189 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.cpp @@ -200,7 +200,6 @@ namespace SHADE auto cubeFS = shaderModuleLibrary.GetBuiltInShaderModule("TestCube_FS"); defaultMaterial = AddMaterial(cubeVS, cubeFS, gBufferSubpass); - } void SHGraphicsSystem::InitMiddleEnd(void) noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h index 4e91ca8d..f657965c 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHGraphicsSystem.h @@ -144,6 +144,7 @@ namespace SHADE Handle AddMaterialInstanceCopy(Handle materialInst); void RemoveMaterialInstance(Handle materialInstance); Handle GetDefaultMaterial() { return defaultMaterial; } + Handle GetDefaultMaterialInstance() { return AddOrGetBaseMaterialInstance(defaultMaterial); } /*-----------------------------------------------------------------------------*/ /* Mesh Registration Functions */ diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp index b6e1974f..b27f48b9 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.cpp @@ -4,6 +4,8 @@ #include "Graphics/Pipeline/SHVkPipeline.h" #include "SHGraphicsConstants.h" #include "Graphics/Shaders/BlockInterface/SHShaderBlockInterface.h" +#include "Math/Vector/SHVec3.h" +#include "Math/Vector/SHVec4.h" namespace SHADE { @@ -49,6 +51,22 @@ namespace SHADE // Reset all the properties to default values if (propMemory) memset(propMemory.get(), 0, propMemorySize); + + // Initialize Vectors to all 1.0 by default + const Handle SHADER_INFO = GetShaderBlockInterface(); + for (int i = 0; i < SHADER_INFO->GetVariableCount(); ++i) + { + const auto& VAR = SHADER_INFO->GetVariable(i); + switch (VAR->type) + { + case SHShaderBlockInterface::Variable::Type::VECTOR3: + setPropertyUnsafe(VAR->offset, SHVec3::One); + break; + case SHShaderBlockInterface::Variable::Type::VECTOR4: + setPropertyUnsafe(VAR->offset, SHVec4::One); + break; + } + } } void SHMaterial::ExportProperties(void* dest) const noexcept diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h index ad7da4a6..964f9e34 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.h @@ -75,6 +75,12 @@ namespace SHADE Handle pipeline; std::unique_ptr propMemory; Byte propMemorySize = 0; + + /*-----------------------------------------------------------------------------*/ + /* Helper Functions */ + /*-----------------------------------------------------------------------------*/ + template + inline void setPropertyUnsafe(uint32_t memOffset, const T& value); // SetProperty() but without checks }; } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp index 3e56bfd5..8c205570 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp @@ -33,7 +33,7 @@ namespace SHADE } // Get offset and modify the memory directly - T* dataPtr = propMemory.get() + PROP_INFO->offset; + T* dataPtr = reinterpret_cast(propMemory.get() + PROP_INFO->offset); *dataPtr = value; } @@ -44,7 +44,7 @@ namespace SHADE if (memOffset + sizeof(T) > propMemorySize) throw std::invalid_argument("Attempted to set an invalid property!"); // Set - (*reinterpret_cast(propMemory.get() + memOffset)) = value; + setPropertyUnsafe(memOffset, value); } template @@ -81,4 +81,10 @@ namespace SHADE { return const_cast(const_cast(this)->GetProperty(memOffset)); } + + template + void SHMaterial::setPropertyUnsafe(uint32_t memOffset, const T& value) + { + (*reinterpret_cast(propMemory.get() + memOffset)) = value; + } } From 7d6af884a46beded910ca49aa8d01fe25376e489 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 28 Oct 2022 13:53:05 +0800 Subject: [PATCH 2/7] Added Color struct --- SHADE_Managed/src/Graphics/Color.cxx | 154 ++++++++++++++ SHADE_Managed/src/Graphics/Color.hxx | 287 +++++++++++++++++++++++++++ SHADE_Managed/src/Math/Math.cxx | 11 + SHADE_Managed/src/Math/Math.hxx | 20 +- 4 files changed, 471 insertions(+), 1 deletion(-) create mode 100644 SHADE_Managed/src/Graphics/Color.cxx create mode 100644 SHADE_Managed/src/Graphics/Color.hxx diff --git a/SHADE_Managed/src/Graphics/Color.cxx b/SHADE_Managed/src/Graphics/Color.cxx new file mode 100644 index 00000000..cf1fff47 --- /dev/null +++ b/SHADE_Managed/src/Graphics/Color.cxx @@ -0,0 +1,154 @@ +/************************************************************************************//*! +\file Color.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Nov 3, 2021 +\brief Contains the definition of the functions of the managed Color struct. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "Graphics/Color.hxx" +// Standard Libraries +#include +// Project Includes +#include "Math/Math.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + Color::Color(float _red) + : Color { _red, 0.0f, 0.0f, 1.0f } + {} + Color::Color(float _red, float _green) + : Color { _red, _green, 0.0f, 1.0f } + {} + Color::Color(float _red, float _green, float _blue) + : Color { _red, _green, _blue, 1.0f } + {} + Color::Color(float _red, float _green, float _blue, float _alpha) + : r { _red } + , g { _green } + , b { _blue } + , a { _alpha } + {} + + /*---------------------------------------------------------------------------------*/ + /* IEquatable */ + /*---------------------------------------------------------------------------------*/ + bool Color::Equals(Object^ o) + { + try + { + Color col = safe_cast(o); + return Equals(col); + } + catch (System::InvalidCastException^) + { + return false; + } + } + + /*---------------------------------------------------------------------------------*/ + /* Object Overrides */ + /*---------------------------------------------------------------------------------*/ + bool Color::Equals(Color other) + { + return Math::CompareFloat(this->r, other.r) + && + Math::CompareFloat(this->g, other.g) + && + Math::CompareFloat(this->b, other.b) + && + Math::CompareFloat(this->a, other.a); + } + int Color::GetHashCode() + { + const int HASH = 19; + const int HASH2 = 23; + const int HASH3 = 29; + return r.GetHashCode() * HASH + + g.GetHashCode() * HASH2 + + b.GetHashCode() * HASH3 + + a.GetHashCode(); + } + + /*---------------------------------------------------------------------------------*/ + /* Static Functions */ + /*---------------------------------------------------------------------------------*/ + Color Color::Lerp(Color colA, Color colB, float t) + { + return LerpUnclamped(colA, colB, std::clamp(t, 0.0f, 1.0f)); + } + + Color Color::LerpUnclamped(Color colA, Color colB, float t) + { + return colA + ((colB - colA) * t); + } + Color Color::operator+(Color lhs, Color rhs) + { + return Color + ( + lhs.r + rhs.r, + lhs.g + rhs.g, + lhs.b + rhs.b, + lhs.a + rhs.a + ); + } + Color Color::operator-(Color lhs, Color rhs) + { + return Color + ( + lhs.r - rhs.r, + lhs.g - rhs.g, + lhs.b - rhs.b, + lhs.a - rhs.a + ); + } + Color Color::operator*(Color lhs, Color rhs) + { + return Color + ( + lhs.r * rhs.r, + lhs.g * rhs.g, + lhs.b * rhs.b, + lhs.a * rhs.a + ); + } + Color Color::operator*(Color lhs, float rhs) + { + return Color + ( + lhs.r * rhs, + lhs.g * rhs, + lhs.b * rhs, + lhs.a * rhs + ); + } + Color Color::operator/(Color lhs, float rhs) + { + return Color + ( + lhs.r / rhs, + lhs.g / rhs, + lhs.b / rhs, + lhs.a / rhs + ); + } + bool Color::operator==(Color lhs, Color rhs) + { + return lhs.Equals(rhs); + } + bool Color::operator!=(Color lhs, Color rhs) + { + return !(lhs == rhs); + } +} diff --git a/SHADE_Managed/src/Graphics/Color.hxx b/SHADE_Managed/src/Graphics/Color.hxx new file mode 100644 index 00000000..d6a46216 --- /dev/null +++ b/SHADE_Managed/src/Graphics/Color.hxx @@ -0,0 +1,287 @@ +/************************************************************************************//*! +\file Color.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the managed Color struct with the + declaration of functions for working with it. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +namespace SHADE +{ + /// + /// CLR version of the the SHADE Engine's Color struct which describes a Color + /// encoded using floating point numbers that range from 0.0f to 1.0f. + /// + [System::Runtime::InteropServices::StructLayout(System::Runtime::InteropServices::LayoutKind::Sequential)] + public value struct Color : public System::IEquatable + { + public: + /*-----------------------------------------------------------------------------*/ + /* Type Definitions */ + /*-----------------------------------------------------------------------------*/ + /// + /// A static class that contains a set of default Colors. + /// + ref class Defaults abstract sealed + { + public: + /*-------------------------------------------------------------------------*/ + /* Properties */ + /*-------------------------------------------------------------------------*/ + /// + /// Pure black. + /// + static property Color Black + { + Color get() { return Color(0.0f, 0.0f, 0.0f); } + } + /// + /// Light Gray, lighter than gray. + /// + static property Color LightGray + { + Color get() { return Color(0.827451f, 0.827451f, 0.827451f); } + } + /// + /// Gray, halfway between black and white. + /// + static property Color Gray + { + Color get() { return Color(0.5f, 0.5f, 0.5f); } + } + /// + /// Dark Gray, darker than gray. + /// + static property Color DarkGray + { + Color get() { return Color(0.622f, 0.622f, 0.622f); } + } + /// + /// Pure white. + /// + static property Color White + { + Color get() { return Color(1.0f, 1.0f, 1.0f); } + } + /// + /// Pure red. + /// + static property Color Red + { + Color get() { return Color(1.0f, 0.0f, 0.0f); } + } + /// + /// Pure green. + /// + static property Color Green + { + Color get() { return Color(0.0f, 1.0f, 0.0f); } + } + /// + /// Pure blue. + /// + static property Color Blue + { + Color get() { return Color(0.0f, 0.0f, 1.0f); } + } + /// + /// Pure cyan, mix of pure green and blue. + /// + static property Color Cyan + { + Color get() { return Color(0.0f, 1.0f, 1.0f); } + } + /// + /// Pure magenta, mix of pure red and blue. + /// + static property Color Magenta + { + Color get() { return Color(1.0f, 0.0f, 1.0f); } + } + /// + /// Pure yellow, mix of pure red and green. + /// + static property Color Yellow + { + Color get() { return Color(1.0f, 1.0f, 0.0f); } + } + }; + + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructor to construct a Color with the specified components with the + /// green, blue and alpha component set to 1.0f. + /// + /// Red component to set. + Color(float _red); + /// + /// Constructor to construct a Color with the specified components with the + /// blue and alpha component set to 1.0f. + /// + /// Red component to set. + /// Green component to set. + Color(float _red, float _green); + /// + /// Constructor to construct a Color with the specified components with the + /// alpha component set to 1.0f. + /// + /// Red component to set. + /// Green component to set. + /// Blue component to set. + Color(float _red, float _green, float _blue); + /// + /// Constructor to construct a Color with the specified components. + /// + /// Red component to set. + /// Green component to set. + /// Blue component to set. + /// Alpha component to set. + Color(float _red, float _green, float _blue, float _alpha); + + /*-----------------------------------------------------------------------------*/ + /* Public Members */ + /*-----------------------------------------------------------------------------*/ + /// + /// Red component of the colour. Ranges from 0.0f to 1.0f. + /// + float r; + /// + /// Green component of the colour. Ranges from 0.0f to 1.0f. + /// + float g; + /// + /// Blue component of the colour. Ranges from 0.0f to 1.0f. + /// + float b; + /// + /// Alpha component of the colour. Ranges from 0.0f to 1.0f. + /// + float a; + + /*-----------------------------------------------------------------------------*/ + /* IEquatable */ + /*-----------------------------------------------------------------------------*/ + /// + /// Compares equality with an object of the same type. + /// + /// The object to compare with. + /// True if both objects are the same. + virtual bool Equals(Color other); + + /*-----------------------------------------------------------------------------*/ + /* Object */ + /*-----------------------------------------------------------------------------*/ + /// + /// Compares equality with another unboxed object. + /// + /// The unboxed object to compare with. + /// True if both objects are the same. + bool Equals(Object^ o) override; + /// + /// Gets a unique hash for this object. + /// + /// Unique hash for this object. + int GetHashCode() override; + + + /*-----------------------------------------------------------------------------*/ + /* Static Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Linearly interpolates between two specified points. + /// This is most commonly used to find a point some fraction of the way along a + /// line between two endpoints. + /// + /// The start Color, returned when t = 0.0. + /// The end Color, returned when t = 1.0. + /// + /// Value used to interpolate between a and b which is clamped to + /// the range[0, 1]. + /// + /// The interpolated Vector3. + static Color Lerp(Color colA, Color colB, float t); + /// + /// Linearly interpolates between two specified points. + /// This is most commonly used to find a point some fraction of the way along a + /// line between two endpoints. + /// Unlike Lerp(), t is not clamped to a range at all. + /// + /// The start Color, returned when t = 0.0. + /// The end Color, returned when t = 1.0. + /// Value used to interpolate between a and b. + /// The interpolated Color. + static Color LerpUnclamped(Color colA, Color colB, float t); + + /*-----------------------------------------------------------------------------*/ + /* Overloaded Operators */ + /*-----------------------------------------------------------------------------*/ + /// + /// Adds two Colors together and returns the result. + /// + /// Color to add. + /// Another Color to add. + /// The result of lhs added to rhs + static Color operator+(Color lhs, Color rhs); + /// + /// Subtracts a Color from another Color and returns the result. + /// + /// Color to subtract from. + /// Another Color to subtract. + /// The result of rhs subtracted from lhs. + static Color operator-(Color lhs, Color rhs); + /// + /// Calculates the component-wise multiplication of two Colors and returns the + /// result. + /// + /// Color to multiply with. + /// Another Color to multiply with. + /// The result of rhs subtracted from lhs. + static Color operator*(Color lhs, Color rhs); + /// + /// Calculates the multiplication of a Color with a scalar value and returns + /// the result. + /// + /// Color to multiply with. + /// Scalar to multiply with. + /// The result of the scalar multiplication. + static Color operator*(Color lhs, float rhs); + /// + /// Calculates the division of a Color with a scalar value and returns + /// the result. + /// + /// Scalar to divide with. + /// Color to divide with. + /// The result of the scalar division. + static Color operator/(Color lhs, float rhs); + /// + /// Checks if two Colors are approximately equal. + /// + /// Color to compare. + /// Another Color to compare. + /// + /// True if all components are approximately equal within the default + /// tolerance value. + /// + static bool operator==(Color lhs, Color rhs); + /// + /// Checks if two Colors are not approximately equal. + /// + /// Color to compare. + /// Another Color to compare. + /// + /// True if all components are not approximately equal within the default + /// tolerance value. + /// + static bool operator!=(Color lhs, Color rhs); + }; +} diff --git a/SHADE_Managed/src/Math/Math.cxx b/SHADE_Managed/src/Math/Math.cxx index fa72e2b6..bc625f5b 100644 --- a/SHADE_Managed/src/Math/Math.cxx +++ b/SHADE_Managed/src/Math/Math.cxx @@ -54,4 +54,15 @@ namespace SHADE { return (value - a) / (b - a); } + + bool Math::CompareFloat(float a, float b) + { + return CompareFloat(a, b, Epsilon); + } + + bool Math::CompareFloat(float a, float b, float tolerance) + { + return System::MathF::Abs(a - b) < tolerance; + } + } diff --git a/SHADE_Managed/src/Math/Math.hxx b/SHADE_Managed/src/Math/Math.hxx index 1578d97c..c6b8394e 100644 --- a/SHADE_Managed/src/Math/Math.hxx +++ b/SHADE_Managed/src/Math/Math.hxx @@ -81,12 +81,30 @@ namespace SHADE /// The interpolated float result between the two float values. static float LerpUnclamped(float a, float b, float t); /// - /// Calculates the linear parameter t that produces the interpolant value within the range [a, b]. + /// Calculates the linear parameter t that produces the interpolant value within + /// the range [a, b]. /// /// Start value. /// End value. /// Value between start and end. /// Percentage of value between start and end. static float InverseLerp(float a, float b, float value); + /// + /// Compares if two float values are close enough to be the same with a tolerance + /// of Epsilon. + /// + /// One of the values to compare. + /// The other value to compare. + /// True if a and b are practically the same. + static bool CompareFloat(float a, float b); + /// + /// Compares if two float values are close enough to be the same with the + /// specified tolerance value. + /// + /// One of the values to compare. + /// The other value to compare. + /// Tolerance for floating point comparison. + /// True if a and b are practically the same. + static bool CompareFloat(float a, float b, float tolerance); }; } From 199897adb4cd084551ae1f2d554955e096867eb7 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 28 Oct 2022 19:00:17 +0800 Subject: [PATCH 3/7] Added GenericHandles to SHADE_Managed --- SHADE_Engine/src/Resource/SHHandle.h | 6 ++ SHADE_Engine/src/Resource/SHHandle.hpp | 5 ++ SHADE_Engine/src/Resource/SparseSet.hpp | 8 +-- SHADE_Managed/src/Engine/GenericHandle.cxx | 47 +++++++++++++++ SHADE_Managed/src/Engine/GenericHandle.hxx | 68 ++++++++++++++++++++++ SHADE_Managed/src/Utility/Convert.cxx | 16 +++++ SHADE_Managed/src/Utility/Convert.hxx | 19 ++++++ 7 files changed, 165 insertions(+), 4 deletions(-) create mode 100644 SHADE_Managed/src/Engine/GenericHandle.cxx create mode 100644 SHADE_Managed/src/Engine/GenericHandle.hxx diff --git a/SHADE_Engine/src/Resource/SHHandle.h b/SHADE_Engine/src/Resource/SHHandle.h index 6acc85ed..49dd56b9 100644 --- a/SHADE_Engine/src/Resource/SHHandle.h +++ b/SHADE_Engine/src/Resource/SHHandle.h @@ -195,6 +195,11 @@ namespace SHADE template inline bool operator==(const Handle& rhs) const noexcept; + /*-----------------------------------------------------------------------------*/ + /* Query Functions */ + /*-----------------------------------------------------------------------------*/ + inline SHResourceLibraryBase* GetLibrary() const; + protected: /*-----------------------------------------------------------------------------*/ /* Data Members */ @@ -206,6 +211,7 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ template friend class Handle; + friend class Convert; }; /// diff --git a/SHADE_Engine/src/Resource/SHHandle.hpp b/SHADE_Engine/src/Resource/SHHandle.hpp index 53061ac7..91eed058 100644 --- a/SHADE_Engine/src/Resource/SHHandle.hpp +++ b/SHADE_Engine/src/Resource/SHHandle.hpp @@ -96,6 +96,11 @@ namespace SHADE return id.Raw == rhs.id.Raw && library == static_cast(rhs.library); } + SHResourceLibraryBase* SHADE::Handle::GetLibrary() const + { + return library; + } + /*---------------------------------------------------------------------------------*/ /* ISelfHandle - Constructors */ /*---------------------------------------------------------------------------------*/ diff --git a/SHADE_Engine/src/Resource/SparseSet.hpp b/SHADE_Engine/src/Resource/SparseSet.hpp index 5afcdee7..816ca432 100644 --- a/SHADE_Engine/src/Resource/SparseSet.hpp +++ b/SHADE_Engine/src/Resource/SparseSet.hpp @@ -70,13 +70,13 @@ namespace SHADE } template - SparseSet::reference SparseSet::at(index_type idx) + typename SparseSet::reference SparseSet::at(index_type idx) { return const_cast(static_cast&>(*this).at(idx)); } template - SparseSet::const_reference SparseSet::at(index_type idx) const + typename SparseSet::const_reference SparseSet::at(index_type idx) const { // Range Check if (idx >= sparseArray.size() || !contains(idx)) @@ -84,7 +84,7 @@ namespace SHADE return denseArray[sparseArray[idx]]; } template - SparseSet::size_type SparseSet::size() const + typename SparseSet::size_type SparseSet::size() const { return denseArray.size(); } @@ -105,7 +105,7 @@ namespace SHADE } template template - SparseSet::reference SparseSet::insert(index_type idx, Args && ...args) + typename SparseSet::reference SparseSet::insert(index_type idx, Args && ...args) { // We need to resize the array if (idx >= sparseArray.size()) diff --git a/SHADE_Managed/src/Engine/GenericHandle.cxx b/SHADE_Managed/src/Engine/GenericHandle.cxx new file mode 100644 index 00000000..41a69c18 --- /dev/null +++ b/SHADE_Managed/src/Engine/GenericHandle.cxx @@ -0,0 +1,47 @@ +/************************************************************************************//*! +\file Renderable.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the functions of the managed Renderable class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#include "SHpch.h" +#include "GenericHandle.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + GenericHandle::GenericHandle(Handle handle) + : id { handle.GetId().Raw } + , library { reinterpret_cast(handle.GetLibrary()) } + {} + + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + System::UInt64 GenericHandle::Id::get() + { + return id; + } + System::IntPtr GenericHandle::Library::get() + { + return library; + } + + /*---------------------------------------------------------------------------------*/ + /* Overloaded Operators */ + /*---------------------------------------------------------------------------------*/ + GenericHandle::operator bool() + { + return library.ToPointer() != nullptr && id != System::UInt64::MaxValue; + } + +} diff --git a/SHADE_Managed/src/Engine/GenericHandle.hxx b/SHADE_Managed/src/Engine/GenericHandle.hxx new file mode 100644 index 00000000..3f8e395f --- /dev/null +++ b/SHADE_Managed/src/Engine/GenericHandle.hxx @@ -0,0 +1,68 @@ +/************************************************************************************//*! +\file Handle.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the managed GenericHandle struct. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// External Dependencies +#include "Resource/SHHandle.h" + +namespace SHADE +{ + /// + /// Managed version of the generic Handle. + /// + public value struct GenericHandle + { + public: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructs a GenericHandle for a native generic Handle. + /// + /// Handle to create a GenericHandle from. + explicit GenericHandle(Handle handle); + + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// The internal ID of the handle. + /// + property System::UInt64 Id + { + System::UInt64 get(); + } + /// + /// The library that the handle was issued by. + /// + property System::IntPtr Library + { + System::IntPtr get(); + } + + /*-----------------------------------------------------------------------------*/ + /* Overloaded Operators */ + /*-----------------------------------------------------------------------------*/ + /// + /// Converts to true if this is a valid Handle. + /// + inline operator bool(); + + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + System::UInt64 id; + System::IntPtr library; + }; +} \ No newline at end of file diff --git a/SHADE_Managed/src/Utility/Convert.cxx b/SHADE_Managed/src/Utility/Convert.cxx index cb4815aa..1d89569f 100644 --- a/SHADE_Managed/src/Utility/Convert.cxx +++ b/SHADE_Managed/src/Utility/Convert.cxx @@ -84,4 +84,20 @@ namespace SHADE { return msclr::interop::marshal_as(str); } + + /*---------------------------------------------------------------------------------*/ + /* Handle Conversions */ + /*---------------------------------------------------------------------------------*/ + Handle Convert::ToNative(GenericHandle handle) + { + Handle nativeHandle; + nativeHandle.id.Raw = handle.Id; + nativeHandle.library = reinterpret_cast(handle.Library.ToPointer()); + return nativeHandle; + } + + GenericHandle Convert::ToCLI(Handle handle) + { + return GenericHandle(handle); + } } diff --git a/SHADE_Managed/src/Utility/Convert.hxx b/SHADE_Managed/src/Utility/Convert.hxx index 19faffde..d3dca740 100644 --- a/SHADE_Managed/src/Utility/Convert.hxx +++ b/SHADE_Managed/src/Utility/Convert.hxx @@ -20,6 +20,7 @@ of DigiPen Institute of Technology is prohibited. #include "Math/Vector/SHVec3.h" #include "Math/SHQuaternion.h" #include "Math/SHRay.h" +#include "Resource/SHHandle.h" // Project Includes #include "Engine/Entity.hxx" @@ -27,6 +28,7 @@ of DigiPen Institute of Technology is prohibited. #include "Math/Vector3.hxx" #include "Math/Quaternion.hxx" #include "Math/Ray.hxx" +#include "Engine/GenericHandle.hxx" namespace SHADE { @@ -118,6 +120,23 @@ namespace SHADE /// The native std::string to convert from. /// Managed copy of a native std::string. static System::String^ ToCLI(const std::string& str); + + /*-----------------------------------------------------------------------------*/ + /* Handle Conversions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Converts from a managed GenericHandle to a Handle. + /// + /// GenericHandle to convert from. + /// Native generic Handle. + static Handle ToNative(GenericHandle handle); + /// + /// Converts from a native generic Handle to a managed GenericHandle. + /// + /// The native handle to convert. + /// Managed copy of the native Handle. + static GenericHandle ToCLI(Handle handle); + }; /// From 541f44c039c4b5234d94ffd3f7d2e213c6566bb2 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 28 Oct 2022 19:02:54 +0800 Subject: [PATCH 4/7] Fixed bugs in SHMaterial, SHMeshLibrary and removed unused functions in SHTextureLibrary --- .../MiddleEnd/Interface/SHMaterial.hpp | 4 +- .../Interface/SHMaterialInstance.hpp | 10 +-- .../MiddleEnd/Interface/SHMeshLibrary.h | 3 +- .../MiddleEnd/Textures/SHTextureLibrary.cpp | 62 ------------------- .../MiddleEnd/Textures/SHTextureLibrary.h | 3 - 5 files changed, 9 insertions(+), 73 deletions(-) diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp index 49587921..dceee512 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterial.hpp @@ -33,7 +33,7 @@ namespace SHADE } // Get offset and modify the memory directly - T* dataPtr = propMemory.get() + PROP_INFO->offset; + T* dataPtr = reinterpret_cast(propMemory.get() + PROP_INFO->offset); *dataPtr = value; } template @@ -47,7 +47,7 @@ namespace SHADE } // Get offset and return the memory directly - T* dataPtr = propMemory.get() + PROP_INFO->offset; + T* dataPtr = reinterpret_cast(propMemory.get() + PROP_INFO->offset); return *dataPtr; } template diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.hpp b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.hpp index 4621c273..e70631ea 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.hpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMaterialInstance.hpp @@ -69,15 +69,15 @@ namespace SHADE // Search Override Data for the property uint32_t PROP_IDX = SHADER_INFO->GetVariableIndex(key); - auto prop = std::find(overrideData.begin(), overrideData.end(), [&](const OverrideData& data) - { - return PROP_IDX == data.Index; - }); + auto prop = std::find_if(overrideData.begin(), overrideData.end(), [&](const OverrideData& data) + { + return PROP_IDX == data.Index; + }); if (prop == overrideData.end()) throw std::invalid_argument("Attempted to get an property that was not set previously!"); // Get offset and return the memory directly - T* dataPtr = dataStore.get() + prop->StoredDataOffset; + T* dataPtr = reinterpret_cast(dataStore.get() + prop->StoredDataOffset); return *dataPtr; } template diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h index 72ac1878..b0cbdce1 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Interface/SHMeshLibrary.h @@ -17,7 +17,8 @@ of DigiPen Institute of Technology is prohibited. // Project Includes #include "Resource/SHHandle.h" #include "Resource/SHResourceLibrary.h" -#include "Math/SHMath.h" +#include "Math/Vector/SHVec2.h" +#include "Math/Vector/SHVec3.h" namespace SHADE { diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.cpp b/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.cpp index 5c315ff6..8719458b 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.cpp +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.cpp @@ -214,66 +214,4 @@ namespace SHADE SHLOG_ERROR("Image layouts are invalid. "); } } - - vk::Format SHTextureLibrary::ddsLoaderToVkFormat(tinyddsloader::DDSFile::DXGIFormat format, bool isLinear) - { - switch (format) - { - case tinyddsloader::DDSFile::DXGIFormat::BC1_UNorm: - return vk::Format::eBc1RgbaUnormBlock; - case tinyddsloader::DDSFile::DXGIFormat::BC1_UNorm_SRGB: - return vk::Format::eBc1RgbaSrgbBlock; - case tinyddsloader::DDSFile::DXGIFormat::BC2_UNorm: - case tinyddsloader::DDSFile::DXGIFormat::BC2_UNorm_SRGB: - return isLinear ? vk::Format::eBc2UnormBlock : vk::Format::eBc2SrgbBlock; - case tinyddsloader::DDSFile::DXGIFormat::BC3_UNorm: - case tinyddsloader::DDSFile::DXGIFormat::BC3_UNorm_SRGB: - return isLinear ? vk::Format::eBc3UnormBlock : vk::Format::eBc3SrgbBlock; - case tinyddsloader::DDSFile::DXGIFormat::BC5_UNorm: - case tinyddsloader::DDSFile::DXGIFormat::BC5_SNorm: - return isLinear ? vk::Format::eBc5UnormBlock : vk::Format::eBc5SnormBlock; - case tinyddsloader::DDSFile::DXGIFormat::R8G8B8A8_UNorm: - case tinyddsloader::DDSFile::DXGIFormat::R8G8B8A8_UNorm_SRGB: - return isLinear ? vk::Format::eR8G8B8A8Unorm : vk::Format::eR8G8B8A8Srgb; - case tinyddsloader::DDSFile::DXGIFormat::R8G8B8A8_SNorm: - return vk::Format::eR8G8B8A8Snorm; - case tinyddsloader::DDSFile::DXGIFormat::B8G8R8A8_UNorm: - case tinyddsloader::DDSFile::DXGIFormat::B8G8R8A8_UNorm_SRGB: - return isLinear ? vk::Format::eB8G8R8A8Unorm : vk::Format::eB8G8R8A8Srgb; - case tinyddsloader::DDSFile::DXGIFormat::B8G8R8X8_UNorm: - case tinyddsloader::DDSFile::DXGIFormat::B8G8R8X8_UNorm_SRGB: - return isLinear ? vk::Format::eB8G8R8A8Unorm : vk::Format::eB8G8R8Srgb; - default: - throw std::runtime_error("Unsupported DDS format."); - } - - //switch (format) - //{ - //case tinyddsloader::DDSFile::DXGIFormat::R8G8B8A8_UNorm: - //case tinyddsloader::DDSFile::DXGIFormat::R8G8B8A8_UNorm_SRGB: - // return (isLinear) ? vk::Format::eR8G8B8A8Unorm : vk::Format::eR8G8B8A8Srgb; - // - - //case tinyddsloader::DDSFile::DXGIFormat::B8G8R8A8_UNorm: - //case tinyddsloader::DDSFile::DXGIFormat::B8G8R8A8_UNorm_SRGB: - // return (isLinear) ? vk::Format::eB8G8R8A8Unorm : vk::Format::eB8G8R8A8Srgb; - // - - //case tinyddsloader::DDSFile::DXGIFormat::BC1_UNorm: - //case tinyddsloader::DDSFile::DXGIFormat::BC1_UNorm_SRGB: - // return (isLinear) ? vk::Format::eBc1RgbaUnormBlock : vk::Format::eBc1RgbaSrgbBlock; - - //case tinyddsloader::DDSFile::DXGIFormat::BC2_UNorm: - //case tinyddsloader::DDSFile::DXGIFormat::BC2_UNorm_SRGB: - // return (isLinear) ? vk::Format::eBc2UnormBlock : vk::Format::eBc2SrgbBlock; - - //case tinyddsloader::DDSFile::DXGIFormat::BC3_UNorm: - //case tinyddsloader::DDSFile::DXGIFormat::BC3_UNorm_SRGB: - // return (isLinear) ? vk::Format::eBc3UnormBlock : vk::Format::eBc3SrgbBlock; - - //case tinyddsloader::DDSFile::DXGIFormat::BC5_UNorm: - // return (isLinear) ? vk::Format::eBc5UnormBlock : vk::Format::eBc5SnormBlock; - // - //} - } } diff --git a/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h b/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h index 935a7a4d..9357b0e0 100644 --- a/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h +++ b/SHADE_Engine/src/Graphics/MiddleEnd/Textures/SHTextureLibrary.h @@ -14,8 +14,6 @@ of DigiPen Institute of Technology is prohibited. // STL Includes #include -// External Dependencies -#include "tinyddsloader.h" // Project Includes #include "Resource/SHHandle.h" #include "Resource/SHResourceLibrary.h" @@ -169,6 +167,5 @@ namespace SHADE /* Helper Functions */ /*-----------------------------------------------------------------------------*/ void preparePipelineBarriers(vk::ImageLayout oldLayout, vk::ImageLayout newLayout, vk::PipelineStageFlagBits& srcStage, vk::PipelineStageFlagBits& dstStage, std::vector& barriers); - vk::Format ddsLoaderToVkFormat(tinyddsloader::DDSFile::DXGIFormat format, bool isLinear); }; } From 7e5c8198133ad264304fb3caddbf97d1f8f30a29 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 28 Oct 2022 19:03:07 +0800 Subject: [PATCH 5/7] Fixed warning with Collider not being made abstract --- SHADE_Managed/src/Components/Collider.hxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Components/Collider.hxx b/SHADE_Managed/src/Components/Collider.hxx index f827ab71..9f88b983 100644 --- a/SHADE_Managed/src/Components/Collider.hxx +++ b/SHADE_Managed/src/Components/Collider.hxx @@ -27,7 +27,7 @@ namespace SHADE /// /// Base interface for all Collider Shapes. /// - public ref class ColliderBound + public ref class ColliderBound abstract { public: /*-----------------------------------------------------------------------------*/ From 9617ed3838d210585413ec5a076343bc231eaaa1 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 28 Oct 2022 19:23:40 +0800 Subject: [PATCH 6/7] Added NativeAsset, Material and Mesh representations along with a stub for Renderable --- SHADE_Managed/src/Assets/Material.cxx | 119 ++++++++++++++++++++ SHADE_Managed/src/Assets/Material.hxx | 81 +++++++++++++ SHADE_Managed/src/Assets/Mesh.cxx | 34 ++++++ SHADE_Managed/src/Assets/Mesh.hxx | 41 +++++++ SHADE_Managed/src/Assets/NativeAsset.cxx | 26 +++++ SHADE_Managed/src/Assets/NativeAsset.h++ | 49 ++++++++ SHADE_Managed/src/Assets/NativeAsset.hxx | 66 +++++++++++ SHADE_Managed/src/Components/Renderable.cxx | 37 ++++++ SHADE_Managed/src/Components/Renderable.hxx | 53 +++++++++ 9 files changed, 506 insertions(+) create mode 100644 SHADE_Managed/src/Assets/Material.cxx create mode 100644 SHADE_Managed/src/Assets/Material.hxx create mode 100644 SHADE_Managed/src/Assets/Mesh.cxx create mode 100644 SHADE_Managed/src/Assets/Mesh.hxx create mode 100644 SHADE_Managed/src/Assets/NativeAsset.cxx create mode 100644 SHADE_Managed/src/Assets/NativeAsset.h++ create mode 100644 SHADE_Managed/src/Assets/NativeAsset.hxx create mode 100644 SHADE_Managed/src/Components/Renderable.cxx create mode 100644 SHADE_Managed/src/Components/Renderable.hxx diff --git a/SHADE_Managed/src/Assets/Material.cxx b/SHADE_Managed/src/Assets/Material.cxx new file mode 100644 index 00000000..f4262c2a --- /dev/null +++ b/SHADE_Managed/src/Assets/Material.cxx @@ -0,0 +1,119 @@ +/************************************************************************************//*! +\file Material.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the implementation of the functions of the managed Material + class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "Material.hxx" +// Standard Library +#include +// Project Includes +#include "Utility/Convert.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Macro Definitions */ + /*---------------------------------------------------------------------------------*/ + #define SET_PROP(NATIVE_TYPE, MANAGED_TYPE) \ + (T::typeid == MANAGED_TYPE::typeid) \ + { \ + const NATIVE_TYPE VAL = safe_cast(System::Convert::ChangeType(value, MANAGED_TYPE::typeid)); \ + NativeObject->SetProperty(PROP_NAME, VAL); \ + } \ + + #define SET_PROP_CONVERT(NATIVE_TYPE, MANAGED_TYPE) \ + (T::typeid == MANAGED_TYPE::typeid) \ + { \ + const NATIVE_TYPE VAL = Convert::ToNative(safe_cast(System::Convert::ChangeType(value, MANAGED_TYPE::typeid))); \ + NativeObject->SetProperty(PROP_NAME, VAL); \ + } \ + + #define GET_PROP(NATIVE_TYPE, MANAGED_TYPE) \ + (T::typeid == MANAGED_TYPE::typeid) \ + { \ + return safe_cast(NativeObject->GetProperty(PROP_NAME)); \ + } \ + + #define GET_PROP_CONVERT(NATIVE_TYPE, MANAGED_TYPE) \ + (T::typeid == MANAGED_TYPE::typeid) \ + { \ + return safe_cast(Convert::ToCLI(NativeObject->GetProperty(PROP_NAME))); \ + } + + /*---------------------------------------------------------------------------------*/ + /* Explicit Template Instantiation */ + /*---------------------------------------------------------------------------------*/ + template ref class NativeAsset; + + /*---------------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*---------------------------------------------------------------------------------*/ + Material::Material(Handle material) + : NativeAsset{ material } + {} + + /*---------------------------------------------------------------------------------*/ + /* Material Properties Functions */ + /*---------------------------------------------------------------------------------*/ + generic + void Material::SetProperty(System::String^ name, T value) + { + if (!NativeObject) + throw gcnew System::InvalidOperationException("Attempted to set property on an invalid material!"); + + // Call the correct one based on type + const std::string PROP_NAME = Convert::ToNative(name); + try + { + if SET_PROP (int, System::Int32) + else if SET_PROP (int, System::Int64) + else if SET_PROP (float, float) + else if SET_PROP (double, double) + else if SET_PROP_CONVERT(SHVec2, Vector2) + else if SET_PROP_CONVERT(SHVec3, Vector3) + // TODO: Vector4 + } + catch (const std::invalid_argument&) + { + throw gcnew System::ArgumentException("Attempted to modify an invalid property on a material."); + } + } + + generic + T Material::GetProperty(System::String^ name) + { + if (!NativeObject) + throw gcnew System::InvalidOperationException("[Material] Attempted to get property of an invalid material!"); + + // Call the correct one based on type + const std::string PROP_NAME = Convert::ToNative(name); + try + { + if GET_PROP (int, System::Int32) + else if GET_PROP (int, System::Int64) + else if GET_PROP (float, float) + else if GET_PROP (double, double) + else if GET_PROP_CONVERT(SHVec2, Vector2) + else if GET_PROP_CONVERT(SHVec3, Vector3) + // TODO: Vector4 + } + catch (const std::invalid_argument&) + { + throw gcnew System::ArgumentException("Attempted to retrieve a property on a material with an invalid type."); + } + + throw gcnew System::ArgumentException("Attempted to retrieve an invalid property on a material."); + } +} diff --git a/SHADE_Managed/src/Assets/Material.hxx b/SHADE_Managed/src/Assets/Material.hxx new file mode 100644 index 00000000..25cc96a6 --- /dev/null +++ b/SHADE_Managed/src/Assets/Material.hxx @@ -0,0 +1,81 @@ +/************************************************************************************//*! +\file Material.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the managed Mesh class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// External Dependencies +#include "Resource/SHHandle.h" +#include "Graphics/MiddleEnd/Interface/SHMaterialInstance.h" +// Project Includes +#include "NativeAsset.hxx" +#include "Engine/GenericHandle.hxx" + +namespace SHADE +{ + /// + /// Managed counterpart of the native MaterialInstance object containing material + /// data that can be fed to Renderables for rendering. + /// + public ref class Material : public NativeAsset + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructor for the Material + /// + /// Handle to the native material object. + Material(Handle material); + + public: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + // TODO: Change Shader + + /*-----------------------------------------------------------------------------*/ + /* Material Properties Functions */ + /*-----------------------------------------------------------------------------*/ + /// + /// Set the value of a specific property. + /// + /// Type of property to set. + /// Name of the property to set. + /// Value to set te property to. + /// + /// If this Material object is invalid. + /// + /// + /// If the name or type was specified that does not match the material's shader's + /// defined properties. + /// + generic + void SetProperty(System::String^ name, T value); + /// + /// Retrieves the value of a specified property on the material. + /// + /// Type of property to get. + /// Name of the property to get. + /// Value of that property on the material. + /// + /// If this Material object is invalid. + /// + /// + /// If the name or type was specified that does not match the material's shader's + /// defined properties. + /// + generic + T GetProperty(System::String^ name); + }; +} diff --git a/SHADE_Managed/src/Assets/Mesh.cxx b/SHADE_Managed/src/Assets/Mesh.cxx new file mode 100644 index 00000000..95a61ff6 --- /dev/null +++ b/SHADE_Managed/src/Assets/Mesh.cxx @@ -0,0 +1,34 @@ +/************************************************************************************//*! +\file Mesh.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the implementation of the functions of the managed Mesh class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "Mesh.hxx" +// Project Headers +#include "Utility/Convert.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Explicit Template Instantiation */ + /*---------------------------------------------------------------------------------*/ + template ref class NativeAsset; + + /*---------------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*---------------------------------------------------------------------------------*/ + Mesh::Mesh(Handle mesh) + : NativeAsset { mesh } + {} +} diff --git a/SHADE_Managed/src/Assets/Mesh.hxx b/SHADE_Managed/src/Assets/Mesh.hxx new file mode 100644 index 00000000..8cd356ba --- /dev/null +++ b/SHADE_Managed/src/Assets/Mesh.hxx @@ -0,0 +1,41 @@ +/************************************************************************************//*! +\file Mesh.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the managed Mesh class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// External Dependencies +#include "Resource/SHHandle.h" +#include "Graphics/MiddleEnd/Interface/SHMeshLibrary.h" +// Project Includes +#include "NativeAsset.hxx" +#include "Engine/GenericHandle.hxx" + +namespace SHADE +{ + /// + /// Managed counterpart of the native Mesh object containing vertex data that can + /// be fed to Renderables for rendering. + /// + public ref class Mesh : public NativeAsset + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructor for the Mesh + /// + /// Handle to the mesh object. + Mesh(Handle mesh); + }; +} diff --git a/SHADE_Managed/src/Assets/NativeAsset.cxx b/SHADE_Managed/src/Assets/NativeAsset.cxx new file mode 100644 index 00000000..674207a1 --- /dev/null +++ b/SHADE_Managed/src/Assets/NativeAsset.cxx @@ -0,0 +1,26 @@ +/************************************************************************************//*! +\file NativeAsset.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the explicit template instantiation for some types of the + templated managed NativeAsset class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#include "SHpch.h" +// Primary Include +#include "NativeAsset.hxx" +// Project Includes +#include "Engine/GenericHandle.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Explicit Tempalte Instantiations */ + /*---------------------------------------------------------------------------------*/ +} \ No newline at end of file diff --git a/SHADE_Managed/src/Assets/NativeAsset.h++ b/SHADE_Managed/src/Assets/NativeAsset.h++ new file mode 100644 index 00000000..a4cd94b4 --- /dev/null +++ b/SHADE_Managed/src/Assets/NativeAsset.h++ @@ -0,0 +1,49 @@ +/************************************************************************************//*! +\file NativeAsset.h++ +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of templated functions for the managed + NativeAsset classes. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// Primary Include +#include "NativeAsset.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + template + GenericHandle NativeAsset::NativeObjectHandle::get() + { + return nativeObjHandle; + } + template + Handle NativeAsset::NativeObject::get() + try + { + return Handle(Convert::ToNative(nativeObjHandle)); + } + catch (const BadHandleCastException&) + { + return Handle(); // Null handle + } + + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + template + NativeAsset::NativeAsset(Handle nativeObj) + : nativeObjHandle{ Convert::ToCLI(Handle(nativeObj)) } + {} + +} diff --git a/SHADE_Managed/src/Assets/NativeAsset.hxx b/SHADE_Managed/src/Assets/NativeAsset.hxx new file mode 100644 index 00000000..68addb75 --- /dev/null +++ b/SHADE_Managed/src/Assets/NativeAsset.hxx @@ -0,0 +1,66 @@ +/************************************************************************************//*! +\file NativeAsset.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the template definition of the managed class that represents + native assets with a pointer to the corresponding native object. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2021 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +#include "Engine/GenericHandle.hxx" + +namespace SHADE +{ + /// + /// Generalised template class for a managed representation of a native asset + /// + /// + /// The type of the asset's native representation. + /// + template + public ref class NativeAsset + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + /// + /// Generic handle for the native object + /// + property GenericHandle NativeObjectHandle + { + GenericHandle get(); + } + /// + /// Copy of the Handle to the native object. + /// + property Handle NativeObject + { + Handle get(); + } + + /*-----------------------------------------------------------------------------*/ + /* Constructors/Destructor */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructor for the native asset + /// + /// Native asset object. + NativeAsset(Handle ptr); + + protected: + /*-----------------------------------------------------------------------------*/ + /* Data Members */ + /*-----------------------------------------------------------------------------*/ + GenericHandle nativeObjHandle; + }; +} + +#include "NativeAsset.h++" diff --git a/SHADE_Managed/src/Components/Renderable.cxx b/SHADE_Managed/src/Components/Renderable.cxx new file mode 100644 index 00000000..08a43506 --- /dev/null +++ b/SHADE_Managed/src/Components/Renderable.cxx @@ -0,0 +1,37 @@ +/************************************************************************************//*! +\file Renderable.cxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the functions of the managed Renderable class. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +// Precompiled Headers +#include "SHpch.h" +// Primary Header +#include "Renderable.hxx" +#include "Assets/NativeAsset.hxx" + +namespace SHADE +{ + /*---------------------------------------------------------------------------------*/ + /* Properties */ + /*---------------------------------------------------------------------------------*/ + + /*---------------------------------------------------------------------------------*/ + /* Constructors */ + /*---------------------------------------------------------------------------------*/ + Renderable::Renderable(Entity entity) + : Component(entity) + {} + + /*---------------------------------------------------------------------------------*/ + /* Usage Functions */ + /*---------------------------------------------------------------------------------*/ + +} diff --git a/SHADE_Managed/src/Components/Renderable.hxx b/SHADE_Managed/src/Components/Renderable.hxx new file mode 100644 index 00000000..c21ebdd6 --- /dev/null +++ b/SHADE_Managed/src/Components/Renderable.hxx @@ -0,0 +1,53 @@ +/************************************************************************************//*! +\file Renderable.hxx +\author Tng Kah Wei, kahwei.tng, 390009620 +\par email: kahwei.tng\@digipen.edu +\date Oct 28, 2022 +\brief Contains the definition of the managed Renderable class with the + declaration of functions for working with it. + + Note: This file is written in C++17/CLI. + +Copyright (C) 2022 DigiPen Institute of Technology. +Reproduction or disclosure of this file or its contents without the prior written consent +of DigiPen Institute of Technology is prohibited. +*//*************************************************************************************/ +#pragma once + +// Project Includes +#include "Components/Component.hxx" +#include "Math/Vector3.hxx" +#include "Math/Quaternion.hxx" +// External Dependencies +#include "Graphics/MiddleEnd/Interface/SHRenderable.h" + +namespace SHADE +{ + /// + /// CLR version of the SHADE Engine's SHRenderableComponent. + /// + public ref class Renderable : public Component + { + internal: + /*-----------------------------------------------------------------------------*/ + /* Constructors */ + /*-----------------------------------------------------------------------------*/ + /// + /// Constructs a Renderable Component that represents a native Renderable + /// component tied to the specified Entity. + /// + /// Entity that this Component will be tied to. + Renderable(Entity entity); + + public: + /*-----------------------------------------------------------------------------*/ + /* Properties */ + /*-----------------------------------------------------------------------------*/ + + /*-----------------------------------------------------------------------------*/ + /* Usage Functions */ + /*-----------------------------------------------------------------------------*/ + + }; +} + From 19dc999e4f2b233945f4c61e0fcbd87b68e4d71f Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sat, 29 Oct 2022 02:19:36 +0800 Subject: [PATCH 7/7] Added full implementation of Renderable script interface --- SHADE_Managed/src/Components/Renderable.cxx | 42 ++++++++++++++++++--- SHADE_Managed/src/Components/Renderable.hxx | 30 ++++++++++++--- 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/SHADE_Managed/src/Components/Renderable.cxx b/SHADE_Managed/src/Components/Renderable.cxx index 08a43506..d1bbb4c8 100644 --- a/SHADE_Managed/src/Components/Renderable.cxx +++ b/SHADE_Managed/src/Components/Renderable.cxx @@ -16,13 +16,10 @@ of DigiPen Institute of Technology is prohibited. // Primary Header #include "Renderable.hxx" #include "Assets/NativeAsset.hxx" +#include "Utility/Convert.hxx" namespace SHADE { - /*---------------------------------------------------------------------------------*/ - /* Properties */ - /*---------------------------------------------------------------------------------*/ - /*---------------------------------------------------------------------------------*/ /* Constructors */ /*---------------------------------------------------------------------------------*/ @@ -31,7 +28,40 @@ namespace SHADE {} /*---------------------------------------------------------------------------------*/ - /* Usage Functions */ + /* Properties */ /*---------------------------------------------------------------------------------*/ - + SHADE::Mesh^ Renderable::Mesh::get() + { + return gcnew SHADE::Mesh(GetNativeComponent()->Mesh); + } + void Renderable::Mesh::set(SHADE::Mesh^ value) + { + if (value == nullptr) + { + GetNativeComponent()->Mesh = Handle(); + } + else + { + GetNativeComponent()->Mesh = Handle(Convert::ToNative(value->NativeObjectHandle)); + } + } + SHADE::Material^ Renderable::Material::get() + { + return gcnew SHADE::Material(GetNativeComponent()->GetMaterial()); + } + void Renderable::Material::set(SHADE::Material^ value) + { + if (value == nullptr) + { + GetNativeComponent()->SetMaterial(Handle()); + } + else + { + GetNativeComponent()->SetMaterial(Handle(Convert::ToNative(value->NativeObjectHandle))); + } + } + System::Byte Renderable::LightLayer::get() + { + return GetNativeComponent()->GetLightLayer(); + } } diff --git a/SHADE_Managed/src/Components/Renderable.hxx b/SHADE_Managed/src/Components/Renderable.hxx index c21ebdd6..e8f11ef6 100644 --- a/SHADE_Managed/src/Components/Renderable.hxx +++ b/SHADE_Managed/src/Components/Renderable.hxx @@ -20,6 +20,8 @@ of DigiPen Institute of Technology is prohibited. #include "Math/Quaternion.hxx" // External Dependencies #include "Graphics/MiddleEnd/Interface/SHRenderable.h" +#include "Assets/Mesh.hxx" +#include "Assets/Material.hxx" namespace SHADE { @@ -43,11 +45,29 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Properties */ /*-----------------------------------------------------------------------------*/ - - /*-----------------------------------------------------------------------------*/ - /* Usage Functions */ - /*-----------------------------------------------------------------------------*/ - + /// + /// Mesh used to render this Renderable. + /// + property SHADE::Mesh^ Mesh + { + SHADE::Mesh^ get(); + void set(SHADE::Mesh^ value); + } + /// + /// Material used to render this Renderable. + /// + property SHADE::Material^ Material + { + SHADE::Material^ get(); + void set(SHADE::Material^ value); + } + /// + /// Material used to render this Renderable. + /// + property System::Byte LightLayer + { + System::Byte get(); + } }; }