diff --git a/Automatic Versions/Version.h b/Automatic Versions/Version.h deleted file mode 100644 index 7b57cfd..0000000 --- a/Automatic Versions/Version.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include - -namespace Version -{ - const unsigned int iMajor = 0; - const unsigned int iMinor = 1; - const unsigned int iPatch = 0; - const unsigned int iBuild = 6; - - const std::wstring strMajor = std::to_wstring(iMajor); - const std::wstring strMinor = std::to_wstring(iMinor); - const std::wstring strPatch = std::to_wstring(iPatch); - const std::wstring strBuild = std::to_wstring(iBuild); - - const std::wstring strVersion = strMajor + L"." + strMinor + L"." + strPatch; - const std::wstring strVersionFull = strVersion + L"." + strBuild; -} diff --git a/Automatic Versions/Version.h_old b/Automatic Versions/Version.h_old deleted file mode 100644 index c366009..0000000 --- a/Automatic Versions/Version.h_old +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once -#include - -namespace Version -{ - const unsigned int iMajor = 0; - const unsigned int iMinor = 1; - const unsigned int iPatch = 0; - const unsigned int iBuild = 5; - - const std::wstring strMajor = std::to_wstring(iMajor); - const std::wstring strMinor = std::to_wstring(iMinor); - const std::wstring strPatch = std::to_wstring(iPatch); - const std::wstring strBuild = std::to_wstring(iBuild); - - const std::wstring strVersion = strMajor + L"." + strMinor + L"." + strPatch; - const std::wstring strVersionFull = strVersion + L"." + strBuild; -} diff --git a/EngineDemo.sln b/EngineDemo.sln index 50c9dec..fa27969 100644 --- a/EngineDemo.sln +++ b/EngineDemo.sln @@ -48,12 +48,6 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection - GlobalSection(Performance) = preSolution - HasPerformanceSessions = true - EndGlobalSection GlobalSection(AutomaticVersions) = postSolution PrimaryVersionType.Release|Win32 = AssemblyVersionAttribute UpdateAssemblyVersion.Release|Win32 = True diff --git a/EngineDemo/EngineDemo.vcxproj b/EngineDemo/EngineDemo.vcxproj index 43d74d4..4fc88f9 100644 --- a/EngineDemo/EngineDemo.vcxproj +++ b/EngineDemo/EngineDemo.vcxproj @@ -1,5 +1,6 @@  + Debug @@ -1185,9 +1186,7 @@ - - Designer - + @@ -1206,4 +1205,10 @@ + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + \ No newline at end of file diff --git a/EngineDemo/EngineDemo.vcxproj.filters b/EngineDemo/EngineDemo.vcxproj.filters index 7199f22..21a4a40 100644 --- a/EngineDemo/EngineDemo.vcxproj.filters +++ b/EngineDemo/EngineDemo.vcxproj.filters @@ -733,6 +733,16 @@ Shaders\Clouds + + + + + + + + + + diff --git a/EngineDemo/RenderStates.cpp b/EngineDemo/RenderStates.cpp index 45f3f3d..20c7226 100644 --- a/EngineDemo/RenderStates.cpp +++ b/EngineDemo/RenderStates.cpp @@ -67,7 +67,7 @@ HRESULT RenderStates::InitAll(ID3D11Device1 * device) // Bilinear Clamp Com Less D3D11_SAMPLER_DESC BilinearClampComLessDesc = {}; - BilinearClampComLessDesc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT; + BilinearClampComLessDesc.Filter = D3D11_FILTER_COMPARISON_MIN_MAG_LINEAR_MIP_POINT; BilinearClampComLessDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP; BilinearClampComLessDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP; BilinearClampComLessDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP; diff --git a/EngineDemo/Terrain2.cpp b/EngineDemo/Terrain2.cpp index 00b07ab..0e5565b 100644 --- a/EngineDemo/Terrain2.cpp +++ b/EngineDemo/Terrain2.cpp @@ -20,6 +20,67 @@ TerrainClass2::~TerrainClass2() } bool TerrainClass2::Init(ID3D11Device1 * device, ID3D11DeviceContext1 * mImmediateContext) +{ + CreateDDSTextureFromFile(device, L"ring.dds", nullptr, &mHeighmapRawSRV); + CreateDDSTextureFromFile(device, L"processedHeighmap.dds", nullptr, &mProDF); + //mImmediateContext->VSSetShaderResources(40, 1, mProDF.GetAddressOf()); + + terrainQuadTree.Init(device, -2048.0f, -2048.0f, 4096.0f, 4096.0f, 7, 20, XMFLOAT3(1.0, 80.0, 1.0)); + + CreateConstantBuffer(device, sizeof(MatrixBuffer), mMatrixCB); + + mPixelShader = ShaderManager::Instance()->GetPS("Terrain2::TerrainPS"); + + // vs & il quad + const D3D11_INPUT_ELEMENT_DESC vertexQuadDesc[] = + { + { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, + { "PSIZE", 0, DXGI_FORMAT_R32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "PSIZE", 1, DXGI_FORMAT_R32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "BLENDINDICES", 0, DXGI_FORMAT_R32_UINT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, + { "PSIZE", 2, DXGI_FORMAT_R32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 } + }; + + UINT numElements = sizeof(vertexQuadDesc) / sizeof(vertexQuadDesc[0]); + + CreateVSAndInputLayout(L"..\\Debug\\Shaders\\Terrain2\\TerrainVS.cso", device, mVertexShader, vertexQuadDesc, numElements, mQuadIL); + + return true; +} + +void TerrainClass2::Draw(ID3D11DeviceContext1 * mImmediateContext, std::shared_ptr Camera, DirectionalLight & light) +{ + terrainQuadTree.GenerateTree(mImmediateContext, Camera); + + XMMATRIX ViewProjTrans = Camera->GetViewProjTransMatrix(); + XMStoreFloat3(&MatrixBuffer.camPos, Camera->GetPosition()); + MatrixBuffer.gWorldProj = ViewProjTrans; + MapResources(mImmediateContext, mMatrixCB.Get(), MatrixBuffer); + + // IA + mImmediateContext->IASetInputLayout(mQuadIL.Get()); + mImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + + // VS + mImmediateContext->VSSetShader(mVertexShader.Get(), nullptr, 0); + mImmediateContext->VSSetConstantBuffers(4, 1, mMatrixCB.GetAddressOf()); + mImmediateContext->VSSetShaderResources(10, 1, mHeighmap->GetAddressOfSRV()); + mImmediateContext->VSSetSamplers(0, 1, &RenderStates::Sampler::BilinearClampSS); + + // PS + mImmediateContext->PSSetShader(mPixelShader, nullptr, 0); + mImmediateContext->PSSetShaderResources(0, 1, mHeighmapRawSRV.GetAddressOf()); + mImmediateContext->PSSetSamplers(0, 1, &RenderStates::Sampler::BilinearWrapSS); + + // RS + mImmediateContext->RSSetState(RenderStates::Rasterizer::DefaultRS); + + terrainQuadTree.Draw(mImmediateContext); + + mImmediateContext->RSSetState(RenderStates::Rasterizer::DefaultRS); +} + +HRESULT TerrainClass2::GenerateDistanceField(ID3D11Device1* device, ID3D11DeviceContext1 * mImmediateContext) { mInitJFA = ShaderManager::Instance()->GetCS("Terrain2::initJFA"); mStepJFA = ShaderManager::Instance()->GetCS("Terrain2::stepJFA"); @@ -29,10 +90,6 @@ bool TerrainClass2::Init(ID3D11Device1 * device, ID3D11DeviceContext1 * mImmedia mCreateHM = ShaderManager::Instance()->GetCS("Terrain2::createHM"); - CreateDDSTextureFromFile(device, L"ring.dds", nullptr, &mHeighmapRawSRV); - CreateDDSTextureFromFile(device, L"processedHeighmap.dds", nullptr, &mProDF); - //mImmediateContext->VSSetShaderResources(40, 1, mProDF.GetAddressOf()); - D3D11_TEXTURE2D_DESC textDesc; ZeroMemory(&textDesc, sizeof(textDesc)); @@ -48,16 +105,16 @@ bool TerrainClass2::Init(ID3D11Device1 * device, ID3D11DeviceContext1 * mImmedia textDesc.CPUAccessFlags = 0; textDesc.MiscFlags = D3D11_RESOURCE_MISC_GENERATE_MIPS; - EXIT_ON_NULL(mOceanDFA = - TextureFactory::CreateTexture(textDesc)); + EXIT_ON_NULL(mOceanDFA = + TextureFactory::CreateTexture(textDesc)); EXIT_ON_NULL(mOceanDFB = - TextureFactory::CreateTexture(textDesc)); + TextureFactory::CreateTexture(textDesc)); EXIT_ON_NULL(mHeighmap = - TextureFactory::CreateTexture(D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS, - DXGI_FORMAT_R16_FLOAT, - 4096, 4096)); + TextureFactory::CreateTexture(D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS, + DXGI_FORMAT_R16_FLOAT, + 4096, 4096)); //textDesc.Format = DXGI_FORMAT_R32_FLOAT; EXIT_ON_NULL(mGenHeightmap = @@ -144,57 +201,5 @@ bool TerrainClass2::Init(ID3D11Device1 * device, ID3D11DeviceContext1 * mImmedia mImmediateContext->PSSetShaderResources(86, 1, mHeighmap->GetAddressOfSRV()); mImmediateContext->PSSetShaderResources(84, 1, mHeighmapRawSRV.GetAddressOf()); - terrainQuadTree.Init(device, -2048.0f, -2048.0f, 4096.0f, 4096.0f, 7, 20, XMFLOAT3(1.0, 80.0, 1.0)); - - CreateConstantBuffer(device, sizeof(MatrixBuffer), mMatrixCB); - - mPixelShader = ShaderManager::Instance()->GetPS("Terrain2::TerrainPS"); - - // vs & il quad - const D3D11_INPUT_ELEMENT_DESC vertexQuadDesc[] = - { - { "POSITION", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, - { "PSIZE", 0, DXGI_FORMAT_R32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, - { "PSIZE", 1, DXGI_FORMAT_R32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, - { "BLENDINDICES", 0, DXGI_FORMAT_R32_UINT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, - { "PSIZE", 2, DXGI_FORMAT_R32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 } - }; - - UINT numElements = sizeof(vertexQuadDesc) / sizeof(vertexQuadDesc[0]); - - CreateVSAndInputLayout(L"..\\Debug\\Shaders\\Terrain2\\TerrainVS.cso", device, mVertexShader, vertexQuadDesc, numElements, mQuadIL); - - return true; -} - -void TerrainClass2::Draw(ID3D11DeviceContext1 * mImmediateContext, std::shared_ptr Camera, DirectionalLight & light) -{ - terrainQuadTree.GenerateTree(mImmediateContext, Camera); - - XMMATRIX ViewProjTrans = Camera->GetViewProjTransMatrix(); - XMStoreFloat3(&MatrixBuffer.camPos, Camera->GetPosition()); - MatrixBuffer.gWorldProj = ViewProjTrans; - MapResources(mImmediateContext, mMatrixCB.Get(), MatrixBuffer); - - // IA - mImmediateContext->IASetInputLayout(mQuadIL.Get()); - mImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); - - // VS - mImmediateContext->VSSetShader(mVertexShader.Get(), nullptr, 0); - mImmediateContext->VSSetConstantBuffers(4, 1, mMatrixCB.GetAddressOf()); - mImmediateContext->VSSetShaderResources(10, 1, mHeighmap->GetAddressOfSRV()); - mImmediateContext->VSSetSamplers(0, 1, &RenderStates::Sampler::BilinearClampSS); - - // PS - mImmediateContext->PSSetShader(mPixelShader, nullptr, 0); - mImmediateContext->PSSetShaderResources(0, 1, mHeighmapRawSRV.GetAddressOf()); - mImmediateContext->PSSetSamplers(0, 1, &RenderStates::Sampler::BilinearWrapSS); - - // RS - mImmediateContext->RSSetState(RenderStates::Rasterizer::DefaultRS); - - terrainQuadTree.Draw(mImmediateContext); - - mImmediateContext->RSSetState(RenderStates::Rasterizer::DefaultRS); + return S_OK; } diff --git a/EngineDemo/Terrain2.h b/EngineDemo/Terrain2.h index 3cd97bf..4fc3f3d 100644 --- a/EngineDemo/Terrain2.h +++ b/EngineDemo/Terrain2.h @@ -34,6 +34,8 @@ __declspec(align(16)) class TerrainClass2 : public HasLogger void Draw(ID3D11DeviceContext1* mImmediateContext, std::shared_ptr Camera, DirectionalLight& light); private: + HRESULT GenerateDistanceField(ID3D11Device1* device, ID3D11DeviceContext1* mImmediateContext); + Microsoft::WRL::ComPtr mHeighmapRawSRV; Microsoft::WRL::ComPtr mProDF; diff --git a/EngineDemo/Utilities/CreateShader.cpp b/EngineDemo/Utilities/CreateShader.cpp index 3a30afd..1eb0f90 100644 --- a/EngineDemo/Utilities/CreateShader.cpp +++ b/EngineDemo/Utilities/CreateShader.cpp @@ -27,10 +27,11 @@ bool LoadShader(std::wstring fileName, char *& data, size_t & size) return false; } -void LoadCompiledShader(const fs::path & fileName, ID3DBlob *& shaderBlob, const std::string target, const std::string entry_point) +void LoadCompiledShader(const fs::path & fileName, Microsoft::WRL::ComPtr& shaderBlob, const std::string& target, const std::string& entry_point) { HRESULT hr; - ID3DBlob* errorBlob{ nullptr }; + Microsoft::WRL::ComPtr errorBlob{ nullptr }; + #ifndef DEPLOYMENT if (fileName.extension() == ".hlsl") { @@ -38,6 +39,7 @@ void LoadCompiledShader(const fs::path & fileName, ID3DBlob *& shaderBlob, const D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION | D3DCOMPILE_ENABLE_BACKWARDS_COMPATIBILITY, 0, &shaderBlob, &errorBlob); } #endif + if (fileName.extension() == ".cso") { hr = D3DReadFileToBlob(fileName.wstring().c_str(), &shaderBlob); diff --git a/EngineDemo/Utilities/CreateShader.h b/EngineDemo/Utilities/CreateShader.h index d4199b8..818f5e0 100644 --- a/EngineDemo/Utilities/CreateShader.h +++ b/EngineDemo/Utilities/CreateShader.h @@ -16,15 +16,16 @@ namespace fs = std::experimental::filesystem; // Load content of file given by 'fileName' to memory at 'data' alocating needed memory at returning size in 'size'. Calle is responsible for dealocating memory +[[deprecated("Use LoadCompileShader insteead")]] bool LoadShader(_In_ std::wstring fileName, _Out_ char* &data, _Out_ size_t &size); // Read compiled shader to the blob (only cso and hlsl in not deployment build) -void LoadCompiledShader(_In_ const fs::path& fileName, _Out_ ID3DBlob* &blob, _In_ const std::string target = "", _In_ const std::string entry_point = "main"); +void LoadCompiledShader(_In_ const fs::path& fileName, _Out_ Microsoft::WRL::ComPtr &blob, _In_ const std::string& target = "", _In_ const std::string& entry_point = "main"); template -bool CreatePSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ T &ps, const std::string& name = "") +bool CreatePSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * &device, _Out_opt_ T &ps, const std::string& name = "") { - ID3DBlob* shaderBlob{ nullptr }; + Microsoft::WRL::ComPtr shaderBlob{ nullptr }; ID3D11PixelShader* shader{ nullptr }; LoadCompiledShader(fileName, shaderBlob, "ps_5_0"); @@ -42,9 +43,9 @@ bool CreatePSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device } template -bool CreateCSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ T &cs, const std::string& name = "") +bool CreateCSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * &device, _Out_opt_ T &cs, const std::string& name = "") { - ID3DBlob* shaderBlob{ nullptr }; + Microsoft::WRL::ComPtr shaderBlob{ nullptr }; ID3D11ComputeShader* shader{ nullptr }; LoadCompiledShader(fileName, shaderBlob, "cs_5_0"); @@ -62,9 +63,9 @@ bool CreateCSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device } template -bool CreateDSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ T &ds, const std::string& name = "") +bool CreateDSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * &device, _Out_opt_ T &ds, const std::string& name = "") { - ID3DBlob* shaderBlob{ nullptr }; + Microsoft::WRL::ComPtr shaderBlob{ nullptr }; ID3D11DomainShader* shader{ nullptr }; LoadCompiledShader(fileName, shaderBlob, "ds_5_0"); @@ -82,9 +83,9 @@ bool CreateDSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device } template -bool CreateHSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ T &hs, const std::string& name = "") +bool CreateHSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * &device, _Out_opt_ T &hs, const std::string& name = "") { - ID3DBlob* shaderBlob{ nullptr }; + Microsoft::WRL::ComPtr shaderBlob{ nullptr }; ID3D11HullShader* shader{ nullptr }; LoadCompiledShader(fileName, shaderBlob, "hs_5_0"); @@ -102,9 +103,9 @@ bool CreateHSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device } template -bool CreateVSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ T &vs, const std::string& name = "") +bool CreateVSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * &device, _Out_opt_ T &vs, const std::string& name = "") { - ID3DBlob* blob{ nullptr }; + Microsoft::WRL::ComPtr blob{ nullptr }; LoadCompiledShader(fileName, blob, "vs_5_0"); @@ -121,7 +122,7 @@ bool CreateVSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device template bool CreateGSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ T &gs, const std::string& name = "") { - ID3DBlob* shaderBlob{ nullptr }; + Microsoft::WRL::ComPtr shaderBlob{ nullptr }; ID3D11GeometryShader* shader{ nullptr }; LoadCompiledShader(fileName, shaderBlob, "hs_5_0"); @@ -141,7 +142,7 @@ bool CreateGSFromFile(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device template bool CreateVSAndInputLayout(_In_ const fs::path& fileName, _In_ ID3D11Device1 * device, _Out_opt_ Tvs &vs, const D3D11_INPUT_ELEMENT_DESC* ilDesc, const UINT numElements, _Out_opt_ Til &il, const std::string& vsname = "", const std::string& ilname = "") { - ID3DBlob* shaderBlob{ nullptr }; + Microsoft::WRL::ComPtr shaderBlob{ nullptr }; ID3D11VertexShader* shader{ nullptr }; ID3D11InputLayout* input{ nullptr }; diff --git a/EngineDemo/Version.h b/EngineDemo/Version.h index b0c1b17..5f5c813 100644 --- a/EngineDemo/Version.h +++ b/EngineDemo/Version.h @@ -6,7 +6,7 @@ namespace Version const unsigned int iMajor = 0; const unsigned int iMinor = 1; const unsigned int iPatch = 0; - const unsigned int iBuild = 60; + const unsigned int iBuild = 71; const std::wstring strMajor = std::to_wstring(iMajor); const std::wstring strMinor = std::to_wstring(iMinor); diff --git a/EngineDemo/Version.h_old b/EngineDemo/Version.h_old index 173af7f..46d9258 100644 --- a/EngineDemo/Version.h_old +++ b/EngineDemo/Version.h_old @@ -6,7 +6,7 @@ namespace Version const unsigned int iMajor = 0; const unsigned int iMinor = 1; const unsigned int iPatch = 0; - const unsigned int iBuild = 59; + const unsigned int iBuild = 70; const std::wstring strMajor = std::to_wstring(iMajor); const std::wstring strMinor = std::to_wstring(iMinor); diff --git a/EngineDemo/imgui.ini b/EngineDemo/imgui.ini index 36c19a1..8b11a50 100644 --- a/EngineDemo/imgui.ini +++ b/EngineDemo/imgui.ini @@ -1,7 +1,7 @@ [Debug] -Pos=882,6 +Pos=814,23 Size=374,274 -Collapsed=0 +Collapsed=1 [Log] Pos=0,570 diff --git a/EngineDemo/map.cpp b/EngineDemo/map.cpp index 1f4835a..a414b0a 100644 --- a/EngineDemo/map.cpp +++ b/EngineDemo/map.cpp @@ -26,11 +26,35 @@ bool MapClass::Init(ID3D11Device1* device, ID3D11DeviceContext1 * dc) Clouds2 = std::make_shared(); Clouds2->Init(device, dc); + Terrain = std::make_shared(); + TerrainClass::InitInfo tii; + tii.HeightMapFilename = L"Textures/terrain.raw"; + tii.LayerMapFilename0 = L"Textures/grass.dds"; + tii.LayerMapFilename1 = L"Textures/darkdirt.dds"; + tii.LayerMapFilename2 = L"Textures/stone.dds"; + tii.LayerMapFilename3 = L"Textures/lightdirt.dds"; + tii.LayerMapFilename4 = L"Textures/snow.dds"; + tii.BlendMapFilename = L"Textures/blend.dds"; + tii.HeightScale = 100.0f; + tii.HeightmapWidth = 2049; + tii.HeightmapHeight = 2049; + tii.CellSpacing = 0.5f; + if (!Terrain->Init(device, dc, tii)) + { + LogError(L"Failed to initiate terrain"); + return false; + } + + Terrain2 = std::make_unique(); + Terrain2->Init(device, dc); + light.Ambient(XMFLOAT4(0.2f, 0.2f, 0.2f, 1.0f)); light.Diffuse(XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f)); light.Specular(XMFLOAT4(0.5f, 0.5f, 0.5f, 1.0f)); light.Direction(XMFLOAT3(-0.707f, -0.707f, 0.0f)); + ShadowMap = std::make_unique(device, 2048, 2048); + // DS D3D11_BUFFER_DESC matrixBufferDesc; matrixBufferDesc.Usage = D3D11_USAGE_DYNAMIC; @@ -137,15 +161,24 @@ void MapClass::Update(float dt, ID3D11DeviceContext1 * mImmediateContext, std::s XMStoreFloat3(&dir_f, dir); light.Direction(dir_f);*/ + light.SetLitWorld(XMFLOAT3(-768.0f, -150.0f, -768.0f), XMFLOAT3(768.0f, 150.0f, 768.0f)); Clouds2->Update(dt); } void MapClass::Draw(ID3D11DeviceContext1 * mImmediateContext, std::shared_ptr Camera) { - Sky->Draw(mImmediateContext, Camera, light); + ShadowMap->BindDsvAndSetNullRenderTarget(mImmediateContext); + ShadowMap->ClearDepthMap(mImmediateContext); + + Terrain->Draw(mImmediateContext, Camera, light, nullptr); - Clouds2->Draw(mImmediateContext, Camera, light, Sky->getTransmittanceSRV()); + RenderTargetStack::Pop(mImmediateContext); + ViewportStack::Pop(mImmediateContext); + + Terrain->Draw(mImmediateContext, Camera, light, ShadowMap->DepthMapSRV()); + + Sky->Draw(mImmediateContext, Camera, light); } void MapClass::Draw20(ID3D11DeviceContext1 * mImmediateContext, std::shared_ptr Camera) diff --git a/EngineDemo/packages.config b/EngineDemo/packages.config index ee63504..3897c68 100644 --- a/EngineDemo/packages.config +++ b/EngineDemo/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file diff --git a/EngineDemo/terrain.cpp b/EngineDemo/terrain.cpp index 655f182..8314013 100644 --- a/EngineDemo/terrain.cpp +++ b/EngineDemo/terrain.cpp @@ -4,6 +4,11 @@ #include "RenderStates.h" +#include +#include +#include +#include + TerrainClass::TerrainClass() : mQuadPatchVB(0), mQuadPatchIB(0), @@ -115,7 +120,10 @@ bool TerrainClass::Init(ID3D11Device1* device, ID3D11DeviceContext1* dc, const I mNumPatchVertices = mNumPatchVertRows*mNumPatchVertCols; mNumPatchQuadFaces = (mNumPatchVertRows - 1)*(mNumPatchVertCols - 1); - LoadHeighMap(); + //LoadHeighMap(); + + CreateRandomHeightMap(); + //Smooth(); CalcAllPatchBoundsY(); @@ -195,7 +203,7 @@ bool TerrainClass::Init(ID3D11Device1* device, ID3D11DeviceContext1* dc, const I void TerrainClass::Draw(ID3D11DeviceContext1 * mImmediateContext, std::shared_ptr Camera, DirectionalLight& light, ID3D11ShaderResourceView * ShadowMap) { mImmediateContext->PSSetShaderResources(2, 1, &mLayerMapArraySRV); - return; + //return; XMMATRIX ShadowViewProjTrans = light.GetViewProjTrans();//XMMatrixTranspose(V*P); XMMATRIX ShadowMapProjTrans = light.GetMapProjTrans();//XMMatrixTranspose(V*P*T); @@ -301,7 +309,7 @@ void TerrainClass::Draw(ID3D11DeviceContext1 * mImmediateContext, std::shared_pt if (ShadowMap) mImmediateContext->RSSetState(mRastStateBasic); else mImmediateContext->RSSetState(mRastStateShadow); - //mImmediateContext->DrawIndexed(mNumPatchQuadFaces * 4, 0, 0); + mImmediateContext->DrawIndexed(mNumPatchQuadFaces * 4, 0, 0); mImmediateContext->HSSetShader(0, 0, 0); mImmediateContext->DSSetShader(0, 0, 0); @@ -311,6 +319,89 @@ void TerrainClass::Draw(ID3D11DeviceContext1 * mImmediateContext, std::shared_pt mImmediateContext->PSSetShaderResources(3, 1, &NULLRes); } +void TerrainClass::CreateRandomHeightMap() +{ + mHeightmap.resize(mInfo.HeightmapWidth*mInfo.HeightmapHeight); + + auto& heightmap = mHeightmap; + + auto set = [&heightmap, sizeX = mInfo.HeightmapWidth](int x, int y, auto val) -> void + { + heightmap[y*sizeX + x] = val; + }; + + set(0, 0, 0.0); + set(0, mInfo.HeightmapHeight - 1, mInfo.HeightmapHeight / 20.0f); + set(mInfo.HeightmapWidth - 1, 0, mInfo.HeightmapHeight / 20.0f); + set(mInfo.HeightmapWidth - 1, mInfo.HeightmapHeight - 1, mInfo.HeightmapHeight / 10.0f); + + DiamondSquareStep(mInfo.HeightmapWidth - 1); +} + +void TerrainClass::DiamondSquareStep(int size) +{ + auto generator = std::minstd_rand{ std::random_device{}() }; + auto dist = std::uniform_real_distribution{ -1.0f, 1.0f }; + + auto sizeY = mInfo.HeightmapHeight - 1; + auto sizeX = mInfo.HeightmapWidth - 1; + + auto half = size / 2; + auto scale = size * 0.1; + if (half < 1) return; + + // average given lift of floats (lowest() is invalid) + static auto average = [](const std::initializer_list& list) + { + float sum = std::accumulate(std::cbegin(list), std::cend(list), 0.0, + [](auto&& a, auto&& b) { + return a + (b != std::numeric_limits::lowest() ? b : 0); + }); + + int total = std::count_if(std::cbegin(list), std::cend(list), [](auto&& val) { return val != std::numeric_limits::lowest(); }); + + return sum / total; + }; + + // square step + for (auto y = half; y < sizeY; y += size) + { + for (auto x = half; x < sizeX; x += size) + { + auto offset = scale * dist(generator); + mHeightmap[y*mInfo.HeightmapWidth + x] = + average({ GetHeightRaw(x - half, y - half), + GetHeightRaw(x + half, y - half), + GetHeightRaw(x - half, y + half), + GetHeightRaw(x + half, y + half) }) + offset; + } + } + + // diamond step + for (auto y = 0; y <= sizeY; y += half) + { + for (auto x = (y + half) % size; x <= sizeX; x += size) + { + auto offset = scale * dist(generator); + mHeightmap[y*mInfo.HeightmapWidth + x] = + average({ GetHeightRaw(x, y - half), + GetHeightRaw(x + half, y), + GetHeightRaw(x, y + half), + GetHeightRaw(x - half, y) }) + offset; + } + } + + DiamondSquareStep(half); +} + +float TerrainClass::GetHeightRaw(int x, int y) const noexcept +{ + if (x < 0 || x >= mInfo.HeightmapWidth || y < 0 || y >= mInfo.HeightmapHeight) + return std::numeric_limits::lowest(); + else + return mHeightmap[y*mInfo.HeightmapWidth + x]; +} + void TerrainClass::LoadHeighMap() { // A height fo reach vertex diff --git a/EngineDemo/terrain.h b/EngineDemo/terrain.h index ca75be2..cab7807 100644 --- a/EngineDemo/terrain.h +++ b/EngineDemo/terrain.h @@ -126,6 +126,11 @@ class TerrainClass : public HasLogger float padding[2]; }; private: + void CreateRandomHeightMap(); + void DiamondSquareStep(int size); + + float GetHeightRaw(int x, int y) const noexcept; + void LoadHeighMap(); void Smooth(); bool InBounds(int i, int j); diff --git a/ocean readme.md b/ocean readme.md deleted file mode 100644 index 50bbac1..0000000 --- a/ocean readme.md +++ /dev/null @@ -1,31 +0,0 @@ -# Ocean - -## Mesh - -My own simplified implementation of Filip Strugar's [CDLOD]. -* VS -> HS -> DS -> PS pipeline - * **Vertex Shader** - First morphing vertex grid between LODs, scalling and moving acording to position (Pos.xyz). - Than read gradient field, and based on that calculate phase (params.x), depth dependency (params.y), direction to a coast (params.zw). - * **Hull Shader** - Tesselation based on depth dependency. - * **Domain Shader** - Read opean sea displacement. - From displacement noise and wind direction parameter calculate amplitude scaling factor. - Amplitude calculate as half of the significant wave height: $A=\frac{1}{2}\frac{0.27*|\bold{w}|^2}{g}$. From this and phase from VS we calculate gernsten waves shape, which is interpolated with open ocean shape with depth dependency (VS) as parameter. - Out: - * PosH : SV_POSITION - * PosF : xy - world space flat position, z - wave height excursion, w - amplitude's factor - * PosW : xyz - world space position - * params : x - phase, y - depth, zw - direction to a coast - * **Pixel Shader** - Analitic normals for gernsten, open ocean normals from slope map. - -## Lighting -Bruneton's lighting model extended by transparency (channel dependent linear light absorption), and screen space refractions. +foam with ramp texture -$$waterLight = lerp(seaLight, seenBottom, 1-extinction)*(foam*0.05 / PI + (1.0 - foam)*(1.0 - fresnelUp))$$ (1) -$$skyLight = skyRadiance*(foam + (1.0 - foam)*fresnelUp)$$ (2) -$$sunLight = Lsun * (foam + (1.0 - foam)*reflectedRadiance)$$ (3) - - -[CDLOD]: http://www.vertexasylum.com/downloads/cdlod/cdlod_latest.pdf \ No newline at end of file