Visual Computing Library
All Classes Functions Variables Typedefs Enumerations Friends Modules Pages Concepts
mesh_render_vectors.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_OPENGL2_DRAWABLE_MESH_MESH_RENDER_VECTORS_H
24#define VCL_OPENGL2_DRAWABLE_MESH_MESH_RENDER_VECTORS_H
25
26#include <vclib/algorithms/mesh/import_export/append_replace_to_buffer.h>
27#include <vclib/algorithms/mesh/import_export/export_buffer.h>
28#include <vclib/algorithms/mesh/stat/topology.h>
29#include <vclib/math/min_max.h>
30#include <vclib/mesh/requirements.h>
31#include <vclib/render/drawable/mesh/mesh_render_data.h>
32#include <vclib/render/drawable/mesh/mesh_render_info.h>
33#include <vclib/space/complex/tri_poly_index_bimap.h>
34#include <vclib/space/core/image.h>
35#include <vclib/space/core/texture.h>
36
37namespace vcl {
38
39template<MeshConcept Mesh>
40class MeshRenderVectors : public MeshRenderData<MeshRenderVectors<Mesh>>
41{
42 using MeshType = Mesh;
44 using MRI = MeshRenderInfo;
45
46 friend Base;
47
48 std::vector<float> mVerts;
49 std::vector<float> mVNormals;
50 std::vector<uint32_t> mVColors;
51 std::vector<float> mVTexCoords;
52 std::vector<float> mWTexCoords;
53
54 std::vector<uint32_t> mTris;
55 std::vector<float> mTNormals;
56 std::vector<uint32_t> mTColors;
57 std::vector<uint32_t> mVTexIds;
58 std::vector<uint32_t> mWTexIds;
59
60 std::vector<uint32_t> mEdges;
61 std::vector<float> mENormals;
62 std::vector<uint32_t> mEColors;
63
64 std::vector<uint32_t> mWireframe;
65
66 std::vector<vcl::Image> mTextures;
67
68 std::array<float, 4> mMeshColor = {0.5, 0.5, 0.5, 1};
69
70public:
71 MeshRenderVectors() = default;
72
74 const MeshType& mesh,
75 MRI::BuffersBitSet buffersToFill = MRI::BUFFERS_ALL) :
77 {
78 Base::update(mesh, buffersToFill);
79 }
80
81 void swap(MeshRenderVectors& other)
82 {
83 using std::swap;
84 Base::swap(other);
85 swap(mVerts, other.mVerts);
86 swap(mVNormals, other.mVNormals);
87 swap(mVColors, other.mVColors);
88 swap(mVTexCoords, other.mVTexCoords);
89 swap(mWTexCoords, other.mWTexCoords);
90 swap(mTris, other.mTris);
91 swap(mTNormals, other.mTNormals);
92 swap(mTColors, other.mTColors);
93 swap(mVTexIds, other.mVTexIds);
94 swap(mWTexIds, other.mWTexIds);
95 swap(mEdges, other.mEdges);
96 swap(mENormals, other.mENormals);
97 swap(mEColors, other.mEColors);
98 swap(mWireframe, other.mWireframe);
99 swap(mTextures, other.mTextures);
100 swap(mMeshColor, other.mMeshColor);
101 }
102
103 uint vertexNumber() const { return mVerts.size() / 3; }
104
105 uint triangleNumber() const { return mTris.size() / 3; }
106
107 uint edgeNumber() const { return mEdges.size() / 2; }
108
109 uint wireframeEdgeNumber() const { return mWireframe.size() / 2; }
110
111 uint textureNumber() const { return mTextures.size(); }
112
113 vcl::Point2i textureSize(uint ti) const
114 {
115 return vcl::Point2i(mTextures[ti].width(), mTextures[ti].height());
116 }
117
118 const float* vertexBufferData() const
119 {
120 if (mVerts.empty())
121 return nullptr;
122 return mVerts.data();
123 }
124
125 uint vertexBufferSize() const { return mVerts.size(); }
126
127 const uint32_t* triangleBufferData() const
128 {
129 if (mTris.empty())
130 return nullptr;
131 return mTris.data();
132 }
133
134 uint triangleBufferSize() const { return mTris.size(); }
135
136 const uint32_t* edgeBufferData() const
137 {
138 if (mEdges.empty())
139 return nullptr;
140 return mEdges.data();
141 }
142
143 uint edgeBufferSize() const { return mEdges.size(); }
144
145 const uint32_t* wireframeBufferData() const
146 {
147 if (mWireframe.empty())
148 return nullptr;
149 return mWireframe.data();
150 }
151
152 uint wireframeBufferSize() const { return mWireframe.size(); }
153
154 const float* vertexNormalBufferData() const
155 {
156 if (mVNormals.empty())
157 return nullptr;
158 return mVNormals.data();
159 }
160
161 const uint32_t* vertexColorBufferData() const
162 {
163 if (mVColors.empty())
164 return nullptr;
165 return mVColors.data();
166 }
167
168 const float* triangleNormalBufferData() const
169 {
170 if (mTNormals.empty())
171 return nullptr;
172 return mTNormals.data();
173 }
174
175 const uint32_t* triangleColorBufferData() const
176 {
177 if (mTColors.empty())
178 return nullptr;
179 return mTColors.data();
180 }
181
182 const float* vertexTexCoordsBufferData() const
183 {
184 if (mVTexCoords.empty())
185 return nullptr;
186 return mVTexCoords.data();
187 }
188
189 const uint32_t* vertexTextureIDsBufferData() const
190 {
191 if (mVTexIds.empty())
192 return nullptr;
193 return mVTexIds.data();
194 }
195
196 const float* wedgeTexCoordsBufferData() const
197 {
198 if (mWTexCoords.empty())
199 return nullptr;
200 return mWTexCoords.data();
201 }
202
203 const uint32_t* wedgeTextureIDsBufferData() const
204 {
205 if (mWTexIds.empty())
206 return nullptr;
207 return mWTexIds.data();
208 }
209
210 const float* edgeNormalBufferData() const
211 {
212 if (mENormals.empty())
213 return nullptr;
214 return mENormals.data();
215 }
216
217 const uint32_t* edgeColorBufferData() const
218 {
219 if (mEColors.empty())
220 return nullptr;
221 return mEColors.data();
222 }
223
224 const float* meshColorBufferData() const { return mMeshColor.data(); }
225
226 const unsigned char* textureBufferData(uint ti) const
227 {
228 return mTextures[ti].data();
229 }
230
231private:
232 void setVertexCoordsBuffer(const MeshType& mesh) // override
233 {
234 uint nv = Base::numVerts();
235
236 mVerts.resize(nv * 3);
237
238 Base::fillVertexCoords(mesh, mVerts.data());
239 }
240
241 void setVertexNormalsBuffer(const MeshType& mesh) // override
242 {
243 uint nv = Base::numVerts();
244
245 mVNormals.resize(nv * 3);
246
247 Base::fillVertexNormals(mesh, mVNormals.data());
248 }
249
250 void setVertexColorsBuffer(const MeshType& mesh) // override
251 {
252 uint nv = Base::numVerts();
253
254 mVColors.resize(nv);
255
256 Base::fillVertexColors(mesh, mVColors.data(), Color::Format::ABGR);
257 }
258
259 void setVertexTexCoordsBuffer(const MeshType& mesh) // override
260 {
261 uint nv = Base::numVerts();
262
263 mVTexCoords.resize(nv * 2);
264
265 Base::fillVertexTexCoords(mesh, mVTexCoords.data());
266 }
267
268 void setWedgeTexCoordsBuffer(const MeshType& mesh) // override
269 {
270 uint nv = Base::numVerts();
271
272 mWTexCoords.resize(nv * 2);
273
274 Base::fillWedgeTexCoords(mesh, mWTexCoords.data());
275 }
276
277 void setTriangleIndicesBuffer(const MeshType& mesh) // override
278 {
279 uint nt = Base::numTris();
280
281 mTris.resize(nt * 3);
282
283 Base::fillTriangleIndices(mesh, mTris.data());
284 }
285
286 void setTriangleNormalsBuffer(const MeshType& mesh) // override
287 {
288 uint nt = Base::numTris();
289
290 mTNormals.resize(nt * 3);
291
292 Base::fillTriangleNormals(mesh, mTNormals.data());
293 }
294
295 void setTriangleColorsBuffer(const MeshType& mesh) // override
296 {
297 uint nt = Base::numTris();
298
299 mTColors.resize(nt);
300
301 Base::fillTriangleColors(mesh, mTColors.data(), Color::Format::ABGR);
302 }
303
304 void setVertexTextureIndicesBuffer(const MeshType& mesh) // override
305 {
306 if (vcl::isPerVertexTexCoordAvailable(mesh)) {
307 uint nt = Base::numTris();
308
309 mVTexIds.resize(nt);
310
311 Base::fillVertexTextureIndices(mesh, mVTexIds.data());
312 }
313 }
314
315 void setWedgeTextureIndicesBuffer(const MeshType& mesh) // override
316 {
317 uint nt = Base::numTris();
318
319 mWTexIds.resize(nt);
320
321 Base::fillWedgeTextureIndices(mesh, mWTexIds.data());
322 }
323
324 void setEdgeIndicesBuffer(const MeshType& mesh) // override
325 {
326 uint ne = Base::numEdges();
327
328 mEdges.resize(ne * 2);
329
330 Base::fillEdgeIndices(mesh, mEdges.data());
331 }
332
333 void setEdgeNormalsBuffer(const MeshType& mesh) // override
334 {
335 uint ne = Base::numEdges();
336
337 mENormals.resize(ne * 3);
338
339 Base::fillEdgeNormals(mesh, mENormals.data());
340 }
341
342 void setEdgeColorsBuffer(const MeshType& mesh) // override
343 {
344 uint ne = Base::numEdges();
345
346 mEColors.resize(ne);
347
348 Base::fillEdgeColors(mesh, mEColors.data(), Color::Format::ABGR);
349 }
350
351 void setWireframeIndicesBuffer(const MeshType& mesh) // override
352 {
353 const uint nw = Base::numWireframeLines();
354
355 mWireframe.resize(nw * 2);
356
357 Base::fillWireframeIndices(mesh, mWireframe.data());
358 }
359
360 void setTextureUnits(const MeshType& mesh) // override
361 {
362 mTextures.clear();
363 if constexpr (vcl::HasTextureImages<MeshType>) {
364 for (const vcl::Texture& t : mesh.textures()) {
365 if (t.image().isNull()) { // the texture has not been loaded
366 vcl::Image txt(mesh.meshBasePath() + t.path());
367 txt.mirror();
368 mTextures.push_back(txt);
369 }
370 else {
371 mTextures.push_back(t.image());
372 mTextures.back().mirror();
373 }
374 }
375 }
376 else {
377 for (uint i = 0; i < mesh.textureNumber(); ++i) {
378 vcl::Image txt(mesh.meshBasePath() + mesh.texturePath(i));
379 txt.mirror();
380 mTextures.push_back(txt);
381 }
382 }
383 }
384
385 void setMeshUniforms(const MeshType& m) // override
386 {
387 if constexpr (vcl::HasColor<MeshType>) {
388 mMeshColor[0] = m.color().redF();
389 mMeshColor[1] = m.color().greenF();
390 mMeshColor[2] = m.color().blueF();
391 mMeshColor[3] = m.color().alphaF();
392 }
393 }
394};
395
396} // namespace vcl
397
398#endif // VCL_OPENGL2_DRAWABLE_MESH_MESH_RENDER_VECTORS_H
The BitSet class allows to treat an integral type as an array of booleans of a guaranteed size.
Definition bit_set.h:53
The Image class stores an Image in 4 bytes RGBA format.
Definition image.h:44
The MeshRenderData class provides a common interface to automatically update the buffers used to rend...
Definition mesh_render_data.h:81
The MeshRenderInfo class is a collection of rendering settings for a Mesh.
Definition mesh_render_info.h:61
Definition mesh_render_vectors.h:41
The Mesh class represents a generic 3D mesh. A mesh is composed of a generic number of containers of ...
Definition mesh.h:68
The Point class represents an N-dimensional point containing N scalar values.
Definition point.h:58
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition texture.h:34
Concept that is evaluated true if a Mesh has the Color component.
Definition per_mesh.h:69
Concept that checks if a Mesh has the TextureImages component.
Definition per_mesh.h:103
Point2< int > Point2i
A convenience alias for a 2-dimensional Point with integer components.
Definition point.h:731