Mon, 13 Jun 2022 02:18:25 +0300
Reworked grid program into a render layer
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #pragma once | |
100 | 20 | #include <QWidget> |
19 | 21 | #include <QColor> |
53 | 22 | #include <QOpenGLBuffer> |
19 | 23 | #include <QOpenGLFunctions> |
53 | 24 | #include <QOpenGLShader> |
25 | #include <QOpenGLShaderProgram> | |
26 | #include <QOpenGLVertexArrayObject> | |
27 | #include <glm/gtc/type_ptr.hpp> | |
19 | 28 | #include "basics.h" |
29 | #include "colors.h" | |
200 | 30 | #include "model.h" |
19 | 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 | { |
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
|
34 | public: |
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
|
35 | 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
|
36 | 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
|
37 | 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
|
38 | virtual void mvpMatrixChanged(const glm::mat4& mvpMatrix) = 0; |
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 | }; |
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 | |
19 | 41 | namespace gl |
42 | { | |
53 | 43 | class ShaderProgram; |
44 | ||
45 | void buildShaders( | |
46 | QOpenGLShaderProgram* shaderProgram, | |
47 | const char* vertexShaderSource, | |
48 | const char* fragmentShaderSource); | |
49 | void checkForGLErrors(QWidget* parent); | |
55 | 50 | inline glm::vec3 colorToVector3(const QColor& color) |
51 | { | |
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
|
52 | return {float_cast(color.redF()), float_cast(color.greenF()), float_cast(color.blueF())}; |
55 | 53 | } |
54 | inline glm::vec4 colorToVector4(const QColor& color) | |
55 | { | |
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
|
56 | return {gl::colorToVector3(color), float_cast(color.alphaF())}; |
55 | 57 | } |
19 | 58 | } |
59 | ||
53 | 60 | class gl::ShaderProgram : public QOpenGLShaderProgram |
61 | { | |
62 | public: | |
63 | using QOpenGLShaderProgram::QOpenGLShaderProgram; | |
64 | // for some reason I cannot overload setUniformValue properly with this template thing | |
65 | template<typename Float, glm::qualifier Prec> | |
66 | void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) | |
67 | { | |
68 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
69 | this->setUniformValue(uniformName, *array); | |
70 | } | |
55 | 71 | template<typename Float, glm::qualifier Prec> |
72 | void setUniformVector(const char* uniformName, const glm::vec<4, Float, Prec>& value) | |
53 | 73 | { |
55 | 74 | this->setUniformValue(uniformName, value.x, value.y, value.z, value.w); |
53 | 75 | } |
76 | }; | |
77 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
170
diff
changeset
|
78 | extern QOpenGLFunctions glfunc; |
19 | 79 | |
80 | namespace gl | |
81 | { | |
91 | 82 | // Different ways to render the scene |
19 | 83 | enum class RenderStyle |
84 | { | |
91 | 85 | // Normal rendering style |
19 | 86 | Normal, |
91 | 87 | // Render all polygons as lines |
19 | 88 | Wireframe, |
91 | 89 | // Use green colour for front faces and red colour for back faces |
19 | 90 | BfcRedGreen, |
91 | 91 | // Use a different colour for each object |
46 | 92 | RandomColors, |
91 | 93 | // Render so that the colour of an object has an one to one correspondence with its id |
119 | 94 | PickScene, |
95 | // Render a scene where vertices can be picked | |
96 | VertexPickScene, | |
19 | 97 | }; |
91 | 98 | |
99 | // Different ways to render fragments. | |
37 | 100 | // These are also defined in shaders |
101 | enum class FragmentStyle | |
102 | { | |
91 | 103 | // Use normal colours |
37 | 104 | Normal = 0, |
91 | 105 | // Use a distinctive green colour for BFC red/green view |
37 | 106 | BfcGreen = 1, |
91 | 107 | // Use a distinctive red colour for BFC red/green view |
37 | 108 | BfcRed = 2, |
91 | 109 | // Use a colour based on the object to distinguish objects |
37 | 110 | RandomColors = 3, |
91 | 111 | // Use a colour that codes the object's id |
46 | 112 | Id = 4, |
119 | 113 | // Render everything black |
114 | Black = 5, | |
37 | 115 | }; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
116 | |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
117 | // User options for rendering |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
118 | struct RenderPreferences |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
119 | { |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
120 | gl::RenderStyle style = gl::RenderStyle::Normal; |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
121 | QColor mainColor{255, 255, 64}; |
40
30cb5e836736
added configurable background color
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
122 | QColor backgroundColor{48, 48, 48}; |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
46
diff
changeset
|
123 | QColor selectedColor{32, 32, 255}; |
44
c6114b3af3a6
added configurable line thickness
Teemu Piippo <teemu@hecknology.net>
parents:
41
diff
changeset
|
124 | GLfloat lineThickness = 2.0f; |
45
272c84c7c87e
added configurable line anti-aliasing
Teemu Piippo <teemu@hecknology.net>
parents:
44
diff
changeset
|
125 | bool lineAntiAliasing = true; |
170
9b655f6fe5a1
Added a toggle for setting whether axes are drawn
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
126 | bool drawAxes = true; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
127 | }; |
19 | 128 | } |