2022-01-04 14:29:01 +01:00
|
|
|
/* Copyright (C) 2022 Wildfire Games.
|
2009-04-18 19:00:33 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
#include "graphics/Terrain.h"
|
2012-11-26 03:28:35 +01:00
|
|
|
#include "graphics/TextureManager.h"
|
2014-07-28 12:14:00 +02:00
|
|
|
#include "graphics/ShaderManager.h"
|
|
|
|
#include "graphics/ShaderProgram.h"
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 20:48:32 +02:00
|
|
|
#include "lib/bits.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
#include "lib/timer.h"
|
2006-05-28 04:13:32 +02:00
|
|
|
#include "maths/MathUtil.h"
|
2012-10-31 19:42:17 +01:00
|
|
|
#include "maths/Vector2D.h"
|
2014-08-06 15:23:02 +02:00
|
|
|
#include "ps/CLogger.h"
|
2021-05-18 13:09:54 +02:00
|
|
|
#include "ps/CStrInternStatic.h"
|
2012-10-31 19:42:17 +01:00
|
|
|
#include "ps/Game.h"
|
2021-12-14 07:34:02 +01:00
|
|
|
#include "ps/VideoMode.h"
|
2012-10-31 19:42:17 +01:00
|
|
|
#include "ps/World.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/IDevice.h"
|
2006-05-28 04:13:32 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2019-08-04 10:28:30 +02:00
|
|
|
#include "renderer/RenderingOptions.h"
|
2022-01-04 14:29:01 +01:00
|
|
|
#include "renderer/SceneRenderer.h"
|
2022-02-05 17:59:23 +01:00
|
|
|
#include "renderer/WaterManager.h"
|
2013-04-27 14:20:42 +02:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
#include "simulation2/components/ICmpWaterManager.h"
|
|
|
|
#include "simulation2/components/ICmpRangeManager.h"
|
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
#include <algorithm>
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
struct CoastalPoint
|
|
|
|
{
|
|
|
|
CoastalPoint(int idx, CVector2D pos) : index(idx), position(pos) {};
|
|
|
|
int index;
|
|
|
|
CVector2D position;
|
|
|
|
};
|
|
|
|
|
2021-11-27 16:01:14 +01:00
|
|
|
struct SWavesVertex
|
|
|
|
{
|
2014-07-28 12:14:00 +02:00
|
|
|
// vertex position
|
|
|
|
CVector3D m_BasePosition;
|
|
|
|
CVector3D m_ApexPosition;
|
|
|
|
CVector3D m_SplashPosition;
|
|
|
|
CVector3D m_RetreatPosition;
|
|
|
|
|
|
|
|
CVector2D m_PerpVect;
|
|
|
|
u8 m_UV[3];
|
2015-01-25 16:38:51 +01:00
|
|
|
|
|
|
|
// pad to a power of two
|
|
|
|
u8 m_Padding[5];
|
2014-07-28 12:14:00 +02:00
|
|
|
};
|
2015-01-25 16:38:51 +01:00
|
|
|
cassert(sizeof(SWavesVertex) == 64);
|
2014-07-28 12:14:00 +02:00
|
|
|
|
|
|
|
struct WaveObject
|
|
|
|
{
|
2022-01-10 17:51:43 +01:00
|
|
|
CVertexBufferManager::Handle m_VBVertices;
|
2014-07-28 12:14:00 +02:00
|
|
|
CBoundingBoxAligned m_AABB;
|
|
|
|
size_t m_Width;
|
|
|
|
float m_TimeDiff;
|
|
|
|
};
|
2006-01-07 02:04:26 +01:00
|
|
|
|
|
|
|
WaterManager::WaterManager()
|
|
|
|
{
|
|
|
|
// water
|
2007-02-10 23:07:20 +01:00
|
|
|
m_RenderWater = false; // disabled until textures are successfully loaded
|
2006-01-07 02:04:26 +01:00
|
|
|
m_WaterHeight = 5.0f;
|
2014-07-03 22:07:15 +02:00
|
|
|
|
2016-06-26 18:54:58 +02:00
|
|
|
m_RefTextureSize = 0;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2006-01-22 00:27:42 +01:00
|
|
|
m_WaterTexTimer = 0.0;
|
2014-07-03 22:07:15 +02:00
|
|
|
|
|
|
|
m_WindAngle = 0.0f;
|
2006-06-05 23:02:29 +02:00
|
|
|
m_Waviness = 8.0f;
|
2014-07-03 22:07:15 +02:00
|
|
|
m_WaterColor = CColor(0.3f, 0.35f, 0.7f, 1.0f);
|
2006-10-07 19:59:06 +02:00
|
|
|
m_WaterTint = CColor(0.28f, 0.3f, 0.59f, 1.0f);
|
|
|
|
m_Murkiness = 0.45f;
|
2006-06-07 21:15:47 +02:00
|
|
|
m_RepeatPeriod = 16.0f;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2017-08-21 02:09:25 +02:00
|
|
|
m_WaterEffects = true;
|
2014-07-01 18:05:05 +02:00
|
|
|
m_WaterFancyEffects = false;
|
2012-11-04 16:54:36 +01:00
|
|
|
m_WaterRealDepth = false;
|
|
|
|
m_WaterRefraction = false;
|
|
|
|
m_WaterReflection = false;
|
2014-07-03 22:07:15 +02:00
|
|
|
m_WaterType = L"ocean";
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
m_NeedsReloading = false;
|
2013-08-18 11:27:11 +02:00
|
|
|
m_NeedInfoUpdate = true;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2013-08-18 11:27:11 +02:00
|
|
|
m_MapSize = 0;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2013-08-18 11:27:11 +02:00
|
|
|
m_updatei0 = 0;
|
|
|
|
m_updatej0 = 0;
|
|
|
|
m_updatei1 = 0;
|
|
|
|
m_updatej1 = 0;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WaterManager::~WaterManager()
|
|
|
|
{
|
|
|
|
// Cleanup if the caller messed up
|
|
|
|
UnloadWaterTextures();
|
2014-07-03 22:07:15 +02:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
m_ShoreWaves.clear();
|
|
|
|
m_ShoreWavesVBIndices.Reset();
|
2014-07-28 12:14:00 +02:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
m_DistanceHeightmap.reset();
|
|
|
|
m_WindStrength.reset();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-02-05 17:59:23 +01:00
|
|
|
m_FancyEffectsFramebuffer.reset();
|
|
|
|
m_RefractionFramebuffer.reset();
|
|
|
|
m_ReflectionFramebuffer.reset();
|
2014-09-27 03:32:03 +02:00
|
|
|
|
2021-12-27 09:14:47 +01:00
|
|
|
m_FancyTexture.reset();
|
|
|
|
m_FancyTextureDepth.reset();
|
|
|
|
m_ReflFboDepthTexture.reset();
|
|
|
|
m_RefrFboDepthTexture.reset();
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Progressive load of water textures
|
|
|
|
int WaterManager::LoadWaterTextures()
|
|
|
|
{
|
2010-09-10 23:02:10 +02:00
|
|
|
// TODO: this doesn't need to be progressive-loading any more
|
|
|
|
// (since texture loading is async now)
|
2006-05-25 07:46:17 +02:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t pathname[PATH_MAX];
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2006-05-28 04:13:32 +02:00
|
|
|
// Load diffuse grayscale images (for non-fancy water)
|
2010-09-10 23:02:10 +02:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(m_WaterTexture); ++i)
|
2006-01-07 02:04:26 +01:00
|
|
|
{
|
2014-07-01 18:05:05 +02:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/default/diffuse%02d.dds", (int)i+1);
|
2010-09-10 23:02:10 +02:00
|
|
|
CTextureProperties textureProps(pathname);
|
2022-02-13 20:30:28 +01:00
|
|
|
textureProps.SetAddressMode(
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT);
|
2010-09-10 23:02:10 +02:00
|
|
|
|
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_WaterTexture[i] = texture;
|
2006-05-25 07:46:17 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 22:46:03 +01:00
|
|
|
m_RenderWater = true;
|
|
|
|
|
2006-05-28 04:13:32 +02:00
|
|
|
// Load normalmaps (for fancy water)
|
2019-12-07 16:32:47 +01:00
|
|
|
ReloadWaterNormalTextures();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Load CoastalWaves
|
|
|
|
{
|
|
|
|
CTextureProperties textureProps(L"art/textures/terrain/types/water/coastalWave.png");
|
2022-02-13 20:30:28 +01:00
|
|
|
textureProps.SetAddressMode(
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT);
|
2014-07-28 12:14:00 +02:00
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_WaveTex = texture;
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Load Foam
|
|
|
|
{
|
|
|
|
CTextureProperties textureProps(L"art/textures/terrain/types/water/foam.png");
|
2022-02-13 20:30:28 +01:00
|
|
|
textureProps.SetAddressMode(
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT);
|
2014-07-28 12:14:00 +02:00
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_FoamTex = texture;
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
RecreateOrLoadTexturesIfNeeded();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
void WaterManager::RecreateOrLoadTexturesIfNeeded()
|
|
|
|
{
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDevice* backendDevice = g_VideoMode.GetBackendDevice();
|
2022-02-13 20:30:28 +01:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
// Use screen-sized textures for minimum artifacts.
|
|
|
|
const size_t newRefTextureSize = round_up_to_pow2(g_Renderer.GetHeight());
|
|
|
|
|
|
|
|
if (m_RefTextureSize != newRefTextureSize)
|
2014-07-04 12:14:35 +02:00
|
|
|
{
|
2022-03-09 22:40:24 +01:00
|
|
|
m_ReflectionFramebuffer.reset();
|
|
|
|
m_ReflectionTexture.reset();
|
|
|
|
m_ReflFboDepthTexture.reset();
|
|
|
|
|
|
|
|
m_RefractionFramebuffer.reset();
|
|
|
|
m_RefractionTexture.reset();
|
|
|
|
m_RefrFboDepthTexture.reset();
|
|
|
|
|
|
|
|
m_RefTextureSize = newRefTextureSize;
|
2014-07-04 12:14:35 +02:00
|
|
|
}
|
2014-07-03 22:07:15 +02:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
// Create reflection textures.
|
|
|
|
const bool needsReflectionTextures =
|
|
|
|
g_RenderingOptions.GetWaterEffects() &&
|
|
|
|
g_RenderingOptions.GetWaterReflection();
|
|
|
|
if (needsReflectionTextures && !m_ReflectionTexture)
|
2014-07-04 12:14:35 +02:00
|
|
|
{
|
2022-03-09 22:40:24 +01:00
|
|
|
m_ReflectionTexture = backendDevice->CreateTexture2D("WaterReflectionTexture",
|
|
|
|
Renderer::Backend::Format::R8G8B8A8_UNORM, m_RefTextureSize, m_RefTextureSize,
|
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::LINEAR,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::MIRRORED_REPEAT));
|
|
|
|
|
|
|
|
m_ReflFboDepthTexture = backendDevice->CreateTexture2D("WaterReflectionDepthTexture",
|
|
|
|
Renderer::Backend::Format::D32, m_RefTextureSize, m_RefTextureSize,
|
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::NEAREST,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT));
|
|
|
|
|
|
|
|
m_ReflectionFramebuffer = backendDevice->CreateFramebuffer("ReflectionFramebuffer",
|
|
|
|
m_ReflectionTexture.get(), m_ReflFboDepthTexture.get(), CColor(0.5f, 0.5f, 1.0f, 0.0f));
|
|
|
|
if (!m_ReflectionFramebuffer)
|
|
|
|
{
|
|
|
|
g_RenderingOptions.SetWaterReflection(false);
|
|
|
|
UpdateQuality();
|
|
|
|
}
|
2014-07-04 12:14:35 +02:00
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
// Create refraction textures.
|
|
|
|
const bool needsRefractionTextures =
|
|
|
|
g_RenderingOptions.GetWaterEffects() &&
|
|
|
|
g_RenderingOptions.GetWaterRefraction();
|
|
|
|
if (needsRefractionTextures && !m_RefractionTexture)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
2022-03-09 22:40:24 +01:00
|
|
|
m_RefractionTexture = backendDevice->CreateTexture2D("WaterRefractionTexture",
|
|
|
|
Renderer::Backend::Format::R8G8B8A8_UNORM, m_RefTextureSize, m_RefTextureSize,
|
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::LINEAR,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::MIRRORED_REPEAT));
|
|
|
|
|
|
|
|
m_RefrFboDepthTexture = backendDevice->CreateTexture2D("WaterRefractionDepthTexture",
|
|
|
|
Renderer::Backend::Format::D32, m_RefTextureSize, m_RefTextureSize,
|
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::NEAREST,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT));
|
|
|
|
|
|
|
|
m_RefractionFramebuffer = backendDevice->CreateFramebuffer("RefractionFramebuffer",
|
|
|
|
m_RefractionTexture.get(), m_RefrFboDepthTexture.get(), CColor(1.0f, 0.0f, 0.0f, 0.0f));
|
|
|
|
if (!m_RefractionFramebuffer)
|
|
|
|
{
|
|
|
|
g_RenderingOptions.SetWaterRefraction(false);
|
|
|
|
UpdateQuality();
|
|
|
|
}
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
const uint32_t newWidth = static_cast<uint32_t>(g_Renderer.GetWidth());
|
|
|
|
const uint32_t newHeight = static_cast<uint32_t>(g_Renderer.GetHeight());
|
|
|
|
if (m_FancyTexture && (m_FancyTexture->GetWidth() != newWidth || m_FancyTexture->GetHeight() != newHeight))
|
|
|
|
{
|
|
|
|
m_FancyEffectsFramebuffer.reset();
|
|
|
|
m_FancyTexture.reset();
|
|
|
|
m_FancyTextureDepth.reset();
|
|
|
|
}
|
2022-02-13 20:30:28 +01:00
|
|
|
|
2022-03-09 22:40:24 +01:00
|
|
|
// Create the Fancy Effects textures.
|
|
|
|
const bool needsFancyTextures =
|
|
|
|
g_RenderingOptions.GetWaterEffects() &&
|
|
|
|
g_RenderingOptions.GetWaterFancyEffects();
|
|
|
|
if (needsFancyTextures && !m_FancyTexture)
|
|
|
|
{
|
|
|
|
m_FancyTexture = backendDevice->CreateTexture2D("WaterFancyTexture",
|
|
|
|
Renderer::Backend::Format::R8G8B8A8_UNORM, g_Renderer.GetWidth(), g_Renderer.GetHeight(),
|
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::LINEAR,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT));
|
|
|
|
|
|
|
|
m_FancyTextureDepth = backendDevice->CreateTexture2D("WaterFancyDepthTexture",
|
|
|
|
Renderer::Backend::Format::D32, g_Renderer.GetWidth(), g_Renderer.GetHeight(),
|
|
|
|
Renderer::Backend::Sampler::MakeDefaultSampler(
|
|
|
|
Renderer::Backend::Sampler::Filter::LINEAR,
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT));
|
|
|
|
|
|
|
|
m_FancyEffectsFramebuffer = backendDevice->CreateFramebuffer("FancyEffectsFramebuffer",
|
|
|
|
m_FancyTexture.get(), m_FancyTextureDepth.get());
|
|
|
|
if (!m_FancyEffectsFramebuffer)
|
|
|
|
{
|
|
|
|
g_RenderingOptions.SetWaterRefraction(false);
|
|
|
|
UpdateQuality();
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 20:41:29 +01:00
|
|
|
}
|
|
|
|
|
2015-02-28 14:50:52 +01:00
|
|
|
void WaterManager::ReloadWaterNormalTextures()
|
|
|
|
{
|
|
|
|
wchar_t pathname[PATH_MAX];
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(m_NormalMap); ++i)
|
|
|
|
{
|
2019-12-07 16:32:47 +01:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/%ls/normal00%02d.png", m_WaterType.c_str(), static_cast<int>(i) + 1);
|
2015-02-28 14:50:52 +01:00
|
|
|
CTextureProperties textureProps(pathname);
|
2022-02-13 20:30:28 +01:00
|
|
|
textureProps.SetAddressMode(
|
|
|
|
Renderer::Backend::Sampler::AddressMode::REPEAT);
|
|
|
|
textureProps.SetAnisotropicFilter(true);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2015-02-28 14:50:52 +01:00
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_NormalMap[i] = texture;
|
|
|
|
}
|
|
|
|
}
|
2015-02-10 20:41:29 +01:00
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Unload water textures
|
|
|
|
void WaterManager::UnloadWaterTextures()
|
|
|
|
{
|
2021-12-27 09:14:47 +01:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(m_WaterTexture); i++)
|
2010-09-10 23:02:10 +02:00
|
|
|
m_WaterTexture[i].reset();
|
2014-09-27 03:32:03 +02:00
|
|
|
|
2021-12-27 09:14:47 +01:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(m_NormalMap); i++)
|
2010-09-10 23:02:10 +02:00
|
|
|
m_NormalMap[i].reset();
|
2014-09-27 03:32:03 +02:00
|
|
|
|
2022-02-05 17:59:23 +01:00
|
|
|
m_RefractionFramebuffer.reset();
|
|
|
|
m_ReflectionFramebuffer.reset();
|
2021-12-27 09:14:47 +01:00
|
|
|
m_ReflectionTexture.reset();
|
|
|
|
m_RefractionTexture.reset();
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
|
2019-01-02 16:23:02 +01:00
|
|
|
template<bool Transpose>
|
|
|
|
static inline void ComputeDirection(float* distanceMap, const u16* heightmap, float waterHeight, size_t SideSize, size_t maxLevel)
|
|
|
|
{
|
|
|
|
#define ABOVEWATER(x, z) (HEIGHT_SCALE * heightmap[z*SideSize + x] >= waterHeight)
|
|
|
|
#define UPDATELOOKAHEAD \
|
|
|
|
for (; lookahead <= id2+maxLevel && lookahead < SideSize && \
|
2019-09-07 10:50:55 +02:00
|
|
|
((!Transpose && !ABOVEWATER(lookahead, id1)) || (Transpose && !ABOVEWATER(id1, lookahead))); ++lookahead)
|
2019-01-02 16:23:02 +01:00
|
|
|
// Algorithm:
|
|
|
|
// We want to know the distance to the closest shore point. Go through each line/column,
|
|
|
|
// keep track of when we encountered the last shore point and how far ahead the next one is.
|
|
|
|
for (size_t id1 = 0; id1 < SideSize; ++id1)
|
|
|
|
{
|
|
|
|
size_t id2 = 0;
|
|
|
|
const size_t& x = Transpose ? id1 : id2;
|
|
|
|
const size_t& z = Transpose ? id2 : id1;
|
|
|
|
|
|
|
|
size_t level = ABOVEWATER(x, z) ? 0 : maxLevel;
|
|
|
|
size_t lookahead = (size_t)(level > 0);
|
|
|
|
|
|
|
|
UPDATELOOKAHEAD;
|
|
|
|
|
|
|
|
// start moving
|
|
|
|
for (; id2 < SideSize; ++id2)
|
|
|
|
{
|
|
|
|
// update current level
|
|
|
|
if (ABOVEWATER(x, z))
|
|
|
|
level = 0;
|
|
|
|
else
|
|
|
|
level = std::min(level+1, maxLevel);
|
|
|
|
|
|
|
|
// move lookahead
|
|
|
|
if (lookahead == id2)
|
|
|
|
++lookahead;
|
|
|
|
UPDATELOOKAHEAD;
|
|
|
|
|
|
|
|
// This is the important bit: set the distance to either:
|
|
|
|
// - the distance to the previous shore point (level)
|
|
|
|
// - the distance to the next shore point (lookahead-id2)
|
|
|
|
distanceMap[z*SideSize + x] = std::min(distanceMap[z*SideSize + x], (float)std::min(lookahead-id2, level));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef ABOVEWATER
|
|
|
|
#undef UPDATELOOKAHEAD
|
|
|
|
}
|
|
|
|
|
2014-07-01 18:05:05 +02:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Calculate our binary heightmap from the terrain heightmap.
|
|
|
|
void WaterManager::RecomputeDistanceHeightmap()
|
|
|
|
{
|
2017-07-01 06:15:49 +02:00
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
if (!terrain || !terrain->GetHeightMap())
|
|
|
|
return;
|
|
|
|
|
2019-01-02 16:23:02 +01:00
|
|
|
size_t SideSize = m_MapSize;
|
|
|
|
|
|
|
|
// we want to look ahead some distance, but not too much (less efficient and not interesting). This is our lookahead.
|
|
|
|
const size_t maxLevel = 5;
|
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
if (!m_DistanceHeightmap)
|
2019-01-02 16:23:02 +01:00
|
|
|
{
|
2022-01-10 17:51:43 +01:00
|
|
|
m_DistanceHeightmap = std::make_unique<float[]>(SideSize * SideSize);
|
|
|
|
std::fill(m_DistanceHeightmap.get(), m_DistanceHeightmap.get() + SideSize * SideSize, static_cast<float>(maxLevel));
|
2019-01-02 16:23:02 +01:00
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Create a manhattan-distance heightmap.
|
|
|
|
// This could be refined to only be done near the coast itself, but it's probably not necessary.
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2019-01-02 16:23:02 +01:00
|
|
|
u16* heightmap = terrain->GetHeightMap();
|
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
ComputeDirection<false>(m_DistanceHeightmap.get(), heightmap, m_WaterHeight, SideSize, maxLevel);
|
|
|
|
ComputeDirection<true>(m_DistanceHeightmap.get(), heightmap, m_WaterHeight, SideSize, maxLevel);
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// This requires m_DistanceHeightmap to be defined properly.
|
|
|
|
void WaterManager::CreateWaveMeshes()
|
|
|
|
{
|
2019-01-02 16:23:02 +01:00
|
|
|
if (m_MapSize == 0)
|
|
|
|
return;
|
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
2017-07-01 06:15:49 +02:00
|
|
|
if (!terrain || !terrain->GetHeightMap())
|
|
|
|
return;
|
2014-07-28 12:14:00 +02:00
|
|
|
|
|
|
|
m_ShoreWaves.clear();
|
2022-01-10 17:51:43 +01:00
|
|
|
m_ShoreWavesVBIndices.Reset();
|
2014-09-08 01:49:24 +02:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
if (m_Waviness < 5.0f && m_WaterType != L"ocean")
|
|
|
|
return;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2019-01-02 16:23:02 +01:00
|
|
|
size_t SideSize = m_MapSize;
|
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// First step: get the points near the coast.
|
|
|
|
std::set<int> CoastalPointsSet;
|
2014-08-06 17:24:38 +02:00
|
|
|
for (size_t z = 1; z < SideSize-1; ++z)
|
|
|
|
for (size_t x = 1; x < SideSize-1; ++x)
|
2019-01-02 16:23:02 +01:00
|
|
|
// get the points not on the shore but near it, ocean-side
|
|
|
|
if (m_DistanceHeightmap[z*m_MapSize + x] > 0.5f && m_DistanceHeightmap[z*m_MapSize + x] < 1.5f)
|
|
|
|
CoastalPointsSet.insert((z)*SideSize + x);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Second step: create chains out of those coastal points.
|
|
|
|
static const int around[8][2] = { { -1,-1 }, { -1,0 }, { -1,1 }, { 0,1 }, { 1,1 }, { 1,0 }, { 1,-1 }, { 0,-1 } };
|
|
|
|
|
2014-09-08 01:49:24 +02:00
|
|
|
std::vector<std::deque<CoastalPoint> > CoastalPointsChains;
|
2014-07-28 12:14:00 +02:00
|
|
|
while (!CoastalPointsSet.empty())
|
|
|
|
{
|
|
|
|
int index = *(CoastalPointsSet.begin());
|
|
|
|
int x = index % SideSize;
|
|
|
|
int y = (index - x ) / SideSize;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
std::deque<CoastalPoint> Chain;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2019-01-02 16:23:02 +01:00
|
|
|
Chain.push_front(CoastalPoint(index,CVector2D(x*4,y*4)));
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Erase us.
|
|
|
|
CoastalPointsSet.erase(CoastalPointsSet.begin());
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// We're our starter points. At most we can have 2 points close to us.
|
|
|
|
// We'll pick the first one and look for its neighbors (he can only have one new)
|
|
|
|
// Up until we either reach the end of the chain, or ourselves.
|
|
|
|
// Then go down the other direction if there is any.
|
|
|
|
int neighbours[2] = { -1, -1 };
|
|
|
|
int nbNeighb = 0;
|
|
|
|
for (int i = 0; i < 8; ++i)
|
|
|
|
{
|
|
|
|
if (CoastalPointsSet.count(x + around[i][0] + (y + around[i][1])*SideSize))
|
|
|
|
{
|
|
|
|
if (nbNeighb < 2)
|
|
|
|
neighbours[nbNeighb] = x + around[i][0] + (y + around[i][1])*SideSize;
|
|
|
|
++nbNeighb;
|
2014-07-01 18:05:05 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-28 12:14:00 +02:00
|
|
|
if (nbNeighb > 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i)
|
2014-07-01 18:05:05 +02:00
|
|
|
{
|
2014-07-28 12:14:00 +02:00
|
|
|
if (neighbours[i] == -1)
|
|
|
|
continue;
|
|
|
|
// Move to our neighboring point
|
|
|
|
int xx = neighbours[i] % SideSize;
|
|
|
|
int yy = (neighbours[i] - xx ) / SideSize;
|
|
|
|
int indexx = xx + yy*SideSize;
|
|
|
|
int endedChain = false;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
if (i == 0)
|
2019-01-02 16:23:02 +01:00
|
|
|
Chain.push_back(CoastalPoint(indexx,CVector2D(xx*4,yy*4)));
|
2014-07-01 18:05:05 +02:00
|
|
|
else
|
2019-01-02 16:23:02 +01:00
|
|
|
Chain.push_front(CoastalPoint(indexx,CVector2D(xx*4,yy*4)));
|
2014-07-28 12:14:00 +02:00
|
|
|
|
|
|
|
// If there's a loop we'll be the "other" neighboring point already so check for that.
|
|
|
|
// We'll readd at the end/front the other one to have full squares.
|
|
|
|
if (CoastalPointsSet.count(indexx) == 0)
|
|
|
|
break;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
CoastalPointsSet.erase(indexx);
|
|
|
|
|
|
|
|
// Start checking from there.
|
|
|
|
while(!endedChain)
|
|
|
|
{
|
|
|
|
bool found = false;
|
|
|
|
nbNeighb = 0;
|
|
|
|
for (int p = 0; p < 8; ++p)
|
|
|
|
{
|
|
|
|
if (CoastalPointsSet.count(xx+around[p][0] + (yy + around[p][1])*SideSize))
|
|
|
|
{
|
|
|
|
if (nbNeighb >= 2)
|
|
|
|
{
|
|
|
|
CoastalPointsSet.erase(xx + yy*SideSize);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++nbNeighb;
|
|
|
|
// We've found a new point around us.
|
|
|
|
// Move there
|
|
|
|
xx = xx + around[p][0];
|
|
|
|
yy = yy + around[p][1];
|
|
|
|
indexx = xx + yy*SideSize;
|
|
|
|
if (i == 0)
|
2019-01-02 16:23:02 +01:00
|
|
|
Chain.push_back(CoastalPoint(indexx,CVector2D(xx*4,yy*4)));
|
2014-07-28 12:14:00 +02:00
|
|
|
else
|
2019-01-02 16:23:02 +01:00
|
|
|
Chain.push_front(CoastalPoint(indexx,CVector2D(xx*4,yy*4)));
|
2014-07-28 12:14:00 +02:00
|
|
|
CoastalPointsSet.erase(xx + yy*SideSize);
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
endedChain = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Chain.size() > 10)
|
|
|
|
CoastalPointsChains.push_back(Chain);
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// (optional) third step: Smooth chains out.
|
|
|
|
// This is also really dumb.
|
|
|
|
for (size_t i = 0; i < CoastalPointsChains.size(); ++i)
|
|
|
|
{
|
|
|
|
// Bump 1 for smoother.
|
|
|
|
for (int p = 0; p < 3; ++p)
|
|
|
|
{
|
|
|
|
for (size_t j = 1; j < CoastalPointsChains[i].size()-1; ++j)
|
2014-07-01 18:05:05 +02:00
|
|
|
{
|
2014-07-28 12:14:00 +02:00
|
|
|
CVector2D realPos = CoastalPointsChains[i][j-1].position + CoastalPointsChains[i][j+1].position;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
CoastalPointsChains[i][j].position = (CoastalPointsChains[i][j].position + realPos/2.0f)/2.0f;
|
2014-07-01 18:05:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Fourth step: create waves themselves, using those chains. We basically create subchains.
|
2022-06-06 09:30:45 +02:00
|
|
|
u16 waveSizes = 14; // maximal size in width.
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// Construct indices buffer (we can afford one for all of them)
|
2022-06-06 09:30:45 +02:00
|
|
|
std::vector<u16> water_indices;
|
|
|
|
for (u16 a = 0; a < waveSizes - 1; ++a)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
2022-06-06 09:30:45 +02:00
|
|
|
for (u16 rect = 0; rect < 7; ++rect)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
2020-11-26 23:28:50 +01:00
|
|
|
water_indices.push_back(a * 9 + rect);
|
|
|
|
water_indices.push_back(a * 9 + 9 + rect);
|
|
|
|
water_indices.push_back(a * 9 + 1 + rect);
|
|
|
|
water_indices.push_back(a * 9 + 9 + rect);
|
|
|
|
water_indices.push_back(a * 9 + 10 + rect);
|
|
|
|
water_indices.push_back(a * 9 + 1 + rect);
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generic indexes, max-length
|
2022-01-10 17:51:43 +01:00
|
|
|
m_ShoreWavesVBIndices = g_VBMan.AllocateChunk(
|
2022-06-06 09:30:45 +02:00
|
|
|
sizeof(u16), water_indices.size(),
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IBuffer::Type::INDEX, false,
|
2022-01-10 17:51:43 +01:00
|
|
|
nullptr, CVertexBufferManager::Group::WATER);
|
|
|
|
m_ShoreWavesVBIndices->m_Owner->UpdateChunkVertices(m_ShoreWavesVBIndices.Get(), &water_indices[0]);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
float diff = (rand() % 50) / 5.0f;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
std::vector<SWavesVertex> vertices, reversed;
|
2014-07-28 12:14:00 +02:00
|
|
|
for (size_t i = 0; i < CoastalPointsChains.size(); ++i)
|
|
|
|
{
|
|
|
|
for (size_t j = 0; j < CoastalPointsChains[i].size()-waveSizes; ++j)
|
|
|
|
{
|
|
|
|
if (CoastalPointsChains[i].size()- 1 - j < waveSizes)
|
|
|
|
break;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-06-06 09:30:45 +02:00
|
|
|
u16 width = waveSizes;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
// First pass to get some parameters out.
|
|
|
|
float outmost = 0.0f; // how far to move on the shore.
|
|
|
|
float avgDepth = 0.0f;
|
|
|
|
int sign = 1;
|
2014-08-06 17:24:38 +02:00
|
|
|
CVector2D firstPerp(0,0), perp(0,0), lastPerp(0,0);
|
2022-06-06 09:30:45 +02:00
|
|
|
for (u16 a = 0; a < waveSizes;++a)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
|
|
|
lastPerp = perp;
|
|
|
|
perp = CVector2D(0,0);
|
|
|
|
int nb = 0;
|
|
|
|
CVector2D pos = CoastalPointsChains[i][j+a].position;
|
|
|
|
CVector2D posPlus;
|
|
|
|
CVector2D posMinus;
|
|
|
|
if (a > 0)
|
|
|
|
{
|
|
|
|
++nb;
|
|
|
|
posMinus = CoastalPointsChains[i][j+a-1].position;
|
|
|
|
perp += pos-posMinus;
|
|
|
|
}
|
|
|
|
if (a < waveSizes-1)
|
|
|
|
{
|
|
|
|
++nb;
|
|
|
|
posPlus = CoastalPointsChains[i][j+a+1].position;
|
|
|
|
perp += posPlus-pos;
|
|
|
|
}
|
|
|
|
perp /= nb;
|
|
|
|
perp = CVector2D(-perp.Y,perp.X).Normalized();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
if (a == 0)
|
|
|
|
firstPerp = perp;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
if ( a > 1 && perp.Dot(lastPerp) < 0.90f && perp.Dot(firstPerp) < 0.70f)
|
|
|
|
{
|
|
|
|
width = a+1;
|
|
|
|
break;
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
if (terrain->GetExactGroundLevel(pos.X+perp.X*1.5f, pos.Y+perp.Y*1.5f) > m_WaterHeight)
|
|
|
|
sign = -1;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
avgDepth += terrain->GetExactGroundLevel(pos.X+sign*perp.X*20.0f, pos.Y+sign*perp.Y*20.0f) - m_WaterHeight;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
float localOutmost = -2.0f;
|
|
|
|
while (localOutmost < 0.0f)
|
|
|
|
{
|
|
|
|
float depth = terrain->GetExactGroundLevel(pos.X+sign*perp.X*localOutmost, pos.Y+sign*perp.Y*localOutmost) - m_WaterHeight;
|
|
|
|
if (depth < 0.0f || depth > 0.6f)
|
|
|
|
localOutmost += 0.2f;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
outmost += localOutmost;
|
|
|
|
}
|
|
|
|
if (width < 5)
|
|
|
|
{
|
|
|
|
j += 6;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
outmost /= width;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
if (outmost > -0.5f)
|
|
|
|
{
|
|
|
|
j += 3;
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-02 16:23:02 +01:00
|
|
|
outmost = -2.5f + outmost * m_Waviness/10.0f;
|
2014-07-28 12:14:00 +02:00
|
|
|
|
|
|
|
avgDepth /= width;
|
|
|
|
|
|
|
|
if (avgDepth > -1.3f)
|
|
|
|
{
|
|
|
|
j += 3;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// we passed the checks, we can create a wave of size "width".
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
std::unique_ptr<WaveObject> shoreWave = std::make_unique<WaveObject>();
|
|
|
|
vertices.clear();
|
|
|
|
vertices.reserve(9 * width);
|
2020-11-11 19:46:52 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
shoreWave->m_Width = width;
|
|
|
|
shoreWave->m_TimeDiff = diff;
|
|
|
|
diff += (rand() % 100) / 25.0f + 4.0f;
|
|
|
|
|
2022-06-06 09:30:45 +02:00
|
|
|
for (u16 a = 0; a < width;++a)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
2020-11-26 23:28:50 +01:00
|
|
|
perp = CVector2D(0,0);
|
2014-07-28 12:14:00 +02:00
|
|
|
int nb = 0;
|
|
|
|
CVector2D pos = CoastalPointsChains[i][j+a].position;
|
|
|
|
CVector2D posPlus;
|
|
|
|
CVector2D posMinus;
|
|
|
|
if (a > 0)
|
|
|
|
{
|
|
|
|
++nb;
|
|
|
|
posMinus = CoastalPointsChains[i][j+a-1].position;
|
|
|
|
perp += pos-posMinus;
|
|
|
|
}
|
|
|
|
if (a < waveSizes-1)
|
|
|
|
{
|
|
|
|
++nb;
|
|
|
|
posPlus = CoastalPointsChains[i][j+a+1].position;
|
|
|
|
perp += posPlus-pos;
|
|
|
|
}
|
|
|
|
perp /= nb;
|
|
|
|
perp = CVector2D(-perp.Y,perp.X).Normalized();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
SWavesVertex point[9];
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
float baseHeight = 0.04f;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
float halfWidth = (width-1.0f)/2.0f;
|
2019-09-18 16:44:31 +02:00
|
|
|
float sideNess = sqrtf(Clamp( (halfWidth - fabsf(a - halfWidth)) / 3.0f, 0.0f, 1.0f));
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
point[0].m_UV[0] = a; point[0].m_UV[1] = 8;
|
|
|
|
point[1].m_UV[0] = a; point[1].m_UV[1] = 7;
|
|
|
|
point[2].m_UV[0] = a; point[2].m_UV[1] = 6;
|
|
|
|
point[3].m_UV[0] = a; point[3].m_UV[1] = 5;
|
|
|
|
point[4].m_UV[0] = a; point[4].m_UV[1] = 4;
|
|
|
|
point[5].m_UV[0] = a; point[5].m_UV[1] = 3;
|
|
|
|
point[6].m_UV[0] = a; point[6].m_UV[1] = 2;
|
|
|
|
point[7].m_UV[0] = a; point[7].m_UV[1] = 1;
|
|
|
|
point[8].m_UV[0] = a; point[8].m_UV[1] = 0;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
point[0].m_PerpVect = perp;
|
|
|
|
point[1].m_PerpVect = perp;
|
|
|
|
point[2].m_PerpVect = perp;
|
|
|
|
point[3].m_PerpVect = perp;
|
|
|
|
point[4].m_PerpVect = perp;
|
|
|
|
point[5].m_PerpVect = perp;
|
|
|
|
point[6].m_PerpVect = perp;
|
|
|
|
point[7].m_PerpVect = perp;
|
|
|
|
point[8].m_PerpVect = perp;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
static const float perpT1[9] = { 6.0f, 6.05f, 6.1f, 6.2f, 6.3f, 6.4f, 6.5f, 6.6f, 9.7f };
|
|
|
|
static const float perpT2[9] = { 2.0f, 2.1f, 2.2f, 2.3f, 2.4f, 3.0f, 3.3f, 3.6f, 9.5f };
|
|
|
|
static const float perpT3[9] = { 1.1f, 0.7f, -0.2f, 0.0f, 0.6f, 1.3f, 2.2f, 3.6f, 9.0f };
|
|
|
|
static const float perpT4[9] = { 2.0f, 2.1f, 1.2f, 1.5f, 1.7f, 1.9f, 2.7f, 3.8f, 9.0f };
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
static const float heightT1[9] = { 0.0f, 0.2f, 0.5f, 0.8f, 0.9f, 0.85f, 0.6f, 0.2f, 0.0 };
|
|
|
|
static const float heightT2[9] = { -0.8f, -0.4f, 0.0f, 0.1f, 0.1f, 0.03f, 0.0f, 0.0f, 0.0 };
|
|
|
|
static const float heightT3[9] = { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0 };
|
|
|
|
|
|
|
|
for (size_t t = 0; t < 9; ++t)
|
|
|
|
{
|
|
|
|
float terrHeight = 0.05f + terrain->GetExactGroundLevel(pos.X+sign*perp.X*(perpT1[t]+outmost),
|
|
|
|
pos.Y+sign*perp.Y*(perpT1[t]+outmost));
|
|
|
|
point[t].m_BasePosition = CVector3D(pos.X+sign*perp.X*(perpT1[t]+outmost), baseHeight + heightT1[t]*sideNess + std::max(m_WaterHeight,terrHeight),
|
|
|
|
pos.Y+sign*perp.Y*(perpT1[t]+outmost));
|
|
|
|
}
|
|
|
|
for (size_t t = 0; t < 9; ++t)
|
|
|
|
{
|
|
|
|
float terrHeight = 0.05f + terrain->GetExactGroundLevel(pos.X+sign*perp.X*(perpT2[t]+outmost),
|
|
|
|
pos.Y+sign*perp.Y*(perpT2[t]+outmost));
|
|
|
|
point[t].m_ApexPosition = CVector3D(pos.X+sign*perp.X*(perpT2[t]+outmost), baseHeight + heightT1[t]*sideNess + std::max(m_WaterHeight,terrHeight),
|
|
|
|
pos.Y+sign*perp.Y*(perpT2[t]+outmost));
|
|
|
|
}
|
|
|
|
for (size_t t = 0; t < 9; ++t)
|
|
|
|
{
|
|
|
|
float terrHeight = 0.05f + terrain->GetExactGroundLevel(pos.X+sign*perp.X*(perpT3[t]+outmost*sideNess),
|
|
|
|
pos.Y+sign*perp.Y*(perpT3[t]+outmost*sideNess));
|
|
|
|
point[t].m_SplashPosition = CVector3D(pos.X+sign*perp.X*(perpT3[t]+outmost*sideNess), baseHeight + heightT2[t]*sideNess + std::max(m_WaterHeight,terrHeight), pos.Y+sign*perp.Y*(perpT3[t]+outmost*sideNess));
|
|
|
|
}
|
|
|
|
for (size_t t = 0; t < 9; ++t)
|
|
|
|
{
|
|
|
|
float terrHeight = 0.05f + terrain->GetExactGroundLevel(pos.X+sign*perp.X*(perpT4[t]+outmost),
|
|
|
|
pos.Y+sign*perp.Y*(perpT4[t]+outmost));
|
|
|
|
point[t].m_RetreatPosition = CVector3D(pos.X+sign*perp.X*(perpT4[t]+outmost), baseHeight + heightT3[t]*sideNess + std::max(m_WaterHeight,terrHeight),
|
|
|
|
pos.Y+sign*perp.Y*(perpT4[t]+outmost));
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
vertices.push_back(point[8]);
|
|
|
|
vertices.push_back(point[7]);
|
|
|
|
vertices.push_back(point[6]);
|
|
|
|
vertices.push_back(point[5]);
|
|
|
|
vertices.push_back(point[4]);
|
|
|
|
vertices.push_back(point[3]);
|
|
|
|
vertices.push_back(point[2]);
|
|
|
|
vertices.push_back(point[1]);
|
|
|
|
vertices.push_back(point[0]);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-28 12:14:00 +02:00
|
|
|
shoreWave->m_AABB += point[8].m_SplashPosition;
|
|
|
|
shoreWave->m_AABB += point[8].m_BasePosition;
|
|
|
|
shoreWave->m_AABB += point[0].m_SplashPosition;
|
|
|
|
shoreWave->m_AABB += point[0].m_BasePosition;
|
|
|
|
shoreWave->m_AABB += point[4].m_ApexPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sign == 1)
|
|
|
|
{
|
|
|
|
// Let's do some fancy reversing.
|
2022-01-10 17:51:43 +01:00
|
|
|
reversed.clear();
|
2019-01-02 16:23:02 +01:00
|
|
|
reversed.reserve(vertices.size());
|
2022-01-10 17:51:43 +01:00
|
|
|
for (int a = width - 1; a >= 0; --a)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
|
|
|
for (size_t t = 0; t < 9; ++t)
|
2022-01-10 17:51:43 +01:00
|
|
|
reversed.push_back(vertices[a * 9 + t]);
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
2022-01-10 17:51:43 +01:00
|
|
|
std::swap(vertices, reversed);
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
|
|
|
j += width/2-1;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
shoreWave->m_VBVertices = g_VBMan.AllocateChunk(
|
|
|
|
sizeof(SWavesVertex), vertices.size(),
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IBuffer::Type::VERTEX, false,
|
2022-01-10 17:51:43 +01:00
|
|
|
nullptr, CVertexBufferManager::Group::WATER);
|
|
|
|
shoreWave->m_VBVertices->m_Owner->UpdateChunkVertices(shoreWave->m_VBVertices.Get(), &vertices[0]);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
m_ShoreWaves.emplace_back(std::move(shoreWave));
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 18:28:47 +01:00
|
|
|
void WaterManager::RenderWaves(
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
|
2022-01-19 18:28:47 +01:00
|
|
|
const CFrustum& frustrum)
|
2014-07-28 12:14:00 +02:00
|
|
|
{
|
2022-02-18 23:38:45 +01:00
|
|
|
GPU_SCOPED_LABEL(deviceCommandContext, "Render Waves");
|
2022-01-03 13:16:16 +01:00
|
|
|
if (!m_WaterFancyEffects)
|
2014-07-28 12:14:00 +02:00
|
|
|
return;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
Renderer::Backend::MakeDefaultGraphicsPipelineStateDesc());
|
2022-02-05 17:59:23 +01:00
|
|
|
deviceCommandContext->SetFramebuffer(m_FancyEffectsFramebuffer.get());
|
|
|
|
deviceCommandContext->ClearFramebuffer();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2021-11-27 16:01:14 +01:00
|
|
|
CShaderTechniquePtr tech = g_Renderer.GetShaderManager().LoadEffect(str_water_waves);
|
2022-01-19 18:28:47 +01:00
|
|
|
deviceCommandContext->SetGraphicsPipelineState(
|
|
|
|
tech->GetGraphicsPipelineStateDesc());
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->BeginPass();
|
2022-05-02 22:57:22 +02:00
|
|
|
Renderer::Backend::IShaderProgram* shader = tech->GetShader();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-02-13 20:30:28 +01:00
|
|
|
m_WaveTex->UploadBackendTextureIfNeeded(deviceCommandContext);
|
|
|
|
m_FoamTex->UploadBackendTextureIfNeeded(deviceCommandContext);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
deviceCommandContext->SetTexture(
|
|
|
|
shader->GetBindingSlot(str_waveTex), m_WaveTex->GetBackendTexture());
|
|
|
|
deviceCommandContext->SetTexture(
|
|
|
|
shader->GetBindingSlot(str_foamTex), m_FoamTex->GetBackendTexture());
|
|
|
|
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_time), static_cast<float>(m_WaterTexTimer));
|
|
|
|
const CMatrix3D transform =
|
|
|
|
g_Renderer.GetSceneRenderer().GetViewCamera().GetViewProjection();
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_transform), transform.AsFloatArray());
|
2014-07-28 12:14:00 +02:00
|
|
|
|
|
|
|
for (size_t a = 0; a < m_ShoreWaves.size(); ++a)
|
|
|
|
{
|
2015-12-28 17:27:31 +01:00
|
|
|
if (!frustrum.IsBoxVisible(m_ShoreWaves[a]->m_AABB))
|
2014-07-28 12:14:00 +02:00
|
|
|
continue;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
CVertexBuffer::VBChunk* VBchunk = m_ShoreWaves[a]->m_VBVertices.Get();
|
2022-04-23 22:11:14 +02:00
|
|
|
VBchunk->m_Owner->UploadIfNeeded(deviceCommandContext);
|
|
|
|
m_ShoreWavesVBIndices->m_Owner->UploadIfNeeded(deviceCommandContext);
|
|
|
|
|
|
|
|
const uint32_t stride = sizeof(SWavesVertex);
|
|
|
|
const uint32_t firstVertexOffset = VBchunk->m_Index * stride;
|
|
|
|
|
|
|
|
deviceCommandContext->SetVertexAttributeFormat(
|
|
|
|
Renderer::Backend::VertexAttributeStream::POSITION,
|
|
|
|
Renderer::Backend::Format::R32G32B32_SFLOAT,
|
2022-05-24 09:11:23 +02:00
|
|
|
firstVertexOffset + offsetof(SWavesVertex, m_BasePosition), stride,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0);
|
2022-04-23 22:11:14 +02:00
|
|
|
deviceCommandContext->SetVertexAttributeFormat(
|
|
|
|
Renderer::Backend::VertexAttributeStream::NORMAL,
|
|
|
|
Renderer::Backend::Format::R32G32_SFLOAT,
|
2022-05-24 09:11:23 +02:00
|
|
|
firstVertexOffset + offsetof(SWavesVertex, m_PerpVect), stride,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0);
|
2022-04-23 22:11:14 +02:00
|
|
|
deviceCommandContext->SetVertexAttributeFormat(
|
|
|
|
Renderer::Backend::VertexAttributeStream::UV0,
|
|
|
|
Renderer::Backend::Format::R8G8_UINT,
|
2022-05-24 09:11:23 +02:00
|
|
|
firstVertexOffset + offsetof(SWavesVertex, m_UV), stride,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0);
|
2022-04-23 22:11:14 +02:00
|
|
|
|
|
|
|
deviceCommandContext->SetVertexAttributeFormat(
|
|
|
|
Renderer::Backend::VertexAttributeStream::UV1,
|
|
|
|
Renderer::Backend::Format::R32G32B32_SFLOAT,
|
2022-05-24 09:11:23 +02:00
|
|
|
firstVertexOffset + offsetof(SWavesVertex, m_ApexPosition), stride,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0);
|
2022-04-23 22:11:14 +02:00
|
|
|
deviceCommandContext->SetVertexAttributeFormat(
|
|
|
|
Renderer::Backend::VertexAttributeStream::UV2,
|
|
|
|
Renderer::Backend::Format::R32G32B32_SFLOAT,
|
2022-05-24 09:11:23 +02:00
|
|
|
firstVertexOffset + offsetof(SWavesVertex, m_SplashPosition), stride,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0);
|
2022-04-23 22:11:14 +02:00
|
|
|
deviceCommandContext->SetVertexAttributeFormat(
|
|
|
|
Renderer::Backend::VertexAttributeStream::UV3,
|
|
|
|
Renderer::Backend::Format::R32G32B32_SFLOAT,
|
2022-05-24 09:11:23 +02:00
|
|
|
firstVertexOffset + offsetof(SWavesVertex, m_RetreatPosition), stride,
|
|
|
|
Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_translation), m_ShoreWaves[a]->m_TimeDiff);
|
|
|
|
deviceCommandContext->SetUniform(
|
|
|
|
shader->GetBindingSlot(str_width), static_cast<float>(m_ShoreWaves[a]->m_Width));
|
2014-07-28 12:14:00 +02:00
|
|
|
|
2022-04-23 22:11:14 +02:00
|
|
|
deviceCommandContext->SetVertexBuffer(0, VBchunk->m_Owner->GetBuffer());
|
2022-03-03 08:09:59 +01:00
|
|
|
deviceCommandContext->SetIndexBuffer(m_ShoreWavesVBIndices->m_Owner->GetBuffer());
|
2022-04-23 22:11:14 +02:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
const uint32_t indexCount = (m_ShoreWaves[a]->m_Width - 1) * (7 * 6);
|
|
|
|
deviceCommandContext->DrawIndexed(m_ShoreWavesVBIndices->m_Index, indexCount, 0);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-05-02 22:57:22 +02:00
|
|
|
g_Renderer.GetStats().m_DrawCalls++;
|
|
|
|
g_Renderer.GetStats().m_WaterTris += indexCount / 3;
|
2014-07-28 12:14:00 +02:00
|
|
|
}
|
2022-03-14 23:16:14 +01:00
|
|
|
deviceCommandContext->EndPass();
|
2022-02-05 17:59:23 +01:00
|
|
|
deviceCommandContext->SetFramebuffer(
|
|
|
|
deviceCommandContext->GetDevice()->GetCurrentBackbuffer());
|
2014-07-01 18:05:05 +02:00
|
|
|
}
|
|
|
|
|
2017-07-01 06:15:49 +02:00
|
|
|
void WaterManager::RecomputeWaterData()
|
|
|
|
{
|
|
|
|
if (!m_MapSize)
|
|
|
|
return;
|
|
|
|
|
|
|
|
RecomputeDistanceHeightmap();
|
|
|
|
RecomputeWindStrength();
|
|
|
|
CreateWaveMeshes();
|
|
|
|
}
|
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
///////////////////////////////////////////////////////////////////
|
2014-07-03 22:07:15 +02:00
|
|
|
// Calculate the strength of the wind at a given point on the map.
|
|
|
|
void WaterManager::RecomputeWindStrength()
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2019-08-15 13:56:00 +02:00
|
|
|
if (m_MapSize <= 0)
|
|
|
|
return;
|
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
if (!m_WindStrength)
|
|
|
|
m_WindStrength = std::make_unique<float[]>(m_MapSize * m_MapSize);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
2017-07-01 06:15:49 +02:00
|
|
|
if (!terrain || !terrain->GetHeightMap())
|
|
|
|
return;
|
|
|
|
|
2021-02-27 11:08:00 +01:00
|
|
|
CVector2D windDir = CVector2D(cos(m_WindAngle), sin(m_WindAngle));
|
2014-07-05 12:20:30 +02:00
|
|
|
|
2021-02-27 11:08:00 +01:00
|
|
|
int stepSize = 10;
|
|
|
|
ssize_t windX = -round(stepSize * windDir.X);
|
|
|
|
ssize_t windY = -round(stepSize * windDir.Y);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2019-08-15 13:56:00 +02:00
|
|
|
struct SWindPoint {
|
|
|
|
SWindPoint(size_t x, size_t y, float strength) : X(x), Y(y), windStrength(strength) {}
|
2019-08-15 19:02:53 +02:00
|
|
|
ssize_t X;
|
|
|
|
ssize_t Y;
|
2019-08-15 13:56:00 +02:00
|
|
|
float windStrength;
|
|
|
|
};
|
2014-07-05 12:20:30 +02:00
|
|
|
|
2019-08-15 13:56:00 +02:00
|
|
|
std::vector<SWindPoint> startingPoints;
|
|
|
|
std::vector<std::pair<int, int>> movement; // Every increment, move each starting point by all of these.
|
|
|
|
|
|
|
|
// Compute starting points (one or two edges of the map) and how much to move each computation increment.
|
|
|
|
if (fabs(windDir.X) < 0.01f)
|
|
|
|
{
|
2021-02-27 11:08:00 +01:00
|
|
|
movement.emplace_back(0, windY > 0.f ? 1 : -1);
|
2019-08-15 13:56:00 +02:00
|
|
|
startingPoints.reserve(m_MapSize);
|
|
|
|
size_t start = windY > 0 ? 0 : m_MapSize - 1;
|
|
|
|
for (size_t x = 0; x < m_MapSize; ++x)
|
|
|
|
startingPoints.emplace_back(x, start, 0.f);
|
|
|
|
}
|
|
|
|
else if (fabs(windDir.Y) < 0.01f)
|
|
|
|
{
|
2021-02-27 11:08:00 +01:00
|
|
|
movement.emplace_back(windX > 0.f ? 1 : - 1, 0);
|
|
|
|
startingPoints.reserve(m_MapSize);
|
2019-08-15 13:56:00 +02:00
|
|
|
size_t start = windX > 0 ? 0 : m_MapSize - 1;
|
|
|
|
for (size_t z = 0; z < m_MapSize; ++z)
|
|
|
|
startingPoints.emplace_back(start, z, 0.f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
startingPoints.reserve(m_MapSize * 2);
|
|
|
|
// Points along X.
|
|
|
|
size_t start = windY > 0 ? 0 : m_MapSize - 1;
|
|
|
|
for (size_t x = 0; x < m_MapSize; ++x)
|
|
|
|
startingPoints.emplace_back(x, start, 0.f);
|
|
|
|
// Points along Z, avoid repeating the corner point.
|
|
|
|
start = windX > 0 ? 0 : m_MapSize - 1;
|
|
|
|
if (windY > 0)
|
|
|
|
for (size_t z = 1; z < m_MapSize; ++z)
|
|
|
|
startingPoints.emplace_back(start, z, 0.f);
|
|
|
|
else
|
|
|
|
for (size_t z = 0; z < m_MapSize-1; ++z)
|
|
|
|
startingPoints.emplace_back(start, z, 0.f);
|
|
|
|
|
|
|
|
// Compute movement array.
|
|
|
|
movement.reserve(std::max(std::abs(windX),std::abs(windY)));
|
|
|
|
while (windX != 0 || windY != 0)
|
2013-08-24 16:12:39 +02:00
|
|
|
{
|
2019-08-15 13:56:00 +02:00
|
|
|
std::pair<ssize_t, ssize_t> move = {
|
|
|
|
windX == 0 ? 0 : windX > 0 ? +1 : -1,
|
|
|
|
windY == 0 ? 0 : windY > 0 ? +1 : -1
|
|
|
|
};
|
|
|
|
windX -= move.first;
|
|
|
|
windY -= move.second;
|
|
|
|
movement.push_back(move);
|
|
|
|
}
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2019-08-15 13:56:00 +02:00
|
|
|
// We have all starting points ready, move them all until the map is covered.
|
|
|
|
for (SWindPoint& point : startingPoints)
|
|
|
|
{
|
|
|
|
// Starting velocity is 1.0 unless in shallow water.
|
|
|
|
m_WindStrength[point.Y * m_MapSize + point.X] = 1.f;
|
|
|
|
float depth = m_WaterHeight - terrain->GetVertexGroundLevel(point.X, point.Y);
|
|
|
|
if (depth > 0.f && depth < 2.f)
|
|
|
|
m_WindStrength[point.Y * m_MapSize + point.X] = depth / 2.f;
|
|
|
|
point.windStrength = m_WindStrength[point.Y * m_MapSize + point.X];
|
|
|
|
|
|
|
|
bool onMap = true;
|
|
|
|
while (onMap)
|
|
|
|
for (size_t step = 0; step < movement.size(); ++step)
|
|
|
|
{
|
|
|
|
// Move wind speed towards the mean.
|
|
|
|
point.windStrength = 0.15f + point.windStrength * 0.85f;
|
|
|
|
|
|
|
|
// Adjust speed based on height difference, a positive height difference slowly increases speed (simulate venturi effect)
|
|
|
|
// and a lower height reduces speed (wind protection from hills/...)
|
|
|
|
float heightDiff = std::max(m_WaterHeight, terrain->GetVertexGroundLevel(point.X + movement[step].first, point.Y + movement[step].second)) -
|
2021-02-27 11:08:00 +01:00
|
|
|
std::max(m_WaterHeight, terrain->GetVertexGroundLevel(point.X, point.Y));
|
2019-08-15 13:56:00 +02:00
|
|
|
if (heightDiff > 0.f)
|
|
|
|
point.windStrength = std::min(2.f, point.windStrength + std::min(4.f, heightDiff) / 40.f);
|
|
|
|
else
|
|
|
|
point.windStrength = std::max(0.f, point.windStrength + std::max(-4.f, heightDiff) / 5.f);
|
|
|
|
|
|
|
|
point.X += movement[step].first;
|
|
|
|
point.Y += movement[step].second;
|
|
|
|
|
2019-08-15 19:02:53 +02:00
|
|
|
if (point.X < 0 || point.X >= static_cast<ssize_t>(m_MapSize) || point.Y < 0 || point.Y >= static_cast<ssize_t>(m_MapSize))
|
2014-07-03 22:07:15 +02:00
|
|
|
{
|
2019-08-15 13:56:00 +02:00
|
|
|
onMap = false;
|
|
|
|
break;
|
2013-04-27 14:20:42 +02:00
|
|
|
}
|
2019-08-15 13:56:00 +02:00
|
|
|
m_WindStrength[point.Y * m_MapSize + point.X] = point.windStrength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: should perhaps blur a little, or change the above code to incorporate neighboring tiles a bit.
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
|
2013-08-18 11:27:11 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
2014-07-01 18:05:05 +02:00
|
|
|
// TODO: This will always recalculate for now
|
2013-08-18 11:27:11 +02:00
|
|
|
void WaterManager::SetMapSize(size_t size)
|
|
|
|
{
|
|
|
|
// TODO: Im' blindly trusting the user here.
|
|
|
|
m_MapSize = size;
|
|
|
|
m_NeedInfoUpdate = true;
|
|
|
|
m_updatei0 = 0;
|
|
|
|
m_updatei1 = size;
|
|
|
|
m_updatej0 = 0;
|
|
|
|
m_updatej1 = size;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2022-01-10 17:51:43 +01:00
|
|
|
m_DistanceHeightmap.reset();
|
|
|
|
m_WindStrength.reset();
|
2013-08-18 11:27:11 +02:00
|
|
|
}
|
|
|
|
|
2012-11-04 16:54:36 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// This will set the bools properly
|
2013-08-18 11:27:11 +02:00
|
|
|
void WaterManager::UpdateQuality()
|
2010-03-21 15:18:15 +01:00
|
|
|
{
|
2019-08-04 10:28:30 +02:00
|
|
|
if (g_RenderingOptions.GetWaterEffects() != m_WaterEffects)
|
2017-08-21 02:09:25 +02:00
|
|
|
{
|
2019-08-04 10:28:30 +02:00
|
|
|
m_WaterEffects = g_RenderingOptions.GetWaterEffects();
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2021-01-15 16:45:39 +01:00
|
|
|
if (g_RenderingOptions.GetWaterFancyEffects() != m_WaterFancyEffects)
|
|
|
|
{
|
2019-08-04 10:28:30 +02:00
|
|
|
m_WaterFancyEffects = g_RenderingOptions.GetWaterFancyEffects();
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2021-01-15 16:45:39 +01:00
|
|
|
if (g_RenderingOptions.GetWaterRealDepth() != m_WaterRealDepth)
|
|
|
|
{
|
2019-08-04 10:28:30 +02:00
|
|
|
m_WaterRealDepth = g_RenderingOptions.GetWaterRealDepth();
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2021-01-15 16:45:39 +01:00
|
|
|
if (g_RenderingOptions.GetWaterRefraction() != m_WaterRefraction)
|
|
|
|
{
|
2019-08-04 10:28:30 +02:00
|
|
|
m_WaterRefraction = g_RenderingOptions.GetWaterRefraction();
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2021-01-15 16:45:39 +01:00
|
|
|
if (g_RenderingOptions.GetWaterReflection() != m_WaterReflection)
|
|
|
|
{
|
2019-08-04 10:28:30 +02:00
|
|
|
m_WaterReflection = g_RenderingOptions.GetWaterReflection();
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
}
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2021-11-26 22:47:04 +01:00
|
|
|
bool WaterManager::WillRenderFancyWater() const
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2021-11-26 22:47:04 +01:00
|
|
|
return
|
2021-12-14 07:34:02 +01:00
|
|
|
m_RenderWater && g_VideoMode.GetBackend() != CVideoMode::Backend::GL_ARB &&
|
2022-02-13 22:46:03 +01:00
|
|
|
g_RenderingOptions.GetWaterEffects();
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
2021-12-09 19:01:28 +01:00
|
|
|
|
|
|
|
size_t WaterManager::GetCurrentTextureIndex(const double& period) const
|
|
|
|
{
|
|
|
|
ENSURE(period > 0.0);
|
|
|
|
return static_cast<size_t>(m_WaterTexTimer * ARRAY_SIZE(m_WaterTexture) / period) % ARRAY_SIZE(m_WaterTexture);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t WaterManager::GetNextTextureIndex(const double& period) const
|
|
|
|
{
|
|
|
|
ENSURE(period > 0.0);
|
|
|
|
return (GetCurrentTextureIndex(period) + 1) % ARRAY_SIZE(m_WaterTexture);
|
|
|
|
}
|