Thu, 15 Jun 2023 16:18:03 +0300
Refactor, make selecting elements from the model select the corresponding line from the editor as well
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 | |
291
42b4953dff85
Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
290
diff
changeset
|
20 | #include <GL/glew.h> |
30 | 21 | #include <glm/gtc/type_ptr.hpp> |
26 | 22 | #include <QOpenGLVertexArrayObject> |
23 | #include <QOpenGLBuffer> | |
24 | #include <QOpenGLShaderProgram> | |
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
25 | #include "src/basics.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
26 | #include "src/types/boundingbox.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
27 | #include "src/model.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
28 | #include "src/gl/common.h" |
19 | 29 | |
21 | 30 | class DocumentManager; |
31 | ||
19 | 32 | namespace gl |
33 | { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
34 | // VAO names |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
35 | enum class ArrayClass : std::uint8_t |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
36 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
37 | Lines, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
38 | Triangles, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
39 | Quads, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
40 | ConditionalLines |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
41 | }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
42 | |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
43 | constexpr ArrayClass ARRAY_CLASSES[] = { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
44 | ArrayClass::Lines, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
45 | ArrayClass::Triangles, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
46 | ArrayClass::Quads, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
47 | ArrayClass::ConditionalLines, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
48 | }; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
49 | constexpr int NUM_ARRAY_CLASSES = countof(ARRAY_CLASSES); |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
50 | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
51 | struct ModelShaders |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
52 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
53 | struct Vertex |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
54 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
55 | glm::vec3 position; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
56 | glm::vec4 color; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
57 | glm::vec3 normal; |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
58 | glm::vec3 pickcolor; |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
59 | glm::int32 id; |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
60 | float selected = 0.0f; |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
61 | }; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
62 | bool initialized = false; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
63 | struct ShaderObject |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
64 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
65 | std::unique_ptr<QOpenGLShaderProgram> program = nullptr; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
66 | QOpenGLShaderProgram* pickSceneProgram = nullptr; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
67 | QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer}; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
68 | QOpenGLVertexArrayObject vertexArray; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
69 | std::vector<Vertex> cachedData; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
70 | std::size_t vertexCount; |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
206
diff
changeset
|
71 | } shaderObjects[gl::NUM_ARRAY_CLASSES]; |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
72 | }; |
19 | 73 | |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
210
diff
changeset
|
74 | void build(ModelShaders* shaders, |
338 | 75 | QTextDocument* model, |
205 | 76 | const ColorTable& colorTable, |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
77 | DocumentManager* context, |
376
3cef3b016330
Split GL preferences that affect GL build to a new build preferences structure, modifying that requires rebuild, modifying render preferences does not
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
338
diff
changeset
|
78 | const build_preferences* preferences); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
79 | void initializeModelShaders(ModelShaders* modelShaders); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
80 | void bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
81 | void releaseModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass); |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
82 | //void setModelShaderSelectedObjects(gl::ModelShaders* shaders, const QSet<ElementId>& ids); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
83 | std::size_t vertexCount(const ModelShaders *shaders, gl::ArrayClass arrayClass); |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
84 | int idFromUcharColor(const std::array<GLubyte, 3>& data); |
47 | 85 | |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
86 | template<typename... Ts> |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
87 | void setShaderUniform(gl::ModelShaders* shaders, const char* uniformName, Ts&&... args) |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
88 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
89 | for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
90 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
91 | shader.program->bind(); |
291
42b4953dff85
Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
290
diff
changeset
|
92 | const int location = glGetUniformLocation(shader.program->programId(), uniformName); |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
93 | if (location != -1) { |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
94 | shader.program->setUniformValue(location, std::forward<Ts>(args)...); |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
95 | } |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
96 | shader.program->release(); |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
97 | } |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
98 | } |
30 | 99 | |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
100 | inline void setShaderUniformMatrix( |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
101 | gl::ModelShaders* shaders, |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
102 | const char* uniformName, |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
103 | const glm::mat<4, 4, float>& value) |
30 | 104 | { |
105 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
106 | setShaderUniform(shaders, uniformName, *array); |
30 | 107 | } |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
108 | |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
109 | inline void setShaderUniformVector( |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
110 | gl::ModelShaders* shaders, |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
111 | const char* uniformName, |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
112 | const glm::vec4& value) |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
113 | { |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
114 | setShaderUniform(shaders, uniformName, value.x, value.y, value.z, value.w); |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
115 | } |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
116 | |
338 | 117 | BoundingBox boundingBoxForModel(QTextDocument* model, DocumentManager* context); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
118 | } |
19 | 119 | |
120 | #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); } | |
121 | void checkGLError (QString file, int line); |