Visual Computing Library  devel
Loading...
Searching...
No Matches
viewer_drawer_bgfx.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_DRAWERS_VIEWER_DRAWER_BGFX_H
24#define VCL_BGFX_DRAWERS_VIEWER_DRAWER_BGFX_H
25
26#include "uniforms/viewer_drawer_uniforms.h"
27
28#include <vclib/render/drawers/abstract_viewer_drawer.h>
29
30#include <vclib/bgfx/context.h>
31#include <vclib/bgfx/drawable/drawable_environment.h>
32#include <vclib/bgfx/drawable/uniforms/directional_light_uniforms.h>
33
34namespace vcl {
35
36template<typename ViewProjEventDrawer>
37class ViewerDrawerBGFX : public AbstractViewerDrawer<ViewProjEventDrawer>
38{
40
41 // flags
42 bool mStatsEnabled = false;
43
44 PBRViewerSettings mPBRSettings;
45
47
48public:
49 ViewerDrawerBGFX(uint width = 1024, uint height = 768) :
50 ParentViewer(width, height)
51 {
52 }
53
55 const std::shared_ptr<DrawableObjectVector>& v,
56 uint width = 1024,
57 uint height = 768) : ViewerDrawerBGFX(width, height)
58 {
59 ParentViewer::setDrawableObjectVector(v);
60 }
61
62 const PBRViewerSettings& pbrSettings() const { return mPBRSettings; }
63
64 void setPbrSettings(const PBRViewerSettings& settings)
65 {
66 mPBRSettings = settings;
67 }
68
69 std::string panoramaFileName() const { return mPanorama.imageFileName(); }
70
71 void setPanorama(const std::string& panorama)
72 {
73 mPanorama = DrawableEnvironment(panorama, ParentViewer::canvasViewId());
74 }
75
76 void onDrawContent(uint viewId) override
77 {
78 DrawObjectSettings settings;
79 settings.viewId = viewId;
80
81 settings.pbrSettings = mPBRSettings;
82
83 settings.environment = &mPanorama;
84
85 setViewTransform(viewId);
86
87 DirectionalLightUniforms::setLight(ParentViewer::light());
88 DirectionalLightUniforms::bind();
89
90 ViewerDrawerUniforms::setExposure(mPBRSettings.exposure);
91 ViewerDrawerUniforms::setToneMapping(mPBRSettings.toneMapping);
92 ViewerDrawerUniforms::setSpecularMipsLevels(
93 mPanorama.specularMipLevels());
94 ViewerDrawerUniforms::bind();
95
96 // background will be drawn only if settings allow it
97 mPanorama.drawBackground(settings.viewId, settings.pbrSettings);
98
99 ParentViewer::drawableObjectVector().draw(settings);
100 }
101
102 void onDrawId(uint viewId) override
103 {
104 DrawObjectSettings settings;
105 settings.objectId = ParentViewer::id();
106 settings.viewId = viewId;
107
108 setViewTransform(viewId);
109
110 ParentViewer::drawableObjectVector().drawId(settings);
111 }
112
113 void onKeyPress(Key::Enum key, const KeyModifiers& modifiers) override
114 {
115 if (key == Key::F1) {
116 if (mStatsEnabled) {
117 mStatsEnabled = false;
118 bgfx::setDebug(BGFX_DEBUG_NONE);
119 }
120 else {
121 mStatsEnabled = true;
122 bgfx::setDebug(BGFX_DEBUG_STATS);
123 }
124 }
125 ParentViewer::onKeyPress(key, modifiers);
126 }
127
128 void onMouseDoubleClick(
129 MouseButton::Enum button,
130 double x,
131 double y,
132 const KeyModifiers& modifiers) override
133 {
134 ParentViewer::onMouseDoubleClick(button, x, y, modifiers);
135
136 if (button == MouseButton::LEFT) {
137 const bool homogeneousNDC =
138 Context::instance().capabilites().homogeneousDepth;
139
140 ParentViewer::readDepthRequest(x, y, homogeneousNDC);
141 }
142 }
143
144private:
145 void setViewTransform(uint viewId)
146 {
147 // need to store the matrices
148 // parent viewer returns by value
149 Matrix44f vm = ParentViewer::viewMatrix();
150 Matrix44f pm = ParentViewer::projectionMatrix();
151
152 bgfx::setViewTransform(viewId, vm.data(), pm.data());
153 }
154};
155
156} // namespace vcl
157
158#endif // VCL_BGFX_DRAWERS_VIEWER_DRAWER_BGFX_H
The AbstractViewerDrawer class is a base class for all viewer drawers implementations.
Definition abstract_viewer_drawer.h:46
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:371
static void setLight(const vcl::DirectionalLight< S > &light)
setLight
Definition directional_light_uniforms.h:70
A class representing an environment for PBR rendering.
Definition drawable_environment.h:46
void drawBackground(uint viewId, const PBRViewerSettings &settings) const
Draws the environment in the background.
Definition drawable_environment.cpp:65
Definition matrix.h:34
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:41
Definition viewer_drawer_bgfx.h:38
Definition pbr_viewer_settings.h:31
float exposure
The exposure value to use in PBR mode.
Definition pbr_viewer_settings.h:75
ToneMapping toneMapping
The tone mapping operator to use in PBR mode.
Definition pbr_viewer_settings.h:80