Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/LOSTexture.cpp
/* Copyright (C) 2019 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
historic_bruno: 2019 | |||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
Show All 12 Lines | |||||
#include "graphics/ShaderManager.h" | #include "graphics/ShaderManager.h" | ||||
#include "graphics/Terrain.h" | #include "graphics/Terrain.h" | ||||
#include "lib/bits.h" | #include "lib/bits.h" | ||||
#include "lib/config2.h" | #include "lib/config2.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/Game.h" | #include "ps/Game.h" | ||||
#include "ps/Profile.h" | #include "ps/Profile.h" | ||||
#include "renderer/Renderer.h" | #include "renderer/Renderer.h" | ||||
#include "renderer/RenderingOptions.h" | |||||
#include "renderer/TimeManager.h" | #include "renderer/TimeManager.h" | ||||
#include "simulation2/Simulation2.h" | #include "simulation2/Simulation2.h" | ||||
#include "simulation2/components/ICmpRangeManager.h" | #include "simulation2/components/ICmpRangeManager.h" | ||||
#include "simulation2/components/ICmpTerrain.h" | #include "simulation2/components/ICmpTerrain.h" | ||||
/* | /* | ||||
The LOS bitmap is computed with one value per map vertex, based on | The LOS bitmap is computed with one value per map vertex, based on | ||||
Show All 20 Lines | |||||
// value. (See Trac #2594). Multiples of 4 are possibly good for performance anyway. | // value. (See Trac #2594). Multiples of 4 are possibly good for performance anyway. | ||||
static const size_t g_SubTextureAlignment = 4; | static const size_t g_SubTextureAlignment = 4; | ||||
CLOSTexture::CLOSTexture(CSimulation2& simulation) | CLOSTexture::CLOSTexture(CSimulation2& simulation) | ||||
: m_Simulation(simulation), m_Dirty(true), m_ShaderInitialized(false), | : m_Simulation(simulation), m_Dirty(true), m_ShaderInitialized(false), | ||||
m_Texture(0), m_TextureSmooth1(0), m_TextureSmooth2(0), m_smoothFbo(0), | m_Texture(0), m_TextureSmooth1(0), m_TextureSmooth2(0), m_smoothFbo(0), | ||||
m_MapSize(0), m_TextureSize(0), whichTex(true) | m_MapSize(0), m_TextureSize(0), whichTex(true) | ||||
{ | { | ||||
if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS) | if (CRenderer::IsInitialised() && g_RenderingOptions.GetSmoothLOS()) | ||||
CreateShader(); | CreateShader(); | ||||
} | } | ||||
CLOSTexture::~CLOSTexture() | CLOSTexture::~CLOSTexture() | ||||
{ | { | ||||
if (m_Texture) | if (m_Texture) | ||||
DeleteTexture(); | DeleteTexture(); | ||||
} | } | ||||
// Create the LOS texture engine. Should be ran only once. | // Create the LOS texture engine. Should be ran only once. | ||||
bool CLOSTexture::CreateShader() | bool CLOSTexture::CreateShader() | ||||
{ | { | ||||
m_smoothShader = g_Renderer.GetShaderManager().LoadEffect(str_los_interp); | m_smoothShader = g_Renderer.GetShaderManager().LoadEffect(str_los_interp); | ||||
CShaderProgramPtr shader = m_smoothShader->GetShader(); | CShaderProgramPtr shader = m_smoothShader->GetShader(); | ||||
m_ShaderInitialized = m_smoothShader && shader; | m_ShaderInitialized = m_smoothShader && shader; | ||||
if (!m_ShaderInitialized) | if (!m_ShaderInitialized) | ||||
{ | { | ||||
LOGERROR("Failed to load SmoothLOS shader, disabling."); | LOGERROR("Failed to load SmoothLOS shader, disabling."); | ||||
g_Renderer.m_Options.m_SmoothLOS = false; | g_RenderingOptions.SetSmoothLOS(false); | ||||
return false; | return false; | ||||
} | } | ||||
pglGenFramebuffersEXT(1, &m_smoothFbo); | pglGenFramebuffersEXT(1, &m_smoothFbo); | ||||
return true; | return true; | ||||
} | } | ||||
void CLOSTexture::DeleteTexture() | void CLOSTexture::DeleteTexture() | ||||
Show All 24 Lines | if (m_Dirty) | ||||
m_Dirty = false; | m_Dirty = false; | ||||
} | } | ||||
g_Renderer.BindTexture(unit, m_Texture); | g_Renderer.BindTexture(unit, m_Texture); | ||||
} | } | ||||
GLuint CLOSTexture::GetTextureSmooth() | GLuint CLOSTexture::GetTextureSmooth() | ||||
{ | { | ||||
if (CRenderer::IsInitialised() && !g_Renderer.m_Options.m_SmoothLOS) | if (CRenderer::IsInitialised() && !g_RenderingOptions.GetSmoothLOS()) | ||||
return GetTexture(); | return GetTexture(); | ||||
else | else | ||||
return whichTex ? m_TextureSmooth1 : m_TextureSmooth2; | return whichTex ? m_TextureSmooth1 : m_TextureSmooth2; | ||||
} | } | ||||
void CLOSTexture::InterpolateLOS() | void CLOSTexture::InterpolateLOS() | ||||
{ | { | ||||
if (CRenderer::IsInitialised() && !g_Renderer.m_Options.m_SmoothLOS) | if (CRenderer::IsInitialised() && !g_RenderingOptions.GetSmoothLOS()) | ||||
return; | return; | ||||
if (!m_ShaderInitialized) | if (!m_ShaderInitialized) | ||||
{ | { | ||||
if (!CreateShader()) | if (!CreateShader()) | ||||
return; | return; | ||||
// RecomputeTexture(0) will not cause the ConstructTexture to run. | // RecomputeTexture(0) will not cause the ConstructTexture to run. | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | void CLOSTexture::ConstructTexture(int unit) | ||||
glGenTextures(1, &m_Texture); | glGenTextures(1, &m_Texture); | ||||
// Initialise texture with SoD color, for the areas we don't | // Initialise texture with SoD color, for the areas we don't | ||||
// overwrite with glTexSubImage2D later | // overwrite with glTexSubImage2D later | ||||
u8* texData = new u8[m_TextureSize * m_TextureSize * 4]; | u8* texData = new u8[m_TextureSize * m_TextureSize * 4]; | ||||
memset(texData, 0x00, m_TextureSize * m_TextureSize * 4); | memset(texData, 0x00, m_TextureSize * m_TextureSize * 4); | ||||
if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS) | if (CRenderer::IsInitialised() && g_RenderingOptions.GetSmoothLOS()) | ||||
{ | { | ||||
glGenTextures(1, &m_TextureSmooth1); | glGenTextures(1, &m_TextureSmooth1); | ||||
glGenTextures(1, &m_TextureSmooth2); | glGenTextures(1, &m_TextureSmooth2); | ||||
g_Renderer.BindTexture(unit, m_TextureSmooth1); | g_Renderer.BindTexture(unit, m_TextureSmooth1); | ||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_TextureSize, m_TextureSize, 0, GL_ALPHA, GL_UNSIGNED_BYTE, texData); | 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_MIN_FILTER, GL_LINEAR); | ||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | void CLOSTexture::RecomputeTexture(int unit) | ||||
CmpPtr<ICmpRangeManager> cmpRangeManager(m_Simulation, SYSTEM_ENTITY); | CmpPtr<ICmpRangeManager> cmpRangeManager(m_Simulation, SYSTEM_ENTITY); | ||||
if (!cmpRangeManager) | if (!cmpRangeManager) | ||||
return; | return; | ||||
ICmpRangeManager::CLosQuerier los(cmpRangeManager->GetLosQuerier(g_Game->GetSimulation2()->GetSimContext().GetCurrentDisplayedPlayer())); | ICmpRangeManager::CLosQuerier los(cmpRangeManager->GetLosQuerier(g_Game->GetSimulation2()->GetSimContext().GetCurrentDisplayedPlayer())); | ||||
GenerateBitmap(los, &losData[0], m_MapSize, m_MapSize, pitch); | GenerateBitmap(los, &losData[0], m_MapSize, m_MapSize, pitch); | ||||
if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS && recreated) | if (CRenderer::IsInitialised() && g_RenderingOptions.GetSmoothLOS() && recreated) | ||||
{ | { | ||||
g_Renderer.BindTexture(unit, m_TextureSmooth1); | g_Renderer.BindTexture(unit, m_TextureSmooth1); | ||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]); | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]); | ||||
g_Renderer.BindTexture(unit, m_TextureSmooth2); | g_Renderer.BindTexture(unit, m_TextureSmooth2); | ||||
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]); | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pitch, m_MapSize, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]); | ||||
} | } | ||||
g_Renderer.BindTexture(unit, m_Texture); | g_Renderer.BindTexture(unit, m_Texture); | ||||
▲ Show 20 Lines • Show All 83 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
2019