• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 4853788370

pending completion
4853788370

push

github

Alexander V. Wolf
Special patch for John Simple

3 of 3 new or added lines in 3 files covered. (100.0%)

14729 of 125046 relevant lines covered (11.78%)

20166.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/plugins/Scenery3d/src/S3DRenderer.hpp
1
/*
2
 * Stellarium Scenery3d Plug-in
3
 *
4
 * Copyright (C) 2011-15 Simon Parzer, Peter Neubauer, Georg Zotti, Andrei Borza, Florian Schaukowitsch
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
19
 */
20

21
#ifndef S3DRENDERER_HPP
22
#define S3DRENDERER_HPP
23

24
#include "StelGui.hpp"
25
#include "StelModule.hpp"
26
#include "StelPainter.hpp"
27
#include "Landscape.hpp"
28
#include "SolarSystem.hpp"
29

30
#include "StelOBJ.hpp"
31
#include "StelOpenGLArray.hpp"
32
#include "Heightmap.hpp"
33
#include "Frustum.hpp"
34
#include "Polyhedron.hpp"
35
#include "S3DEnum.hpp"
36
#include "SceneInfo.hpp"
37
#include "ShaderManager.hpp"
38

39
#include <QMatrix4x4>
40
#include <QOpenGLVertexArrayObject>
41

42
//predeclarations
43
class LandscapeMgr;
44
class S3DScene;
45

46
Q_DECLARE_LOGGING_CATEGORY(s3drenderer)
47

