Visual Computing Library
Loading...
Searching...
No Matches
drawable_mesh.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_BGFX_DRAWABLE_DRAWABLE_MESH_H
24#define VCL_BGFX_DRAWABLE_DRAWABLE_MESH_H
25
26#include <vclib/algorithms/mesh/stat/bounding_box.h>
27#include <vclib/render/drawable/abstract_drawable_mesh.h>
28
29#include <vclib/bgfx/context.h>
30#include <vclib/bgfx/drawable/mesh/mesh_render_buffers.h>
31#include <vclib/bgfx/drawable/uniforms/mesh_render_settings_uniforms.h>
32
33#include <bgfx/bgfx.h>
34
35namespace vcl {
36
37template<MeshConcept MeshType>
38class DrawableMeshBGFX : public AbstractDrawableMesh, public MeshType
39{
40 using MRI = MeshRenderInfo;
41
42 Box3d mBoundingBox;
43
45
46 bgfx::ProgramHandle mProgramEdges =
48 .programManager()
49 .getProgram<VertFragProgram::DRAWABLE_MESH_EDGES>();
50
51 bgfx::ProgramHandle mProgramPoints =
53 .programManager()
54 .getProgram<VertFragProgram::DRAWABLE_MESH_POINTS>();
55
56 bgfx::ProgramHandle mProgramSurface =
58 .programManager()
59 .getProgram<VertFragProgram::DRAWABLE_MESH_SURFACE>();
60
61 bgfx::ProgramHandle mProgramWireframe =
63 .programManager()
64 .getProgram<VertFragProgram::DRAWABLE_MESH_WIREFRAME>();
65
66 mutable MeshRenderSettingsUniforms mMeshRenderSettingsUniforms;
67
68public:
69 DrawableMeshBGFX() = default;
70
71 DrawableMeshBGFX(const MeshType& mesh) :
72 AbstractDrawableMesh(mesh), MeshType(mesh)
73 {
74 updateBuffers();
75 }
76
77 DrawableMeshBGFX(MeshType&& mesh) :
78 AbstractDrawableMesh(mesh), MeshType(std::move(mesh))
79 {
80 updateBuffers();
81 }
82
85 MeshType(drawableMesh), mBoundingBox(drawableMesh.mBoundingBox),
86 mMeshRenderSettingsUniforms(
87 drawableMesh.mMeshRenderSettingsUniforms)
88 {
89 if constexpr (HasName<MeshType>) {
91 }
92 mMRB.update(*this);
93 }
94
96
97 ~DrawableMeshBGFX() = default;
98
100 {
101 swap(drawableMesh);
102 return *this;
103 }
104
105 void updateBuffers(
106 MRI::BuffersBitSet buffersToUpdate = MRI::BUFFERS_ALL) override
107 {
108 if constexpr (HasName<MeshType>) {
109 AbstractDrawableMesh::name() = MeshType::name();
110 }
111
113 if constexpr (vcl::HasBoundingBox<MeshType>) {
114 if (this->MeshType::boundingBox().isNull()) {
115 bbToInitialize = true;
116 }
117 else {
118 mBoundingBox =
119 this->MeshType::boundingBox().template cast<double>();
120 }
121 }
122
123 if (bbToInitialize) {
124 mBoundingBox = vcl::boundingBox(*this);
125 }
126
127 mMRB.update(*this, buffersToUpdate);
128 mMRS.setRenderCapabilityFrom(*this);
129 mMeshRenderSettingsUniforms.updateSettings(mMRS);
130 }
131
132 std::string& name() override { return MeshType::name(); }
133
134 const std::string& name() const override { return MeshType::name(); }
135
136 void swap(DrawableMeshBGFX& other)
137 {
138 using std::swap;
139 AbstractDrawableMesh::swap(other);
140 MeshType::swap(other);
141 swap(mBoundingBox, other.mBoundingBox);
142 swap(mMRB, other.mMRB);
143 swap(mMeshRenderSettingsUniforms, other.mMeshRenderSettingsUniforms);
144 }
145
146 friend void swap(DrawableMeshBGFX& a, DrawableMeshBGFX& b) { a.swap(b); }
147
148 // DrawableObject implementation
149
150 void init() override {}
151
152 void draw(uint viewId) const override
153 {
157
158 if (mMRS.isPoints(MRI::Points::VISIBLE)) {
159 if (bgfx::isValid(mProgramPoints)) {
160 mMRB.bindVertexBuffers(mMRS);
161 bindUniforms();
162
163 bgfx::setState(state | BGFX_STATE_PT_POINTS);
164
165 bgfx::submit(viewId, mProgramPoints);
166 }
167 }
168
169 if (mMRS.isSurface(MRI::Surface::VISIBLE)) {
170 if (bgfx::isValid(mProgramSurface)) {
171 mMRB.bindTextures(); // Bind textures before vertex buffers!!
172 mMRB.bindVertexBuffers(mMRS);
173 mMRB.bindIndexBuffers(mMRS);
174 bindUniforms();
175
176 bgfx::setState(state);
177
178 bgfx::submit(viewId, mProgramSurface);
179 }
180 }
181
182 if (mMRS.isWireframe(MRI::Wireframe::VISIBLE)) {
183 if (bgfx::isValid(mProgramWireframe)) {
184 mMRB.bindVertexBuffers(mMRS);
185 mMRB.bindIndexBuffers(mMRS, MRI::Buffers::WIREFRAME);
186 bindUniforms();
187
188 bgfx::setState(state | BGFX_STATE_PT_LINES);
189
190 bgfx::submit(viewId, mProgramWireframe);
191 }
192 }
193
194 if (mMRS.isEdges(MRI::Edges::VISIBLE)) {
195 if (bgfx::isValid(mProgramEdges)) {
196 mMRB.bindVertexBuffers(mMRS);
197 mMRB.bindIndexBuffers(mMRS, MRI::Buffers::EDGES);
198 bindUniforms();
199
200 bgfx::setState(state | BGFX_STATE_PT_LINES);
201
202 bgfx::submit(viewId, mProgramEdges);
203 }
204 }
205 }
206
207 Box3d boundingBox() const override { return mBoundingBox; }
208
209 std::shared_ptr<DrawableObject> clone() const& override
210 {
211 return std::make_shared<DrawableMeshBGFX>(*this);
212 }
213
214 std::shared_ptr<DrawableObject> clone() && override
215 {
216 return std::make_shared<DrawableMeshBGFX>(std::move(*this));
217 }
218
219 void setVisibility(bool vis) override
220 {
222 mMeshRenderSettingsUniforms.updateSettings(mMRS);
223 }
224
225 void setRenderSettings(const MeshRenderSettings& rs) override
226 {
227 AbstractDrawableMesh::setRenderSettings(rs);
228 mMeshRenderSettingsUniforms.updateSettings(rs);
229 }
230
231private:
232 void bindUniforms() const
233 {
234 mMeshRenderSettingsUniforms.bind();
235 mMRB.bindUniforms();
236 }
237};
238
239} // namespace vcl
240
241#endif // VCL_BGFX_DRAWABLE_DRAWABLE_MESH_H
Definition abstract_drawable_mesh.h:32
void setVisibility(bool vis)
This member function is used to set the visibility of the object.
Definition abstract_drawable_mesh.h:58
The BitSet class allows to treat an integral type as an array of booleans of a guaranteed size.
Definition bit_set.h:53
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:365
Definition drawable_mesh.h:39
std::shared_ptr< DrawableObject > clone() const &override
This member function is used to create a new copy of the DrawableObject. Each derived class must impl...
Definition drawable_mesh.h:209
const std::string & name() const override
Returns the name of the object.
Definition drawable_mesh.h:134
Box3d boundingBox() const override
This member function is used to find a good camera position to render object. It should return the th...
Definition drawable_mesh.h:207
std::shared_ptr< DrawableObject > clone() &&override
This member function is used to create a new DrawableObject that is a moved from the current one....
Definition drawable_mesh.h:214
std::string & name() override
Returns a reference of the name of the object, that allows to modify it.
Definition drawable_mesh.h:132
void draw(uint viewId) const override
This member function must draw the object. It will be called at every frame.
Definition drawable_mesh.h:152
void setVisibility(bool vis) override
This member function is used to set the visibility of the object.
Definition drawable_mesh.h:219
void init() override
This member function is called after the initialization of the Context. It must initialize and bind d...
Definition drawable_mesh.h:150
virtual const std::string & name() const
Returns the name of the object.
Definition drawable_object.h:153
The MeshRenderInfo class is a collection of rendering settings for a Mesh.
Definition mesh_render_info.h:61
Definition mesh_render_settings_uniforms.h:34
The MeshRenderSettings class allows an easy management of render settings of a Mesh....
Definition mesh_render_settings.h:71
bool isPoints(MeshRenderInfo::Points p) const
Returns whether the given points option is set.
Definition mesh_render_settings.h:226
bool isSurface(MeshRenderInfo::Surface s) const
Returns whether the given surface option is set.
Definition mesh_render_settings.h:251
bool isWireframe(MeshRenderInfo::Wireframe w) const
Returns whether the given wireframe option is set.
Definition mesh_render_settings.h:271
bool isEdges(MeshRenderInfo::Edges e) const
Returns whether the given edges option is set.
Definition mesh_render_settings.h:296
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Concept that is evaluated true if a Mesh has the BoundingBox component.
Definition per_mesh.h:60
Concept that checks if a Mesh has the Name component.
Definition per_mesh.h:95
auto boundingBox(const PointType &p)
Compute the bounding box of a single point.
Definition bounding_box.h:65