23#ifndef VCL_BGFX_PRIMITIVES_LINES_H
24#define VCL_BGFX_PRIMITIVES_LINES_H
26#include <vclib/bgfx/primitives/lines/cpu_generated_lines.h>
27#include <vclib/bgfx/primitives/lines/primitive_lines.h>
28#include <vclib/bgfx/uniform.h>
30#include <vclib/base.h>
31#include <vclib/space/core.h>
58 enum class ColorToUse {
64 enum class ImplementationType {
80 float mThickness = 5.0f;
85 bool mShadingPerVertexCapability =
false;
86 bool mShadingPerVertex =
false;
88 BitSet8 mColorCapability = {
false,
false,
true};
89 ColorToUse mColorToUse = ColorToUse::GENERAL;
90 Color mGeneralColor = Color::ColorABGR::LightGray;
92 ImplementationType mType = ImplementationType::COUNT;
94 Uniform mSettingUH =
Uniform(
"u_settings", bgfx::UniformType::Vec4);
95 std::variant<detail::PrimitiveLines, detail::CPUGeneratedLines>
111 Lines(ImplementationType type = ImplementationType::COUNT)
113 if (type == ImplementationType::COUNT)
115 setImplementationType(type);
156 ImplementationType type = ImplementationType::COUNT) :
204 ImplementationType type = ImplementationType::COUNT) :
241 ImplementationType type = ImplementationType::COUNT)
243 using enum ImplementationType;
244 using namespace detail;
248 setImplementationType(type);
252 std::get<PrimitiveLines>(mLinesImplementation)
257 std::get<CPUGeneratedLines>(mLinesImplementation)
296 ImplementationType type = ImplementationType::COUNT)
298 using enum ImplementationType;
299 using namespace detail;
303 setImplementationType(type);
307 std::get<PrimitiveLines>(mLinesImplementation)
317 std::get<CPUGeneratedLines>(mLinesImplementation)
393 if (!mShadingPerVertexCapability &&
perVertex) {
394 throw std::runtime_error(
395 "Lines::setShading(): shading per vertex not supported by the "
413 if (!mColorCapability[toUnderlying(color)]) {
414 throw std::runtime_error(
415 "Lines::setColorToUse(): color option not supported by the "
429 using enum ImplementationType;
430 using namespace detail;
432 bindSettingsUniform();
433 if (mType == PRIMITIVE)
434 std::get<PrimitiveLines>(mLinesImplementation).draw(viewId);
436 if (mType == CPU_GENERATED)
437 std::get<CPUGeneratedLines>(mLinesImplementation).draw(viewId);
451 using enum ImplementationType;
454 return CPU_GENERATED;
459 bool setImplementationType(ImplementationType type)
461 using enum ImplementationType;
470 mLinesImplementation = detail::PrimitiveLines();
475 mLinesImplementation = detail::CPUGeneratedLines();
479 default:
return false;
483 void updateShadingCapability(
const std::vector<float>& vertNormals)
485 mShadingPerVertexCapability = !vertNormals.empty();
486 if (!mShadingPerVertexCapability)
487 mShadingPerVertex =
false;
490 void updateColorCapability(
491 const std::vector<uint>& vertColors,
492 const std::vector<uint>& lineColors)
494 using enum ColorToUse;
496 mColorCapability[toUnderlying(PER_VERTEX)] = !vertColors.empty();
497 mColorCapability[toUnderlying(PER_EDGE)] = !lineColors.empty();
499 if (!mColorCapability[toUnderlying(mColorToUse)])
500 mColorToUse = GENERAL;
503 void bindSettingsUniform()
const
507 static_cast<float>(mColorToUse),
508 std::bit_cast<float>(mGeneralColor.abgr()),
509 static_cast<float>(mShadingPerVertex)};
510 mSettingUH.
bind(data);
A class representing a box in N-dimensional space.
Definition box.h:46
The Color class represents a 32 bit color.
Definition color.h:48
The Lines class provides an abstraction for rendering 3D lines with variable thickness and different ...
Definition lines.h:56
ImplementationType implementationType() const
Returns the current implementation type that is used to render the lines.
Definition lines.h:379
void setPoints(const std::vector< float > &vertCoords, const std::vector< uint > &lineIndices, const std::vector< float > &vertNormals, const std::vector< uint > &vertColors, const std::vector< uint > &lineColors, ImplementationType type=ImplementationType::COUNT)
Sets the points of the lines.
Definition lines.h:290
ColorToUse colorToUse() const
Returns the color that is used to render the lines.
Definition lines.h:355
void draw(uint viewId) const
Draws in the given view the lines with the current settings.
Definition lines.h:427
ImplementationType defaultImplementationType(bool cpuMemPointsProvided) const
Returns the default supported implementation type that can be used, depending on the capabilities of ...
Definition lines.h:448
Lines(const std::vector< float > &vertCoords, const std::vector< float > &vertNormals, const std::vector< uint > &vertColors, const std::vector< uint > &lineColors, float thickness=5.0f, bool shadingPerVertex=false, ColorToUse colorToUse=ColorToUse::GENERAL, ImplementationType type=ImplementationType::COUNT)
Creates a Lines object with given points and parameters.
Definition lines.h:148
Color & generalColor()
Returns a reference to the general color that is used to render the lines when colorToUse() is set to...
Definition lines.h:371
void setColorToUse(ColorToUse color)
Sets which color to use for rendering the lines.
Definition lines.h:411
Lines(const std::vector< float > &vertCoords, const std::vector< uint > &lineIndices, const std::vector< float > &vertNormals, const std::vector< uint > &vertColors, const std::vector< uint > &lineColors, float thickness=5.0f, bool shadingPerVertex=false, ColorToUse colorToUse=ColorToUse::GENERAL, ImplementationType type=ImplementationType::COUNT)
Creates a Lines object with given points and parameters.
Definition lines.h:195
Lines(ImplementationType type=ImplementationType::COUNT)
Creates a Lines object with default parameters and without points.
Definition lines.h:111
float thickness() const
Returns the thickness of the lines (in pixels).
Definition lines.h:335
float & thickness()
Returns a reference to the thickness of the lines (in pixels). This allows to modify the thickness di...
Definition lines.h:342
bool shadingPerVertex() const
Returns true if shading is computed per vertex using vertex normals, false if no shading is applied.
Definition lines.h:349
Color generalColor() const
Returns the general color that is used to render the lines when colorToUse() is set to ColorToUse::GE...
Definition lines.h:362
void setPoints(const std::vector< float > &vertCoords, const std::vector< float > &vertNormals, const std::vector< uint > &vertColors, const std::vector< uint > &lineColors, ImplementationType type=ImplementationType::COUNT)
Sets the points of the lines.
Definition lines.h:236
void setShading(bool perVertex)
Sets wether to use per vertex shading (using vertex normals) or not.
Definition lines.h:391