From 0d556f12b4fb6f27b1b83ddacc7c081ae9c7668e Mon Sep 17 00:00:00 2001 From: Bram Verhulst Date: Mon, 13 Jan 2025 17:12:23 +0100 Subject: [PATCH] Exam Done --- project/CMakeLists.txt | 2 + project/resources/Fire.fx | 3 +- project/resources/PosCol3D.fx | 2 +- project/src/Camera.cpp | 2 +- project/src/Camera.h | 2 +- project/src/Effects/BaseEffect.cpp | 4 +- project/src/Effects/BaseEffect.h | 7 + project/src/Effects/Effect.cpp | 8 + project/src/Effects/Effect.h | 6 +- project/src/Effects/FireEffect.cpp | 52 +- project/src/Effects/FireEffect.h | 13 + project/src/HitTest.cpp | 1 + project/src/InstancedMesh.cpp | 3 +- project/src/InstancedMesh.h | 2 +- project/src/Math/Matrix.cpp | 5 + project/src/Math/Matrix.h | 6 +- project/src/Mesh.cpp | 6 +- project/src/PerlinNoise.hpp | 659 ++++++++++++++++++++++++++ project/src/Renderer.cpp | 52 +- project/src/Scenes/BaseScene.h | 2 +- project/src/Scenes/DioramaScene.cpp | 2 +- project/src/Scenes/DioramaScene.h | 2 +- project/src/Scenes/InstancedScene.cpp | 24 +- project/src/Scenes/InstancedScene.h | 6 +- project/src/Scenes/MainScene.cpp | 4 +- project/src/Scenes/MainScene.h | 2 +- project/src/Scenes/PlanetScene.cpp | 12 +- project/src/Scenes/PlanetScene.h | 6 +- project/src/Texture.cpp | 1 + 29 files changed, 837 insertions(+), 59 deletions(-) create mode 100644 project/src/PerlinNoise.hpp diff --git a/project/CMakeLists.txt b/project/CMakeLists.txt index 1a337c6..5f0fda5 100644 --- a/project/CMakeLists.txt +++ b/project/CMakeLists.txt @@ -20,6 +20,8 @@ set(SOURCES "src/Effects/BaseEffect.cpp" "src/Effects/FireEffect.cpp" + "src/PerlinNoise.hpp" + "src/Scenes/BaseScene.cpp" "src/Scenes/MainScene.cpp" "src/Scenes/DioramaScene.cpp" diff --git a/project/resources/Fire.fx b/project/resources/Fire.fx index 4e8df86..c4cca98 100644 --- a/project/resources/Fire.fx +++ b/project/resources/Fire.fx @@ -1,5 +1,6 @@ float4x4 gWorldViewProj : WorldViewProjection; float4x4 gWorldMatrix : WorldMatrix; +SamplerState gSampleState : SampleState; texture2D gDiffuseMap : DiffuseMap; @@ -79,7 +80,7 @@ VS_OUTPUT VS(VS_INPUT input){ } float4 PS(VS_OUTPUT input) : SV_TARGET{ - return gDiffuseMap.Sample(samPoint, input.TexCoord); + return gDiffuseMap.Sample(gSampleState, input.TexCoord); } diff --git a/project/resources/PosCol3D.fx b/project/resources/PosCol3D.fx index be29655..b038f93 100644 --- a/project/resources/PosCol3D.fx +++ b/project/resources/PosCol3D.fx @@ -104,7 +104,7 @@ float3 Shade(VS_OUTPUT input) // Compute Phong specular lighting float ks = (specularSample.x + specularSample.y + specularSample.z) / 3.f; - float3 specular = Phong(ks, glossSample * gShininess, invLightDirection, invViewDirection, normal); + float3 specular = Phong(ks, glossSample * gShininess, gLightDirection, invViewDirection, normal); // Compute observed area based on the cosine of the light angle float cosAngle = dot(invLightDirection, normal); diff --git a/project/src/Camera.cpp b/project/src/Camera.cpp index 367dd2f..6dd9dfe 100644 --- a/project/src/Camera.cpp +++ b/project/src/Camera.cpp @@ -189,7 +189,7 @@ dae::Matrix dae::Camera::GetViewProjectionMatrix() const { return viewMatrix * ProjectionMatrix; } -const dae::Vector3 &dae::Camera::GetPosition() { +const dae::Vector3 & dae::Camera::GetPosition() const { return origin; } diff --git a/project/src/Camera.h b/project/src/Camera.h index 4e9b344..f642193 100644 --- a/project/src/Camera.h +++ b/project/src/Camera.h @@ -29,7 +29,7 @@ namespace dae { Matrix GetViewProjectionMatrix() const; - const Vector3 &GetPosition(); + const Vector3 & GetPosition() const; void SetPosition(const Vector3& position); const Vector3 GetRotation() const; diff --git a/project/src/Effects/BaseEffect.cpp b/project/src/Effects/BaseEffect.cpp index 4be6691..a3d9f5d 100644 --- a/project/src/Effects/BaseEffect.cpp +++ b/project/src/Effects/BaseEffect.cpp @@ -22,8 +22,6 @@ BaseEffect::BaseEffect(ID3D11Device *devicePtr, const std::wstring &filePath) { } BaseEffect::~BaseEffect() { - m_EffectPtr->Release(); - m_EffectPtr = nullptr; } ID3DX11Effect *BaseEffect::LoadEffect(ID3D11Device *devicePtr, const std::wstring &filePath) { @@ -31,6 +29,7 @@ ID3DX11Effect *BaseEffect::LoadEffect(ID3D11Device *devicePtr, const std::wstrin ID3D10Blob *errorBlobPtr{nullptr}; ID3DX11Effect *effectPtr; + DWORD shaderFlags = 0; #if defined( DEBUG ) || defined( _DEBUG ) shaderFlags |= D3DCOMPILE_DEBUG; @@ -55,6 +54,7 @@ ID3DX11Effect *BaseEffect::LoadEffect(ID3D11Device *devicePtr, const std::wstrin ss << errorsPtr[i]; OutputDebugStringW(ss.str().c_str()); + errorBlobPtr->Release(); errorBlobPtr = nullptr; return nullptr; diff --git a/project/src/Effects/BaseEffect.h b/project/src/Effects/BaseEffect.h index 0dd9d9d..f29761b 100644 --- a/project/src/Effects/BaseEffect.h +++ b/project/src/Effects/BaseEffect.h @@ -11,6 +11,13 @@ #include "../Material.h" #include "../Math/Matrix.h" +enum class TechniqueType { + Point = 0, + Linear = 1, + Anisotropic = 2 +}; + + class BaseEffect { public: BaseEffect(ID3D11Device* devicePtr, const std::wstring& filePath); diff --git a/project/src/Effects/Effect.cpp b/project/src/Effects/Effect.cpp index a041fae..027bc90 100644 --- a/project/src/Effects/Effect.cpp +++ b/project/src/Effects/Effect.cpp @@ -11,6 +11,7 @@ Effect::Effect(ID3D11Device *devicePtr, const std::wstring &filePath) if(!m_LightPosVariablePtr->IsValid()) std::wcout << L"gLightDirection Vector is not valid" << std::endl; + m_SamplerVariablePtr = m_EffectPtr->GetVariableByName("gSampleState")->AsSampler(); if(!m_SamplerVariablePtr->IsValid()) std::wcout << L"gSampleState Sampler is not valid" << std::endl; @@ -105,6 +106,13 @@ Effect::~Effect() { for(auto sampler : m_SamplerStates){ sampler->Release(); } + + for(auto rasterizer : m_RasterizerStates){ + rasterizer->Release(); + } + + m_EffectPtr->Release(); + m_EffectPtr = nullptr; } void Effect::SetWorldMatrix(const dae::Matrix &world) const { diff --git a/project/src/Effects/Effect.h b/project/src/Effects/Effect.h index 04b5fc3..bae4077 100644 --- a/project/src/Effects/Effect.h +++ b/project/src/Effects/Effect.h @@ -3,11 +3,7 @@ #include "BaseEffect.h" #include -enum class TechniqueType { - Point = 0, - Linear = 1, - Anisotropic = 2 -}; + enum class CullMode { None = 0, diff --git a/project/src/Effects/FireEffect.cpp b/project/src/Effects/FireEffect.cpp index 90ebadb..af6425b 100644 --- a/project/src/Effects/FireEffect.cpp +++ b/project/src/Effects/FireEffect.cpp @@ -10,14 +10,64 @@ FireEffect::FireEffect(ID3D11Device *devicePtr, const std::wstring &filePath) : m_DiffuseMapVariablePtr = m_EffectPtr->GetVariableByName("gDiffuseMap")->AsShaderResource(); if (!m_DiffuseMapVariablePtr->IsValid()) std::wcout << L"gDiffuseMap ShaderResource is not valid" << std::endl; + + m_SamplerVariablePtr = m_EffectPtr->GetVariableByName("gSampleState")->AsSampler(); + if(!m_SamplerVariablePtr->IsValid()) + std::wcout << L"gSampleState Sampler is not valid" << std::endl; + + InitSamplers(devicePtr); } + + FireEffect::~FireEffect() { m_DiffuseMapVariablePtr->Release(); m_DiffuseMapVariablePtr = nullptr; + + m_SamplerVariablePtr->Release(); + m_SamplerVariablePtr = nullptr; + + m_EffectPtr->Release(); + m_EffectPtr = nullptr; + } void FireEffect::SetMaterial(Material *material) { if(material->diffuseTexturePtr) m_DiffuseMapVariablePtr->SetResource(material->diffuseTexturePtr->GetSrv()); -} \ No newline at end of file +} + +void FireEffect::NextSamplingState() { + switch(m_TechniqueType){ + case TechniqueType::Point: + m_TechniqueType = TechniqueType::Linear; + break; + case TechniqueType::Linear: + m_TechniqueType = TechniqueType::Anisotropic; + break; + case TechniqueType::Anisotropic: + m_TechniqueType = TechniqueType::Point; + break; + } + m_SamplerVariablePtr->SetSampler(0,m_SamplerStates[static_cast(m_TechniqueType)]); +} + +void FireEffect::InitSamplers(ID3D11Device *devicePtr) { + D3D11_SAMPLER_DESC samplerDesc{}; + samplerDesc.Filter = D3D11_FILTER_ANISOTROPIC; + samplerDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; + samplerDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; + samplerDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; + samplerDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; + + devicePtr->CreateSamplerState(&samplerDesc, &m_SamplerStates[static_cast(TechniqueType::Anisotropic)]); + + samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; + devicePtr->CreateSamplerState(&samplerDesc, &m_SamplerStates[static_cast(TechniqueType::Linear)]); + + samplerDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; + devicePtr->CreateSamplerState(&samplerDesc, &m_SamplerStates[static_cast(TechniqueType::Point)]); + + m_SamplerVariablePtr->SetSampler(0,m_SamplerStates[static_cast(m_TechniqueType)]); +} + diff --git a/project/src/Effects/FireEffect.h b/project/src/Effects/FireEffect.h index 4bab1c9..078f205 100644 --- a/project/src/Effects/FireEffect.h +++ b/project/src/Effects/FireEffect.h @@ -6,6 +6,7 @@ #define GP1_DIRECTX_FIREEFFECT_H +#include #include "BaseEffect.h" class FireEffect: public BaseEffect { @@ -16,8 +17,20 @@ public: void SetMaterial(Material *material) override; + void NextSamplingState() override; + private: + void InitSamplers(ID3D11Device* devicePtr); + + ID3DX11EffectShaderResourceVariable *m_DiffuseMapVariablePtr{}; + + ID3DX11EffectSamplerVariable *m_SamplerVariablePtr{}; + + std::array m_SamplerStates{}; + + TechniqueType m_TechniqueType{TechniqueType::Linear}; + }; diff --git a/project/src/HitTest.cpp b/project/src/HitTest.cpp index 6bdb950..526e8d1 100644 --- a/project/src/HitTest.cpp +++ b/project/src/HitTest.cpp @@ -43,6 +43,7 @@ std::optional TriangleHitTest(const Vector3& fragPos, const VertexOut& v v1.uv * depth * normWeights.y / v1.position.w + v2.uv * depth * normWeights.z / v2.position.w; + const float interpolatedDepth = 1 / (normWeights.x / v0.position.w + normWeights.y / v1.position.w + normWeights.z / v2.position.w); auto interpolate = diff --git a/project/src/InstancedMesh.cpp b/project/src/InstancedMesh.cpp index 5ade7cc..3d92b63 100644 --- a/project/src/InstancedMesh.cpp +++ b/project/src/InstancedMesh.cpp @@ -199,12 +199,11 @@ void InstancedMesh::UpdateInstanceData(ID3D11DeviceContext *deviceContextPtr, co HRESULT result = deviceContextPtr->Map(m_InstanceBufferPtr, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); assert(result == S_OK && "Mapping instance buffer failed"); - assert(mappedResource.pData != nullptr && "Mapped resource data is null"); if (!instanceData.empty()) { // Ensure that the data fits in the buffer - std::memcpy(mappedResource.pData, instanceData.data(), sizeof(InstancedData) * instanceData.size()); + std::memcpy(mappedResource.pData, instanceData.data(), sizeof(InstancedData) * instanceData.size()); // NOLINT(*-undefined-memory-manipulation) } deviceContextPtr->Unmap(m_InstanceBufferPtr, 0); diff --git a/project/src/InstancedMesh.h b/project/src/InstancedMesh.h index 5e6dc01..d110bbc 100644 --- a/project/src/InstancedMesh.h +++ b/project/src/InstancedMesh.h @@ -8,7 +8,6 @@ #include #include - struct alignas(16) InstancedData { Matrix worldMatrix; Vector4 color; @@ -60,6 +59,7 @@ private: std::shared_ptr m_Material{}; PrimitiveTopology m_PrimitiveTopology{PrimitiveTopology::TriangleList}; + }; diff --git a/project/src/Math/Matrix.cpp b/project/src/Math/Matrix.cpp index eda83bf..49a961f 100644 --- a/project/src/Math/Matrix.cpp +++ b/project/src/Math/Matrix.cpp @@ -297,5 +297,10 @@ namespace dae { return *this; } + + void Matrix::SetTranslation(Vector3 vector3) { + data[3] = {vector3, 1}; + } + #pragma endregion } \ No newline at end of file diff --git a/project/src/Math/Matrix.h b/project/src/Math/Matrix.h index e359db7..39768cc 100644 --- a/project/src/Math/Matrix.h +++ b/project/src/Math/Matrix.h @@ -56,7 +56,9 @@ namespace dae { Matrix operator*(const Matrix& m) const; const Matrix& operator*=(const Matrix& m); - private: + void SetTranslation(Vector3 vector3); + + private: //Row-Major Matrix Vector4 data[4] @@ -71,5 +73,5 @@ namespace dae { // v1x v1y v1z v1w // v2x v2y v2z v2w // v3x v3y v3z v3w - }; + }; } \ No newline at end of file diff --git a/project/src/Mesh.cpp b/project/src/Mesh.cpp index 89ef45a..9b7a679 100644 --- a/project/src/Mesh.cpp +++ b/project/src/Mesh.cpp @@ -98,21 +98,17 @@ Mesh::~Mesh() { void Mesh::Render(ID3D11DeviceContext *deviceContextPtr, const Matrix &worldViewProj) const { m_EffectPtr->SetWorldViewProjMatrix(worldViewProj); m_EffectPtr->SetWorldMatrix(m_WorldMatrix); - //1. Set primitive topology + deviceContextPtr->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - //2. Set input layout deviceContextPtr->IASetInputLayout(m_InputLayoutPtr); - //3. Set vertex buffer constexpr UINT stride = sizeof(VertexIn); constexpr UINT offset = 0; deviceContextPtr->IASetVertexBuffers(0, 1, &m_VertexBufferPtr, &stride, &offset); - //4. Set index buffer deviceContextPtr->IASetIndexBuffer(m_IndexBufferPtr, DXGI_FORMAT_R32_UINT, 0); - //5. Draw D3DX11_TECHNIQUE_DESC techniqueDesc{}; m_EffectPtr->GetTechniquePtr()->GetDesc(&techniqueDesc); for (UINT p{}; p < techniqueDesc.Passes; p++) { diff --git a/project/src/PerlinNoise.hpp b/project/src/PerlinNoise.hpp new file mode 100644 index 0000000..9dc3106 --- /dev/null +++ b/project/src/PerlinNoise.hpp @@ -0,0 +1,659 @@ +//---------------------------------------------------------------------------------------- +// +// siv::PerlinNoise +// Perlin noise library for modern C++ +// +// Copyright (C) 2013-2021 Ryo Suzuki +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files(the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions : +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +// +//---------------------------------------------------------------------------------------- + +# pragma once +# include +# include +# include +# include +# include +# include +# include + +# if __has_include() && defined(__cpp_concepts) +# include +# endif + + +// Library major version +# define SIVPERLIN_VERSION_MAJOR 3 + +// Library minor version +# define SIVPERLIN_VERSION_MINOR 0 + +// Library revision version +# define SIVPERLIN_VERSION_REVISION 0 + +// Library version +# define SIVPERLIN_VERSION ((SIVPERLIN_VERSION_MAJOR * 100 * 100) + (SIVPERLIN_VERSION_MINOR * 100) + (SIVPERLIN_VERSION_REVISION)) + + +// [[nodiscard]] for constructors +# if (201907L <= __has_cpp_attribute(nodiscard)) +# define SIVPERLIN_NODISCARD_CXX20 [[nodiscard]] +# else +# define SIVPERLIN_NODISCARD_CXX20 +# endif + + +// std::uniform_random_bit_generator concept +# if __cpp_lib_concepts +# define SIVPERLIN_CONCEPT_URBG template +# define SIVPERLIN_CONCEPT_URBG_ template +# else +# define SIVPERLIN_CONCEPT_URBG template , std::is_unsigned>>>* = nullptr> +# define SIVPERLIN_CONCEPT_URBG_ template , std::is_unsigned>>>*> +# endif + + +// arbitrary value for increasing entropy +# ifndef SIVPERLIN_DEFAULT_Y +# define SIVPERLIN_DEFAULT_Y (0.12345) +# endif + +// arbitrary value for increasing entropy +# ifndef SIVPERLIN_DEFAULT_Z +# define SIVPERLIN_DEFAULT_Z (0.34567) +# endif + + +namespace siv +{ + template + class BasicPerlinNoise + { + public: + + static_assert(std::is_floating_point_v); + + /////////////////////////////////////// + // + // Typedefs + // + + using state_type = std::array; + + using value_type = Float; + + using default_random_engine = std::mt19937; + + using seed_type = typename default_random_engine::result_type; + + /////////////////////////////////////// + // + // Constructors + // + + SIVPERLIN_NODISCARD_CXX20 + constexpr BasicPerlinNoise() noexcept; + + SIVPERLIN_NODISCARD_CXX20 + explicit BasicPerlinNoise(seed_type seed); + + SIVPERLIN_CONCEPT_URBG + SIVPERLIN_NODISCARD_CXX20 + explicit BasicPerlinNoise(URBG&& urbg); + + /////////////////////////////////////// + // + // Reseed + // + + void reseed(seed_type seed); + + SIVPERLIN_CONCEPT_URBG + void reseed(URBG&& urbg); + + /////////////////////////////////////// + // + // Serialization + // + + [[nodiscard]] + constexpr const state_type& serialize() const noexcept; + + constexpr void deserialize(const state_type& state) noexcept; + + /////////////////////////////////////// + // + // Noise (The result is in the range [-1, 1]) + // + + [[nodiscard]] + value_type noise1D(value_type x) const noexcept; + + [[nodiscard]] + value_type noise2D(value_type x, value_type y) const noexcept; + + [[nodiscard]] + value_type noise3D(value_type x, value_type y, value_type z) const noexcept; + + /////////////////////////////////////// + // + // Noise (The result is remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type noise1D_01(value_type x) const noexcept; + + [[nodiscard]] + value_type noise2D_01(value_type x, value_type y) const noexcept; + + [[nodiscard]] + value_type noise3D_01(value_type x, value_type y, value_type z) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result can be out of the range [-1, 1]) + // + + [[nodiscard]] + value_type octave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is clamped to the range [-1, 1]) + // + + [[nodiscard]] + value_type octave1D_11(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D_11(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D_11(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is clamped and remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type octave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type octave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is normalized to the range [-1, 1]) + // + + [[nodiscard]] + value_type normalizedOctave1D(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave2D(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave3D(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + /////////////////////////////////////// + // + // Octave noise (The result is normalized and remapped to the range [0, 1]) + // + + [[nodiscard]] + value_type normalizedOctave1D_01(value_type x, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave2D_01(value_type x, value_type y, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + [[nodiscard]] + value_type normalizedOctave3D_01(value_type x, value_type y, value_type z, std::int32_t octaves, value_type persistence = value_type(0.5)) const noexcept; + + private: + + state_type m_permutation; + }; + + using PerlinNoise = BasicPerlinNoise; + + namespace perlin_detail + { + //////////////////////////////////////////////// + // + // These functions are provided for consistency. + // You may get different results from std::shuffle() with different standard library implementations. + // + SIVPERLIN_CONCEPT_URBG + [[nodiscard]] + inline std::uint64_t Random(const std::uint64_t max, URBG&& urbg) + { + return (urbg() % (max + 1)); + } + + template + inline void Shuffle(RandomIt first, RandomIt last, URBG&& urbg) + { + if (first == last) + { + return; + } + + using difference_type = typename std::iterator_traits::difference_type; + + for (RandomIt it = first + 1; it < last; ++it) + { + const std::uint64_t n = static_cast(it - first); + std::iter_swap(it, first + static_cast(Random(n, std::forward(urbg)))); + } + } + // + //////////////////////////////////////////////// + + template + [[nodiscard]] + inline constexpr Float Fade(const Float t) noexcept + { + return t * t * t * (t * (t * 6 - 15) + 10); + } + + template + [[nodiscard]] + inline constexpr Float Lerp(const Float a, const Float b, const Float t) noexcept + { + return (a + (b - a) * t); + } + + template + [[nodiscard]] + inline constexpr Float Grad(const std::uint8_t hash, const Float x, const Float y, const Float z) noexcept + { + const std::uint8_t h = hash & 15; + const Float u = h < 8 ? x : y; + const Float v = h < 4 ? y : h == 12 || h == 14 ? x : z; + return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); + } + + template + [[nodiscard]] + inline constexpr Float Remap_01(const Float x) noexcept + { + return (x * Float(0.5) + Float(0.5)); + } + + template + [[nodiscard]] + inline constexpr Float Clamp_11(const Float x) noexcept + { + return std::clamp(x, Float(-1.0), Float(1.0)); + } + + template + [[nodiscard]] + inline constexpr Float RemapClamp_01(const Float x) noexcept + { + if (x <= Float(-1.0)) + { + return Float(0.0); + } + else if (Float(1.0) <= x) + { + return Float(1.0); + } + + return (x * Float(0.5) + Float(0.5)); + } + + template + [[nodiscard]] + inline auto Octave1D(const Noise& noise, Float x, const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += (noise.noise1D(x) * amplitude); + x *= 2; + amplitude *= persistence; + } + + return result; + } + + template + [[nodiscard]] + inline auto Octave2D(const Noise& noise, Float x, Float y, const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += (noise.noise2D(x, y) * amplitude); + x *= 2; + y *= 2; + amplitude *= persistence; + } + + return result; + } + + template + [[nodiscard]] + inline auto Octave3D(const Noise& noise, Float x, Float y, Float z, const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += (noise.noise3D(x, y, z) * amplitude); + x *= 2; + y *= 2; + z *= 2; + amplitude *= persistence; + } + + return result; + } + + template + [[nodiscard]] + inline constexpr Float MaxAmplitude(const std::int32_t octaves, const Float persistence) noexcept + { + using value_type = Float; + value_type result = 0; + value_type amplitude = 1; + + for (std::int32_t i = 0; i < octaves; ++i) + { + result += amplitude; + amplitude *= persistence; + } + + return result; + } + } + + /////////////////////////////////////// + + template + inline constexpr BasicPerlinNoise::BasicPerlinNoise() noexcept + : m_permutation{ 151,160,137,91,90,15, + 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, + 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, + 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, + 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, + 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, + 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, + 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, + 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, + 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, + 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, + 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, + 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 } {} + + template + inline BasicPerlinNoise::BasicPerlinNoise(const seed_type seed) + { + reseed(seed); + } + + template + SIVPERLIN_CONCEPT_URBG_ + inline BasicPerlinNoise::BasicPerlinNoise(URBG&& urbg) + { + reseed(std::forward(urbg)); + } + + /////////////////////////////////////// + + template + inline void BasicPerlinNoise::reseed(const seed_type seed) + { + reseed(default_random_engine{ seed }); + } + + template + SIVPERLIN_CONCEPT_URBG_ + inline void BasicPerlinNoise::reseed(URBG&& urbg) + { + std::iota(m_permutation.begin(), m_permutation.end(), uint8_t{ 0 }); + + perlin_detail::Shuffle(m_permutation.begin(), m_permutation.end(), std::forward(urbg)); + } + + /////////////////////////////////////// + + template + inline constexpr const typename BasicPerlinNoise::state_type& BasicPerlinNoise::serialize() const noexcept + { + return m_permutation; + } + + template + inline constexpr void BasicPerlinNoise::deserialize(const state_type& state) noexcept + { + m_permutation = state; + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D(const value_type x) const noexcept + { + return noise3D(x, + static_cast(SIVPERLIN_DEFAULT_Y), + static_cast(SIVPERLIN_DEFAULT_Z)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D(const value_type x, const value_type y) const noexcept + { + return noise3D(x, + y, + static_cast(SIVPERLIN_DEFAULT_Z)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D(const value_type x, const value_type y, const value_type z) const noexcept + { + const value_type _x = std::floor(x); + const value_type _y = std::floor(y); + const value_type _z = std::floor(z); + + const std::int32_t ix = static_cast(_x) & 255; + const std::int32_t iy = static_cast(_y) & 255; + const std::int32_t iz = static_cast(_z) & 255; + + const value_type fx = (x - _x); + const value_type fy = (y - _y); + const value_type fz = (z - _z); + + const value_type u = perlin_detail::Fade(fx); + const value_type v = perlin_detail::Fade(fy); + const value_type w = perlin_detail::Fade(fz); + + const std::uint8_t A = (m_permutation[ix & 255] + iy) & 255; + const std::uint8_t B = (m_permutation[(ix + 1) & 255] + iy) & 255; + + const std::uint8_t AA = (m_permutation[A] + iz) & 255; + const std::uint8_t AB = (m_permutation[(A + 1) & 255] + iz) & 255; + + const std::uint8_t BA = (m_permutation[B] + iz) & 255; + const std::uint8_t BB = (m_permutation[(B + 1) & 255] + iz) & 255; + + const value_type p0 = perlin_detail::Grad(m_permutation[AA], fx, fy, fz); + const value_type p1 = perlin_detail::Grad(m_permutation[BA], fx - 1, fy, fz); + const value_type p2 = perlin_detail::Grad(m_permutation[AB], fx, fy - 1, fz); + const value_type p3 = perlin_detail::Grad(m_permutation[BB], fx - 1, fy - 1, fz); + const value_type p4 = perlin_detail::Grad(m_permutation[(AA + 1) & 255], fx, fy, fz - 1); + const value_type p5 = perlin_detail::Grad(m_permutation[(BA + 1) & 255], fx - 1, fy, fz - 1); + const value_type p6 = perlin_detail::Grad(m_permutation[(AB + 1) & 255], fx, fy - 1, fz - 1); + const value_type p7 = perlin_detail::Grad(m_permutation[(BB + 1) & 255], fx - 1, fy - 1, fz - 1); + + const value_type q0 = perlin_detail::Lerp(p0, p1, u); + const value_type q1 = perlin_detail::Lerp(p2, p3, u); + const value_type q2 = perlin_detail::Lerp(p4, p5, u); + const value_type q3 = perlin_detail::Lerp(p6, p7, u); + + const value_type r0 = perlin_detail::Lerp(q0, q1, v); + const value_type r1 = perlin_detail::Lerp(q2, q3, v); + + return perlin_detail::Lerp(r0, r1, w); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise1D_01(const value_type x) const noexcept + { + return perlin_detail::Remap_01(noise1D(x)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise2D_01(const value_type x, const value_type y) const noexcept + { + return perlin_detail::Remap_01(noise2D(x, y)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::noise3D_01(const value_type x, const value_type y, const value_type z) const noexcept + { + return perlin_detail::Remap_01(noise3D(x, y, z)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave1D(*this, x, octaves, persistence); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave2D(*this, x, y, octaves, persistence); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Octave3D(*this, x, y, z, octaves, persistence); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_11(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_11(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_11(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Clamp_11(octave3D(x, y, z, octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::octave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::RemapClamp_01(octave3D(x, y, z, octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave1D(x, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave2D(x, y, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return (octave3D(x, y, z, octaves, persistence) / perlin_detail::MaxAmplitude(octaves, persistence)); + } + + /////////////////////////////////////// + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave1D_01(const value_type x, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave1D(x, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave2D_01(const value_type x, const value_type y, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave2D(x, y, octaves, persistence)); + } + + template + inline typename BasicPerlinNoise::value_type BasicPerlinNoise::normalizedOctave3D_01(const value_type x, const value_type y, const value_type z, const std::int32_t octaves, const value_type persistence) const noexcept + { + return perlin_detail::Remap_01(normalizedOctave3D(x, y, z, octaves, persistence)); + } +} + +# undef SIVPERLIN_NODISCARD_CXX20 +# undef SIVPERLIN_CONCEPT_URBG +# undef SIVPERLIN_CONCEPT_URBG_ diff --git a/project/src/Renderer.cpp b/project/src/Renderer.cpp index bb701ea..ab8e3b8 100644 --- a/project/src/Renderer.cpp +++ b/project/src/Renderer.cpp @@ -37,7 +37,7 @@ namespace dae { InitializeSDLRasterizer(); m_pScene = new MainScene(); - m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr); + m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr, nullptr); if (!m_pScene->GetMeshes().empty()) { m_pFireMesh = m_pScene->GetMeshes().back(); @@ -409,8 +409,8 @@ namespace dae { Vector4 vertPos{WorldViewProjectionMatrix.TransformPoint({vert.position, 1})}; - const Vector3 normal{mesh->GetWorldMatrix().TransformVector(vert.normal)}; - const Vector3 tangent{mesh->GetWorldMatrix().TransformVector(vert.tangent)}; + const Vector3 normal{mesh->GetWorldMatrix().TransformVector(vert.normal).Normalized()}; + const Vector3 tangent{mesh->GetWorldMatrix().TransformVector(vert.tangent).Normalized()}; vertPos.x /= vertPos.w; @@ -433,8 +433,9 @@ namespace dae { vertex_out.normal = normal; vertex_out.tangent = tangent; vertex_out.mesh = mesh; -// vertex_out.viewDir = WorldViewProjectionMatrix.TransformVector(vert.viewDir); vertex_out.valid = isValid; + vertex_out.viewDir = (mesh->GetWorldMatrix().TransformPoint(vertex_out.position) + - m_Camera.GetPosition().ToPoint4()).Normalized().GetXYZ(); vertices_out.push_back(vertex_out); } @@ -465,27 +466,31 @@ namespace dae { constexpr ColorRGB ambient{.03f, .03f, .03f}; if (m_useNormals) { + + const Vector3 biNormal{Vector3::Cross(normal, sample.tangent)}; + + const Matrix tangentToWorld{ + sample.tangent, + biNormal, + normal, + {0.f, 0.f, 0.f} + }; + const ColorRGB normalSample{sample.mesh->GetMaterial()->normalTexturePtr->Sample(sample.uv)}; - const Vector4 normalMapSample{ + + Vector4 normalMapSample{ 2.f * normalSample.r - 1.f, 2.f * normalSample.g - 1.f, 2.f * normalSample.b - 1.f, 0.f }; - const Vector3 biNormal{Vector3::Cross(normal, sample.tangent)}; - const Matrix tangentToWorld{ - Vector4{sample.tangent, 0.f}, - Vector4{biNormal, 0.f}, - Vector4{normal, 0.f}, - Vector4{0.f, 0.f, 0.f, 1.f} - }; normal = tangentToWorld.TransformVector(normalMapSample).Normalized(); } const ColorRGB diffuseSample{currentMaterial->diffuseTexturePtr->Sample(sample.uv)}; - double invPi = 1.0 / PI; - const ColorRGB lambert{diffuseSample * lightIntensity * invPi}; + double invPi = 1.0f / PI; + const ColorRGB lambert{diffuseSample * lightIntensity / PI}; //TODO: ask why deviding by PI causses Segmentation fault // const ColorRGB lambert{ diffuseSample * lightIntensity / PI }; @@ -494,12 +499,16 @@ namespace dae { float specularReflectance{1.f}; float shininess{25.f}; - specularReflectance *= currentMaterial->glossTexturePtr->Sample(sample.uv).r; - shininess *= currentMaterial->specularTexturePtr->Sample(sample.uv).r; + specularReflectance *= currentMaterial->specularTexturePtr->Sample(sample.uv).r; + shininess *= currentMaterial->glossTexturePtr->Sample(sample.uv).r; const float cosAngle = Vector3::Dot(normal, -lightDirection); - const ColorRGB specular = specularReflectance * powf(cosAngle, shininess) * colors::White; + const Vector3 reflect = Vector3::Reflect(lightDirection, normal); + float cosSpecular = Vector3::Dot(reflect, sample.viewDirection); + cosSpecular = std::max(cosSpecular, 0.f); + + const ColorRGB specular = specularReflectance * powf(cosSpecular, shininess) * colors::White; if (cosAngle < 0) { return ambient; @@ -507,6 +516,7 @@ namespace dae { switch (m_ShadeMode) { case ShadeMode::ObservedArea: + color = ColorRGB{cosAngle, cosAngle, cosAngle}; break; case ShadeMode::Diffuse: color = lambert; @@ -516,10 +526,10 @@ namespace dae { break; case ShadeMode::Combined: color = lambert + specular + ambient; + color *= ColorRGB{cosAngle, cosAngle, cosAngle}; break; } - color *= ColorRGB{cosAngle, cosAngle, cosAngle}; return color; } @@ -554,10 +564,6 @@ namespace dae { std::string mode = m_isHitbox ? "ON" : "OFF"; std::cout << MAGENTA << "[SOFTWARE]" << BLUE << " Hitbox " << mode << RESET << std::endl; - - std::cout << "Camera pos: " << m_Camera.GetPosition().x << " " << m_Camera.GetPosition().y << " " << m_Camera.GetPosition().z << std::endl; - std::cout << "Camera rot: " << m_Camera.GetRotation().x << " " << m_Camera.GetRotation().y << " " << m_Camera.GetRotation().z << std::endl; - } void Renderer::CycleRenderingMode() { @@ -694,7 +700,7 @@ namespace dae { m_Camera.SetPosition(m_SceneCameraPositions[m_CurrentScene].first); m_Camera.SetRotation(m_SceneCameraPositions[m_CurrentScene].second); - m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr); + m_pScene->Initialize(m_DevicePtr, m_DeviceContextPtr, &m_Camera); if (m_CurrentScene == SceneNames::Main) { diff --git a/project/src/Scenes/BaseScene.h b/project/src/Scenes/BaseScene.h index 9c7bb1b..8cfa842 100644 --- a/project/src/Scenes/BaseScene.h +++ b/project/src/Scenes/BaseScene.h @@ -17,7 +17,7 @@ public: virtual ~BaseScene() = default; - virtual void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) = 0; + virtual void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) = 0; virtual void Update() = 0; diff --git a/project/src/Scenes/DioramaScene.cpp b/project/src/Scenes/DioramaScene.cpp index b0cde24..63ba776 100644 --- a/project/src/Scenes/DioramaScene.cpp +++ b/project/src/Scenes/DioramaScene.cpp @@ -10,7 +10,7 @@ #include -void DioramaScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) { +void DioramaScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) { std::vector> materialMeshes; Utils::LoadObjWithMaterials("resources/scene.obj", materialMeshes, true, DevicePtr); diff --git a/project/src/Scenes/DioramaScene.h b/project/src/Scenes/DioramaScene.h index d47a52e..e288162 100644 --- a/project/src/Scenes/DioramaScene.h +++ b/project/src/Scenes/DioramaScene.h @@ -7,7 +7,7 @@ class DioramaScene : public BaseScene { public: void Cleanup() override; - void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override; + void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override; std::vector &GetMeshes() override; diff --git a/project/src/Scenes/InstancedScene.cpp b/project/src/Scenes/InstancedScene.cpp index 0ec166f..7688621 100644 --- a/project/src/Scenes/InstancedScene.cpp +++ b/project/src/Scenes/InstancedScene.cpp @@ -6,7 +6,7 @@ #include "../Utils.h" #include "../Effects/Effect.h" -void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) { +void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) { m_DeviceContextPtr = DeviceContextPtr; std::vector vertices{}; @@ -20,15 +20,30 @@ void InstancedScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *De std::shared_ptr cubeMaterial = std::make_shared(); cubeMaterial->diffuseTexturePtr = Texture::LoadFromFile("resources/grass_block.png", DevicePtr); +// std::vector instanceData; +// for (int x = 0; x < 100; ++x) { +// for (int y = 0; y < 100; ++y) { +// InstancedData data; +// float scale = 2; +// //Generate sine wave based on x and y +// float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f); +// //Add random but predictable randomness +// YOffset += (x * 0.1f + y * 0.1f) * 0.1f; +// data.worldMatrix = Matrix::CreateTranslation(x * scale, YOffset, y * scale); +// data.worldMatrix *= Matrix::CreateScale(0.5f, 0.5f, 0.5f); +// data.color = Vector4(float(x) / 255.f, float(y) / 255.f, 1.f, 1.0f); +// instanceData.push_back(data); +// } +// } + std::vector instanceData; for (int x = 0; x < 100; ++x) { for (int y = 0; y < 100; ++y) { InstancedData data; float scale = 2; - //Generate sine wave based on x and y + + //use the perlin noise to generate the YOffset float YOffset = sin(x * 0.5f + SDL_GetTicks() * 0.001f) + cos(y * 0.5f + SDL_GetTicks() * 0.001f); - //Add random but predictable randomness - YOffset += (x * 0.1f + y * 0.1f) * 0.1f; data.worldMatrix = Matrix::CreateTranslation(x * scale, YOffset, y * scale); data.worldMatrix *= Matrix::CreateScale(0.5f, 0.5f, 0.5f); data.color = Vector4(float(x) / 255.f, float(y) / 255.f, 1.f, 1.0f); @@ -92,4 +107,5 @@ void InstancedScene::Update() { } m_instancedMeshes[0]->UpdateInstanceData(m_DeviceContextPtr, instanceData); } + } diff --git a/project/src/Scenes/InstancedScene.h b/project/src/Scenes/InstancedScene.h index 8e5da52..9df5156 100644 --- a/project/src/Scenes/InstancedScene.h +++ b/project/src/Scenes/InstancedScene.h @@ -4,10 +4,11 @@ #include "BaseScene.h" #include "../Camera.h" #include "../InstancedMesh.h" +#include "../PerlinNoise.hpp" class InstancedScene : public BaseScene { public: - void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override; + void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override; void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override; @@ -27,6 +28,9 @@ private: //Kind of hack since InstancedMesh doesnt extend mesh std::vector m_instancedMeshes; std::vector> m_materials; + + const siv::PerlinNoise::seed_type seed = 123456u; + const siv::PerlinNoise m_perlinNoise{seed}; }; #endif //GP1_DIRECTX_INSTANCEDSCENE_H diff --git a/project/src/Scenes/MainScene.cpp b/project/src/Scenes/MainScene.cpp index 15945b3..8aaa3c0 100644 --- a/project/src/Scenes/MainScene.cpp +++ b/project/src/Scenes/MainScene.cpp @@ -11,12 +11,12 @@ #include -void MainScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) { +void MainScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) { std::vector vertices{}; std::vector indices{}; - if (!Utils::ParseOBJNew("resources/vehicle.obj", vertices, indices, false)) { + if (!Utils::ParseOBJNew("resources/vehicle.obj", vertices, indices, true)) { assert(true && "Model failed to load"); } diff --git a/project/src/Scenes/MainScene.h b/project/src/Scenes/MainScene.h index 950a250..e90b0d6 100644 --- a/project/src/Scenes/MainScene.h +++ b/project/src/Scenes/MainScene.h @@ -12,7 +12,7 @@ class MainScene : public BaseScene { public: - void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override; + void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override; void Cleanup() override; diff --git a/project/src/Scenes/PlanetScene.cpp b/project/src/Scenes/PlanetScene.cpp index 7378574..8790986 100644 --- a/project/src/Scenes/PlanetScene.cpp +++ b/project/src/Scenes/PlanetScene.cpp @@ -3,9 +3,9 @@ #include "../Utils.h" #include "../Effects/Effect.h" -void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) { +void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) { m_DeviceContextPtr = DeviceContextPtr; - + m_Camera = camera; std::vector vertices{}; std::vector indices{}; @@ -90,6 +90,7 @@ void PlanetScene::Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *Devic auto* skyboxEffect = new Effect(DevicePtr, L"resources/SimpleDiffuse.fx"); m_meshes.push_back(new Mesh(DevicePtr, vertices, indices, skyboxMaterial, skyboxEffect)); + m_skyboxMesh = m_meshes.back(); Matrix worldMatrix = m_meshes.back()->GetWorldMatrix(); worldMatrix *= Matrix::CreateScale(0.1f, 0.1f, 0.1f); @@ -133,4 +134,11 @@ void PlanetScene::Update() { } m_instancedMeshes[0]->UpdateInstanceData(m_DeviceContextPtr, m_InstancedData); + + //set the skybox to the camera + Matrix skyboxMatrix = m_skyboxMesh->GetWorldMatrix(); + + Vector3 cameraPos = m_Camera->GetPosition(); + skyboxMatrix.SetTranslation(cameraPos); + m_skyboxMesh->SetWorldMatrix(skyboxMatrix); } diff --git a/project/src/Scenes/PlanetScene.h b/project/src/Scenes/PlanetScene.h index 500d5db..60581d4 100644 --- a/project/src/Scenes/PlanetScene.h +++ b/project/src/Scenes/PlanetScene.h @@ -8,7 +8,7 @@ class PlanetScene : public BaseScene { public: - void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr) override; + void Initialize(ID3D11Device *DevicePtr, ID3D11DeviceContext *DeviceContextPtr, Camera *camera) override; void Render(ID3D11DeviceContext* devicePtr, ID3D11RenderTargetView *renderTargetViewPtr, ID3D11DepthStencilView *depthStencilViewPtr, const Camera& camera) override; @@ -29,6 +29,10 @@ private: std::vector m_instancedMeshes; std::vector m_InstancedData; std::vector> m_materials; + + Mesh* m_skyboxMesh; + + Camera* m_Camera; }; diff --git a/project/src/Texture.cpp b/project/src/Texture.cpp index faebadd..2a3a501 100644 --- a/project/src/Texture.cpp +++ b/project/src/Texture.cpp @@ -52,6 +52,7 @@ Texture::Texture(SDL_Surface *surfacePtr, ID3D11Device *devicePtr) { hr = devicePtr->CreateShaderResourceView(m_TexturePtr, &SRVDesc, &m_TextureResourceViewPtr); + m_SurfacePtr = surfacePtr; m_pSurfacePixels = static_cast(surfacePtr->pixels); }