2014-10-05 22:50:10 +02:00
|
|
|
/* Copyright (C) 2014 Wildfire Games.
|
2011-02-03 02:12:24 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
#include "LOSTexture.h"
|
|
|
|
|
2012-08-07 00:38:42 +02:00
|
|
|
#include "graphics/ShaderManager.h"
|
2011-02-03 02:12:24 +01:00
|
|
|
#include "graphics/Terrain.h"
|
|
|
|
#include "lib/bits.h"
|
2013-11-12 02:17:16 +01:00
|
|
|
#include "lib/config2.h"
|
2012-08-07 00:38:42 +02:00
|
|
|
#include "ps/CLogger.h"
|
2011-02-03 02:12:24 +01:00
|
|
|
#include "ps/Game.h"
|
|
|
|
#include "ps/Profile.h"
|
|
|
|
#include "renderer/Renderer.h"
|
2012-08-07 00:38:42 +02:00
|
|
|
#include "renderer/TimeManager.h"
|
2011-02-03 02:12:24 +01:00
|
|
|
#include "simulation2/Simulation2.h"
|
|
|
|
#include "simulation2/components/ICmpRangeManager.h"
|
2011-04-05 22:32:03 +02:00
|
|
|
#include "simulation2/components/ICmpTerrain.h"
|
2011-02-03 02:12:24 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
The LOS bitmap is computed with one value per map vertex, based on
|
|
|
|
CCmpRangeManager's visibility information.
|
|
|
|
|
2011-02-05 19:19:28 +01:00
|
|
|
The bitmap is then blurred using an NxN filter (in particular a
|
|
|
|
7-tap Binomial filter as an efficient integral approximation of a Gaussian).
|
|
|
|
To implement the blur efficiently without using extra memory for a second copy
|
|
|
|
of the bitmap, we generate the bitmap with (N-1)/2 pixels of padding on each side,
|
|
|
|
then the blur shifts the image back into the corner.
|
2011-02-03 02:12:24 +01:00
|
|
|
|
|
|
|
The blurred bitmap is then uploaded into a GL texture for use by the renderer.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-02-05 19:19:28 +01:00
|
|
|
// Blur with a NxN filter, where N = g_BlurSize must be an odd number.
|
|
|
|
static const size_t g_BlurSize = 7;
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2014-05-25 15:30:39 +02:00
|
|
|
// Alignment (in bytes) of the pixel data passed into glTexSubImage2D.
|
|
|
|
// This must be a multiple of GL_UNPACK_ALIGNMENT, which ought to be 1 (since
|
|
|
|
// that's what we set it to) but in some weird cases appears to have a different
|
|
|
|
// value. (See Trac #2594). Multiples of 4 are possibly good for performance anyway.
|
|
|
|
static const size_t g_SubTextureAlignment = 4;
|
|
|
|
|
2014-10-12 21:27:49 +02:00
|
|
|
CLOSTexture::CLOSTexture(CSimulation2& simulation)
|
|
|
|
: m_Simulation(simulation), m_Dirty(true), m_ShaderInitialized(false),
|
|
|
|
m_Texture(0), m_TextureSmooth1(0), m_TextureSmooth2(0), m_smoothFbo(0),
|
|
|
|
m_MapSize(0), m_TextureSize(0), whichTex(true)
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
2012-08-07 20:28:23 +02:00
|
|
|
if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS)
|
2014-10-05 22:02:04 +02:00
|
|
|
CreateShader();
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CLOSTexture::~CLOSTexture()
|
|
|
|
{
|
|
|
|
if (m_Texture)
|
2011-05-29 17:02:02 +02:00
|
|
|
DeleteTexture();
|
|
|
|
}
|
|
|
|
|
2014-10-05 22:02:04 +02:00
|
|
|
// Create the LOS texture engine. Should be ran only once.
|
|
|
|
bool CLOSTexture::CreateShader()
|
|
|
|
{
|
|
|
|
m_smoothShader = g_Renderer.GetShaderManager().LoadEffect(str_los_interp);
|
|
|
|
CShaderProgramPtr shader = m_smoothShader->GetShader();
|
|
|
|
|
|
|
|
m_ShaderInitialized = m_smoothShader && shader;
|
|
|
|
|
|
|
|
if (!m_ShaderInitialized)
|
|
|
|
{
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGERROR("Failed to load SmoothLOS shader, disabling.");
|
2014-10-05 22:02:04 +02:00
|
|
|
g_Renderer.m_Options.m_SmoothLOS = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pglGenFramebuffersEXT(1, &m_smoothFbo);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-29 17:02:02 +02:00
|
|
|
void CLOSTexture::DeleteTexture()
|
|
|
|
{
|
|
|
|
glDeleteTextures(1, &m_Texture);
|
2014-10-05 22:02:04 +02:00
|
|
|
|
|
|
|
if (m_TextureSmooth1)
|
2012-08-07 00:38:42 +02:00
|
|
|
glDeleteTextures(1, &m_TextureSmooth1);
|
2014-10-05 22:02:04 +02:00
|
|
|
|
|
|
|
if (m_TextureSmooth2)
|
2012-08-07 00:38:42 +02:00
|
|
|
glDeleteTextures(1, &m_TextureSmooth2);
|
2014-10-05 22:02:04 +02:00
|
|
|
|
2011-05-29 17:02:02 +02:00
|
|
|
m_Texture = 0;
|
2014-10-05 22:02:04 +02:00
|
|
|
m_TextureSmooth1 = 0;
|
|
|
|
m_TextureSmooth2 = 0;
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLOSTexture::MakeDirty()
|
|
|
|
{
|
|
|
|
m_Dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLOSTexture::BindTexture(int unit)
|
|
|
|
{
|
|
|
|
if (m_Dirty)
|
|
|
|
{
|
|
|
|
RecomputeTexture(unit);
|
|
|
|
m_Dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_Renderer.BindTexture(unit, m_Texture);
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:38:42 +02:00
|
|
|
GLuint CLOSTexture::GetTextureSmooth()
|
|
|
|
{
|
2012-08-07 20:28:23 +02:00
|
|
|
if (CRenderer::IsInitialised() && !g_Renderer.m_Options.m_SmoothLOS)
|
2012-08-07 00:38:42 +02:00
|
|
|
return GetTexture();
|
|
|
|
else
|
|
|
|
return whichTex ? m_TextureSmooth1 : m_TextureSmooth2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CLOSTexture::InterpolateLOS()
|
|
|
|
{
|
2012-08-07 20:28:23 +02:00
|
|
|
if (CRenderer::IsInitialised() && !g_Renderer.m_Options.m_SmoothLOS)
|
2012-08-07 00:38:42 +02:00
|
|
|
return;
|
2014-10-05 22:02:04 +02:00
|
|
|
|
|
|
|
if (!m_ShaderInitialized)
|
|
|
|
{
|
|
|
|
if (!CreateShader())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// RecomputeTexture(0) will not cause the ConstructTexture to run.
|
|
|
|
// Force the textures to be created.
|
|
|
|
DeleteTexture();
|
|
|
|
ConstructTexture(0);
|
|
|
|
m_Dirty = true;
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:38:42 +02:00
|
|
|
if (m_Dirty)
|
|
|
|
{
|
|
|
|
RecomputeTexture(0);
|
|
|
|
m_Dirty = false;
|
|
|
|
}
|
2014-10-05 22:02:04 +02:00
|
|
|
|
2012-08-07 00:38:42 +02:00
|
|
|
GLint originalFBO;
|
2012-08-19 09:05:11 +02:00
|
|
|
glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &originalFBO);
|
2014-10-05 22:02:04 +02:00
|
|
|
|
2012-08-19 09:05:11 +02:00
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, m_smoothFbo);
|
2016-11-23 14:02:58 +01:00
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D,
|
2012-08-07 00:38:42 +02:00
|
|
|
whichTex ? m_TextureSmooth2 : m_TextureSmooth1, 0);
|
|
|
|
|
|
|
|
GLenum status = pglCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
|
|
|
|
if (status != GL_FRAMEBUFFER_COMPLETE_EXT)
|
|
|
|
{
|
2015-01-22 21:31:30 +01:00
|
|
|
LOGWARNING("LOS framebuffer object incomplete: 0x%04X", status);
|
2012-08-07 00:38:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_smoothShader->BeginPass();
|
|
|
|
CShaderProgramPtr shader = m_smoothShader->GetShader();
|
|
|
|
|
2012-08-31 20:33:03 +02:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
|
|
|
|
shader->Bind();
|
2012-08-07 00:38:42 +02:00
|
|
|
|
2013-09-29 15:19:52 +02:00
|
|
|
shader->BindTexture(str_losTex1, m_Texture);
|
|
|
|
shader->BindTexture(str_losTex2, whichTex ? m_TextureSmooth1 : m_TextureSmooth2);
|
2012-08-07 00:38:42 +02:00
|
|
|
|
2013-09-29 15:19:52 +02:00
|
|
|
shader->Uniform(str_delta, (float)g_Renderer.GetTimeManager().GetFrameDelta() * 4.0f, 0.0f, 0.0f, 0.0f);
|
2012-08-07 00:38:42 +02:00
|
|
|
|
2013-11-28 01:57:39 +01:00
|
|
|
const SViewPort oldVp = g_Renderer.GetViewport();
|
|
|
|
const SViewPort vp = { 0, 0, m_TextureSize, m_TextureSize };
|
|
|
|
g_Renderer.SetViewport(vp);
|
2012-08-07 00:38:42 +02:00
|
|
|
|
2013-11-12 02:30:17 +01:00
|
|
|
float quadVerts[] = {
|
|
|
|
1.0f, 1.0f,
|
|
|
|
-1.0f, 1.0f,
|
|
|
|
-1.0f, -1.0f,
|
|
|
|
|
|
|
|
-1.0f, -1.0f,
|
|
|
|
1.0f, -1.0f,
|
|
|
|
1.0f, 1.0f
|
|
|
|
};
|
|
|
|
float quadTex[] = {
|
|
|
|
1.0f, 1.0f,
|
|
|
|
0.0f, 1.0f,
|
|
|
|
0.0f, 0.0f,
|
|
|
|
|
|
|
|
0.0f, 0.0f,
|
|
|
|
1.0f, 0.0f,
|
|
|
|
1.0f, 1.0f
|
|
|
|
};
|
|
|
|
shader->TexCoordPointer(GL_TEXTURE0, 2, GL_FLOAT, 0, quadTex);
|
|
|
|
shader->VertexPointer(2, GL_FLOAT, 0, quadVerts);
|
|
|
|
shader->AssertPointersBound();
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
2012-08-07 00:38:42 +02:00
|
|
|
|
2013-11-28 01:57:39 +01:00
|
|
|
g_Renderer.SetViewport(oldVp);
|
2013-11-12 02:30:17 +01:00
|
|
|
|
2012-08-07 00:38:42 +02:00
|
|
|
shader->Unbind();
|
|
|
|
m_smoothShader->EndPass();
|
|
|
|
|
2012-08-19 09:05:11 +02:00
|
|
|
pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, 0, 0);
|
2012-08-07 00:38:42 +02:00
|
|
|
|
|
|
|
pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, originalFBO);
|
|
|
|
|
|
|
|
whichTex = !whichTex;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-26 21:17:21 +01:00
|
|
|
GLuint CLOSTexture::GetTexture()
|
|
|
|
{
|
|
|
|
if (m_Dirty)
|
|
|
|
{
|
|
|
|
RecomputeTexture(0);
|
|
|
|
m_Dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_Texture;
|
|
|
|
}
|
|
|
|
|
2012-01-29 13:04:39 +01:00
|
|
|
const CMatrix3D& CLOSTexture::GetTextureMatrix()
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(!m_Dirty);
|
2012-01-29 13:04:39 +01:00
|
|
|
return m_TextureMatrix;
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
2014-07-14 19:15:22 +02:00
|
|
|
const CMatrix3D* CLOSTexture::GetMinimapTextureMatrix()
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
2011-04-30 15:01:45 +02:00
|
|
|
ENSURE(!m_Dirty);
|
2014-07-14 19:15:22 +02:00
|
|
|
return &m_MinimapTextureMatrix;
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLOSTexture::ConstructTexture(int unit)
|
|
|
|
{
|
2011-04-05 22:32:03 +02:00
|
|
|
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
|
2012-02-08 03:46:15 +01:00
|
|
|
if (!cmpTerrain)
|
2011-04-05 22:32:03 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_MapSize = cmpTerrain->GetVerticesPerSide();
|
|
|
|
|
2014-05-25 15:30:39 +02:00
|
|
|
m_TextureSize = (GLsizei)round_up_to_pow2(round_up((size_t)m_MapSize + g_BlurSize - 1, g_SubTextureAlignment));
|
2011-02-03 02:12:24 +01:00
|
|
|
|
|
|
|
glGenTextures(1, &m_Texture);
|
2011-06-26 23:47:07 +02:00
|
|
|
|
2015-03-16 00:59:48 +01:00
|
|
|
// Initialise texture with SoD color, for the areas we don't
|
2011-06-26 23:47:07 +02:00
|
|
|
// overwrite with glTexSubImage2D later
|
2012-08-07 00:38:42 +02:00
|
|
|
u8* texData = new u8[m_TextureSize * m_TextureSize * 4];
|
|
|
|
memset(texData, 0x00, m_TextureSize * m_TextureSize * 4);
|
|
|
|
|
2012-08-07 20:28:23 +02:00
|
|
|
if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS)
|
2012-08-07 00:38:42 +02:00
|
|
|
{
|
|
|
|
glGenTextures(1, &m_TextureSmooth1);
|
|
|
|
glGenTextures(1, &m_TextureSmooth2);
|
|
|
|
|
|
|
|
g_Renderer.BindTexture(unit, m_TextureSmooth1);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_TextureSize, m_TextureSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texData);
|
|
|
|
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);
|
|
|
|
|
|
|
|
g_Renderer.BindTexture(unit, m_TextureSmooth2);
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_TextureSize, m_TextureSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texData);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_Renderer.BindTexture(unit, m_Texture);
|
2012-02-13 15:02:14 +01:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, m_TextureSize, m_TextureSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texData);
|
2011-02-03 02:12:24 +01:00
|
|
|
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);
|
2012-08-07 00:38:42 +02:00
|
|
|
|
|
|
|
delete[] texData;
|
|
|
|
|
2011-02-03 03:33:37 +01:00
|
|
|
{
|
|
|
|
// Texture matrix: We want to map
|
|
|
|
// world pos (0, y, 0) (i.e. first vertex)
|
|
|
|
// onto texcoord (0.5/texsize, 0.5/texsize) (i.e. middle of first texel);
|
|
|
|
// world pos ((mapsize-1)*cellsize, y, (mapsize-1)*cellsize) (i.e. last vertex)
|
|
|
|
// onto texcoord ((mapsize-0.5) / texsize, (mapsize-0.5) / texsize) (i.e. middle of last texel)
|
|
|
|
|
2012-01-12 13:51:10 +01:00
|
|
|
float s = (m_MapSize-1) / (float)(m_TextureSize * (m_MapSize-1) * TERRAIN_TILE_SIZE);
|
2011-02-03 03:33:37 +01:00
|
|
|
float t = 0.5f / m_TextureSize;
|
|
|
|
m_TextureMatrix.SetZero();
|
|
|
|
m_TextureMatrix._11 = s;
|
|
|
|
m_TextureMatrix._23 = s;
|
|
|
|
m_TextureMatrix._14 = t;
|
|
|
|
m_TextureMatrix._24 = t;
|
|
|
|
m_TextureMatrix._44 = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Minimap matrix: We want to map UV (0,0)-(1,1) onto (0,0)-(mapsize/texsize, mapsize/texsize)
|
|
|
|
|
|
|
|
float s = m_MapSize / (float)m_TextureSize;
|
|
|
|
m_MinimapTextureMatrix.SetZero();
|
|
|
|
m_MinimapTextureMatrix._11 = s;
|
|
|
|
m_MinimapTextureMatrix._22 = s;
|
|
|
|
m_MinimapTextureMatrix._44 = 1;
|
|
|
|
}
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLOSTexture::RecomputeTexture(int unit)
|
|
|
|
{
|
2011-05-29 17:02:02 +02:00
|
|
|
// If the map was resized, delete and regenerate the texture
|
|
|
|
if (m_Texture)
|
|
|
|
{
|
|
|
|
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
|
2012-02-08 03:46:15 +01:00
|
|
|
if (cmpTerrain && m_MapSize != (ssize_t)cmpTerrain->GetVerticesPerSide())
|
2011-05-29 17:02:02 +02:00
|
|
|
DeleteTexture();
|
|
|
|
}
|
|
|
|
|
2012-08-07 00:38:42 +02:00
|
|
|
bool recreated = false;
|
2011-02-03 02:12:24 +01:00
|
|
|
if (!m_Texture)
|
2012-08-07 00:38:42 +02:00
|
|
|
{
|
2011-02-03 02:12:24 +01:00
|
|
|
ConstructTexture(unit);
|
2012-08-07 00:38:42 +02:00
|
|
|
recreated = true;
|
|
|
|
}
|
2011-02-03 02:12:24 +01:00
|
|
|
|
|
|
|
PROFILE("recompute LOS texture");
|
|
|
|
|
|
|
|
std::vector<u8> losData;
|
2014-05-25 15:30:39 +02:00
|
|
|
size_t pitch;
|
|
|
|
losData.resize(GetBitmapSize(m_MapSize, m_MapSize, &pitch));
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2011-04-05 22:32:03 +02:00
|
|
|
CmpPtr<ICmpRangeManager> cmpRangeManager(m_Simulation, SYSTEM_ENTITY);
|
2012-02-08 03:46:15 +01:00
|
|
|
if (!cmpRangeManager)
|
2011-02-03 02:12:24 +01:00
|
|
|
return;
|
|
|
|
|
2016-02-06 13:56:41 +01:00
|
|
|
ICmpRangeManager::CLosQuerier los(cmpRangeManager->GetLosQuerier(g_Game->GetSimulation2()->GetSimContext().GetCurrentDisplayedPlayer()));
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2014-05-25 15:30:39 +02:00
|
|
|
GenerateBitmap(los, &losData[0], m_MapSize, m_MapSize, pitch);
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2012-08-07 20:28:23 +02:00
|
|
|
if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS && recreated)
|
2012-08-07 00:38:42 +02:00
|
|
|
{
|
|
|
|
g_Renderer.BindTexture(unit, m_TextureSmooth1);
|
2014-05-25 15:30:39 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]);
|
2012-08-07 00:38:42 +02:00
|
|
|
g_Renderer.BindTexture(unit, m_TextureSmooth2);
|
2014-05-25 15:30:39 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]);
|
2012-08-07 00:38:42 +02:00
|
|
|
}
|
|
|
|
|
2011-02-03 02:12:24 +01:00
|
|
|
g_Renderer.BindTexture(unit, m_Texture);
|
2014-05-25 15:30:39 +02:00
|
|
|
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]);
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
2014-05-25 15:30:39 +02:00
|
|
|
size_t CLOSTexture::GetBitmapSize(size_t w, size_t h, size_t* pitch)
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
2014-05-25 15:30:39 +02:00
|
|
|
*pitch = round_up(w + g_BlurSize - 1, g_SubTextureAlignment);
|
|
|
|
return *pitch * (h + g_BlurSize - 1);
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
|
2014-05-25 15:30:39 +02:00
|
|
|
void CLOSTexture::GenerateBitmap(ICmpRangeManager::CLosQuerier los, u8* losData, size_t w, size_t h, size_t pitch)
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
|
|
|
u8 *dataPtr = losData;
|
|
|
|
|
|
|
|
// Initialise the top padding
|
|
|
|
for (size_t j = 0; j < g_BlurSize/2; ++j)
|
2014-05-25 15:30:39 +02:00
|
|
|
for (size_t i = 0; i < pitch; ++i)
|
2011-02-03 02:12:24 +01:00
|
|
|
*dataPtr++ = 0;
|
|
|
|
|
|
|
|
for (size_t j = 0; j < h; ++j)
|
|
|
|
{
|
|
|
|
// Initialise the left padding
|
|
|
|
for (size_t i = 0; i < g_BlurSize/2; ++i)
|
|
|
|
*dataPtr++ = 0;
|
|
|
|
|
|
|
|
// Fill in the visibility data
|
|
|
|
for (size_t i = 0; i < w; ++i)
|
|
|
|
{
|
2011-04-27 23:03:08 +02:00
|
|
|
if (los.IsVisible_UncheckedRange(i, j))
|
2011-02-03 02:12:24 +01:00
|
|
|
*dataPtr++ = 255;
|
2011-04-27 23:03:08 +02:00
|
|
|
else if (los.IsExplored_UncheckedRange(i, j))
|
2011-02-03 02:12:24 +01:00
|
|
|
*dataPtr++ = 127;
|
|
|
|
else
|
|
|
|
*dataPtr++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the right padding
|
2014-05-25 15:30:39 +02:00
|
|
|
for (size_t i = 0; i < pitch - w - g_BlurSize/2; ++i)
|
2011-02-03 02:12:24 +01:00
|
|
|
*dataPtr++ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the bottom padding
|
|
|
|
for (size_t j = 0; j < g_BlurSize/2; ++j)
|
2014-05-25 15:30:39 +02:00
|
|
|
for (size_t i = 0; i < pitch; ++i)
|
2011-02-03 02:12:24 +01:00
|
|
|
*dataPtr++ = 0;
|
|
|
|
|
|
|
|
// Horizontal blur:
|
|
|
|
|
|
|
|
for (size_t j = g_BlurSize/2; j < h + g_BlurSize/2; ++j)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < w; ++i)
|
|
|
|
{
|
2014-05-25 15:30:39 +02:00
|
|
|
u8* d = &losData[i+j*pitch];
|
2011-02-05 19:19:28 +01:00
|
|
|
*d = (
|
|
|
|
1*d[0] +
|
|
|
|
6*d[1] +
|
|
|
|
15*d[2] +
|
|
|
|
20*d[3] +
|
|
|
|
15*d[4] +
|
|
|
|
6*d[5] +
|
|
|
|
1*d[6]
|
|
|
|
) / 64;
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertical blur:
|
|
|
|
|
2011-02-06 12:39:06 +01:00
|
|
|
for (size_t j = 0; j < h; ++j)
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
2011-02-06 12:39:06 +01:00
|
|
|
for (size_t i = 0; i < w; ++i)
|
2011-02-03 02:12:24 +01:00
|
|
|
{
|
2014-05-25 15:30:39 +02:00
|
|
|
u8* d = &losData[i+j*pitch];
|
2011-02-05 19:19:28 +01:00
|
|
|
*d = (
|
2014-05-25 15:30:39 +02:00
|
|
|
1*d[0*pitch] +
|
|
|
|
6*d[1*pitch] +
|
|
|
|
15*d[2*pitch] +
|
|
|
|
20*d[3*pitch] +
|
|
|
|
15*d[4*pitch] +
|
|
|
|
6*d[5*pitch] +
|
|
|
|
1*d[6*pitch]
|
2011-02-05 19:19:28 +01:00
|
|
|
) / 64;
|
2011-02-03 02:12:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|