48
//! Scenery3d renderer class
49
class S3DRenderer : public QObject, protected QOpenGLFunctions
50
{
51
        Q_OBJECT
52
public:
53
        //! Initializes an empty Scenery3d object.
54
        S3DRenderer(QObject* parent = Q_NULLPTR);
55
        virtual ~S3DRenderer() Q_DECL_OVERRIDE;
56

57
        //! Draw scenery, called by Scenery3dMgr.
58
        void draw(StelCore* core, S3DScene &scene);
59
        //! Performs initialization that requires an valid OpenGL context
60
        void init();
61

62
        bool getDebugEnabled() const { return debugEnabled; }
×
63
        void setDebugEnabled(bool debugEnabled) { this->debugEnabled = debugEnabled; }
×
64
        bool getPixelLightingEnabled() const { return shaderParameters.pixelLighting; }
×
65
        void setPixelLightingEnabled(const bool val) { shaderParameters.pixelLighting = val; invalidateCubemap(); }
×
66
        bool getShadowsEnabled(void) const { return shaderParameters.shadows; }
×
67
        void setShadowsEnabled(bool shadowsEnabled) { shaderParameters.shadows = shadowsEnabled; reinitShadowmapping = true; invalidateCubemap(); }
×
68
        bool getUseSimpleShadows() const {return simpleShadows; }
×
69
        void setUseSimpleShadows(bool val) { simpleShadows = val; reinitShadowmapping = true; invalidateCubemap(); }
×
70
        bool getBumpsEnabled(void) const { return shaderParameters.bump; }
×
71
        void setBumpsEnabled(bool bumpsEnabled) { shaderParameters.bump = bumpsEnabled; invalidateCubemap(); }
×
72
        bool getTorchEnabled(void) const { return shaderParameters.torchLight; }
×
73
        void setTorchEnabled(bool torchEnabled) { shaderParameters.torchLight = torchEnabled; invalidateCubemap(); }
×
74
        S3DEnum::ShadowFilterQuality getShadowFilterQuality() const { return shaderParameters.shadowFilterQuality; }
×
75
        void setShadowFilterQuality(S3DEnum::ShadowFilterQuality quality) { shaderParameters.shadowFilterQuality = quality; reinitShadowmapping=true; invalidateCubemap();}
×
76
        bool getPCSS() const { return shaderParameters.pcss; }
×
77
        void setPCSS(bool val) { shaderParameters.pcss = val; reinitShadowmapping = true; invalidateCubemap(); }
×
78
        bool getLocationInfoEnabled(void) const { return textEnabled; }
×
79
        void setLocationInfoEnabled(bool locationinfoenabled) { this->textEnabled = locationinfoenabled; }
×
80

81
        bool getLazyCubemapEnabled() const { return lazyDrawing; }
×
82
        void setLazyCubemapEnabled(bool val) { lazyDrawing = val; }
×
83
        double getLazyCubemapInterval() const { return lazyInterval; }
×
84
        void setLazyCubemapInterval(double val) { lazyInterval = val; }
×
85

86
        //This has the be the most crazy method name in the plugin
87
        void getLazyCubemapUpdateOnlyDominantFaceOnMoving(bool &val, bool &alsoSecondDominantFace) const { val = updateOnlyDominantOnMoving; alsoSecondDominantFace = updateSecondDominantOnMoving; }
×
88
        void setLazyCubemapUpdateOnlyDominantFaceOnMoving(bool val, bool alsoSecondDominantFace) { updateOnlyDominantOnMoving = val; updateSecondDominantOnMoving = alsoSecondDominantFace; }
×
89

90
        //! Does a cubemap redraw at the next possible opportunity when lazy-drawing is enabled.
91
        inline void invalidateCubemap() {  lastCubemapUpdate = 0.0; }
×
92

93
        S3DEnum::CubemappingMode getCubemappingMode() const { return cubemappingMode; }
×
94
        //! Changes cubemapping mode and forces re-initialization on next draw call.
95
        //! This may not set the actual mode to the parameter, call getCubemappingMode to find out what was set.
96
        void setCubemappingMode(S3DEnum::CubemappingMode mode)
×
97
        {
98
                if(mode == S3DEnum::CM_CUBEMAP_GSACCEL && !isGeometryShaderCubemapSupported())
×
99
                {
100
                        //fallback to 6 Textures mode
101
                        mode = S3DEnum::CM_TEXTURES;
×
102
                }
103
                if(mode == S3DEnum::CM_CUBEMAP && isANGLEContext())
×
104
                {
105
                        //Cubemap mode disabled on ANGLE because of an implementation bug with Qt 5.4's version
106
                        mode = S3DEnum::CM_TEXTURES;
×
107
                }
108
                cubemappingMode = mode; reinitCubemapping = true;
×
109
        }
×
110

111
        void setUseFullCubemapShadows(bool val) { fullCubemapShadows = val; invalidateCubemap();}
×
112
        bool getUseFullCubemapShadows() const { return fullCubemapShadows; }
×
113

114
        int getCubemapSize() const { return cubemapSize; }
×
115
        //! Note: This may not set the size to the desired one because of hardware limits, call getCubemapSize to receive the value set after this call.
116
        void setCubemapSize(int size) { cubemapSize = (size > maximumFramebufferSize ? maximumFramebufferSize : size); reinitCubemapping = true; }
×
117
        int getShadowmapSize() const { return shadowmapSize; }
×
118
        //! Note: This may not set the size to the desired one because of hardware limits, call getShadowmapSize to receive the value set after this call.
119
        void setShadowmapSize(int size) { shadowmapSize = (size > maximumFramebufferSize ? maximumFramebufferSize : size); reinitShadowmapping = true; }
×
120
        float getTorchBrightness() const { return torchBrightness; }
×
121
        void setTorchBrightness(float brightness) { torchBrightness = brightness; invalidateCubemap(); }
×
122
        float getTorchRange() const { return torchRange; }
×
123
        void setTorchRange(float range) { torchRange = range; invalidateCubemap(); }
×
124

125
        //Debugging method, save the Frustum to be able to move away from it and analyze it
126
        void saveFrusts();
127

128
        //! Returns the shader manager this instance uses
129
        ShaderMgr& getShaderManager() { return shaderManager; }
×
130

131
        //these are some properties that determine the features supported in the current GL context
132
        //available after init() is called
133
        bool isGeometryShaderCubemapSupported() const { return supportsGSCubemapping; }
×
134
        bool areShadowsSupported() const { return supportsShadows; }
×
135
        bool isShadowFilteringSupported() const { return supportsShadowFiltering; }
×
136
        bool isANGLEContext() const { return isANGLE; }
×
137
        int getMaximumFramebufferSize() const { return maximumFramebufferSize; }
×
138
signals:
139
        void message(const QString& msg) const; // ignore clazy warning here
140

141
private:
142
        QOpenGLVertexArrayObject vao;
143
        ShaderMgr shaderManager;
144
        PlanetP sun,moon,venus;
145
        // a pointer to the scene currently being drawn. only valid during draw() calls
146
        S3DScene* currentScene;
147

148
        bool supportsGSCubemapping; //if the GL context supports geometry shader cubemapping
149
        bool supportsShadows; //if shadows are supported
150
        bool supportsShadowFiltering; //if shadow filtering is supported
151
        bool isANGLE; //true if running on ANGLE
152
        int maximumFramebufferSize;
153
        GLuint defaultFBO; //the default background FBO handle
154

155
        float torchBrightness; // toggle light brightness
156
        float torchRange; // used to calculate attenuation like in the second form at http://framebunker.com/blog/lighting-2-attenuation/
157

158
        bool textEnabled;           // switchable value: display coordinates on screen. THIS IS NOT FOR DEBUGGING, BUT A PROGRAM FEATURE!
159
        bool debugEnabled;          // switchable value: display debug graphics and debug texts on screen
160
        bool fixShadowData; //for debugging, fixes all shadow mapping related data (shadowmap contents, matrices, frustums, focus bodies...) at their current values
161
        bool simpleShadows;
162
        bool fullCubemapShadows;
163
        S3DEnum::CubemappingMode cubemappingMode;
164
        bool reinitCubemapping,reinitShadowmapping;
165

166
        int cubemapSize;            // configurable values, typically 512/1024/2048/4096
167
        int shadowmapSize;
168

169
        bool wasMovedInLastDrawCall;
170
        Vec3d lastDrawnPosition;
171

172
        StelCore* core;
173
        LandscapeMgr* landscapeMgr;
174
        StelProjectorP altAzProjector;
175

176
        // state required during drawing
177
        GlobalShaderParameters renderShaderParameters;
178
        bool backfaceCullState;
179
        bool blendEnabled;
180
        const S3DScene::Material* lastMaterial;
181
        QOpenGLShaderProgram* curShader;
182
        QSet<QOpenGLShaderProgram*> initializedShaders;
183
        QVector<const StelOBJ::MaterialGroup*> transparentGroups;
184

185
        // debug info
186
        int drawnTriangles,drawnModels;
187
        int materialSwitches, shaderSwitches;
188

189
        /// ---- Cubemapping variables ----
190
        bool requiresCubemap; //true if cubemapping is required (if projection is anything else than Perspective)
191
        bool cubemappingUsedLastFrame; //true if cubemapping was used for the last frame. Used to determine if a projection switch occurred.
192
        bool lazyDrawing; //if lazy-drawing mode is enabled
193
        bool updateOnlyDominantOnMoving; //if movement updates only dominant face directly
194
        bool updateSecondDominantOnMoving; //if movement also updates the second-most dominant face
195
        bool needsMovementEndUpdate;
196
        bool needsCubemapUpdate; //if the draw-call has to recreate the cubemap completely
197
        bool needsMovementUpdate; //if the draw-call has to recreate either the whole cubemap or the dominant face
198
        double lazyInterval; //the lazy-drawing time interval
199
        double lastCubemapUpdate; //when the last lazy draw happened (JDay)
200
        qint64 lastCubemapUpdateRealTime; //when the last lazy draw happened (real system time, QDateTime::currentMSecsSinceEpoch)
201
        qint64 lastMovementEndRealTime; //the timepoint when the last movement was stopped
202
        GLuint cubeMapCubeTex; //GL_TEXTURE_CUBE_MAP, used in CUBEMAP or CUBEMAP_GSACCEL modes
203
        GLuint cubeMapCubeDepth; //this is a depth-cubemap, only used in CUBEMAP_GSACCEL mode
204
        GLuint cubeMapTex[6]; //GL_TEXTURE_2D, for "legacy" TEXTURES mode
205
        GLuint cubeRB; //renderbuffer for depth of a single face in TEXTURES and CUBEMAP modes (attached to multiple FBOs)
206
        int dominantFace,secondDominantFace;
207

208
        //because of use that deviates very much from QOpenGLFramebufferObject typical usage, we manage the FBOs ourselves
209
        GLuint cubeFBO; //used in CUBEMAP_GSACCEL mode - only a single FBO exists, with a cubemap for color and one for depth
210
        GLuint cubeSideFBO[6]; //used in TEXTURES and CUBEMAP mode, 6 textures/cube faces for color and a shared depth renderbuffer (we don't require the depth after rendering)
211

212
        bool cubeMappingCreated; //true if any cubemapping objects have been initialized and need to be cleaned up eventually
213

214
        //cube geometry
215
        QVector<Vec3f> cubeVertices, transformedCubeVertices;
216
        QVector<Vec2f> cubeTexcoords;
217
        QOpenGLBuffer cubeVertexBuffer;
218
        QOpenGLBuffer transformedCubeVertexBuffer;
219
        QOpenGLBuffer cubeIndexBuffer;
220
        int cubeIndexCount;
221

222
        //cube rendering matrices
223
        QMatrix4x4 cubeRotation[6]; //rotational matrices for cube faces. The cam position is added to this in each frame.
224
        QMatrix4x4 cubeMVP[6]; //cube face MVP matrices
225

226
        /// ---- Rendering information ----
227
        //final model view matrix for shader upload
228
        QMatrix4x4 modelViewMatrix;
229
        //currently valid projection matrix for shader upload
230
        QMatrix4x4 projectionMatrix;
231

232
        // a struct which encapsulates lighting info
233
        struct LightParameters
234
        {
235
                //this is used for shadow calculation
236
                enum ShadowCaster { SC_None, SC_Sun, SC_Moon, SC_Venus } shadowCaster;
237
                QMatrix4x4 shadowModelView;
238

239
                Vec3f lightDirectionV3f;
240
                //same as lightDirectionV3f, but as QVector3D
241
                QVector3D lightDirectionWorld;
242
                QVector3D ambient;
243
                QVector3D directional;
244
                QVector3D specular;
245
                QVector3D emissive;
246

247
                QVector3D torchDiffuse;
248
                float torchAttenuation;
249

250
                //these are just used for debugging
251
                enum DirectionalSource { DS_Sun_Horiz, DS_Sun, DS_Moon, DS_Venus, DS_Venus_Ambient } directionalSource;
252
                float sunAmbient;
253
                float moonAmbient;
254
                float backgroundAmbient;
255
                float landscapeOpacity;
256
        } lightInfo;
257

258
        GlobalShaderParameters shaderParameters;
259

260
        //Shadow Map FBO handles
261
        QVector<GLuint> shadowFBOs;
262
        //Holds the shadow textures
263
        QVector<GLuint> shadowMapsArray;
264
        //Holds the shadow transformation matrix per split (Crop/Projection/View)
265
        QVector<QMatrix4x4> shadowCPM;
266
        //Holds the xy-scaling of the orthographic light cam + pos of near/far planes in view coords
267
        //Needed for consistent shadow filter sizes and PCSS effect
268
        QVector<QVector4D> shadowFrustumSize;
269
        // Frustum of the view camera, constrained to the shadowFarZ instead of the camFarZ
270
        Frustum camFrustShadow;
271
        //near/far planes for the orthographic light that fits the whole scene
272
        float lightOrthoNear;
273
        float lightOrthoFar;
274
        //Array holding the split frustums
275
        QVector<Frustum> frustumArray;
276
        //Vector holding the convex split bodies for focused shadow mapping
277
        QVector<Polyhedron> focusBodies;
278

279
        float parallaxScale;
280

281
        QFont debugTextFont;
282

283
        // --- initialization
284
        //! Determines what features the current opengl context supports
285
        void determineFeatureSupport();
286
        //! Initializes cubemapping to the currently set parameters (6tex/cubemap/GS approaches)
287
        bool initCubemapping();
288
        //! Cleans up cubemapping related objects
289
        void deleteCubemapping();
290

291
        //! Re-initializes shadowmapping related objects
292
        bool initShadowmapping();
293
        //! Cleans up shadowmapping related objects
294
        void deleteShadowmapping();
295

296
        void calcCubeMVP(const Vec3d translation);
297

298
        // --- drawing methods ---
299
        //! Basic setup for default perspective drawing. Standard OpenGL forward rendering.
300
        void drawDirect();
301
        //! When another projection than perspective is selected, rendering is performed using a cubemap.
302
        void drawWithCubeMap();
303
        //! Performs the actual rendering of the shadow map
304
        bool renderShadowMaps();
305
        //! Creates shadowmaps for the specified cubemap face
306
        void renderShadowMapsForFace(int face);
307
        //! Generates a 6-sided cube map by drawing a view in each direction
308
        void generateCubeMap();
309
        //! Uses a geometry shader to render 6 faces in 1 pass
310
        void renderIntoCubemapGeometryShader();
311
        //! Uses 6 traditional rendering passes to render into a cubemap or 6 textures.
312
        void renderIntoCubemapSixPasses();
313
        //! Uses the StelPainter to draw a warped cube textured with our cubemap
314
        void drawFromCubeMap();
315
        //! This is the method that performs the actual drawing.
316
        //! If shading is true, a suitable shader for each material is selected and initialized. Submits 1 draw call for each StelModel.
317
        //! @return false on shader errors
318
        bool drawArrays(bool shading=true, bool blendAlphaAdditive=false);
319
        //! Draws a single material group, to be use from within drawArrays
320
        bool drawMaterialGroup(const StelOBJ::MaterialGroup& matGroup, bool shading, bool blendAlphaAdditive);
321

322
        //! Draw observer grid coordinates as text.
323
        void drawCoordinatesText();
324
        //! Draw some text output. This can be filled as needed by development.
325
        void drawDebug();
326

327
        // --- shading related stuff ---
328
        //! Finds out the shadow caster and determines shadow parameters for current frame
329
        void calculateShadowCaster();
330
        //! Calculates lighting related stuff and puts it in the lightInfo structure
331
        void calculateLighting();
332
        //! Sets uniforms constant over the whole pass (=projection matrix, lighting & shadow info)
333
        void setupPassUniforms(QOpenGLShaderProgram *shader);
334
        //! Sets up shader uniforms constant over the whole frame/side of cubemap (=modelview dependent stuff)
335
        void setupFrameUniforms(QOpenGLShaderProgram *shader);
336
        //! Sets up shader uniforms specific to one material
337
        void setupMaterialUniforms(QOpenGLShaderProgram *shader, const S3DScene::Material& mat);
338

339
        //! Adjust the frustum to the loaded scene bounding box according to Zhang et al.
340
        void adjustShadowFrustum(const Vec3d &viewPos, const Vec3d &viewDir, const Vec3d &viewUp, const float fov, const float aspect);
341
        //Computes the frustum splits
342
        void computeFrustumSplits(const Vec3d& viewPos, const Vec3d& viewDir, const Vec3d& viewUp);
343
        //Computes the focus body for given frustum
344
        void computePolyhedron(Polyhedron& body, const Frustum& frustum, const Vec3f &shadowDir);
345
        //Computes the crop matrix to focus the light
346
        void computeCropMatrix(QMatrix4x4& cropMatrix, QVector4D &orthoScale, Polyhedron &focusBody, const QMatrix4x4 &lightProj, const QMatrix4x4 &lightMVP);
347
        //Computes the light projection values
348
        void computeOrthoProjVals(const Vec3f &shadowDir, float &orthoExtent, float &orthoNear, float &orthoFar);
349

350
        void rendererMessage(const QString& msg) const;
351
};
352

353
#endif // S3DRENDERER_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc