Pyrogenesis  trunk
LOSTexture.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_LOSTEXTURE
19 #define INCLUDED_LOSTEXTURE
20 
22 #include "maths/Matrix3D.h"
28 
29 #include <memory>
30 
31 class CLosQuerier;
32 class CSimulation2;
33 
34 /**
35  * Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for
36  * rendering and for the minimap.
37  */
39 {
41  friend class TestLOSTexture;
42 
43 public:
44  CLOSTexture(CSimulation2& simulation);
45  ~CLOSTexture();
46 
47  /**
48  * Marks the LOS texture as needing recomputation. Call this after each
49  * simulation update, to ensure responsive updates.
50  */
51  void MakeDirty();
52 
53  /**
54  * Recomputes the LOS texture if necessary, and returns the texture handle.
55  * Also potentially switches the current active texture unit, and enables texturing on it.
56  * The texture is in 8-bit ALPHA format.
57  */
60 
61  void InterpolateLOS(Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
62 
63  /**
64  * Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture
65  * coordinates, in the form expected by a matrix uniform.
66  * This must only be called after InterpolateLOS.
67  */
68  const CMatrix3D& GetTextureMatrix();
69 
70  /**
71  * Returns a matrix to map (0,0)-(1,1) texture coordinates onto LOS texture
72  * coordinates, in the form expected by a matrix uniform.
73  * This must only be called after InterpolateLOS.
74  */
76 
77 private:
78  void DeleteTexture();
79  bool CreateShader();
82 
83  size_t GetBitmapSize(size_t w, size_t h, size_t* pitch);
84  void GenerateBitmap(const CLosQuerier& los, u8* losData, size_t w, size_t h, size_t pitch);
85 
87 
88  bool m_Dirty = true;
89 
90  bool m_ShaderInitialized = false;
91 
92  // We need to choose the smallest format. We always use the red channel but
93  // R8_UNORM might be unavailable on some platforms. So we fallback to
94  // R8G8B8A8_UNORM.
98  std::unique_ptr<Renderer::Backend::ITexture>
101 
104 
105  // We update textures once a frame, so we change a Framebuffer once a frame.
106  // That allows us to use two ping-pong FBOs instead of checking completeness
107  // of Framebuffer each frame.
108  std::unique_ptr<Renderer::Backend::IFramebuffer>
111 
112  size_t m_MapSize = 0; // vertexes per side
113 
116 };
117 
118 #endif // INCLUDED_LOSTEXTURE
size_t GetBitmapSize(size_t w, size_t h, size_t *pitch)
Definition: LOSTexture.cpp:405
~CLOSTexture()
Definition: LOSTexture.cpp:72
void DeleteTexture()
Definition: LOSTexture.cpp:107
std::unique_ptr< Renderer::Backend::ITexture > m_Texture
Definition: LOSTexture.h:99
CLOSTexture(CSimulation2 &simulation)
Definition: LOSTexture.cpp:65
CMatrix3D m_MinimapTextureMatrix
Definition: LOSTexture.h:115
Definition: ITexture.h:33
friend class TestLOSTexture
Definition: LOSTexture.h:41
NONCOPYABLE(CLOSTexture)
const CMatrix3D & GetMinimapTextureMatrix()
Returns a matrix to map (0,0)-(1,1) texture coordinates onto LOS texture coordinates, in the form expected by a matrix uniform.
Definition: LOSTexture.cpp:230
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:73
double m_LastTextureRecomputeTime
Definition: LOSTexture.h:103
void InterpolateLOS(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: LOSTexture.cpp:127
uint8_t u8
Definition: types.h:37
bool m_ShaderInitialized
Definition: LOSTexture.h:90
Public API for simulation system.
Definition: Simulation2.h:46
Renderer::Backend::Format m_TextureFormat
Definition: LOSTexture.h:95
Renderer::Backend::IVertexInputLayout * m_VertexInputLayout
Definition: LOSTexture.h:100
Definition: Matrix3D.h:33
Format
Definition: Format.h:27
bool m_Dirty
Definition: LOSTexture.h:88
Renderer::Backend::ITexture * GetTextureSmooth()
Definition: LOSTexture.cpp:119
CSimulation2 & m_Simulation
Definition: LOSTexture.h:86
std::unique_ptr< Renderer::Backend::ITexture > m_SmoothTextures[2]
Definition: LOSTexture.h:99
void MakeDirty()
Marks the LOS texture as needing recomputation.
Definition: LOSTexture.cpp:114
void ConstructTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: LOSTexture.cpp:236
size_t m_TextureFormatStride
Definition: LOSTexture.h:97
void GenerateBitmap(const CLosQuerier &los, u8 *losData, size_t w, size_t h, size_t pitch)
Definition: LOSTexture.cpp:411
bool CreateShader()
Definition: LOSTexture.cpp:82
Renderer::Backend::ITexture * GetTexture()
Recomputes the LOS texture if necessary, and returns the texture handle.
Definition: LOSTexture.cpp:218
CMatrix3D m_TextureMatrix
Definition: LOSTexture.h:114
void RecomputeTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: LOSTexture.cpp:341
CShaderTechniquePtr m_SmoothTech
Definition: LOSTexture.h:110
unsigned int uint32_t
Definition: wposix_types.h:53
size_t m_MapSize
Definition: LOSTexture.h:112
uint32_t m_WhichTexture
Definition: LOSTexture.h:102
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:27
Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for rendering and for the minimap...
Definition: LOSTexture.h:38
std::unique_ptr< Renderer::Backend::IFramebuffer > m_SmoothFramebuffers[2]
Definition: LOSTexture.h:109
const CMatrix3D & GetTextureMatrix()
Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture coordinates, in the form expected by a matrix uniform.
Definition: LOSTexture.cpp:224
Definition: IDeviceCommandContext.h:40
Object providing efficient abstracted access to the LOS state.
Definition: Los.h:46