FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SkyRenderer.h
1#pragma once
2#ifdef FH_VISUALIZATION
3
4# include "CloudImage.h"
5# include "CloudLayer.h"
6# include "CloudShadow.h"
7# include "SkyModel.h"
8# include "StarField.h"
9# include "VisualSettings.h"
10
11# include <OgreMatrix3.h>
12# include <OgreTexture.h>
13# include <OgreVector.h>
14# include <cstdint>
15# include <functional>
16# include <future>
17# include <memory>
18# include <vector>
19
20namespace Ogre
21{
22class SceneManager;
23}
24
25namespace visual
26{
27
43class SkyRenderer
44{
45public:
52 SkyRenderer(Ogre::SceneManager* sceneMgr, const VisualSettings& settings);
53 ~SkyRenderer();
54
55 SkyRenderer(const SkyRenderer&) = delete;
56 SkyRenderer& operator=(const SkyRenderer&) = delete;
57
64 void Update(double time, const Ogre::Vector3& camera, double windDirection);
65
67 Ogre::TexturePtr ReflectionCube() const { return m_reflection; }
68
70 Ogre::TexturePtr CloudTexture() const { return m_cloudTexture; }
71
73 Ogre::Vector3 SunDirection() const { return m_sunDirection; }
74
76 Ogre::Vector3 LightDirection() const { return m_moonIsLight ? m_moonDirection : m_sunDirection; }
78 Ogre::Vector3 LightIrradiance() const { return m_moonIsLight ? m_moonIrradiance : m_sunIrradiance; }
80 bool MoonIsLight() const { return m_moonIsLight; }
81
83 Ogre::Vector3 SunIrradiance() const { return m_sunIrradiance; }
84
87 Ogre::Vector3 SkyIrradiance() const { return m_skyIrradiance + m_moonSky * kPiF; }
88
93 float CloudTransmittance(const Ogre::Vector3& point) const;
94
96 Ogre::Vector4 CloudParams() const;
98 Ogre::Vector4 CloudScale() const;
100 Ogre::Vector4 CloudDrift() const;
101
103 bool Volumetric() const { return m_volumetric; }
106 bool CloudsInScenePass() const { return m_volumetric && CloudsVisible() && -m_eye.z > m_cloudVolume.x - 50.0f; }
108 bool CloudsVisible() const;
112 void SetView(Ogre::Viewport* viewport, bool pipelineActive);
114 void SetSeaLevel(double level) { m_seaZ = float(-level); }
116 Ogre::TexturePtr CloudImageTexture() const { return m_cloudImage ? m_cloudImage->Texture() : Ogre::TexturePtr(); }
117
119 Ogre::TexturePtr CloudShapeTexture() const { return m_cloudShape; }
121 Ogre::Vector4 CloudVolume() const { return m_cloudVolume; }
123 Ogre::Vector3 CloudSun() const { return ToOgreRgb(m_cloudLight.sun); }
125 Ogre::Vector3 CloudSky() const { return ToOgreRgb(m_cloudLight.sky); }
127 Ogre::Vector3 AirExtinction() const { return ToOgreRgb(m_airExtinction); }
128
131 Ogre::TexturePtr CloudShadowTexture() const { return m_cloudShadow; }
133 Ogre::Vector4 CloudShadowRect() const;
134
136 double DisplayScale() const { return m_scale; }
137
138private:
139 static Ogre::Vector3 ToOgreRgb(const sky::Rgb& c) { return {float(c[0]), float(c[1]), float(c[2])}; }
140 struct Reflection
141 {
142 std::vector<float> faces[6]; // RGBA encoded, level 0
144 std::vector<std::vector<std::uint16_t>> levels[6];
145 Ogre::Vector3 skyIrradiance;
146 double buildMs = 0.0;
147 };
149 struct ClearSky
150 {
151 std::shared_ptr<sky::SkyModel> model; // null for an image
152 std::function<sky::Rgb(const sky::Vec3&)> clear; // physical units
153 double scale = 1.0;
154 Ogre::Vector3 sunDirection {0.0f, 0.0f, -1.0f};
155 Ogre::Vector3 sunIrradiance {0.0f, 0.0f, 0.0f};
156 Ogre::Vector3 sunDisc {0.0f, 0.0f, 0.0f};
157 sky::CloudLight cloudLight;
158 sky::Rgb airExtinction {0.0, 0.0, 0.0};
159 std::vector<sky::Rgb> clearReflection; // display units, per reflection texel
160 std::vector<float> display[6]; // encoded RGBA, the drawn sky
161 double buildMs = 0.0;
162 double altitude = 10.0; // the eye's altitude it was built for [m]
163 };
165 ClearSky BuildClearSky(const sky::Vec3& sun, double altitude) const;
167 void FillCubes(ClearSky& c) const;
169 void ApplyClearSky(ClearSky&& c);
170 Reflection BuildReflection(const sky::CloudDrift& drift) const;
171 void UploadCube(const Ogre::TexturePtr& texture, int size, const std::vector<float>* faces) const;
172 void UploadLevels(const Reflection& reflection) const;
173 static void MakeLevels(Reflection& reflection, int size);
174 void SetupProcedural(const VisualSettings& settings);
175 void SetupImage(const VisualSettings& settings);
176 void CreateMaterial(Ogre::SceneManager* sceneMgr, double distance);
177 Ogre::SceneManager* m_skySceneMgr = nullptr;
178 sky::Rgb CloudyRadiance(const Ogre::Vector3& world, const sky::Rgb& clear, const sky::CloudDrift& drift) const;
179
181 int m_displaySize = 256;
182 static constexpr int kReflectionSize = 128;
183
184 Ogre::Matrix3 m_cubeToWorld;
185 std::function<sky::Rgb(const sky::Vec3&)> m_clear; // clear-sky radiance, physical units
186 Ogre::Vector3 m_camera {0.0f, 0.0f, 0.0f};
187 std::shared_ptr<sky::SkyModel> m_model;
188 std::unique_ptr<CloudImage> m_cloudImage;
189 std::unique_ptr<StarField> m_stars; // the night sky's stars (Visual.SunDirection = "Time"; D32)
190 Ogre::TexturePtr m_reflectionGpu; // the volumetric clouds' reflection faces, drawn on the GPU (level 0)
191 CloudImage::Clouds CloudUniforms() const;
192 Reflection ReflectionFromFaces(std::array<std::vector<float>, 6> faces) const;
193 Ogre::Viewport* m_viewport = nullptr;
194 bool m_pipelineActive = true;
195 float m_seaZ = 0.0f;
196 static constexpr int kShadowResolution = 384;
197 static constexpr double kShadowSize = 20000.0;
198 std::unique_ptr<sky::CloudShadowField> m_shadowField;
199 sky::CloudShadowMap m_shadowMap;
200 std::future<sky::CloudShadowMap> m_pendingShadow;
201 double m_lastShadow = -1e9;
202 Ogre::TexturePtr m_cloudShadow;
203 void UploadShadow();
204 // The moon (Visual.SunDirection = "Time"; D31): direction, its light at sea level, its disc's light, whether it lights
205 // the scene (the sun well below the horizon and the moon up).
206 Ogre::Vector3 m_moonDirection {0.0f, 0.0f, 1.0f};
207 Ogre::Vector3 m_moonIrradiance {0.0f, 0.0f, 0.0f};
208 Ogre::Vector3 m_moonDisc {0.0f, 0.0f, 0.0f};
209 Ogre::Vector3 m_moonSky {0.0f, 0.0f, 0.0f}; // radiance of the moonlit sky (D33)
210 Ogre::Vector3 m_moonPole {0.0f, 0.0f, -1.0f}; // the moon's north: the celestial pole
211 float m_moonCosRadius = 0.9999898f; // cosine of its angular radius
212 bool m_moonFull = false; // always a full disc (Visual.MoonStyle)
213 static constexpr float kPiF = 3.14159265f;
214 bool m_moonUp = false;
215 bool m_moonIsLight = false;
216 void UpdateMoon(double time);
217 bool m_volumetric = false;
218 Ogre::Vector3 m_eye {0.0f, 0.0f, -10.0f}; // the camera this frame (m_camera is the worker's)
219 Ogre::TexturePtr m_cloudShape;
220 Ogre::Vector4 m_cloudVolume {1500.0f, 2400.0f, 0.02f, 1.0f / 4000.0f};
221 const VisualSettings* m_settings = nullptr; // owned by the Environment, outlives the sky
222 double m_visibility = 30000.0;
223 std::future<ClearSky> m_pendingSky; // a clear sky for a sun that has moved (Visual.SunDirection = "Time")
224 int m_skyBuilds = 0;
225 std::unique_ptr<sky::CloudLayer> m_clouds;
226 // The weather while running (D34): the cloud layer's shape now and asked for, its worker; the noise cube's bytes for
227 // the shadow field; a clear sky to rebuild (haze, fixed sun, clouds).
228 struct CloudShape
229 {
230 double cover = -1.0, thickness = -1.0, altitude = -1.0;
231 bool operator==(const CloudShape& o) const { return cover == o.cover && thickness == o.thickness && altitude == o.altitude; }
232 };
233 CloudShape m_appliedClouds;
234 CloudShape m_requestedClouds;
235 std::future<std::unique_ptr<sky::CloudLayer>> m_pendingClouds;
236 std::vector<std::uint8_t> m_shapeBytes;
237 std::uint32_t m_cloudSeed = 0;
238 bool m_skyDirty = false;
239 void InstallClouds(std::unique_ptr<sky::CloudLayer> layer, const CloudShape& shape);
240 void UpdateLiveWeather(double time);
241 std::vector<sky::Rgb> m_clearReflection; // display units, per reflection texel (6 faces)
242 double m_scale = 1.0;
243 double m_cloudSpeed = 0.0;
244 double m_cameraAltitude = 10.0;
245 sky::CloudLight m_cloudLight; // display units
246 sky::Rgb m_airExtinction {0.0, 0.0, 0.0};
247 sky::CloudDrift m_drift;
248 double m_lastTime = 0.0;
249 Ogre::Vector3 m_sunDirection {0.0f, 0.0f, -1.0f};
250 Ogre::Vector3 m_sunIrradiance {0.0f, 0.0f, 0.0f};
251 Ogre::Vector3 m_skyIrradiance {0.0f, 0.0f, 0.0f};
252 Ogre::Vector3 m_sunDisc {0.0f, 0.0f, 0.0f};
253 Ogre::TexturePtr m_display, m_reflection, m_cloudTexture;
254 std::string m_material;
255 std::future<Reflection> m_pending;
256 double m_lastRefresh = -1e9;
257 int m_refreshes = 0;
258};
259
260} // namespace visual
261#endif
Visualization utilities.
Definition VisualDomain.h:12