1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the demonstration applications of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #ifndef SCENE_H |
52 | #define SCENE_H |
53 | |
54 | #include "glbuffers.h" |
55 | #include "glextensions.h" |
56 | #include "gltrianglemesh.h" |
57 | #include "qtbox.h" |
58 | #include "roundedbox.h" |
59 | #include "trackball.h" |
60 | |
61 | QT_BEGIN_NAMESPACE |
62 | class QMatrix4x4; |
63 | QT_END_NAMESPACE |
64 | |
65 | class ParameterEdit : public QWidget |
66 | { |
67 | public: |
68 | virtual void emitChange() = 0; |
69 | }; |
70 | |
71 | class ColorEdit : public ParameterEdit |
72 | { |
73 | Q_OBJECT |
74 | public: |
75 | ColorEdit(QRgb initialColor, int id); |
76 | QRgb color() const {return m_color;} |
77 | void emitChange() override { emit colorChanged(color: m_color, id: m_id); } |
78 | public slots: |
79 | void editDone(); |
80 | signals: |
81 | void colorChanged(QRgb color, int id); |
82 | protected: |
83 | void mousePressEvent(QMouseEvent *event) override; |
84 | void setColor(QRgb color); // also emits colorChanged() |
85 | private: |
86 | QGraphicsScene *m_dialogParentScene; |
87 | QLineEdit *m_lineEdit; |
88 | QFrame *m_button; |
89 | QRgb m_color; |
90 | int m_id; |
91 | }; |
92 | |
93 | class FloatEdit : public ParameterEdit |
94 | { |
95 | Q_OBJECT |
96 | public: |
97 | FloatEdit(float initialValue, int id); |
98 | float value() const {return m_value;} |
99 | void emitChange() override { emit valueChanged(value: m_value, id: m_id); } |
100 | public slots: |
101 | void editDone(); |
102 | signals: |
103 | void valueChanged(float value, int id); |
104 | private: |
105 | QGraphicsScene *m_dialogParentScene; |
106 | QLineEdit *m_lineEdit; |
107 | float m_value; |
108 | int m_id; |
109 | }; |
110 | |
111 | class GraphicsWidget : public QGraphicsProxyWidget |
112 | { |
113 | public: |
114 | GraphicsWidget() : QGraphicsProxyWidget(nullptr, Qt::Window) {} |
115 | protected: |
116 | QVariant itemChange(GraphicsItemChange change, const QVariant &value) override; |
117 | void resizeEvent(QGraphicsSceneResizeEvent *event) override; |
118 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override; |
119 | }; |
120 | |
121 | class TwoSidedGraphicsWidget : public QObject |
122 | { |
123 | Q_OBJECT |
124 | public: |
125 | using QObject::QObject; |
126 | void setWidget(int index, QWidget *widget); |
127 | QWidget *widget(int index); |
128 | public slots: |
129 | void flip(); |
130 | protected slots: |
131 | void animateFlip(); |
132 | private: |
133 | GraphicsWidget *m_proxyWidgets[2] = {nullptr, nullptr}; |
134 | int m_current = 0; |
135 | int m_angle = 0; // angle in degrees |
136 | int m_delta = 0; |
137 | }; |
138 | |
139 | class RenderOptionsDialog : public QDialog |
140 | { |
141 | Q_OBJECT |
142 | public: |
143 | RenderOptionsDialog(); |
144 | int addTexture(const QString &name); |
145 | int addShader(const QString &name); |
146 | void emitParameterChanged(); |
147 | protected slots: |
148 | void setColorParameter(QRgb color, int id); |
149 | void setFloatParameter(float value, int id); |
150 | signals: |
151 | void dynamicCubemapToggled(int); |
152 | void colorParameterChanged(const QString &, QRgb); |
153 | void floatParameterChanged(const QString &, float); |
154 | void textureChanged(int); |
155 | void shaderChanged(int); |
156 | void doubleClicked(); |
157 | protected: |
158 | void mouseDoubleClickEvent(QMouseEvent *event) override; |
159 | |
160 | QVector<QByteArray> m_parameterNames; |
161 | QComboBox *m_textureCombo; |
162 | QComboBox *m_shaderCombo; |
163 | QVector<ParameterEdit *> m_parameterEdits; |
164 | }; |
165 | |
166 | class ItemDialog : public QDialog |
167 | { |
168 | Q_OBJECT |
169 | public: |
170 | enum ItemType { |
171 | QtBoxItem, |
172 | CircleItem, |
173 | SquareItem, |
174 | }; |
175 | |
176 | ItemDialog(); |
177 | public slots: |
178 | void triggerNewQtBox(); |
179 | void triggerNewCircleItem(); |
180 | void triggerNewSquareItem(); |
181 | signals: |
182 | void doubleClicked(); |
183 | void newItemTriggered(ItemDialog::ItemType type); |
184 | protected: |
185 | void mouseDoubleClickEvent(QMouseEvent *event) override; |
186 | }; |
187 | |
188 | class Scene : public QGraphicsScene |
189 | { |
190 | Q_OBJECT |
191 | public: |
192 | Scene(int width, int height, int maxTextureSize); |
193 | ~Scene(); |
194 | void drawBackground(QPainter *painter, const QRectF &rect) override; |
195 | |
196 | public slots: |
197 | void setShader(int index); |
198 | void setTexture(int index); |
199 | void toggleDynamicCubemap(int state); |
200 | void setColorParameter(const QString &name, QRgb color); |
201 | void setFloatParameter(const QString &name, float value); |
202 | void newItem(ItemDialog::ItemType type); |
203 | protected: |
204 | void renderBoxes(const QMatrix4x4 &view, int excludeBox = -2); |
205 | void setStates(); |
206 | void setLights(); |
207 | void defaultStates(); |
208 | void renderCubemaps(); |
209 | |
210 | void mousePressEvent(QGraphicsSceneMouseEvent *event) override; |
211 | void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; |
212 | void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; |
213 | void wheelEvent(QGraphicsSceneWheelEvent * event) override; |
214 | private: |
215 | void initGL(); |
216 | QPointF pixelPosToViewPos(const QPointF& p); |
217 | |
218 | int m_lastTime; |
219 | int m_mouseEventTime; |
220 | int m_distExp; |
221 | int m_frame; |
222 | int m_maxTextureSize; |
223 | |
224 | int m_currentShader; |
225 | int m_currentTexture; |
226 | bool m_dynamicCubemap; |
227 | bool m_updateAllCubemaps; |
228 | |
229 | RenderOptionsDialog *m_renderOptions; |
230 | ItemDialog *m_itemDialog; |
231 | QTimer *m_timer; |
232 | GLRoundedBox *m_box; |
233 | TrackBall m_trackBalls[3]; |
234 | QVector<GLTexture *> m_textures; |
235 | GLTextureCube *m_environment; |
236 | GLTexture3D *m_noise; |
237 | GLRenderTargetCube *m_mainCubemap; |
238 | QVector<GLRenderTargetCube *> m_cubemaps; |
239 | QVector<QGLShaderProgram *> m_programs; |
240 | QGLShader *m_vertexShader; |
241 | QVector<QGLShader *> m_fragmentShaders; |
242 | QGLShader *m_environmentShader; |
243 | QGLShaderProgram *m_environmentProgram; |
244 | }; |
245 | |
246 | #endif |
247 | |