Visual Computing Library  devel
Loading...
Searching...
No Matches
directional_light_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_DIRECTIONAL_LIGHT_UNIFORMS_H
24#define VCL_BGFX_DRAWABLE_UNIFORMS_DIRECTIONAL_LIGHT_UNIFORMS_H
25
26#include <vclib/bgfx/uniform.h>
27#include <vclib/render/viewer/lights/directional_light.h>
28
29#include <bgfx/bgfx.h>
30
31namespace vcl {
32
52{
53 // just first 3 components are used
54 inline static std::array<float, 4> sDir = {0.0, 0.0, 1.0, 0.0};
55
56 // just first 3 components are used
57 inline static std::array<float, 4> sCol = {1.0, 1.0, 1.0, 1.0};
58
59 inline static Uniform sLightDirUniform;
60 inline static Uniform sLightColorUniform;
61
62public:
63 DirectionalLightUniforms() = delete;
64
69 template<typename S>
70 static void setLight(const vcl::DirectionalLight<S>& light)
71 {
72 sDir[0] = light.direction().x();
73 sDir[1] = light.direction().y();
74 sDir[2] = light.direction().z();
75
76 sCol[0] = light.color().redF();
77 sCol[1] = light.color().greenF();
78 sCol[2] = light.color().blueF();
79 // light color alpha is not used
80 }
81
82 static void bind()
83 {
84 // lazy initialization
85 // to avoid creating uniforms before bgfx is initialized
86 if (!sLightDirUniform.isValid())
87 sLightDirUniform =
88 Uniform("u_lightDirPack", bgfx::UniformType::Vec4);
89 if (!sLightColorUniform.isValid())
90 sLightColorUniform =
91 Uniform("u_lightColorPack", bgfx::UniformType::Vec4);
92 sLightDirUniform.bind(sDir.data());
93 sLightColorUniform.bind(sCol.data());
94 }
95};
96
97} // namespace vcl
98
99#endif // VCL_BGFX_DRAWABLE_UNIFORMS_DIRECTIONAL_LIGHT_UNIFORMS_H
The DirectionalLightUniforms class is responsible for managing the uniforms which describe a directio...
Definition directional_light_uniforms.h:52
static void setLight(const vcl::DirectionalLight< S > &light)
setLight
Definition directional_light_uniforms.h:70
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