22 #include "precompiled.h"
138 static const wchar_t*
const water_type = L
"default";
145 swprintf_s(pathname,
ARRAY_SIZE(pathname), L
"art/textures/animated/water/%ls/diffuse%02d.dds", water_type, (
int)i+1);
147 textureProps.
SetWrap(GL_REPEAT);
157 swprintf_s(pathname,
ARRAY_SIZE(pathname), L
"art/textures/animated/water/%ls/normal%02d.dds", water_type, (
int)i+1);
159 textureProps.
SetWrap(GL_REPEAT);
169 textureProps.
SetWrap(GL_REPEAT);
178 textureProps.
SetWrap(GL_REPEAT);
196 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
198 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
199 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
200 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
201 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
202 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
207 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB,
209 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
211 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
212 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
256 if (!cmpWaterManager)
261 if (!cmpRangeManager)
265 float halfSize = (m_MapSize/2.0);
297 ssize_t blurZstart = Zstart-4 < 0 ? 0 : Zstart - 4;
299 ssize_t blurXstart = Xstart-4 < 0 ? 0 : Xstart - 4;
301 for (
ssize_t j = blurZstart; j < blurZend; ++j)
303 for (
ssize_t i = blurXstart; i < blurXend; ++i)
305 normals[j*m_MapSize + i] = terrain->
CalcExactNormal(((
float)i)*4.0f,((
float)j)*4.0f);
338 for (
ssize_t j = Zstart; j < Zend; ++j)
340 for (
ssize_t i = Xstart; i < Xend; ++i)
342 if (circular && (i-halfSize)*(i-halfSize)+(j-halfSize)*(j-halfSize) > mSize)
344 m_WaveX[j*m_MapSize + i] = 0.0f;
345 m_WaveZ[j*m_MapSize + i] = 0.0f;
351 int distanceToShore = 10000;
359 for (
int yy = -5; yy <= 5; ++yy)
361 for (
int xx = -5; xx <= 5; ++xx)
363 if (i+xx >= 0 && i + xx < (
long)m_MapSize)
364 if (j + yy >= 0 && j + yy < (
long)m_MapSize)
367 if (hereDepth < 0 && xx*xx + yy*yy < distanceToShore)
368 distanceToShore = xx*xx + yy*yy;
373 if (distanceToShore < 9)
375 for (
float yy = -2.5f; yy <= 2.5f; ++yy)
377 for (
float xx = -2.5f; xx <= 2.5f; ++xx)
380 if (hereDepth < 0 && xx*xx + yy*yy < distanceToShore)
381 distanceToShore = xx*xx + yy*yy;
388 for (
int yy = -2; yy <= 2; ++yy)
390 for (
int xx = -2; xx <= 2; ++xx)
400 if (distanceToShore == 10000)
402 m_WaveX[j*m_MapSize + i] = 0.0f;
403 m_WaveZ[j*m_MapSize + i] = 0.0f;
412 for (
int yy = -4; yy <= 4; yy += 2)
414 for (
int xx = -4; xx <= 4; xx += 2)
416 if (j+yy < (
long)m_MapSize && i+xx < (
long)m_MapSize && i+xx >= 0 && j+yy >= 0)
417 normal += normals[(j+yy)*m_MapSize + (i+xx)];
419 waterRaise += heightmap[j*m_MapSize + i]*HEIGHT_SCALE - terrain->
GetVertexGroundLevel(i+xx,j+yy);
423 normal *= 0.012345679f;
427 m_WaveX[j*m_MapSize + i] = normal[0];
428 m_WaveZ[j*m_MapSize + i] = normal[2];
435 depth =
clamp(depth,0.0f,10.0f);
436 float foamAmount = (waterRaise/255.0f) * (1.0f - depth/10.0f) * (
m_Waviness/8.0f);
438 foamAmount = foamAmount > 1.0f ? 1.0f: foamAmount;
454 std::vector<CVector2D> waveSquares;
457 for (
size_t j = 0; j < m_MapSize/size; ++j)
459 for (
size_t i = 0; i < m_MapSize/size; ++i)
466 for (
int yy = 0; yy < size; ++yy)
468 for (
int xx = 0; xx < size; ++xx)
473 landPosition +=
CVector2D(i*size+xx,j*size+yy);
477 waterPosition +=
CVector2D(i*size+xx,j*size+yy);
479 avnormal += terrain->
CalcExactNormal( (i*size+xx)*4.0f,(j*size+yy)*4.0f);
483 if (landTexel < size/2)
486 landPosition /= landTexel;
487 waterPosition /= waterTexel;
494 if (avnormal.
Length() <= 0.2f)
504 for (
u8 i = 0; i < 40; i++)
506 squarePos = landPosition * (i/40.0f) + waterPosition * (1.0f-(i/40.0f));
510 if (squarePos.
X == -1)
515 for (
unsigned long i = 0; i < waveSquares.size(); i++)
523 waveSquares.push_back(squarePos);
528 std::vector<SWavesVertex> waves_vertex_data;
529 std::vector<GLushort> waves_indices;
533 for (
unsigned long i = 0; i < waveSquares.size(); i++)
538 for (
int yy = -size/2; yy < size/2; ++yy)
540 for (
int xx = -size/2; xx < size/2; ++xx)
553 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);
556 vertex[0].
m_UV[1] = 1;
557 vertex[0].
m_UV[0] = 0;
559 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);
562 vertex[1].
m_UV[1] = 1;
563 vertex[1].
m_UV[0] = 1;
565 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));
568 vertex[3].
m_UV[1] = 0;
569 vertex[3].
m_UV[0] = 0;
571 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));
574 vertex[2].
m_UV[1] = 0;
575 vertex[2].
m_UV[0] = 1;
577 waves_indices.push_back(waves_vertex_data.size());
578 waves_vertex_data.push_back(vertex[0]);
579 waves_indices.push_back(waves_vertex_data.size());
580 waves_vertex_data.push_back(vertex[1]);
581 waves_indices.push_back(waves_vertex_data.size());
582 waves_vertex_data.push_back(vertex[2]);
583 waves_indices.push_back(waves_vertex_data.size());
584 waves_vertex_data.push_back(vertex[3]);
588 if (waves_indices.empty())
657 if (!
g_Renderer.GetCapabilities().m_FragmentShader)
CTexturePtr m_WaterTexture[60]
float LengthSquared() const
size_t m_RefractionTextureSize
CVertexBufferManager g_VBMan
Represents the filename and GL parameters of a texture, for passing to CTextureManager::CreateTexture...
CVertexBuffer::VBChunk * m_VBWaves
size_t m_ReflectionTextureSize
CTexturePtr m_NormalMap[60]
float GetExactGroundLevel(float x, float z) const
GLuint m_RefractionTexture
float m_TWaterScrollCounter
int swprintf_s(wchar_t *buf, size_t max_chars, const wchar_t *fmt,...) WPRINTF_ARGS(3)
virtual bool GetLosCircular()=0
Returns whether the LOS is restricted to a circular map.
const entity_id_t SYSTEM_ENTITY
Entity ID for singleton 'system' components.
Public API for simulation system.
void SetMapSize(size_t size)
Updates the map size.
CTerrain * GetTerrain()
Get the pointer to the terrain object.
T round_up_to_pow2(T x)
round up to next larger power of two.
void UpdateQuality()
Updates the settings to the one from the renderer, and sets m_NeedsReloading.
CVertexBuffer::VBChunk * Allocate(size_t vertexSize, size_t numVertices, GLenum usage, GLenum target)
Try to allocate a vertex buffer of the given size and type.
const float HEIGHT_SCALE
metres per u16 height unit
CVector2D Normalized() const
int LoadWaterTextures()
LoadWaterTextures: Load water textures from within the progressive load framework.
float m_SWaterScrollCounter
virtual float GetExactWaterLevel(float x, float z)=0
Get the current water level at the given point.
CGame * g_Game
Globally accessible pointer to the CGame object.
void UpdateChunkVertices(VBChunk *chunk, void *data)
Update vertex data for given chunk. Transfers the provided data to the actual OpenGL vertex buffer...
void UnloadWaterTextures()
UnloadWaterTextures: Free any loaded water textures and reset the internal state so that another call...
A simplified syntax for accessing entity components.
void CreateSuperfancyInfo(CSimulation2 *simulation)
CreateSuperfancyInfo: creates textures and wave vertices for superfancy water.
void Release(CVertexBuffer::VBChunk *chunk)
Returns the given chunk to its owning buffer.
void SetMaxAnisotropy(float aniso)
Set maximum anisotropy value.
bool WillRenderFancyWater()
Returns true if fancy water shaders will be used (i.e.
float m_ReflectionTintStrength
GLuint m_ReflectionTexture
CVertexBuffer * m_Owner
Owning (parent) vertex buffer.
float GetVertexGroundLevel(ssize_t i, ssize_t j) const
CVertexBuffer::VBChunk * m_VBWavesIndices
CWorld * GetWorld()
Get the pointer to the game world object.
#define SAFE_ARRAY_DELETE(p)
delete memory ensuing from new[] and set the pointer to zero (thus making double-frees safe / a no-op...
u16 * GetHeightMap() const
float * m_DistanceToShore
void SetWrap(GLint wrap)
Set wrapping mode (typically GL_REPEAT, GL_CLAMP_TO_EDGE, etc).
CVector3D Normalized() const
T clamp(T value, T min, T max)
CVector3D CalcExactNormal(float x, float z) const
shared_ptr< CTexture > CTexturePtr