1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/* Copyright (C) 2018 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/>.
*/
#include "precompiled.h"
#include "TerritoryTexture.h"
#include "graphics/Terrain.h"
#include "lib/bits.h"
#include "ps/Profile.h"
#include "ps/Shapes.h"
#include "renderer/Renderer.h"
#include "simulation2/Simulation2.h"
#include "simulation2/helpers/Pathfinding.h"
#include "simulation2/components/ICmpPlayer.h"
#include "simulation2/components/ICmpPlayerManager.h"
#include "simulation2/components/ICmpTerrain.h"
#include "simulation2/components/ICmpTerritoryManager.h"
// TODO: There's a lot of duplication with CLOSTexture - might be nice to refactor a bit
CTerritoryTexture::CTerritoryTexture(CSimulation2& simulation) :
m_Simulation(simulation), m_DirtyID(0), m_Texture(0), m_MapSize(0), m_TextureSize(0)
{
}
CTerritoryTexture::~CTerritoryTexture()
{
if (m_Texture)
DeleteTexture();
}
void CTerritoryTexture::DeleteTexture()
{
glDeleteTextures(1, &m_Texture);
m_Texture = 0;
}
bool CTerritoryTexture::UpdateDirty()
{
CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
return cmpTerritoryManager && cmpTerritoryManager->NeedUpdateTexture(&m_DirtyID);
}
void CTerritoryTexture::BindTexture(int unit)
{
if (UpdateDirty())
RecomputeTexture(unit);
g_Renderer.BindTexture(unit, m_Texture);
}
GLuint CTerritoryTexture::GetTexture()
{
if (UpdateDirty())
RecomputeTexture(0);
return m_Texture;
}
const float* CTerritoryTexture::GetTextureMatrix()
{
ENSURE(!UpdateDirty());
return &m_TextureMatrix._11;
}
const CMatrix3D* CTerritoryTexture::GetMinimapTextureMatrix()
{
ENSURE(!UpdateDirty());
return &m_MinimapTextureMatrix;
}
void CTerritoryTexture::ConstructTexture(int unit)
{
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
if (!cmpTerrain)
return;
// Convert size from terrain tiles to territory tiles
m_MapSize = cmpTerrain->GetTilesPerSide() * Pathfinding::NAVCELLS_PER_TILE / ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE;
m_TextureSize = (GLsizei)round_up_to_pow2((size_t)m_MapSize);
glGenTextures(1, &m_Texture);
g_Renderer.BindTexture(unit, m_Texture);
// Initialise texture with transparency, for the areas we don't
// overwrite with glTexSubImage2D later
u8* texData = new u8[m_TextureSize * m_TextureSize * 4];
memset(texData, 0x00, m_TextureSize * m_TextureSize * 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_TextureSize, m_TextureSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
delete[] 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);
{
// Texture matrix: We want to map
// world pos (0, y, 0) (i.e. bottom-left of first tile)
// onto texcoord (0, 0) (i.e. bottom-left of first texel);
// world pos (mapsize*cellsize, y, mapsize*cellsize) (i.e. top-right of last tile)
// onto texcoord (mapsize / texsize, mapsize / texsize) (i.e. top-right of last texel)
float s = 1.f / (float)(m_TextureSize * TERRAIN_TILE_SIZE);
float t = 0.f;
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;
}
}
void CTerritoryTexture::RecomputeTexture(int unit)
{
// If the map was resized, delete and regenerate the texture
if (m_Texture)
{
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
if (cmpTerrain && m_MapSize != (ssize_t)cmpTerrain->GetVerticesPerSide())
DeleteTexture();
}
if (!m_Texture)
ConstructTexture(unit);
PROFILE("recompute territory texture");
CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
if (!cmpTerritoryManager)
return;
std::vector<u8> bitmap(m_MapSize * m_MapSize * 4);
GenerateBitmap(cmpTerritoryManager->GetTerritoryGrid(), &bitmap[0], m_MapSize, m_MapSize);
g_Renderer.BindTexture(unit, m_Texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_MapSize, m_MapSize, GL_RGBA, GL_UNSIGNED_BYTE, &bitmap[0]);
}
void CTerritoryTexture::GenerateBitmap(const Grid<u8>& territories, u8* bitmap, ssize_t w, ssize_t h)
{
int alphaMax = 0xC0;
int alphaFalloff = 0x20;
CmpPtr<ICmpPlayerManager> cmpPlayerManager(m_Simulation, SYSTEM_ENTITY);
std::vector<CColor> colors;
i32 numPlayers = cmpPlayerManager->GetNumPlayers();
for (i32 p = 0; p < numPlayers; ++p)
{
CColor color(1, 0, 1, 1);
CmpPtr<ICmpPlayer> cmpPlayer(m_Simulation, cmpPlayerManager->GetPlayerByID(p));
if (cmpPlayer)
color = cmpPlayer->GetDisplayedColor();
colors.push_back(color);
}
u8* p = bitmap;
for (ssize_t j = 0; j < h; ++j)
for (ssize_t i = 0; i < w; ++i)
{
u8 val = territories.get(i, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK;
CColor color(1, 0, 1, 1);
if (val < colors.size())
color = colors[val];
*p++ = (int)(color.r * 255.f);
*p++ = (int)(color.g * 255.f);
*p++ = (int)(color.b * 255.f);
// Use alphaMax for borders and gaia territory; these tiles will be deleted later
if (val == 0 ||
(i > 0 && (territories.get(i-1, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
(i < w-1 && (territories.get(i+1, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
(j > 0 && (territories.get(i, j-1) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
(j < h-1 && (territories.get(i, j+1) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val))
*p++ = alphaMax;
else
*p++ = 0x00;
}
// Do a low-quality cheap blur effect
for (ssize_t j = 0; j < h; ++j)
{
int a;
a = 0;
for (ssize_t i = 0; i < w; ++i)
{
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
bitmap[(j*w+i)*4 + 3] = a;
}
a = 0;
for (ssize_t i = w-1; i >= 0; --i)
{
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
bitmap[(j*w+i)*4 + 3] = a;
}
}
for (ssize_t i = 0; i < w; ++i)
{
int a;
a = 0;
for (ssize_t j = 0; j < w; ++j)
{
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
bitmap[(j*w+i)*4 + 3] = a;
}
a = 0;
for (ssize_t j = w-1; j >= 0; --j)
{
a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
bitmap[(j*w+i)*4 + 3] = a;
}
}
// Add a gap between the boundaries, by deleting the max-alpha tiles
for (ssize_t j = 0; j < h; ++j)
for (ssize_t i = 0; i < w; ++i)
if (bitmap[(j*w+i)*4 + 3] == alphaMax)
bitmap[(j*w+i)*4 + 3] = 0;
}
|