2013-04-03 05:30:10 +02:00
|
|
|
/* Copyright (C) 2013 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Water settings (speed, height) and texture management
|
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"
|
2010-09-10 23:02:10 +02:00
|
|
|
|
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"
|
2007-12-20 21:21:45 +01:00
|
|
|
#include "lib/tex/tex.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
#include "lib/res/graphics/ogl_tex.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"
|
|
|
|
|
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/World.h"
|
2006-05-28 04:13:32 +02:00
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
#include "renderer/WaterManager.h"
|
2006-05-28 04:13:32 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
#include "simulation2/components/ICmpWaterManager.h"
|
|
|
|
#include "simulation2/components/ICmpRangeManager.h"
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WaterManager implementation
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Construction/Destruction
|
|
|
|
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;
|
|
|
|
m_WaterColor = CColor(0.3f, 0.35f, 0.7f, 1.0f);
|
2012-10-31 19:42:17 +01:00
|
|
|
m_WaterFullDepth = 5.0f;
|
|
|
|
m_WaterMaxAlpha = 1.0f;
|
2006-01-07 02:04:26 +01:00
|
|
|
m_WaterAlphaOffset = -0.05f;
|
2006-01-22 00:27:42 +01:00
|
|
|
m_SWaterTrans = 0;
|
|
|
|
m_TWaterTrans = 0;
|
|
|
|
m_SWaterSpeed = 0.0015f;
|
|
|
|
m_TWaterSpeed = 0.0015f;
|
|
|
|
m_SWaterScrollCounter = 0;
|
|
|
|
m_TWaterScrollCounter = 0;
|
|
|
|
m_WaterCurrentTex = 0;
|
2006-05-28 04:13:32 +02:00
|
|
|
m_ReflectionTexture = 0;
|
|
|
|
m_RefractionTexture = 0;
|
2007-02-10 23:07:20 +01:00
|
|
|
m_ReflectionTextureSize = 0;
|
|
|
|
m_RefractionTextureSize = 0;
|
2006-01-22 00:27:42 +01:00
|
|
|
m_WaterTexTimer = 0.0;
|
2006-09-10 07:31:41 +02:00
|
|
|
m_Shininess = 150.0f;
|
2012-10-31 19:42:17 +01:00
|
|
|
m_SpecularStrength = 0.6f;
|
2006-06-05 23:02:29 +02:00
|
|
|
m_Waviness = 8.0f;
|
2006-10-07 19:59:06 +02:00
|
|
|
m_ReflectionTint = CColor(0.28f, 0.3f, 0.59f, 1.0f);
|
2006-09-10 07:31:41 +02:00
|
|
|
m_ReflectionTintStrength = 0.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;
|
2013-04-27 14:20:42 +02:00
|
|
|
m_WaveX = NULL;
|
|
|
|
m_WaveZ = NULL;
|
|
|
|
m_DistanceToShore = NULL;
|
|
|
|
m_FoamFactor = NULL;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
|
|
|
m_WaterNormal = false;
|
|
|
|
m_WaterRealDepth = false;
|
|
|
|
m_WaterFoam = false;
|
|
|
|
m_WaterCoastalWaves = false;
|
|
|
|
m_WaterRefraction = false;
|
|
|
|
m_WaterReflection = false;
|
|
|
|
m_WaterShadows = false;
|
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
m_NeedsReloading = false;
|
2013-04-27 14:20:42 +02:00
|
|
|
m_NeedsFullReloading = true;
|
2012-11-04 16:54:36 +01:00
|
|
|
m_TerrainChangeThisTurn = false;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
|
|
|
m_VBWaves = NULL;
|
|
|
|
m_VBWavesIndices = NULL;
|
|
|
|
|
|
|
|
m_depthTT = 0;
|
|
|
|
m_waveTT = 0;
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WaterManager::~WaterManager()
|
|
|
|
{
|
|
|
|
// Cleanup if the caller messed up
|
|
|
|
UnloadWaterTextures();
|
2013-04-27 14:20:42 +02:00
|
|
|
delete[] m_WaveX;
|
|
|
|
delete[] m_WaveZ;
|
|
|
|
delete[] m_DistanceToShore;
|
|
|
|
delete[] m_FoamFactor;
|
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
glDeleteTextures(1, &m_depthTT);
|
|
|
|
glDeleteTextures(1, &m_waveTT);
|
|
|
|
|
|
|
|
if (m_VBWaves) g_VBMan.Release(m_VBWaves);
|
|
|
|
if (m_VBWavesIndices) g_VBMan.Release(m_VBWavesIndices);
|
|
|
|
|
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
|
|
|
|
|
|
|
// TODO: add a member variable and setter for this. (can't make this
|
|
|
|
// a parameter because this function is called via delay-load code)
|
2009-11-03 22:46:35 +01:00
|
|
|
static const wchar_t* const water_type = L"default";
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
wchar_t pathname[PATH_MAX];
|
2006-05-25 07:46:17 +02: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
|
|
|
{
|
2010-09-10 23:02:10 +02:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/%ls/diffuse%02d.dds", water_type, (int)i+1);
|
|
|
|
CTextureProperties textureProps(pathname);
|
|
|
|
textureProps.SetWrap(GL_REPEAT);
|
|
|
|
|
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_WaterTexture[i] = texture;
|
2006-05-25 07:46:17 +02:00
|
|
|
}
|
|
|
|
|
2006-05-28 04:13:32 +02:00
|
|
|
// Load normalmaps (for fancy water)
|
2010-09-10 23:02:10 +02:00
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(m_NormalMap); ++i)
|
2006-05-25 07:46:17 +02:00
|
|
|
{
|
2010-09-10 23:02:10 +02:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/%ls/normal%02d.dds", water_type, (int)i+1);
|
|
|
|
CTextureProperties textureProps(pathname);
|
|
|
|
textureProps.SetWrap(GL_REPEAT);
|
|
|
|
|
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_NormalMap[i] = texture;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2012-10-31 19:42:17 +01:00
|
|
|
// Load foam (for fancy water)
|
|
|
|
{
|
|
|
|
CTextureProperties textureProps("art/textures/terrain/types/water/foam.png");
|
|
|
|
textureProps.SetWrap(GL_REPEAT);
|
|
|
|
|
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_Foam = texture;
|
|
|
|
}
|
|
|
|
// Load waves (for fancy water)
|
|
|
|
{
|
|
|
|
CTextureProperties textureProps("art/textures/terrain/types/water/shore_wave.png");
|
|
|
|
textureProps.SetWrap(GL_REPEAT);
|
|
|
|
|
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_Wave = texture;
|
|
|
|
}
|
2006-05-28 04:13:32 +02:00
|
|
|
// Set the size to the largest power of 2 that is <= to the window height, so
|
2010-09-10 23:02:10 +02:00
|
|
|
// the reflection/refraction images will fit within the window
|
2006-05-28 04:13:32 +02:00
|
|
|
// (alternative: use FBO's, which can have arbitrary size - but do we need
|
|
|
|
// the reflection/refraction textures to be that large?)
|
2010-01-01 16:33:07 +01:00
|
|
|
int size = (int)round_up_to_pow2((unsigned)g_Renderer.GetHeight());
|
2006-05-28 04:13:32 +02:00
|
|
|
if(size > g_Renderer.GetHeight()) size /= 2;
|
|
|
|
m_ReflectionTextureSize = size;
|
|
|
|
m_RefractionTextureSize = size;
|
|
|
|
|
|
|
|
// Create reflection texture
|
|
|
|
glGenTextures(1, &m_ReflectionTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_ReflectionTexture);
|
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
|
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
|
|
|
(GLsizei)m_ReflectionTextureSize, (GLsizei)m_ReflectionTextureSize,
|
2006-05-28 04:13:32 +02:00
|
|
|
0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
|
|
|
// Create refraction texture
|
|
|
|
glGenTextures(1, &m_RefractionTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_RefractionTexture);
|
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
|
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
|
|
|
(GLsizei)m_RefractionTextureSize, (GLsizei)m_RefractionTextureSize,
|
2006-05-28 04:13:32 +02:00
|
|
|
0, GL_RGB, GL_UNSIGNED_BYTE, 0);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2012-10-31 19:42:17 +01:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
|
2006-05-28 04:13:32 +02:00
|
|
|
|
2007-02-10 23:07:20 +01:00
|
|
|
// Enable rendering, now that we've succeeded this far
|
|
|
|
m_RenderWater = true;
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Unload water textures
|
|
|
|
void WaterManager::UnloadWaterTextures()
|
|
|
|
{
|
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
|
|
|
for(size_t i = 0; i < ARRAY_SIZE(m_WaterTexture); i++)
|
2006-01-07 02:04:26 +01:00
|
|
|
{
|
2010-09-10 23:02:10 +02:00
|
|
|
m_WaterTexture[i].reset();
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2006-05-25 07:46:17 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
for(size_t i = 0; i < ARRAY_SIZE(m_NormalMap); i++)
|
2006-05-25 07:46:17 +02:00
|
|
|
{
|
2010-09-10 23:02:10 +02:00
|
|
|
m_NormalMap[i].reset();
|
2006-05-25 07:46:17 +02:00
|
|
|
}
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Create information about the terrain and wave vertices.
|
2013-04-27 14:20:42 +02:00
|
|
|
void WaterManager::CreateSuperfancyInfo(CSimulation2* simulation)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2012-11-02 14:08:58 +01:00
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
2013-04-27 14:20:42 +02:00
|
|
|
ssize_t mapSize = terrain->GetVerticesPerSide();
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
CmpPtr<ICmpWaterManager> cmpWaterManager(*simulation, SYSTEM_ENTITY);
|
|
|
|
if (!cmpWaterManager)
|
|
|
|
return; // REALLY shouldn't happen and will most likely crash.
|
|
|
|
|
|
|
|
// Using this to get some more optimization on circular maps
|
|
|
|
CmpPtr<ICmpRangeManager> cmpRangeManager(*simulation, SYSTEM_ENTITY);
|
|
|
|
if (!cmpRangeManager)
|
|
|
|
return;
|
|
|
|
bool circular = cmpRangeManager->GetLosCircular();
|
|
|
|
float mSize = mapSize*mapSize;
|
|
|
|
float halfSize = (mapSize/2.0);
|
|
|
|
|
|
|
|
// Warning: this won't work with multiple water planes
|
|
|
|
m_WaterHeight = cmpWaterManager->GetExactWaterLevel(0,0);
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
// TODO: change this whenever we incrementally update because it's def. not too efficient
|
|
|
|
delete[] m_WaveX;
|
|
|
|
delete[] m_WaveZ;
|
|
|
|
delete[] m_DistanceToShore;
|
|
|
|
delete[] m_FoamFactor;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
m_WaveX = new float[mapSize*mapSize];
|
|
|
|
m_WaveZ = new float[mapSize*mapSize];
|
|
|
|
m_DistanceToShore = new float[mapSize*mapSize];
|
|
|
|
m_FoamFactor = new float[mapSize*mapSize];
|
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
u16* heightmap = terrain->GetHeightMap();
|
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
// some temporary stuff for wave intensity
|
|
|
|
// not really used too much right now.
|
|
|
|
u8* waveForceHQ = new u8[mapSize*mapSize];
|
|
|
|
u16 waterHeightInu16 = m_WaterHeight/HEIGHT_SCALE;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
|
|
|
for (ssize_t i = 0; i < mapSize; ++i)
|
|
|
|
{
|
|
|
|
for (ssize_t j = 0; j < mapSize; ++j)
|
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
if (circular && (i-halfSize)*(i-halfSize)+(j-halfSize)*(j-halfSize) > mSize)
|
|
|
|
{
|
|
|
|
waveForceHQ[j*mapSize + i] = 255;
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-31 19:42:17 +01:00
|
|
|
u8 color = 0;
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int v = 0; v <= 18; v += 3){
|
2012-10-31 19:42:17 +01:00
|
|
|
if (j-v >= 0 && i-v >= 0 && heightmap[(j-v)*mapSize + i-v] > waterHeightInu16)
|
|
|
|
{
|
|
|
|
if (color == 0)
|
|
|
|
color = 5;
|
|
|
|
else
|
|
|
|
color++;
|
|
|
|
}
|
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
waveForceHQ[j*mapSize + i] = 255 - color * 40;
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
// this creates information for waves and stores it in float arrays. PatchRData then puts it in the vertex info for speed.
|
2012-10-31 19:42:17 +01:00
|
|
|
for (ssize_t i = 0; i < mapSize; ++i)
|
|
|
|
{
|
|
|
|
for (ssize_t j = 0; j < mapSize; ++j)
|
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
if (circular && (i-halfSize)*(i-halfSize)+(j-halfSize)*(j-halfSize) > mSize)
|
|
|
|
{
|
|
|
|
m_WaveX[j*mapSize + i] = 0.0f;
|
|
|
|
m_WaveZ[j*mapSize + i] = 0.0f;
|
|
|
|
m_DistanceToShore[j*mapSize + i] = 100;
|
|
|
|
m_FoamFactor[j*mapSize + i] = 0.0f;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
float depth = m_WaterHeight - heightmap[j*mapSize + i]*HEIGHT_SCALE;
|
2012-10-31 19:42:17 +01:00
|
|
|
int distanceToShore = 10000;
|
2013-04-27 14:20:42 +02:00
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
// calculation of the distance to the shore.
|
2013-04-27 14:20:42 +02:00
|
|
|
// TODO: this is fairly dumb, though it returns a good result
|
|
|
|
// Could be sped up a fair bit.
|
2012-10-31 19:42:17 +01:00
|
|
|
if (depth >= 0)
|
|
|
|
{
|
|
|
|
// check in the square around.
|
|
|
|
for (int xx = -5; xx <= 5; ++xx)
|
|
|
|
{
|
|
|
|
for (int yy = -5; yy <= 5; ++yy)
|
|
|
|
{
|
|
|
|
if (i+xx >= 0 && i + xx < mapSize)
|
|
|
|
if (j + yy >= 0 && j + yy < mapSize)
|
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
float hereDepth = m_WaterHeight - heightmap[(j+yy)*mapSize + (i+xx)]*HEIGHT_SCALE;
|
2012-10-31 19:42:17 +01:00
|
|
|
if (hereDepth < 0 && xx*xx + yy*yy < distanceToShore)
|
|
|
|
distanceToShore = xx*xx + yy*yy;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
// refine the calculation if we're close enough
|
|
|
|
if (distanceToShore < 9)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (float xx = -2.5f; xx <= 2.5f; ++xx)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (float yy = -2.5f; yy <= 2.5f; ++yy)
|
|
|
|
{
|
|
|
|
float hereDepth = m_WaterHeight - terrain->GetExactGroundLevel( (i+xx)*4, (j+yy)*4 );
|
|
|
|
if (hereDepth < 0 && xx*xx + yy*yy < distanceToShore)
|
|
|
|
distanceToShore = xx*xx + yy*yy;
|
|
|
|
}
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int xx = -2; xx <= 2; ++xx)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int yy = -2; yy <= 2; ++yy)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
float hereDepth = m_WaterHeight - terrain->GetVertexGroundLevel(i+xx, j+yy);
|
2012-10-31 19:42:17 +01:00
|
|
|
if (hereDepth > 0)
|
|
|
|
distanceToShore = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
// speedup with default values for land squares
|
|
|
|
if (distanceToShore == 10000)
|
|
|
|
{
|
|
|
|
m_WaveX[j*mapSize + i] = 0.0f;
|
|
|
|
m_WaveZ[j*mapSize + i] = 0.0f;
|
|
|
|
m_DistanceToShore[j*mapSize + i] = 100;
|
|
|
|
m_FoamFactor[j*mapSize + i] = 0.0f;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We'll compute the normals and the "water raise", to know about foam
|
|
|
|
// Normals are a pretty good calculation but it's slow since we normalize so much.
|
2012-10-31 19:42:17 +01:00
|
|
|
CVector3D normal;
|
|
|
|
int waterRaise = 0;
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int xx = -4; xx <= 4; xx += 2) // every 2 tile is good enough.
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int yy = -4; yy <= 4; yy += 2)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2012-11-01 11:17:54 +01:00
|
|
|
normal += terrain->CalcExactNormal(((float)i+xx)*4.0f,((float)j+yy)*4.0f);
|
2012-10-31 19:42:17 +01:00
|
|
|
if (terrain->GetVertexGroundLevel(i+xx,j+yy) < heightmap[j*mapSize + i]*HEIGHT_SCALE)
|
|
|
|
waterRaise += heightmap[j*mapSize + i]*HEIGHT_SCALE - terrain->GetVertexGroundLevel(i+xx,j+yy);
|
|
|
|
}
|
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
// normalizes the terrain info to avoid foam moving at too different speeds.
|
|
|
|
normal *= 0.012345679f;
|
2013-04-28 00:43:22 +02:00
|
|
|
normal[1] = 0.1f;
|
2012-10-31 19:42:17 +01:00
|
|
|
normal = normal.Normalized();
|
2013-04-27 14:20:42 +02:00
|
|
|
|
|
|
|
m_WaveX[j*mapSize + i] = normal[0];
|
|
|
|
m_WaveZ[j*mapSize + i] = normal[2];
|
|
|
|
// distance is /5.0 to be a [0,1] value.
|
|
|
|
|
2013-04-28 00:43:22 +02:00
|
|
|
m_DistanceToShore[j*mapSize + i] = sqrtf(distanceToShore)/5.0f; // TODO: this can probably be cached as I'm integer here.
|
2013-04-27 14:20:42 +02:00
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
// computing the amount of foam I want
|
2013-04-27 14:20:42 +02:00
|
|
|
|
|
|
|
depth = clamp(depth,0.0f,10.0f);
|
|
|
|
float foamAmount = (waterRaise/255.0f) * (1.0f - depth/10.0f) * (waveForceHQ[j*mapSize+i]/255.0f) * (m_Waviness/8.0f);
|
|
|
|
foamAmount += clamp(m_Waviness/2.0f - distanceToShore,0.0f,m_Waviness/2.0f)/(m_Waviness/2.0f) * clamp(m_Waviness/9.0f,0.3f,1.0f);
|
2012-11-01 11:17:54 +01:00
|
|
|
foamAmount = foamAmount > 1.0f ? 1.0f: foamAmount;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
m_FoamFactor[j*mapSize + i] = foamAmount;
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
// TODO: The rest should be cleaned up
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
// okay let's create the waves squares. i'll divide the map in arbitrary squares
|
|
|
|
// For each of these squares, check if waves are needed.
|
|
|
|
// If yes, look for the best positionning (in order to have a nice blending with the shore)
|
|
|
|
// Then clean-up: remove squares that are too close to each other
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
std::vector<CVector2D> waveSquares;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
int size = 8; // I think this is the size of the squares.
|
|
|
|
for (int i = 0; i < mapSize/size; ++i)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int j = 0; j < mapSize/size; ++j)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
|
|
|
|
int landTexel = 0;
|
|
|
|
int waterTexel = 0;
|
|
|
|
CVector3D avnormal (0.0f,0.0f,0.0f);
|
|
|
|
CVector2D landPosition(0.0f,0.0f);
|
|
|
|
CVector2D waterPosition(0.0f,0.0f);
|
|
|
|
for (int xx = 0; xx < size; ++xx)
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
for (int yy = 0; yy < size; ++yy)
|
2012-11-04 16:54:36 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
if (terrain->GetVertexGroundLevel(i*size+xx,j*size+yy) > m_WaterHeight)
|
2012-11-04 16:54:36 +01:00
|
|
|
{
|
2013-04-27 14:20:42 +02:00
|
|
|
landTexel++;
|
|
|
|
landPosition += CVector2D(i*size+xx,j*size+yy);
|
2012-11-04 16:54:36 +01:00
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
waterPosition += CVector2D(i*size+xx,j*size+yy);
|
|
|
|
waterTexel++;
|
2012-11-04 16:54:36 +01:00
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
avnormal += terrain->CalcExactNormal( (i*size+xx)*4.0f,(j*size+yy)*4.0f);
|
2012-11-04 16:54:36 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
landPosition /= landTexel;
|
|
|
|
waterPosition /= waterTexel;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
avnormal[1] = 1.0f;
|
|
|
|
avnormal.Normalize();
|
|
|
|
avnormal[1] = 0.0f;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
if (landTexel < size/2)
|
|
|
|
continue;
|
|
|
|
// this should help ensure that the shore is pretty flat.
|
|
|
|
if (avnormal.Length() <= 0.2f)
|
|
|
|
continue;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
// To get the best position for squares, I start at the mean "ocean" position
|
|
|
|
// And step by step go to the mean "land" position. I keep the position where I change from water to land.
|
|
|
|
// If this never happens, the square is scrapped.
|
|
|
|
if (terrain->GetExactGroundLevel(waterPosition.X*4.0f,waterPosition.Y*4.0f) > m_WaterHeight)
|
|
|
|
continue;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
CVector2D squarePos(-1,-1);
|
|
|
|
for (u8 i = 0; i < 40; i++)
|
|
|
|
{
|
|
|
|
squarePos = landPosition * (i/40.0f) + waterPosition * (1.0f-(i/40.0f));
|
|
|
|
if (terrain->GetExactGroundLevel(squarePos.X*4.0f,squarePos.Y*4.0f) > m_WaterHeight)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (squarePos.X == -1)
|
|
|
|
continue;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
u8 enter = 1;
|
|
|
|
// okaaaaaay. Got a square. Check for proximity.
|
|
|
|
for (unsigned long i = 0; i < waveSquares.size(); i++)
|
|
|
|
{
|
|
|
|
if ( CVector2D(waveSquares[i]-squarePos).LengthSquared() < 80) {
|
|
|
|
enter = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (enter == 1)
|
|
|
|
waveSquares.push_back(squarePos);
|
2012-11-04 16:54:36 +01:00
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Actually create the waves' meshes.
|
|
|
|
std::vector<SWavesVertex> waves_vertex_data;
|
|
|
|
std::vector<GLushort> waves_indices;
|
|
|
|
|
|
|
|
// loop through each square point. Look in the square around it, calculate the normal
|
|
|
|
// create the square.
|
|
|
|
for (unsigned long i = 0; i < waveSquares.size(); i++)
|
|
|
|
{
|
|
|
|
CVector2D pos(waveSquares[i]);
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
CVector3D avgnorm(0.0f,0.0f,0.0f);
|
|
|
|
for (int xx = -size/2; xx < size/2; ++xx)
|
|
|
|
{
|
|
|
|
for (int yy = -size/2; yy < size/2; ++yy)
|
|
|
|
{
|
|
|
|
avgnorm += terrain->CalcExactNormal((pos.X+xx)*4.0f,(pos.Y+yy)*4.0f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
avgnorm[1] = 0.1f;
|
|
|
|
// okay crank out a square.
|
|
|
|
// we have the direction of the square. We'll get the perpendicular vector too
|
|
|
|
CVector2D perp(-avgnorm[2],avgnorm[0]);
|
|
|
|
perp = perp.Normalized();
|
|
|
|
avgnorm = avgnorm.Normalized();
|
|
|
|
|
|
|
|
SWavesVertex vertex[4];
|
|
|
|
vertex[0].m_Position = CVector3D(pos.X + perp.X*(size/2.2f) - avgnorm[0]*1.0f, 0.0f,pos.Y + perp.Y*(size/2.2f) - avgnorm[2]*1.0f);
|
|
|
|
vertex[0].m_Position *= 4.0f;
|
|
|
|
vertex[0].m_Position.Y = m_WaterHeight + 1.0f;
|
|
|
|
vertex[0].m_UV[1] = 1;
|
|
|
|
vertex[0].m_UV[0] = 0;
|
|
|
|
|
|
|
|
vertex[1].m_Position = CVector3D(pos.X - perp.X*(size/2.2f) - avgnorm[0]*1.0f, 0.0f,pos.Y - perp.Y*(size/2.2f) - avgnorm[2]*1.0f);
|
|
|
|
vertex[1].m_Position *= 4.0f;
|
|
|
|
vertex[1].m_Position.Y = m_WaterHeight + 1.0f;
|
|
|
|
vertex[1].m_UV[1] = 1;
|
|
|
|
vertex[1].m_UV[0] = 1;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-04-27 14:20:42 +02:00
|
|
|
vertex[3].m_Position = CVector3D(pos.X + perp.X*(size/2.2f) + avgnorm[0]*(size/1.5f), 0.0f,pos.Y + perp.Y*(size/2.2f) + avgnorm[2]*(size/1.5f));
|
|
|
|
vertex[3].m_Position *= 4.0f;
|
|
|
|
vertex[3].m_Position.Y = m_WaterHeight + 1.0f;
|
|
|
|
vertex[3].m_UV[1] = 0;
|
|
|
|
vertex[3].m_UV[0] = 0;
|
|
|
|
|
|
|
|
vertex[2].m_Position = CVector3D(pos.X - perp.X*(size/2.2f) + avgnorm[0]*(size/1.5f), 0.0f,pos.Y - perp.Y*(size/2.2f) + avgnorm[2]*(size/1.5f));
|
|
|
|
vertex[2].m_Position *= 4.0f;
|
|
|
|
vertex[2].m_Position.Y = m_WaterHeight + 1.0f;
|
|
|
|
vertex[2].m_UV[1] = 0;
|
|
|
|
vertex[2].m_UV[0] = 1;
|
|
|
|
|
|
|
|
waves_indices.push_back(waves_vertex_data.size());
|
|
|
|
waves_vertex_data.push_back(vertex[0]);
|
|
|
|
waves_indices.push_back(waves_vertex_data.size());
|
|
|
|
waves_vertex_data.push_back(vertex[1]);
|
|
|
|
waves_indices.push_back(waves_vertex_data.size());
|
|
|
|
waves_vertex_data.push_back(vertex[2]);
|
|
|
|
waves_indices.push_back(waves_vertex_data.size());
|
|
|
|
waves_vertex_data.push_back(vertex[3]);
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
2013-04-27 14:20:42 +02:00
|
|
|
|
|
|
|
// waves
|
|
|
|
// allocate vertex buffer
|
|
|
|
m_VBWaves = g_VBMan.Allocate(sizeof(SWavesVertex), waves_vertex_data.size(), GL_STATIC_DRAW, GL_ARRAY_BUFFER);
|
|
|
|
m_VBWaves->m_Owner->UpdateChunkVertices(m_VBWaves, &waves_vertex_data[0]);
|
|
|
|
|
|
|
|
// Construct indices buffer
|
|
|
|
m_VBWavesIndices = g_VBMan.Allocate(sizeof(GLushort), waves_indices.size(), GL_STATIC_DRAW, GL_ELEMENT_ARRAY_BUFFER);
|
|
|
|
m_VBWavesIndices->m_Owner->UpdateChunkVertices(m_VBWavesIndices, &waves_indices[0]);
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
|
2012-11-04 16:54:36 +01:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// This will set the bools properly
|
|
|
|
void WaterManager::updateQuality()
|
2010-03-21 15:18:15 +01:00
|
|
|
{
|
2012-11-04 16:54:36 +01:00
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERNORMAL) != m_WaterNormal) {
|
|
|
|
m_WaterNormal = g_Renderer.GetOptionBool(CRenderer::OPT_WATERNORMAL);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERREALDEPTH) != m_WaterRealDepth) {
|
|
|
|
m_WaterRealDepth = g_Renderer.GetOptionBool(CRenderer::OPT_WATERREALDEPTH);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERFOAM) != m_WaterFoam) {
|
|
|
|
m_WaterFoam = g_Renderer.GetOptionBool(CRenderer::OPT_WATERFOAM);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
m_NeedsFullReloading = true;
|
|
|
|
}
|
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERCOASTALWAVES) != m_WaterCoastalWaves) {
|
|
|
|
m_WaterCoastalWaves = g_Renderer.GetOptionBool(CRenderer::OPT_WATERCOASTALWAVES);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
m_NeedsFullReloading = true;
|
|
|
|
}
|
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERREFRACTION) != m_WaterRefraction) {
|
|
|
|
m_WaterRefraction = g_Renderer.GetOptionBool(CRenderer::OPT_WATERREFRACTION);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERREFLECTION) != m_WaterReflection) {
|
|
|
|
m_WaterReflection = g_Renderer.GetOptionBool(CRenderer::OPT_WATERREFLECTION);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERSHADOW) != m_WaterShadows) {
|
|
|
|
m_WaterShadows = g_Renderer.GetOptionBool(CRenderer::OPT_WATERSHADOW);
|
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
}
|
2012-11-04 16:54:36 +01:00
|
|
|
|
|
|
|
bool WaterManager::WillRenderFancyWater()
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2012-11-04 16:54:36 +01:00
|
|
|
if (!g_Renderer.GetCapabilities().m_FragmentShader)
|
|
|
|
return false;
|
|
|
|
if (!m_RenderWater)
|
|
|
|
return false;
|
|
|
|
return true;
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|