2023-01-06 01:39:25 +01:00
|
|
|
/* Copyright (C) 2023 Wildfire Games.
|
2023-12-03 01:30:12 +01:00
|
|
|
* This file is part of 0 A.D.
|
2011-02-03 02:12:24 +01:00
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2011-02-03 02:12:24 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2023-12-03 01:30:12 +01:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2011-02-03 02:12:24 +01:00
|
|
|
* 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
|
2023-12-03 01:30:12 +01:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2011-02-03 02:12:24 +01:00
|
|
|
*/
|
|
|
|
|
2021-05-31 22:47:03 +02:00
|
|
|
#ifndef INCLUDED_LOSTEXTURE
|
|
|
|
#define INCLUDED_LOSTEXTURE
|
|
|
|
|
2020-11-21 12:20:29 +01:00
|
|
|
#include "graphics/ShaderTechniquePtr.h"
|
2021-05-15 19:27:01 +02:00
|
|
|
#include "maths/Matrix3D.h"
|
2022-05-26 18:36:57 +02:00
|
|
|
#include "renderer/backend/Format.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/IDeviceCommandContext.h"
|
|
|
|
#include "renderer/backend/IFramebuffer.h"
|
2023-01-06 01:39:25 +01:00
|
|
|
#include "renderer/backend/IShaderProgram.h"
|
2022-05-09 00:02:46 +02:00
|
|
|
#include "renderer/backend/ITexture.h"
|
2012-08-07 00:38:42 +02:00
|
|
|
|
2022-01-05 15:49:54 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2020-11-21 12:20:29 +01:00
|
|
|
class CLosQuerier;
|
2011-04-05 22:32:03 +02:00
|
|
|
class CSimulation2;
|
|
|
|
|
2011-02-03 02:12:24 +01:00
|
|
|
/**
|
|
|
|
* Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for
|
|
|
|
* rendering and for the minimap.
|
|
|
|
*/
|
|
|
|
class CLOSTexture
|
|
|
|
{
|
|
|
|
NONCOPYABLE(CLOSTexture);
|
|
|
|
friend class TestLOSTexture;
|
|
|
|
|
|
|
|
public:
|
2011-04-05 22:32:03 +02:00
|
|
|
CLOSTexture(CSimulation2& simulation);
|
2011-02-03 02:12:24 +01:00
|
|
|
~CLOSTexture();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the LOS texture as needing recomputation. Call this after each
|
|
|
|
* simulation update, to ensure responsive updates.
|
|
|
|
*/
|
|
|
|
void MakeDirty();
|
|
|
|
|
2011-03-26 21:17:21 +01:00
|
|
|
/**
|
|
|
|
* Recomputes the LOS texture if necessary, and returns the texture handle.
|
|
|
|
* Also potentially switches the current active texture unit, and enables texturing on it.
|
|
|
|
* The texture is in 8-bit ALPHA format.
|
|
|
|
*/
|
2022-05-09 00:02:46 +02:00
|
|
|
Renderer::Backend::ITexture* GetTexture();
|
|
|
|
Renderer::Backend::ITexture* GetTextureSmooth();
|
2011-03-26 21:17:21 +01:00
|
|
|
|
2022-05-09 00:02:46 +02:00
|
|
|
void InterpolateLOS(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
|
2022-01-05 15:49:54 +01:00
|
|
|
|
2011-02-03 02:12:24 +01:00
|
|
|
/**
|
|
|
|
* Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture
|
2021-10-11 14:39:01 +02:00
|
|
|
* coordinates, in the form expected by a matrix uniform.
|
2022-01-10 18:58:51 +01:00
|
|
|
* This must only be called after InterpolateLOS.
|
2011-02-03 02:12:24 +01:00
|
|
|
*/
|
2012-01-29 13:04:39 +01:00
|
|
|
const CMatrix3D& GetTextureMatrix();
|
2011-02-03 02:12:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a matrix to map (0,0)-(1,1) texture coordinates onto LOS texture
|
2021-10-11 14:39:01 +02:00
|
|
|
* coordinates, in the form expected by a matrix uniform.
|
2022-01-10 18:58:51 +01:00
|
|
|
* This must only be called after InterpolateLOS.
|
2011-02-03 02:12:24 +01:00
|
|
|
*/
|
2022-01-05 15:49:54 +01:00
|
|
|
const CMatrix3D& GetMinimapTextureMatrix();
|
2011-02-03 02:12:24 +01:00
|
|
|
|
|
|
|
private:
|
2011-05-29 17:02:02 +02:00
|
|
|
void DeleteTexture();
|
2014-10-05 22:02:04 +02:00
|
|
|
bool CreateShader();
|
2022-05-09 00:02:46 +02:00
|
|
|
void ConstructTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
|
|
|
|
void RecomputeTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2014-05-25 15:30:39 +02:00
|
|
|
size_t GetBitmapSize(size_t w, size_t h, size_t* pitch);
|
2020-06-14 22:39:03 +02:00
|
|
|
void GenerateBitmap(const CLosQuerier& los, u8* losData, size_t w, size_t h, size_t pitch);
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2011-04-05 22:32:03 +02:00
|
|
|
CSimulation2& m_Simulation;
|
|
|
|
|
2022-01-06 23:44:54 +01:00
|
|
|
bool m_Dirty = true;
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2022-01-06 23:44:54 +01:00
|
|
|
bool m_ShaderInitialized = false;
|
2014-10-05 22:02:04 +02:00
|
|
|
|
2022-05-26 18:36:57 +02:00
|
|
|
// We need to choose the smallest format. We always use the red channel but
|
|
|
|
// R8_UNORM might be unavailable on some platforms. So we fallback to
|
|
|
|
// R8G8B8A8_UNORM.
|
|
|
|
Renderer::Backend::Format m_TextureFormat =
|
|
|
|
Renderer::Backend::Format::UNDEFINED;
|
|
|
|
size_t m_TextureFormatStride = 0;
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::ITexture>
|
2022-02-05 17:59:23 +01:00
|
|
|
m_Texture, m_SmoothTextures[2];
|
2023-01-06 01:39:25 +01:00
|
|
|
Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2022-02-05 17:59:23 +01:00
|
|
|
uint32_t m_WhichTexture = 0;
|
2022-06-06 22:48:18 +02:00
|
|
|
double m_LastTextureRecomputeTime = 0.0;
|
2016-11-23 12:18:37 +01:00
|
|
|
|
2022-02-05 17:59:23 +01:00
|
|
|
// We update textures once a frame, so we change a Framebuffer once a frame.
|
|
|
|
// That allows us to use two ping-pong FBOs instead of checking completeness
|
|
|
|
// of Framebuffer each frame.
|
2022-05-09 00:02:46 +02:00
|
|
|
std::unique_ptr<Renderer::Backend::IFramebuffer>
|
2022-02-05 17:59:23 +01:00
|
|
|
m_SmoothFramebuffers[2];
|
2022-01-06 23:44:54 +01:00
|
|
|
CShaderTechniquePtr m_SmoothTech;
|
2011-02-03 02:12:24 +01:00
|
|
|
|
2022-01-06 23:44:54 +01:00
|
|
|
size_t m_MapSize = 0; // vertexes per side
|
2011-02-03 03:33:37 +01:00
|
|
|
|
|
|
|
CMatrix3D m_TextureMatrix;
|
|
|
|
CMatrix3D m_MinimapTextureMatrix;
|
2011-02-03 02:12:24 +01:00
|
|
|
};
|
2021-05-31 22:47:03 +02:00
|
|
|
|
2021-12-25 01:26:10 +01:00
|
|
|
#endif // INCLUDED_LOSTEXTURE
|