Wed, 09 Mar 2022 12:42:45 +0200
Render vertices as spheres
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" |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
3 | |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
4 | PolygonCache::PolygonCache(Model *model) : |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
5 | model{model} |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
6 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
7 | const auto mark = [this](){ this->needRecache = true; }; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
8 | connect(model, &Model::dataChanged, mark); |
152 | 9 | connect(model, &Model::rowsInserted, mark); |
10 | connect(model, &Model::rowsRemoved, mark); | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
11 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
12 | |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
13 | /** |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
14 | * @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
|
15 | * @details Will build polygons if polygons are outdated. |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
16 | * @param documents Documents to use to resolve subfile references |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
17 | * @return vector of GL polygons |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
18 | */ |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
19 | std::vector<gl::Polygon> PolygonCache::getPolygons(DocumentManager* documents) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
20 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
21 | if (this->needRecache) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
22 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
23 | this->cachedPolygons.clear(); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
24 | const std::optional<ModelId> modelId = documents->findIdForModel(this->model); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
25 | if (modelId.has_value()) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
26 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
27 | ldraw::GetPolygonsContext context{modelId.value(), documents}; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
28 | for (int i = 0; i < this->model->size(); i += 1) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
29 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
30 | this->getObjectPolygons(i, this->cachedPolygons, &context); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
31 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
32 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
33 | this->needRecache = false; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
34 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
35 | return this->cachedPolygons; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
36 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
37 | |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
38 | /** |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
39 | * @brief Gets the GL polygons of the object at the specified position in the model |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
40 | * @param index Index of object in the model |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
41 | * @param polygons_out Vector to add polygons into |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
42 | * @param context Context to use to resolve subfile references |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | */ |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
44 | void PolygonCache::getObjectPolygons( |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
45 | const int index, |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
46 | std::vector<gl::Polygon>& polygons_out, |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | ldraw::GetPolygonsContext* context) const |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
48 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
49 | const ldraw::Object* object = (*this->model)[unsigned_cast(index)]; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
50 | object->getPolygons(polygons_out, context); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
51 | } |