Mon, 20 Jun 2022 21:14:17 +0300
Fix pick scene rendering: we cannot use glReadPixels when using multisampling, so we need to render the pick scene to a separate frame buffer
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
1 | #include "polygoncache.h" |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
2 | #include "documentmanager.h" |
200 | 3 | #include "invert.h" |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
4 | #include "circularprimitive.h" |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | |
200 | 6 | Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents) |
7 | { | |
8 | return documents->findDependencyByName(callingModelId, name); | |
9 | } | |
10 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
11 | static PolygonElement transformed( |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
12 | PolygonElement element, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
13 | const glm::mat4& transform) |
200 | 14 | { |
211 | 15 | visitPoints([&transform](glm::vec3& p) { |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
16 | p = transform * glm::vec4{p, 1}; |
211 | 17 | }, element); |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
18 | return element; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
19 | } |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
20 | |
211 | 21 | PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context) |
22 | { | |
23 | std::optional<ModelId> modelId = context->findIdForModel(model); | |
24 | if (modelId.has_value()) { | |
25 | return context->getPolygonCacheForModel(modelId.value()); | |
26 | } | |
27 | else { | |
28 | return nullptr; | |
29 | } | |
30 | } | |
31 | ||
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
32 | static std::vector<WithId<PolygonElement>> getPolygonsAt( |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
33 | const Model* model, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
34 | GetPolygonsContext* context) |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
35 | { |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
36 | std::vector<WithId<PolygonElement>> result; |
200 | 37 | for (int i = 0; i < model->size(); i += 1) |
38 | { | |
39 | const ModelElement& element = (*model)[i]; | |
40 | const ModelId id = model->idAt(i); | |
41 | std::visit<void>(overloaded{ | |
42 | [&](const Colored<LineSegment>& edge) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
43 | result.push_back({{edge, edge.color}, id}); |
200 | 44 | }, |
45 | [&](const Colored<Triangle>& triangle) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
46 | result.push_back({{triangle, triangle.color}, id}); |
200 | 47 | }, |
48 | [&](const Colored<Quadrilateral>& quad) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
49 | result.push_back({{quad, quad.color}, id}); |
200 | 50 | }, |
51 | [&](const Colored<ConditionalEdge>& cedge) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
52 | result.push_back({{cedge, cedge.color}, id}); |
200 | 53 | }, |
211 | 54 | [&result, &id, context](const Colored<SubfileReference>& ref) { |
200 | 55 | Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name); |
56 | PolygonCache* cache = nullptr; | |
211 | 57 | if (dependency != nullptr) { |
58 | cache = findPolygonCacheForModel(dependency, context->documents); | |
200 | 59 | } |
60 | if (cache != nullptr) { | |
61 | const bool needInverting = glm::determinant(ref.transformation) < 0; | |
211 | 62 | const PolygonCache::vector_type* modelPolygons = getCachedPolygons( |
200 | 63 | cache, |
64 | dependency, | |
65 | context->documents); | |
211 | 66 | reserveMore(result, modelPolygons->size()); |
67 | for (WithId<PolygonElement> polygon : *modelPolygons) { | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
68 | polygon = {transformed(polygon, ref.transformation), polygon.id}; |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
69 | if (needInverting != ref.inverted) { |
200 | 70 | gl::invert(polygon); |
71 | } | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
72 | if (polygon.color == MAIN_COLOR) { |
200 | 73 | polygon.color = ref.color; |
74 | } | |
75 | polygon.id = id; | |
76 | result.push_back(polygon); | |
77 | } | |
78 | } | |
79 | }, | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
80 | [&result, id](const Colored<CircularPrimitive>& circ) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
81 | rasterize(circ, [&](const ModelElement& element){ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
82 | std::visit<void>(overloaded{ |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
83 | // TODO: :-( |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
84 | [&](const Colored<LineSegment>& edge) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
85 | result.push_back({{edge, edge.color}, id}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
86 | }, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
87 | [&](const Colored<Triangle>& triangle) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
88 | result.push_back({{triangle, triangle.color}, id}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
89 | }, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
90 | [&](const Colored<Quadrilateral>& quad) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
91 | result.push_back({{quad, quad.color}, id}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
92 | }, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
93 | [&](const Colored<ConditionalEdge>& cedge) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
94 | result.push_back({{cedge, cedge.color}, id}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
95 | }, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
96 | [&](const auto&){}, |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
97 | }, element); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
98 | }); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
211
diff
changeset
|
99 | }, |
200 | 100 | [](const ModelElement&) {} |
101 | }, element); | |
102 | } | |
103 | return result; | |
104 | } | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
105 | |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
106 | /** |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
107 | * @brief Gets a list of GL polygons that are used to represent this model. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
108 | * @details Will build polygons if polygons are outdated. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
109 | * @param documents Documents to use to resolve subfile references |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
110 | * @return vector of GL polygons |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
111 | */ |
211 | 112 | const PolygonCache::vector_type* getCachedPolygons( |
193 | 113 | PolygonCache *cache, |
114 | Model *model, | |
115 | DocumentManager *documents) | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
116 | { |
193 | 117 | if (cache->needRecache) |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
118 | { |
193 | 119 | cache->cachedPolygons.clear(); |
120 | const std::optional<ModelId> modelId = documents->findIdForModel(model); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
121 | if (modelId.has_value()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
122 | { |
200 | 123 | GetPolygonsContext context{modelId.value(), documents}; |
124 | cache->cachedPolygons = getPolygonsAt(model, &context); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
125 | } |
193 | 126 | cache->needRecache = false; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
127 | } |
211 | 128 | return &cache->cachedPolygons; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
129 | } |