Visual Computing Library  devel
Loading...
Searching...
No Matches
drawable_mesh_uniforms.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2026 *
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_UNIFORMS_DRAWABLE_MESH_UNIFORMS_H
24#define VCL_BGFX_DRAWABLE_UNIFORMS_DRAWABLE_MESH_UNIFORMS_H
25
26#include <vclib/bgfx/uniform.h>
27#include <vclib/mesh.h>
28
29namespace vcl {
30
39{
40 inline static std::array<float, 4> sMeshColor = {0.5, 0.5, 0.5, 1.0};
41
42 // sMeshData[0] -> as uint, mesh id
43 // sMeshData[1] -> as uint, first chunk primitive id drawn
44 inline static std::array<float, 4> sMeshData = {0.0, 0.0, 0.0, 0.0};
45
46 inline static Uniform sMeshColorUniform;
47 inline static Uniform sMeshDataUniform;
48
49public:
50 DrawableMeshUniforms() = delete;
51
52 template<MeshConcept MeshType>
53 static void setColor(const MeshType& m)
54 {
55 if constexpr (HasColor<MeshType>) {
56 sMeshColor[0] = m.color().redF();
57 sMeshColor[1] = m.color().greenF();
58 sMeshColor[2] = m.color().blueF();
59 sMeshColor[3] = m.color().alphaF();
60 }
61 }
62
63 static void setMeshId(uint meshId)
64 {
65 sMeshData[0] = std::bit_cast<float>(meshId);
66 }
67
68 static void setFirstChunkIndex(uint firstChunkIndex)
69 {
70 sMeshData[1] = std::bit_cast<float>(firstChunkIndex);
71 }
72
73 static void bind()
74 {
75 // lazy initialization
76 // to avoid creating uniforms before bgfx is initialized
77 if (!sMeshColorUniform.isValid())
78 sMeshColorUniform = Uniform("u_meshColor", bgfx::UniformType::Vec4);
79 if (!sMeshDataUniform.isValid())
80 sMeshDataUniform = Uniform("u_meshData", bgfx::UniformType::Vec4);
81 sMeshColorUniform.bind(sMeshColor.data());
82 sMeshDataUniform.bind(sMeshData.data());
83 }
84};
85
86} // namespace vcl
87
88#endif // VCL_BGFX_DRAWABLE_UNIFORMS_DRAWABLE_MESH_UNIFORMS_H
The DrawableMeshUniforms class is responsible for managing the shader uniforms related to a drawable ...
Definition drawable_mesh_uniforms.h:39
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
The Uniform class wraps a bgfx::UniformHandle and provides a simple interface to set the uniform data...
Definition uniform.h:43
void bind(const void *data) const
Sets the uniform data for the current shader program.
Definition uniform.h:165
bool isValid() const
Checks if the Uniform is valid (i.e., if it has a valid bgfx::UniformHandle).
Definition uniform.h:129
Concept that is evaluated true if a Mesh has the Color component.
Definition mesh_requirements.h:62