1 /* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "precompiled.h"
20 #include "renderer/SkyManager.h"
22 #include "graphics/LightEnv.h"
23 #include "graphics/ShaderManager.h"
24 #include "graphics/Terrain.h"
25 #include "graphics/TextureManager.h"
27 #include "lib/tex/tex.h"
28 #include "lib/timer.h"
29 #include "maths/MathUtil.h"
30 #include "ps/CLogger.h"
31 #include "ps/ConfigDB.h"
33 #include "ps/CStrInternStatic.h"
34 #include "ps/Filesystem.h"
36 #include "ps/Loader.h"
37 #include "ps/VideoMode.h"
39 #include "renderer/backend/gl/Device.h"
40 #include "renderer/Renderer.h"
41 #include "renderer/SceneRenderer.h"
42 #include "renderer/RenderingOptions.h"
46 SkyManager::SkyManager()
47 : m_VertexArray(Renderer::Backend::GL::CBuffer::Type::VERTEX
, false)
49 CFG_GET_VAL("showsky", m_RenderSky
);
52 void SkyManager::LoadAndUploadSkyTexturesIfNeeded(
53 Renderer::Backend::GL::CDeviceCommandContext
* deviceCommandContext
)
58 GPU_SCOPED_LABEL(deviceCommandContext
, "Load Sky Textures");
59 static const CStrW images
[NUMBER_OF_TEXTURES
+ 1] = {
68 /*for (size_t i = 0; i < ARRAY_SIZE(m_SkyTexture); ++i)
70 VfsPath path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(s_imageNames[i])+L".dds");
72 CTextureProperties textureProps(path);
73 textureProps.SetWrap(GL_CLAMP_TO_EDGE);
74 CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
76 m_SkyTexture[i] = texture;
79 ///////////////////////////////////////////////////////////////////////////
80 // HACK: THE HORRIBLENESS HERE IS OVER 9000. The following code is a HUGE hack and will be removed completely
81 // as soon as all the hardcoded GL_TEXTURE_2D references are corrected in the TextureManager/OGL/tex libs.
83 Tex textures
[NUMBER_OF_TEXTURES
+ 1];
85 for (size_t i
= 0; i
< NUMBER_OF_TEXTURES
+ 1; ++i
)
87 VfsPath path
= VfsPath("art/textures/skies") / m_SkySet
/ (Path::String(images
[i
]) + L
".dds");
89 std::shared_ptr
<u8
> file
;
91 if (g_VFS
->LoadFile(path
, file
, fileSize
) != INFO::OK
)
93 path
= VfsPath("art/textures/skies") / m_SkySet
/ (Path::String(images
[i
]) + L
".dds.cached.dds");
94 if (g_VFS
->LoadFile(path
, file
, fileSize
) != INFO::OK
)
96 LOGERROR("Error creating sky cubemap '%s', can't load file: '%s'.", m_SkySet
.ToUTF8().c_str(), path
.string8().c_str());
101 textures
[i
].decode(file
, fileSize
);
102 textures
[i
].transform_to((textures
[i
].m_Flags
| TEX_BOTTOM_UP
| TEX_ALPHA
) & ~(TEX_DXT
| TEX_MIPMAPS
));
104 if (!is_pow2(textures
[i
].m_Width
) || !is_pow2(textures
[i
].m_Height
))
106 LOGERROR("Error creating sky cubemap '%s', cube textures should have power of 2 sizes.", m_SkySet
.ToUTF8().c_str());
110 if (textures
[i
].m_Width
!= textures
[0].m_Width
|| textures
[i
].m_Height
!= textures
[0].m_Height
)
112 LOGERROR("Error creating sky cubemap '%s', cube textures have different sizes.", m_SkySet
.ToUTF8().c_str());
117 m_SkyCubeMap
= g_VideoMode
.GetBackendDevice()->CreateTexture("SkyCubeMap",
118 Renderer::Backend::GL::CTexture::Type::TEXTURE_CUBE
,
119 Renderer::Backend::Format::R8G8B8A8
, textures
[0].m_Width
, textures
[0].m_Height
,
120 Renderer::Backend::Sampler::MakeDefaultSampler(
121 Renderer::Backend::Sampler::Filter::LINEAR
,
122 Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE
), 1, 1);
124 std::vector
<u8
> rotated
;
125 for (size_t i
= 0; i
< NUMBER_OF_TEXTURES
+ 1; ++i
)
127 u8
* data
= textures
[i
].get_data();
129 // We need to rotate the side if it's looking up or down.
130 // TODO: maybe it should be done during texture conversion.
131 if (i
== 2 || i
== 3)
133 rotated
.resize(textures
[i
].m_DataSize
);
135 for (size_t y
= 0; y
< textures
[i
].m_Height
; ++y
)
137 for (size_t x
= 0; x
< textures
[i
].m_Width
; ++x
)
139 const size_t invX
= y
;
140 const size_t invY
= textures
[i
].m_Width
- x
- 1;
142 rotated
[(y
* textures
[i
].m_Width
+ x
) * 4 + 0] = data
[(invY
* textures
[i
].m_Width
+ invX
) * 4 + 0];
143 rotated
[(y
* textures
[i
].m_Width
+ x
) * 4 + 1] = data
[(invY
* textures
[i
].m_Width
+ invX
) * 4 + 1];
144 rotated
[(y
* textures
[i
].m_Width
+ x
) * 4 + 2] = data
[(invY
* textures
[i
].m_Width
+ invX
) * 4 + 2];
145 rotated
[(y
* textures
[i
].m_Width
+ x
) * 4 + 3] = data
[(invY
* textures
[i
].m_Width
+ invX
) * 4 + 3];
149 deviceCommandContext
->UploadTexture(
150 m_SkyCubeMap
.get(), Renderer::Backend::Format::R8G8B8A8
,
151 &rotated
[0], textures
[i
].m_DataSize
, 0, i
);
155 deviceCommandContext
->UploadTexture(
156 m_SkyCubeMap
.get(), Renderer::Backend::Format::R8G8B8A8
,
157 data
, textures
[i
].m_DataSize
, 0, i
);
160 ///////////////////////////////////////////////////////////////////////////
163 void SkyManager::SetSkySet(const CStrW
& newSet
)
165 if (newSet
== m_SkySet
)
168 m_SkyCubeMap
.reset();
173 std::vector
<CStrW
> SkyManager::GetSkySets() const
175 std::vector
<CStrW
> skies
;
177 // Find all subdirectories in art/textures/skies
179 const VfsPath
path(L
"art/textures/skies/");
180 DirectoryNames subdirectories
;
181 if (g_VFS
->GetDirectoryEntries(path
, 0, &subdirectories
) != INFO::OK
)
183 LOGERROR("Error opening directory '%s'", path
.string8());
184 return std::vector
<CStrW
>(1, GetSkySet()); // just return what we currently have
187 for(size_t i
= 0; i
< subdirectories
.size(); i
++)
188 skies
.push_back(subdirectories
[i
].string());
189 sort(skies
.begin(), skies
.end());
194 void SkyManager::RenderSky(
195 Renderer::Backend::GL::CDeviceCommandContext
* deviceCommandContext
)
197 GPU_SCOPED_LABEL(deviceCommandContext
, "Render sky");
199 UNUSED2(deviceCommandContext
);
200 #warning TODO: implement SkyManager::RenderSky for GLES
205 // Do nothing unless SetSkySet was called
206 if (m_SkySet
.empty() || !m_SkyCubeMap
)
209 if (m_VertexArray
.GetNumberOfVertices() == 0)
212 const CCamera
& camera
= g_Renderer
.GetSceneRenderer().GetViewCamera();
214 CShaderTechniquePtr skytech
=
215 g_Renderer
.GetShaderManager().LoadEffect(str_sky_simple
);
216 skytech
->BeginPass();
217 deviceCommandContext
->SetGraphicsPipelineState(
218 skytech
->GetGraphicsPipelineStateDesc());
219 const CShaderProgramPtr
& shader
= skytech
->GetShader();
220 shader
->BindTexture(str_baseTex
, m_SkyCubeMap
.get());
222 // Translate so the sky center is at the camera space origin.
224 translate
.SetTranslation(camera
.GetOrientation().GetTranslation());
226 // Currently we have a hardcoded near plane in the projection matrix.
228 scale
.SetScaling(10.0f
, 10.0f
, 10.0f
);
230 // Rotate so that the "left" face, which contains the brightest part of
231 // each skymap, is in the direction of the sun from our light
234 rotate
.SetYRotation(M_PI
+ g_Renderer
.GetSceneRenderer().GetLightEnv().GetRotation());
238 camera
.GetViewProjection() * translate
* rotate
* scale
);
240 m_VertexArray
.PrepareForRendering();
242 u8
* base
= m_VertexArray
.Bind(deviceCommandContext
);
243 const GLsizei stride
= static_cast<GLsizei
>(m_VertexArray
.GetStride());
245 shader
->VertexPointer(
246 3, GL_FLOAT
, stride
, base
+ m_AttributePosition
.offset
);
247 shader
->TexCoordPointer(
248 GL_TEXTURE0
, 3, GL_FLOAT
, stride
, base
+ m_AttributeUV
.offset
);
249 shader
->AssertPointersBound();
251 glDrawArrays(GL_TRIANGLES
, 0, m_VertexArray
.GetNumberOfVertices());
257 void SkyManager::CreateSkyCube()
259 m_AttributePosition
.type
= GL_FLOAT
;
260 m_AttributePosition
.elems
= 3;
261 m_VertexArray
.AddAttribute(&m_AttributePosition
);
263 m_AttributeUV
.type
= GL_FLOAT
;
264 m_AttributeUV
.elems
= 3;
265 m_VertexArray
.AddAttribute(&m_AttributeUV
);
267 // 6 sides of cube with 6 vertices.
268 m_VertexArray
.SetNumberOfVertices(6 * 6);
269 m_VertexArray
.Layout();
271 VertexArrayIterator
<CVector3D
> attrPosition
= m_AttributePosition
.GetIterator
<CVector3D
>();
272 VertexArrayIterator
<CVector3D
> attrUV
= m_AttributeUV
.GetIterator
<CVector3D
>();
274 #define ADD_VERTEX(U, V, W, VX, VY, VZ) \
276 attrPosition->X = VX; \
277 attrPosition->Y = VY; \
278 attrPosition->Z = VZ; \
286 ADD_VERTEX(+1, +1, +1, -1.0f
, -1.0f
, -1.0f
);
287 ADD_VERTEX(+1, +1, -1, -1.0f
, -1.0f
, +1.0f
);
288 ADD_VERTEX(+1, -1, -1, -1.0f
, +1.0f
, +1.0f
);
289 ADD_VERTEX(+1, +1, +1, -1.0f
, -1.0f
, -1.0f
);
290 ADD_VERTEX(+1, -1, -1, -1.0f
, +1.0f
, +1.0f
);
291 ADD_VERTEX(+1, -1, +1, -1.0f
, +1.0f
, -1.0f
);
294 ADD_VERTEX(-1, +1, -1, +1.0f
, -1.0f
, +1.0f
);
295 ADD_VERTEX(-1, +1, +1, +1.0f
, -1.0f
, -1.0f
);
296 ADD_VERTEX(-1, -1, +1, +1.0f
, +1.0f
, -1.0f
);
297 ADD_VERTEX(-1, +1, -1, +1.0f
, -1.0f
, +1.0f
);
298 ADD_VERTEX(-1, -1, +1, +1.0f
, +1.0f
, -1.0f
);
299 ADD_VERTEX(-1, -1, -1, +1.0f
, +1.0f
, +1.0f
);
302 ADD_VERTEX(-1, +1, +1, +1.0f
, -1.0f
, -1.0f
);
303 ADD_VERTEX(-1, +1, -1, +1.0f
, -1.0f
, +1.0f
);
304 ADD_VERTEX(+1, +1, -1, -1.0f
, -1.0f
, +1.0f
);
305 ADD_VERTEX(-1, +1, +1, +1.0f
, -1.0f
, -1.0f
);
306 ADD_VERTEX(+1, +1, -1, -1.0f
, -1.0f
, +1.0f
);
307 ADD_VERTEX(+1, +1, +1, -1.0f
, -1.0f
, -1.0f
);
310 ADD_VERTEX(+1, -1, +1, -1.0f
, +1.0f
, -1.0f
);
311 ADD_VERTEX(+1, -1, -1, -1.0f
, +1.0f
, +1.0f
);
312 ADD_VERTEX(-1, -1, -1, +1.0f
, +1.0f
, +1.0f
);
313 ADD_VERTEX(+1, -1, +1, -1.0f
, +1.0f
, -1.0f
);
314 ADD_VERTEX(-1, -1, -1, +1.0f
, +1.0f
, +1.0f
);
315 ADD_VERTEX(-1, -1, +1, +1.0f
, +1.0f
, -1.0f
);
318 ADD_VERTEX(-1, +1, +1, +1.0f
, -1.0f
, -1.0f
);
319 ADD_VERTEX(+1, +1, +1, -1.0f
, -1.0f
, -1.0f
);
320 ADD_VERTEX(+1, -1, +1, -1.0f
, +1.0f
, -1.0f
);
321 ADD_VERTEX(-1, +1, +1, +1.0f
, -1.0f
, -1.0f
);
322 ADD_VERTEX(+1, -1, +1, -1.0f
, +1.0f
, -1.0f
);
323 ADD_VERTEX(-1, -1, +1, +1.0f
, +1.0f
, -1.0f
);
326 ADD_VERTEX(+1, +1, -1, -1.0f
, -1.0f
, +1.0f
);
327 ADD_VERTEX(-1, +1, -1, +1.0f
, -1.0f
, +1.0f
);
328 ADD_VERTEX(-1, -1, -1, +1.0f
, +1.0f
, +1.0f
);
329 ADD_VERTEX(+1, +1, -1, -1.0f
, -1.0f
, +1.0f
);
330 ADD_VERTEX(-1, -1, -1, +1.0f
, +1.0f
, +1.0f
);
331 ADD_VERTEX(+1, -1, -1, -1.0f
, +1.0f
, +1.0f
);
334 m_VertexArray
.Upload();
335 m_VertexArray
.FreeBackingStore();