Sat, 05 Mar 2022 12:47:10 +0200
fix crash when loading a file
`object` is moved so we cannot access it after moving
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 | |
20 | #include "main.h" | |
21 | #include "gl/common.h" | |
22 | #include "types/boundingbox.h" | |
30 | 23 | #include <glm/gtc/type_ptr.hpp> |
19 | 24 | #include <QMap> |
25 | #include <QSet> | |
26 | 26 | #include <QOpenGLVertexArrayObject> |
27 | #include <QOpenGLBuffer> | |
28 | #include <QOpenGLShaderProgram> | |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
29 | #include <QOpenGLExtraFunctions> |
19 | 30 | |
21 | 31 | class Model; |
32 | class DocumentManager; | |
33 | ||
19 | 34 | namespace gl |
35 | { | |
36 | class Compiler; | |
37 | } | |
38 | ||
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
39 | class gl::Compiler : public QObject, protected QOpenGLExtraFunctions |
19 | 40 | { |
41 | Q_OBJECT | |
42 | public: | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
43 | Compiler(Model* model, const ldraw::ColorTable& colorTable, QObject* parent); |
19 | 44 | ~Compiler(); |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
45 | void build(DocumentManager* context, const RenderPreferences& preferences); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
46 | std::size_t vertexCount(gl::ArrayClass arrayClass) const; |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
47 | QColor getColorForPolygon(const gl::Polygon& polygon, const RenderPreferences& preferences); |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
48 | glm::vec3 modelCenter() const; |
51 | 49 | double modelDistance() const; |
26 | 50 | void initialize(); |
51 | void bindVertexArray(gl::ArrayClass arrayClass); | |
52 | void releaseVertexArray(gl::ArrayClass arrayClass); | |
53 | void buildShaders(int arrayId); | |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
54 | void setSelectedObjects(const QSet<ldraw::id_t> ids); |
30 | 55 | |
78
97c3ce5aa498
fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
56 | static ldraw::id_t idFromColor(const std::array<GLubyte, 3>& data); |
47 | 57 | |
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
|
58 | template<typename T> |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
59 | void setUniform(const char* uniformName, T&& value) |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
60 | { |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
61 | Q_ASSERT(this->initialized); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
62 | for (auto& object : this->glObjects) |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
63 | { |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
64 | object.program->bind(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
65 | const int location = glGetUniformLocation(object.program->programId(), uniformName); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
66 | Q_ASSERT(location != -1); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
67 | object.program->setUniformValue(location, std::forward<T>(value)); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
68 | object.program->release(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
69 | } |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
70 | } |
30 | 71 | |
72 | template<typename Float, glm::qualifier Prec> | |
73 | void setUniformMatrix(const char* uniformName, const glm::mat<4, 4, Float, Prec>& value) | |
74 | { | |
75 | const float (*array)[4][4] = reinterpret_cast<const float(*)[4][4]>(glm::value_ptr(value)); | |
76 | this->setUniform(uniformName, *array); | |
77 | } | |
19 | 78 | private: |
80 | 79 | struct Vertex |
80 | { | |
81 | glm::vec3 position; | |
82 | glm::vec4 color; | |
83 | glm::vec3 normal; | |
84 | glm::int32 id; | |
85 | glm::int32 selected = 0; | |
86 | }; | |
87 | void buildPolygon(Polygon polygon, std::vector<Vertex>* vboData, const gl::RenderPreferences& preferences); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
80
diff
changeset
|
88 | Model* const model; |
80 | 89 | std::size_t storedVertexCounts[gl::NUM_POLYGON_TYPES] = {0}; |
21 | 90 | bool initialized = false; |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
91 | BoundingBox boundingBox; |
35
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
Teemu Piippo <teemu@hecknology.net>
parents:
34
diff
changeset
|
92 | const ldraw::ColorTable& colorTable; |
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
93 | ldraw::id_t hovered = ldraw::NULL_ID; |
26 | 94 | struct |
95 | { | |
96 | QOpenGLShaderProgram* program = nullptr; | |
46 | 97 | QOpenGLShaderProgram* pickSceneProgram = nullptr; |
26 | 98 | QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer}; |
99 | QOpenGLVertexArrayObject vertexArray; | |
80 | 100 | std::vector<Vertex> cachedData; |
101 | } glObjects[gl::NUM_POLYGON_TYPES]; | |
19 | 102 | }; |
103 | ||
104 | #define CHECK_GL_ERROR() { checkGLError(__FILE__, __LINE__); } | |
105 | void checkGLError (QString file, int line); |