2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* 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"
|
|
|
|
|
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"
|
|
|
|
|
2010-07-04 12:15:53 +02:00
|
|
|
#include "ps/Filesystem.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
#include "ps/CLogger.h"
|
|
|
|
#include "ps/Loader.h"
|
|
|
|
|
|
|
|
#include "renderer/WaterManager.h"
|
2006-05-28 04:13:32 +02:00
|
|
|
#include "renderer/Renderer.h"
|
2006-01-07 02:04:26 +01:00
|
|
|
|
2009-11-03 22:46:35 +01:00
|
|
|
#define LOG_CATEGORY L"graphics"
|
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);
|
|
|
|
m_WaterFullDepth = 4.0f;
|
|
|
|
m_WaterMaxAlpha = 0.85f;
|
|
|
|
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;
|
2006-10-04 06:47:58 +02:00
|
|
|
m_SpecularStrength = 0.4f;
|
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;
|
2006-01-07 02:04:26 +01: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_WaterTexture); i++)
|
2006-01-07 02:04:26 +01:00
|
|
|
m_WaterTexture[i] = 0;
|
|
|
|
|
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
|
|
|
m_NormalMap[i] = 0;
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
cur_loading_water_tex = 0;
|
2006-05-25 07:46:17 +02:00
|
|
|
cur_loading_normal_map = 0;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WaterManager::~WaterManager()
|
|
|
|
{
|
|
|
|
// Cleanup if the caller messed up
|
|
|
|
UnloadWaterTextures();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////
|
|
|
|
// Progressive load of water textures
|
|
|
|
int WaterManager::LoadWaterTextures()
|
|
|
|
{
|
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
|
|
|
const size_t num_textures = ARRAY_SIZE(m_WaterTexture);
|
|
|
|
const size_t num_normal_maps = ARRAY_SIZE(m_NormalMap);
|
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
|
|
|
|
|
|
|
// yield after this time is reached. balances increased progress bar
|
|
|
|
// smoothness vs. slowing down loading.
|
2008-01-07 21:03:19 +01:00
|
|
|
const double end_time = timer_Time() + 100e-3;
|
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)
|
2006-01-07 02:04:26 +01:00
|
|
|
while (cur_loading_water_tex < num_textures)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/%ls/diffuse%02d.dds", water_type, (int)cur_loading_water_tex+1);
|
2010-07-04 12:15:53 +02:00
|
|
|
Handle ht = ogl_tex_load(g_VFS, pathname);
|
2006-01-07 02:04:26 +01:00
|
|
|
if (ht <= 0)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"LoadWaterTextures failed on \"%ls\"", pathname);
|
2006-01-07 02:04:26 +01:00
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
m_WaterTexture[cur_loading_water_tex] = ht;
|
|
|
|
RETURN_ERR(ogl_tex_upload(ht));
|
|
|
|
cur_loading_water_tex++;
|
2006-05-25 07:46:17 +02:00
|
|
|
LDR_CHECK_TIMEOUT(cur_loading_water_tex, num_textures + num_normal_maps);
|
|
|
|
}
|
|
|
|
|
2006-05-28 04:13:32 +02:00
|
|
|
// Load normalmaps (for fancy water)
|
2006-05-25 07:46:17 +02:00
|
|
|
while (cur_loading_normal_map < num_normal_maps)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
swprintf_s(pathname, ARRAY_SIZE(pathname), L"art/textures/animated/water/%ls/normal%02d.dds", water_type, (int)cur_loading_normal_map+1);
|
2010-07-04 12:15:53 +02:00
|
|
|
Handle ht = ogl_tex_load(g_VFS, pathname);
|
2006-05-25 07:46:17 +02:00
|
|
|
if (ht <= 0)
|
|
|
|
{
|
2009-11-03 22:46:35 +01:00
|
|
|
LOG(CLogger::Error, LOG_CATEGORY, L"LoadWaterTextures failed on \"%ls\"", pathname);
|
2006-05-25 07:46:17 +02:00
|
|
|
return ht;
|
|
|
|
}
|
|
|
|
m_NormalMap[cur_loading_normal_map] = ht;
|
|
|
|
RETURN_ERR(ogl_tex_upload(ht));
|
|
|
|
cur_loading_normal_map++;
|
|
|
|
LDR_CHECK_TIMEOUT(num_textures + cur_loading_normal_map, num_textures + num_normal_maps);
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
|
|
|
|
2006-05-28 04:13:32 +02:00
|
|
|
// Set the size to the largest power of 2 that is <= to the window height, so
|
|
|
|
// the reflection/reflaction images will fit within the window
|
|
|
|
// (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);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
ogl_tex_free(m_WaterTexture[i]);
|
|
|
|
m_WaterTexture[i] = 0;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
ogl_tex_free(m_NormalMap[i]);
|
|
|
|
m_NormalMap[i] = 0;
|
|
|
|
}
|
|
|
|
|
2006-01-07 02:04:26 +01:00
|
|
|
cur_loading_water_tex = 0; // so they will be reloaded if LoadWaterTextures is called again
|
2006-05-25 07:46:17 +02:00
|
|
|
cur_loading_normal_map = 0;
|
2006-01-07 02:04:26 +01:00
|
|
|
}
|
2010-03-21 15:18:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
bool WaterManager::WillRenderFancyWater()
|
|
|
|
{
|
|
|
|
return (g_Renderer.GetCapabilities().m_FragmentShader && g_Renderer.GetOptionBool(CRenderer::OPT_FANCYWATER));
|
|
|
|
}
|