src/gl/common.h

Mon, 20 Jun 2022 21:49:39 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 21:49:39 +0300
changeset 240
414d392b9ae6
parent 231
a9bf6bab5ea2
child 245
a41ccc6924e3
permissions
-rw-r--r--

some improvements to dock widget handling

19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2013 - 2018 Teemu Piippo
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
100
43ce3672648a fix build
Teemu Piippo <teemu@hecknology.net>
parents: 92
diff changeset
20 #include <QWidget>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include <QColor>
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
22 #include <QOpenGLBuffer>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include <QOpenGLFunctions>
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
24 #include <QOpenGLShader>
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
25 #include <QOpenGLShaderProgram>
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
26 #include <QOpenGLVertexArrayObject>
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
27 #include <glm/gtc/type_ptr.hpp>
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 #include "basics.h"
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 #include "colors.h"
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 189
diff changeset
30 #include "model.h"
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
32 class RenderLayer
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
33 {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
34 protected:
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
35 class PartRenderer* renderer;
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
36 public:
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
37 virtual ~RenderLayer(){}
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
38 virtual void initializeGL(){}
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
39 virtual void paintGL(){}
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
40 virtual void overpaint(QPainter*){}
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
41 virtual void mvpMatrixChanged(const glm::mat4& mvpMatrix) = 0;
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
42 virtual void mouseMoved(const QMouseEvent*){}
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
43 virtual void mouseClick(const QMouseEvent*){}
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
44 void setRendererPointer(class PartRenderer* renderer)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
45 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
46 this->renderer = renderer;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
47 }
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
48 };
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
49
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 namespace gl
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
52 class ShaderProgram;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
54 void buildShaders(
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
55 QOpenGLShaderProgram* shaderProgram,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
56 const char* vertexShaderSource,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
57 const char* fragmentShaderSource);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
58 void checkForGLErrors(QWidget* parent);
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
59 inline glm::vec3 colorToVector3(const QColor& color)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
60 {
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
61 return {float_cast(color.redF()), float_cast(color.greenF()), float_cast(color.blueF())};
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
62 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
63 inline glm::vec4 colorToVector4(const QColor& color)
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
64 {
206
654661eab7f3 More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
Teemu Piippo <teemu@hecknology.net>
parents: 200
diff changeset
65 return {gl::colorToVector3(color), float_cast(color.alphaF())};
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
66 }
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 }
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
69 class gl::ShaderProgram : public QOpenGLShaderProgram
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
70 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
71 public:
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
72 using QOpenGLShaderProgram::QOpenGLShaderProgram;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
73 // for some reason I cannot overload setUniformValue properly with this template thing
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
74 template<typename Float, glm::qualifier Prec>
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
75 void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
76 {
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
77 const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value));
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
78 this->setUniformValue(uniformName, *array);
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
79 }
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
80 template<typename Float, glm::qualifier Prec>
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
81 void setUniformVector(const char* uniformName, const glm::vec<4, Float, Prec>& value)
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
82 {
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
83 this->setUniformValue(uniformName, value.x, value.y, value.z, value.w);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
84 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
85 };
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
86
189
815fbaae9cb2 cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents: 170
diff changeset
87 extern QOpenGLFunctions glfunc;
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 namespace gl
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 {
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
91 // Different ways to render the scene
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 enum class RenderStyle
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 {
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
94 // Normal rendering style
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 Normal,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
96 // Use green colour for front faces and red colour for back faces
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 BfcRedGreen,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
98 // Use a different colour for each object
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
99 RandomColors,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
100 // Render so that the colour of an object has an one to one correspondence with its id
119
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
101 PickScene,
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
102 // Render a scene where vertices can be picked
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
103 VertexPickScene,
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 };
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
105
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
106 // Different ways to render fragments.
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
107 // These are also defined in shaders
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
108 enum class FragmentStyle
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
109 {
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
110 // Use normal colours
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
111 Normal = 0,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
112 // Use a distinctive green colour for BFC red/green view
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
113 BfcGreen = 1,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
114 // Use a distinctive red colour for BFC red/green view
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
115 BfcRed = 2,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
116 // Use a colour based on the object to distinguish objects
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
117 RandomColors = 3,
91
9eb5e06f34c3 add comments
Teemu Piippo <teemu@hecknology.net>
parents: 73
diff changeset
118 // Use a colour that codes the object's id
46
98645c8e7704 added the pick scene
Teemu Piippo <teemu@hecknology.net>
parents: 45
diff changeset
119 Id = 4,
119
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
120 // Render everything black
Teemu Piippo <teemu@hecknology.net>
parents: 115
diff changeset
121 Black = 5,
37
90443fb328ea wired renderstyle up
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
122 };
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
123
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
124 // User options for rendering
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
125 struct RenderPreferences
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
126 {
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
127 gl::RenderStyle style = gl::RenderStyle::Normal;
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
128 QColor mainColor{255, 255, 64};
40
30cb5e836736 added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
129 QColor backgroundColor{48, 48, 48};
48
3c10f0e2fbe0 added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents: 46
diff changeset
130 QColor selectedColor{32, 32, 255};
44
c6114b3af3a6 added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents: 41
diff changeset
131 GLfloat lineThickness = 2.0f;
45
272c84c7c87e added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents: 44
diff changeset
132 bool lineAntiAliasing = true;
170
9b655f6fe5a1 Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents: 119
diff changeset
133 bool drawAxes = true;
231
a9bf6bab5ea2 Add wireframe button
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 218
diff changeset
134 bool wireframe = false;
39
caac957e9834 Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents: 37
diff changeset
135 };
19
ed9685f44ab3 added missing files
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
136 }
218
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
137
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
138 Q_DECLARE_METATYPE(gl::RenderStyle)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
139
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
140 inline QDataStream &operator<<(QDataStream& stream, const gl::RenderStyle style)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
141 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
142 return stream << enum_value_cast(style);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
143 }
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
144
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
145 inline QDataStream &operator>>(QDataStream& stream, gl::RenderStyle& style)
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
146 {
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
147 return stream >> enum_value_cast(style);
63125c36de73 Replace config collector with a simpler system
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
148 }

mercurial