1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the QtDataVisualization API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13
14#ifndef ABSTRACT3DRENDERER_P_H
15#define ABSTRACT3DRENDERER_P_H
16
17#include <QtGui/QOpenGLFunctions>
18#if !QT_CONFIG(opengles2)
19# include <QtOpenGL/QOpenGLFunctions_2_1>
20#endif
21#include "datavisualizationglobal_p.h"
22#include "abstract3dcontroller_p.h"
23#include "axisrendercache_p.h"
24#include "seriesrendercache_p.h"
25#include "customrenderitem_p.h"
26
27QT_FORWARD_DECLARE_CLASS(QOffscreenSurface)
28
29QT_BEGIN_NAMESPACE
30
31class TextureHelper;
32class Theme;
33class Drawer;
34
35class Abstract3DRenderer : public QObject, protected QOpenGLFunctions
36{
37 Q_OBJECT
38
39protected:
40 enum SelectionState {
41 SelectNone = 0,
42 SelectOnScene,
43 SelectOnOverview,
44 SelectOnSlice
45 };
46
47 enum RenderingState {
48 RenderingNormal = 0,
49 RenderingSelection,
50 RenderingDepth
51 };
52
53public:
54 virtual ~Abstract3DRenderer();
55
56 virtual void updateData() = 0;
57 virtual void updateSeries(const QList<QAbstract3DSeries *> &seriesList);
58 virtual void updateCustomData(const QList<QCustom3DItem *> &customItems);
59 virtual void updateCustomItems();
60 virtual void updateCustomItemPositions();
61 virtual SeriesRenderCache *createNewCache(QAbstract3DSeries *series);
62 virtual void cleanCache(SeriesRenderCache *cache);
63 virtual void render(GLuint defaultFboHandle);
64
65 virtual void updateTheme(Q3DTheme *theme);
66 virtual void updateSelectionMode(QAbstract3DGraph::SelectionFlags newMode);
67 virtual void updateOptimizationHint(QAbstract3DGraph::OptimizationHints hint);
68 virtual void updateScene(Q3DScene *scene);
69 virtual void updateTextures();
70 virtual void initSelectionBuffer() = 0;
71 virtual void updateSelectionState(SelectionState state);
72
73 virtual void updateDepthBuffer() = 0;
74 virtual void updateShadowQuality(QAbstract3DGraph::ShadowQuality quality) = 0;
75 virtual void initShaders(const QString &vertexShader, const QString &fragmentShader) = 0;
76 virtual void initGradientShaders(const QString &vertexShader, const QString &fragmentShader);
77 virtual void initStaticSelectedItemShaders(const QString &vertexShader,
78 const QString &fragmentShader,
79 const QString &gradientVertexShader,
80 const QString &gradientFragmentShader);
81 virtual void initBackgroundShaders(const QString &vertexShader,
82 const QString &fragmentShader) = 0;
83 virtual void initCustomItemShaders(const QString &vertexShader,
84 const QString &fragmentShader);
85 virtual void initVolumeTextureShaders(const QString &vertexShader,
86 const QString &fragmentShader,
87 const QString &fragmentLowDefShader,
88 const QString &sliceShader,
89 const QString &sliceFrameVertexShader,
90 const QString &sliceFrameShader);
91 virtual void initLabelShaders(const QString &vertexShader, const QString &fragmentShader);
92 virtual void initCursorPositionShaders(const QString &vertexShader,
93 const QString &fragmentShader);
94 virtual void initCursorPositionBuffer();
95
96 virtual void updateAxisType(QAbstract3DAxis::AxisOrientation orientation,
97 QAbstract3DAxis::AxisType type);
98 virtual void updateAxisTitle(QAbstract3DAxis::AxisOrientation orientation,
99 const QString &title);
100 virtual void updateAxisLabels(QAbstract3DAxis::AxisOrientation orientation,
101 const QStringList &labels);
102 virtual void updateAxisRange(QAbstract3DAxis::AxisOrientation orientation, float min,
103 float max);
104 virtual void updateAxisSegmentCount(QAbstract3DAxis::AxisOrientation orientation, int count);
105 virtual void updateAxisSubSegmentCount(QAbstract3DAxis::AxisOrientation orientation,
106 int count);
107 virtual void updateAxisLabelFormat(QAbstract3DAxis::AxisOrientation orientation,
108 const QString &format);
109 virtual void updateAxisReversed(QAbstract3DAxis::AxisOrientation orientation,
110 bool enable);
111 virtual void updateAxisFormatter(QAbstract3DAxis::AxisOrientation orientation,
112 QValue3DAxisFormatter *formatter);
113 virtual void updateAxisLabelAutoRotation(QAbstract3DAxis::AxisOrientation orientation,
114 float angle);
115 virtual void updateAxisTitleVisibility(QAbstract3DAxis::AxisOrientation orientation,
116 bool visible);
117 virtual void updateAxisTitleFixed(QAbstract3DAxis::AxisOrientation orientation,
118 bool fixed);
119 virtual void modifiedSeriesList(const QList<QAbstract3DSeries *> &seriesList);
120
121 virtual void fixMeshFileName(QString &fileName, QAbstract3DSeries::Mesh mesh);
122
123 virtual CustomRenderItem *addCustomItem(QCustom3DItem *item);
124 virtual void updateCustomItem(CustomRenderItem *renderItem);
125
126 virtual void updateAspectRatio(float ratio);
127 virtual void updateHorizontalAspectRatio(float ratio);
128 virtual void updatePolar(bool enable);
129 virtual void updateRadialLabelOffset(float offset);
130 virtual void updateMargin(float margin);
131
132 virtual QVector3D convertPositionToTranslation(const QVector3D &position,
133 bool isAbsolute) = 0;
134
135 void generateBaseColorTexture(const QColor &color, GLuint *texture);
136 void fixGradientAndGenerateTexture(QLinearGradient *gradient, GLuint *gradientTexture);
137
138 inline bool isClickQueryResolved() const { return m_clickResolved; }
139 inline void clearClickQueryResolved() { m_clickResolved = false; }
140 inline QPoint cachedClickQuery() const { return m_cachedScene->selectionQueryPosition(); }
141 inline QAbstract3DSeries *clickedSeries() const { return m_clickedSeries; }
142 inline QAbstract3DGraph::ElementType clickedType() { return m_clickedType; }
143 inline bool isGraphPositionQueryResolved() const { return m_graphPositionQueryResolved; }
144 inline void clearGraphPositionQueryResolved() { m_graphPositionQueryResolved = false; }
145 inline QVector3D queriedGraphPosition() const { return m_queriedGraphPosition; }
146 inline QPoint cachedGraphPositionQuery() const { return m_cachedScene->graphPositionQuery(); }
147
148 LabelItem &selectionLabelItem();
149 void setSelectionLabel(const QString &label);
150 QString &selectionLabel();
151
152 void drawCustomItems(RenderingState state, ShaderHelper *regularShader,
153 const QMatrix4x4 &viewMatrix,
154 const QMatrix4x4 &projectionViewMatrix,
155 const QMatrix4x4 &depthProjectionViewMatrix,
156 GLuint depthTexture, GLfloat shadowQuality, GLfloat reflection = 1.0f);
157
158 QVector4D indexToSelectionColor(GLint index);
159 void calculatePolarXZ(const QVector3D &dataPos, float &x, float &z) const;
160
161Q_SIGNALS:
162 void needRender(); // Emit this if something in renderer causes need for another render pass.
163 void requestShadowQuality(QAbstract3DGraph::ShadowQuality quality); // For automatic quality adjustments
164
165protected:
166 Abstract3DRenderer(Abstract3DController *controller);
167
168 virtual void contextCleanup();
169 virtual void initializeOpenGL();
170
171 void reInitShaders();
172 virtual void handleShadowQualityChange();
173 virtual void handleResize();
174
175 AxisRenderCache &axisCacheForOrientation(QAbstract3DAxis::AxisOrientation orientation);
176
177 virtual void lowerShadowQuality();
178
179 void fixGradient(QLinearGradient *gradient, GLuint *gradientTexture);
180
181 void calculateZoomLevel();
182 void drawAxisTitleY(const QVector3D &sideLabelRotation, const QVector3D &backLabelRotation,
183 const QVector3D &sideLabelTrans, const QVector3D &backLabelTrans,
184 const QQuaternion &totalSideRotation, const QQuaternion &totalBackRotation,
185 AbstractRenderItem &dummyItem, const Q3DCamera *activeCamera,
186 float labelsMaxWidth,
187 const QMatrix4x4 &viewMatrix, const QMatrix4x4 &projectionMatrix,
188 ShaderHelper *shader);
189 void drawAxisTitleX(const QVector3D &labelRotation, const QVector3D &labelTrans,
190 const QQuaternion &totalRotation, AbstractRenderItem &dummyItem,
191 const Q3DCamera *activeCamera, float labelsMaxWidth,
192 const QMatrix4x4 &viewMatrix, const QMatrix4x4 &projectionMatrix,
193 ShaderHelper *shader, bool radial = false);
194 void drawAxisTitleZ(const QVector3D &labelRotation, const QVector3D &labelTrans,
195 const QQuaternion &totalRotation, AbstractRenderItem &dummyItem,
196 const Q3DCamera *activeCamera, float labelsMaxWidth,
197 const QMatrix4x4 &viewMatrix, const QMatrix4x4 &projectionMatrix,
198 ShaderHelper *shader);
199
200 void loadGridLineMesh();
201 void loadLabelMesh();
202 void loadPositionMapperMesh();
203
204 void drawRadialGrid(ShaderHelper *shader, float yFloorLinePos,
205 const QMatrix4x4 &projectionViewMatrix, const QMatrix4x4 &depthMatrix);
206 void drawAngularGrid(ShaderHelper *shader, float yFloorLinePos,
207 const QMatrix4x4 &projectionViewMatrix, const QMatrix4x4 &depthMatrix);
208
209 float calculatePolarBackgroundMargin();
210 virtual void fixCameraTarget(QVector3D &target) = 0;
211 void updateCameraViewport();
212
213 void recalculateCustomItemScalingAndPos(CustomRenderItem *item);
214 virtual void getVisibleItemBounds(QVector3D &minBounds, QVector3D &maxBounds) = 0;
215 void drawVolumeSliceFrame(const CustomRenderItem *item, Qt::Axis axis,
216 const QMatrix4x4 &projectionViewMatrix);
217 void queriedGraphPosition(const QMatrix4x4 &projectionViewMatrix, const QVector3D &scaling,
218 GLuint defaultFboHandle);
219
220 bool m_hasNegativeValues;
221 Q3DTheme *m_cachedTheme;
222 Drawer *m_drawer;
223 QRect m_viewport;
224 QAbstract3DGraph::ShadowQuality m_cachedShadowQuality;
225 GLfloat m_autoScaleAdjustment;
226
227 QAbstract3DGraph::SelectionFlags m_cachedSelectionMode;
228 QAbstract3DGraph::OptimizationHints m_cachedOptimizationHint;
229
230 AxisRenderCache m_axisCacheX;
231 AxisRenderCache m_axisCacheY;
232 AxisRenderCache m_axisCacheZ;
233 TextureHelper *m_textureHelper;
234 GLuint m_depthTexture;
235
236 Q3DScene *m_cachedScene;
237 bool m_selectionDirty;
238 SelectionState m_selectionState;
239 QPoint m_inputPosition;
240 QHash<QAbstract3DSeries *, SeriesRenderCache *> m_renderCacheList;
241 CustomRenderItemArray m_customRenderCache;
242 QList<QCustom3DItem *> m_customItemDrawOrder;
243 QRect m_primarySubViewport;
244 QRect m_secondarySubViewport;
245 float m_devicePixelRatio;
246 bool m_selectionLabelDirty;
247 bool m_clickResolved;
248 bool m_graphPositionQueryPending;
249 bool m_graphPositionQueryResolved;
250 QAbstract3DSeries *m_clickedSeries;
251 QAbstract3DGraph::ElementType m_clickedType;
252 int m_selectedLabelIndex;
253 int m_selectedCustomItemIndex;
254 QVector3D m_queriedGraphPosition;
255 QPoint m_graphPositionQuery;
256
257 QString m_selectionLabel;
258 LabelItem *m_selectionLabelItem;
259 int m_visibleSeriesCount;
260
261 ShaderHelper *m_customItemShader;
262 ShaderHelper *m_volumeTextureShader;
263 ShaderHelper *m_volumeTextureLowDefShader;
264 ShaderHelper *m_volumeTextureSliceShader;
265 ShaderHelper *m_volumeSliceFrameShader;
266 ShaderHelper *m_labelShader;
267 ShaderHelper *m_cursorPositionShader;
268 GLuint m_cursorPositionFrameBuffer;
269 GLuint m_cursorPositionTexture;
270
271 bool m_useOrthoProjection;
272 bool m_xFlipped;
273 bool m_yFlipped;
274 bool m_zFlipped;
275 bool m_yFlippedForGrid;
276
277 ObjectHelper *m_backgroundObj; // Shared reference
278 ObjectHelper *m_gridLineObj; // Shared reference
279 ObjectHelper *m_labelObj; // Shared reference
280 ObjectHelper *m_positionMapperObj; // Shared reference
281
282 float m_graphAspectRatio;
283 float m_graphHorizontalAspectRatio;
284 bool m_polarGraph;
285 float m_radialLabelOffset;
286 float m_polarRadius;
287
288 QQuaternion m_xRightAngleRotation;
289 QQuaternion m_yRightAngleRotation;
290 QQuaternion m_zRightAngleRotation;
291 QQuaternion m_xRightAngleRotationNeg;
292 QQuaternion m_yRightAngleRotationNeg;
293 QQuaternion m_zRightAngleRotationNeg;
294 QQuaternion m_xFlipRotation;
295 QQuaternion m_zFlipRotation;
296
297 float m_requestedMargin;
298 float m_vBackgroundMargin;
299 float m_hBackgroundMargin;
300 float m_scaleXWithBackground;
301 float m_scaleYWithBackground;
302 float m_scaleZWithBackground;
303
304 QVector3D m_oldCameraTarget;
305
306 bool m_reflectionEnabled;
307 qreal m_reflectivity;
308
309 QLocale m_locale;
310#if !QT_CONFIG(opengles2)
311 QOpenGLFunctions_2_1 *m_funcs_2_1;
312#endif
313 QPointer<QOpenGLContext> m_context; // Not owned
314 bool m_isOpenGLES;
315
316private:
317 friend class Abstract3DController;
318};
319
320QT_END_NAMESPACE
321
322#endif
323

source code of qtdatavis3d/src/datavisualization/engine/abstract3drenderer_p.h