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;
|
2014-07-03 22:07:15 +02:00
|
|
|
|
2006-01-22 00:27:42 +01:00
|
|
|
m_WaterCurrentTex = 0;
|
2014-07-03 22:07:15 +02:00
|
|
|
|
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;
|
2014-07-03 22:07:15 +02:00
|
|
|
|
|
|
|
m_ReflectionFbo = 0;
|
|
|
|
m_RefractionFbo = 0;
|
|
|
|
|
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
|
|
|
|
2014-07-01 18:05:05 +02:00
|
|
|
m_DistanceHeightmap = NULL;
|
|
|
|
m_BlurredNormalMap = NULL;
|
2014-07-03 22:07:15 +02:00
|
|
|
m_WindStrength = NULL;
|
2014-07-01 18:05:05 +02:00
|
|
|
|
|
|
|
m_WaterUgly = false;
|
|
|
|
m_WaterFancyEffects = false;
|
2012-11-04 16:54:36 +01:00
|
|
|
m_WaterRealDepth = false;
|
|
|
|
m_WaterRefraction = false;
|
|
|
|
m_WaterReflection = false;
|
|
|
|
m_WaterShadows = false;
|
2014-07-03 22:07:15 +02:00
|
|
|
m_WaterType = L"ocean";
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
m_NeedsReloading = false;
|
2013-08-18 11:27:11 +02:00
|
|
|
m_NeedInfoUpdate = true;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
|
|
|
m_depthTT = 0;
|
2014-07-01 18:05:05 +02:00
|
|
|
m_FancyTexture = 0;
|
2014-07-04 12:14:35 +02:00
|
|
|
m_ReflFboDepthTexture = 0;
|
|
|
|
m_RefrFboDepthTexture = 0;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2013-08-18 11:27:11 +02:00
|
|
|
m_MapSize = 0;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
SAFE_ARRAY_DELETE(m_DistanceHeightmap);
|
|
|
|
SAFE_ARRAY_DELETE(m_BlurredNormalMap);
|
|
|
|
SAFE_ARRAY_DELETE(m_WindStrength);
|
2013-04-27 14:20:42 +02:00
|
|
|
|
2012-10-31 19:42:17 +01:00
|
|
|
glDeleteTextures(1, &m_depthTT);
|
2014-07-01 18:05:05 +02:00
|
|
|
glDeleteTextures(1, &m_FancyTexture);
|
2014-07-04 12:14:35 +02:00
|
|
|
glDeleteTextures(1, &m_ReflFboDepthTexture);
|
|
|
|
glDeleteTextures(1, &m_RefrFboDepthTexture);
|
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];
|
2014-07-03 22:07:15 +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
|
|
|
{
|
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);
|
|
|
|
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
|
|
|
{
|
2014-07-03 22:07:15 +02:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/%ls/normal00%02d.png", m_WaterType.c_str(), (int)i+1);
|
2010-09-10 23:02:10 +02:00
|
|
|
CTextureProperties textureProps(pathname);
|
|
|
|
textureProps.SetWrap(GL_REPEAT);
|
2013-08-24 16:12:39 +02:00
|
|
|
textureProps.SetMaxAnisotropy(4);
|
|
|
|
|
2010-09-10 23:02:10 +02:00
|
|
|
CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
|
|
|
|
texture->Prefetch();
|
|
|
|
m_NormalMap[i] = texture;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2014-07-01 18:05:05 +02:00
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
m_ReflectionTextureSize = g_Renderer.GetHeight() * 0.66; // Higher settings give a better result
|
|
|
|
m_RefractionTextureSize = g_Renderer.GetHeight() * 0.33; // Lower settings actually sorta look better since it blurs.
|
2006-05-28 04:13:32 +02:00
|
|
|
|
|
|
|
// Create reflection texture
|
|
|
|
glGenTextures(1, &m_ReflectionTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_ReflectionTexture);
|
2014-07-03 22:07:15 +02:00
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA,
|
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);
|
2014-07-03 22:07:15 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2006-05-28 04:13:32 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
2013-08-24 16:12:39 +02: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
|
|
|
|
|
|
|
// 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
|
|
|
|
2014-07-04 12:14:35 +02:00
|
|
|
// Create depth textures
|
|
|
|
glGenTextures(1, &m_ReflFboDepthTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_ReflFboDepthTexture);
|
2014-07-03 22:07:15 +02:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
2014-07-04 12:14:35 +02:00
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, (GLsizei)m_ReflectionTextureSize, (GLsizei)m_ReflectionTextureSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
|
|
|
|
|
|
|
|
glGenTextures(1, &m_RefrFboDepthTexture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, m_RefrFboDepthTexture);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
|
|
glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, (GLsizei)m_RefractionTextureSize, (GLsizei)m_RefractionTextureSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, NULL);
|
2014-07-03 22:07:15 +02:00
|
|
|
|
2014-07-04 12:14:35 +02:00
|
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
// Create the water framebuffers
|
2014-07-04 12:14:35 +02:00
|
|
|
|
|
|
|
GLint currentFbo;
|
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, ¤tFbo);
|
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
m_ReflectionFbo = 0;
|
|
|
|
pglGenFramebuffersEXT(1, &m_ReflectionFbo);
|
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_ReflectionFbo);
|
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_ReflectionTexture, 0);
|
2014-07-04 12:14:35 +02:00
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_ReflFboDepthTexture, 0);
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
|
|
|
|
GLenum status = pglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
|
|
|
|
{
|
|
|
|
LOGWARNING(L"Reflection framebuffer object incomplete: 0x%04X", status);
|
|
|
|
g_Renderer.m_Options.m_WaterReflection = false;
|
|
|
|
}
|
2014-07-03 22:07:15 +02:00
|
|
|
|
|
|
|
m_RefractionFbo = 0;
|
|
|
|
pglGenFramebuffersEXT(1, &m_RefractionFbo);
|
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_RefractionFbo);
|
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, m_RefractionTexture, 0);
|
2014-07-04 12:14:35 +02:00
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_TEXTURE_2D, m_RefrFboDepthTexture, 0);
|
|
|
|
|
|
|
|
ogl_WarnIfError();
|
|
|
|
|
|
|
|
status = pglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
|
|
|
|
{
|
|
|
|
LOGWARNING(L"Refraction framebuffer object incomplete: 0x%04X", status);
|
|
|
|
g_Renderer.m_Options.m_WaterRefraction = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, currentFbo);
|
2014-07-03 22:07:15 +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
|
|
|
}
|
2014-07-03 22:07:15 +02:00
|
|
|
glDeleteTextures(1, &m_ReflectionTexture);
|
|
|
|
glDeleteTextures(1, &m_RefractionTexture);
|
|
|
|
pglDeleteFramebuffersEXT(1, &m_RefractionFbo);
|
|
|
|
pglDeleteFramebuffersEXT(1, &m_ReflectionFbo);
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
|
2014-07-01 18:05:05 +02:00
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Calculate our binary heightmap from the terrain heightmap.
|
|
|
|
void WaterManager::RecomputeDistanceHeightmap()
|
|
|
|
{
|
|
|
|
if (m_DistanceHeightmap == NULL)
|
|
|
|
m_DistanceHeightmap = new u8[m_MapSize*m_MapSize];
|
|
|
|
|
|
|
|
// Custom copy the heightmap.
|
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
|
|
|
|
u16 waterLevel = m_WaterHeight/HEIGHT_SCALE;
|
|
|
|
|
|
|
|
u16* heightmap = terrain->GetHeightMap();
|
|
|
|
|
|
|
|
// We will "expand" the heightmap. That is we'll set each vertex on land as "3", and "bleed" that onto neighboring pixels.
|
|
|
|
// So 3 is "on land", 2 is "close", 1 "somewhat close" and 0 is "water".
|
|
|
|
// This gives a basic manhattan approximation of how close to the coast we are.
|
|
|
|
// I have a heathen fondness for ternary operators so there are some below.
|
|
|
|
u8 level = 0;
|
|
|
|
for (size_t z = 0; z < m_MapSize; ++z)
|
|
|
|
{
|
|
|
|
level = 0;
|
|
|
|
for (size_t x = 0; x < m_MapSize; ++x)
|
|
|
|
m_DistanceHeightmap[z*m_MapSize + x] = heightmap[z*m_MapSize + x] >= waterLevel ? level = 3
|
|
|
|
: level > 0 ? --level : 0;
|
|
|
|
level = 0;
|
|
|
|
for (size_t x = m_MapSize-1; x != (size_t)-1; --x)
|
|
|
|
{
|
|
|
|
if (heightmap[z*m_MapSize + x] >= waterLevel)
|
|
|
|
level = 3; // no need to set m_distanceHeightmap, it's already been done by the other loop.
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level > 0 ? --level : 0;
|
|
|
|
if (level > m_DistanceHeightmap[z*m_MapSize + x])
|
|
|
|
m_DistanceHeightmap[z*m_MapSize + x] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (size_t x = 0; x < m_MapSize; ++x)
|
|
|
|
{
|
|
|
|
level = 0;
|
|
|
|
for (size_t z = 0; z < m_MapSize; ++z)
|
|
|
|
{
|
|
|
|
if (heightmap[z*m_MapSize + x] >= waterLevel)
|
|
|
|
level = 3;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level > 0 ? --level : 0;
|
|
|
|
if (level > m_DistanceHeightmap[z*m_MapSize + x])
|
|
|
|
m_DistanceHeightmap[z*m_MapSize + x] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
level = 0;
|
|
|
|
for (size_t z = m_MapSize-1; z != (size_t)-1; --z)
|
|
|
|
{
|
|
|
|
if (heightmap[z*m_MapSize + x] >= waterLevel)
|
|
|
|
level = 3;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
level > 0 ? --level : 0;
|
|
|
|
if (level > m_DistanceHeightmap[z*m_MapSize + x])
|
|
|
|
m_DistanceHeightmap[z*m_MapSize + x] = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Calculate The blurred normal map to get an idea of where water ought to go.
|
|
|
|
void WaterManager::RecomputeBlurredNormalMap()
|
|
|
|
{
|
|
|
|
// used to cache terrain normals since otherwise we'd recalculate them a lot (I'm blurring the "normal" map).
|
|
|
|
// this might be updated to actually cache in the terrain manager but that's not for now.
|
|
|
|
if (m_BlurredNormalMap == NULL)
|
|
|
|
m_BlurredNormalMap = new CVector3D[m_MapSize*m_MapSize];
|
|
|
|
|
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
|
|
|
|
// It's really slow to calculate normals so cache them first.
|
|
|
|
CVector3D* normals = new CVector3D[m_MapSize*m_MapSize];
|
|
|
|
|
|
|
|
// Not the edges, we won't care about them.
|
|
|
|
float ii = 8.0f, jj = 8.0f;
|
|
|
|
for (size_t j = 2; j < m_MapSize-2; ++j, jj += 4.0f)
|
|
|
|
for (size_t i = 2; i < m_MapSize-2; ++i, ii += 4.0f)
|
|
|
|
{
|
|
|
|
CVector3D norm;
|
|
|
|
terrain->CalcNormal(i,j,norm);
|
|
|
|
normals[j*m_MapSize + i] = norm;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We could be way fancier (and faster) for our blur but we probably don't need the complexity.
|
|
|
|
// Two pass filter, nothing complicated here.
|
|
|
|
CVector3D blurValue;
|
|
|
|
ii = 8.0f; jj = 8.0f;
|
|
|
|
size_t idx = 2;
|
|
|
|
for (size_t j = 2; j < m_MapSize-2; ++j, jj += 4.0f)
|
|
|
|
for (size_t i = 2; i < m_MapSize-2; ++i, ii += 4.0f,++idx)
|
|
|
|
{
|
|
|
|
blurValue = normals[idx-2];
|
|
|
|
blurValue += normals[idx-1];
|
|
|
|
blurValue += normals[idx];
|
|
|
|
blurValue += normals[idx+1];
|
|
|
|
blurValue += normals[idx+2];
|
|
|
|
m_BlurredNormalMap[idx] = blurValue * 0.2f;
|
|
|
|
}
|
|
|
|
// y direction, probably slower because of cache misses but I don't see an easy way around that.
|
|
|
|
ii = 8.0f; jj = 8.0f;
|
|
|
|
for (size_t i = 2; i < m_MapSize-2; ++i, ii += 4.0f)
|
|
|
|
{
|
|
|
|
for (size_t j = 2; j < m_MapSize-2; ++j, jj += 4.0f)
|
|
|
|
{
|
|
|
|
blurValue = normals[(j-2)*m_MapSize + i];
|
|
|
|
blurValue += normals[(j-1)*m_MapSize + i];
|
|
|
|
blurValue += normals[j*m_MapSize + i];
|
|
|
|
blurValue += normals[(j+1)*m_MapSize + i];
|
|
|
|
blurValue += normals[(j+2)*m_MapSize + i];
|
|
|
|
m_BlurredNormalMap[j*m_MapSize + i] = blurValue * 0.2f;
|
|
|
|
}
|
|
|
|
}
|
2014-07-05 12:20:30 +02:00
|
|
|
|
|
|
|
delete[] normals;
|
2014-07-01 18:05:05 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
// This is too slow and should support limited recomputation.
|
|
|
|
void WaterManager::RecomputeWindStrength()
|
2012-10-31 19:42:17 +01:00
|
|
|
{
|
2014-07-03 22:07:15 +02:00
|
|
|
if (m_WindStrength == NULL)
|
|
|
|
m_WindStrength = new float[m_MapSize*m_MapSize];
|
2014-07-05 12:20:30 +02:00
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
CTerrain* terrain = g_Game->GetWorld()->GetTerrain();
|
|
|
|
float waterLevel = m_WaterHeight;
|
2012-10-31 19:42:17 +01:00
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
CVector2D windDir = CVector2D(cos(m_WindAngle),sin(m_WindAngle));
|
2014-07-05 12:20:30 +02:00
|
|
|
CVector2D perp = CVector2D(-windDir.Y, windDir.X);
|
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
// Our kernel will sample 5 points going towards the wind (generally).
|
2014-07-04 12:14:35 +02:00
|
|
|
int kernel[5][2] = { {(int)windDir.X*2,(int)windDir.Y*2}, {(int)windDir.X*5,(int)windDir.Y*5}, {(int)windDir.X*9,(int)windDir.Y*9}, {(int)windDir.X*16,(int)windDir.Y*16}, {(int)windDir.X*25,(int)windDir.Y*25} };
|
2014-01-05 17:15:20 +01:00
|
|
|
|
2014-07-05 12:20:30 +02:00
|
|
|
float* Temp = new float[m_MapSize*m_MapSize];
|
|
|
|
std::fill(Temp, Temp + m_MapSize*m_MapSize, 1.0f);
|
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
for (size_t j = 0; j < m_MapSize; ++j)
|
|
|
|
for (size_t i = 0; i < m_MapSize; ++i)
|
2013-08-24 16:12:39 +02:00
|
|
|
{
|
2014-07-03 22:07:15 +02:00
|
|
|
float curHeight = terrain->GetVertexGroundLevel(i,j);
|
|
|
|
if (curHeight >= waterLevel)
|
2013-04-27 14:20:42 +02:00
|
|
|
{
|
2014-07-05 12:20:30 +02:00
|
|
|
Temp[j*m_MapSize + i] = 0.3f; // blurs too strong otherwise
|
2013-04-27 14:20:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-03 22:07:15 +02:00
|
|
|
if (terrain->GetVertexGroundLevel(i + ceil(windDir.X),j + ceil(windDir.Y)) < waterLevel)
|
2013-05-21 10:55:49 +02:00
|
|
|
continue;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
// Calculate how dampened our waves should be.
|
|
|
|
float tendency = 0.0f;
|
|
|
|
float oldHeight = std::max(waterLevel,terrain->GetVertexGroundLevel(i+kernel[4][0],j+kernel[4][1]));
|
|
|
|
float currentHeight = std::max(waterLevel,terrain->GetVertexGroundLevel(i+kernel[3][0],j+kernel[3][1]));
|
2014-07-05 12:20:30 +02:00
|
|
|
float avgheight = oldHeight + currentHeight;
|
2014-07-03 22:07:15 +02:00
|
|
|
tendency = currentHeight - oldHeight;
|
|
|
|
oldHeight = currentHeight;
|
|
|
|
currentHeight = std::max(waterLevel,terrain->GetVertexGroundLevel(i+kernel[2][0],j+kernel[2][1]));
|
2014-07-05 12:20:30 +02:00
|
|
|
avgheight += currentHeight;
|
2014-07-03 22:07:15 +02:00
|
|
|
tendency += currentHeight - oldHeight;
|
|
|
|
oldHeight = currentHeight;
|
|
|
|
currentHeight = std::max(waterLevel,terrain->GetVertexGroundLevel(i+kernel[1][0],j+kernel[1][1]));
|
2014-07-05 12:20:30 +02:00
|
|
|
avgheight += currentHeight;
|
2014-07-03 22:07:15 +02:00
|
|
|
tendency += currentHeight - oldHeight;
|
|
|
|
oldHeight = currentHeight;
|
|
|
|
currentHeight = std::max(waterLevel,terrain->GetVertexGroundLevel(i+kernel[0][0],j+kernel[0][1]));
|
2014-07-05 12:20:30 +02:00
|
|
|
avgheight += currentHeight;
|
2014-07-03 22:07:15 +02:00
|
|
|
tendency += currentHeight - oldHeight;
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2014-07-05 12:20:30 +02:00
|
|
|
float baseLevel = std::max(0.0f,1.0f - (avgheight/5.0f-waterLevel)/20.0f);
|
2014-07-03 22:07:15 +02:00
|
|
|
baseLevel *= baseLevel;
|
2014-07-05 12:20:30 +02:00
|
|
|
tendency /= 15.0f;
|
2014-07-03 22:07:15 +02:00
|
|
|
baseLevel -= tendency; // if the terrain was sloping downwards, increase baselevel. Otherwise reduce.
|
|
|
|
baseLevel = clamp(baseLevel,0.0f,1.0f);
|
2012-11-04 16:54:36 +01:00
|
|
|
|
2014-07-03 22:07:15 +02:00
|
|
|
// Draw on map. This is pretty slow.
|
2014-07-05 12:20:30 +02:00
|
|
|
float length = 35.0f * (1.0f-baseLevel/1.8f);
|
2014-07-03 22:07:15 +02:00
|
|
|
for (float y = 0; y < length; y += 0.6f)
|
|
|
|
{
|
2014-07-05 12:20:30 +02:00
|
|
|
int xx = clamp(i - y * windDir.X,0.0f,(float)(m_MapSize-1));
|
|
|
|
int yy = clamp(j - y * windDir.Y,0.0f,(float)(m_MapSize-1));
|
|
|
|
Temp[yy*m_MapSize + xx] = Temp[yy*m_MapSize + xx] < (0.0f+baseLevel/1.5f) * (1.0f-y/length) + y/length * 1.0f ?
|
|
|
|
Temp[yy*m_MapSize + xx] : (0.0f+baseLevel/1.5f) * (1.0f-y/length) + y/length * 1.0f;
|
2013-04-27 14:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-05 12:20:30 +02:00
|
|
|
|
|
|
|
int blurKernel[4][2] = { {(int)ceil(windDir.X),(int)ceil(windDir.Y)}, {(int)windDir.X*3,(int)windDir.Y*3}, {(int)ceil(perp.X),(int)ceil(perp.Y)}, {(int)-ceil(perp.X),(int)-ceil(perp.Y)} };
|
|
|
|
float blurValue;
|
|
|
|
for (size_t j = 2; j < m_MapSize-2; ++j)
|
|
|
|
for (size_t i = 2; i < m_MapSize-2; ++i)
|
|
|
|
{
|
|
|
|
blurValue = Temp[(j+blurKernel[0][1])*m_MapSize + i+blurKernel[0][0]];
|
|
|
|
blurValue += Temp[(j+blurKernel[0][1])*m_MapSize + i+blurKernel[0][0]];
|
|
|
|
blurValue += Temp[(j+blurKernel[0][1])*m_MapSize + i+blurKernel[0][0]];
|
|
|
|
blurValue += Temp[(j+blurKernel[0][1])*m_MapSize + i+blurKernel[0][0]];
|
|
|
|
m_WindStrength[j*m_MapSize + i] = blurValue * 0.25f;
|
|
|
|
}
|
|
|
|
delete[] Temp;
|
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;
|
|
|
|
|
2014-07-01 18:05:05 +02:00
|
|
|
SAFE_ARRAY_DELETE(m_DistanceHeightmap);
|
|
|
|
SAFE_ARRAY_DELETE(m_BlurredNormalMap);
|
2014-07-03 22:07:15 +02:00
|
|
|
SAFE_ARRAY_DELETE(m_WindStrength);
|
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
|
|
|
{
|
2014-07-01 18:05:05 +02:00
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERUGLY) != m_WaterUgly) {
|
|
|
|
m_WaterUgly = g_Renderer.GetOptionBool(CRenderer::OPT_WATERUGLY);
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2014-07-01 18:05:05 +02:00
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERFANCYEFFECTS) != m_WaterFancyEffects) {
|
|
|
|
m_WaterFancyEffects = g_Renderer.GetOptionBool(CRenderer::OPT_WATERFANCYEFFECTS);
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = true;
|
|
|
|
}
|
2014-07-01 18:05:05 +02:00
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_WATERREALDEPTH) != m_WaterRealDepth) {
|
|
|
|
m_WaterRealDepth = g_Renderer.GetOptionBool(CRenderer::OPT_WATERREALDEPTH);
|
2012-11-04 16:54:36 +01:00
|
|
|
m_NeedsReloading = 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;
|
|
|
|
}
|
2014-07-01 18:05:05 +02:00
|
|
|
if (g_Renderer.GetOptionBool(CRenderer::OPT_SHADOWSONWATER) != m_WaterShadows) {
|
|
|
|
m_WaterShadows = g_Renderer.GetOptionBool(CRenderer::OPT_SHADOWSONWATER);
|
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
|
|
|
|
|
|
|
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;
|
2014-07-01 18:05:05 +02:00
|
|
|
if (!m_RenderWater || m_WaterUgly)
|
2012-11-04 16:54:36 +01:00
|
|
|
return false;
|
|
|
|
return true;
|
2012-10-31 19:42:17 +01:00
|
|
|
}
|