Visual Computing Library  devel
Loading...
Searching...
No Matches
viewer_drawer_bgfx.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_DRAWERS_VIEWER_DRAWER_BGFX_H
24#define VCL_BGFX_DRAWERS_VIEWER_DRAWER_BGFX_H
25
26#include <vclib/render/drawers/abstract_viewer_drawer.h>
27
28#include <vclib/bgfx/context.h>
29#include <vclib/bgfx/drawable/uniforms/directional_light_uniforms.h>
30#include <vclib/bgfx/drawable/uniforms/mesh_render_settings_uniforms.h>
31
32namespace vcl {
33
34template<typename ViewProjEventDrawer>
35class ViewerDrawerBGFX : public AbstractViewerDrawer<ViewProjEventDrawer>
36{
38
39 DirectionalLightUniforms mDirectionalLightUniforms;
40
41 // flags
42 bool mStatsEnabled = false;
43
44public:
45 ViewerDrawerBGFX(uint width = 1024, uint height = 768) :
46 ParentViewer(width, height)
47 {
48 mDirectionalLightUniforms.updateLight(ParentViewer::light());
49 }
50
52 const std::shared_ptr<DrawableObjectVector>& v,
53 uint width = 1024,
54 uint height = 768) : ViewerDrawerBGFX(width, height)
55 {
56 ParentViewer::setDrawableObjectVector(v);
57 }
58
59 void onDrawContent(uint viewId) override
60 {
61 DrawObjectSettings settings;
62 settings.viewId = viewId;
63
64 settings.pbrMode = ParentViewer::isPBREnabled();
65
66 bgfx::setViewTransform(
67 viewId,
68 ParentViewer::viewMatrix().data(),
69 ParentViewer::projectionMatrix().data());
70
71 mDirectionalLightUniforms.updateLight(ParentViewer::light());
72 mDirectionalLightUniforms.bind();
73
74 ParentViewer::drawableObjectVector().draw(settings);
75 }
76
77 void onDrawId(uint viewId) override
78 {
79 DrawObjectSettings settings;
80 settings.objectId = ParentViewer::id();
81 settings.viewId = viewId;
82
83 bgfx::setViewTransform(
84 viewId,
85 ParentViewer::viewMatrix().data(),
86 ParentViewer::projectionMatrix().data());
87
88 ParentViewer::drawableObjectVector().drawId(settings);
89 }
90
91 void onKeyPress(Key::Enum key, const KeyModifiers& modifiers) override
92 {
93 if (key == Key::F1) {
94 if (mStatsEnabled) {
95 mStatsEnabled = false;
96 bgfx::setDebug(BGFX_DEBUG_NONE);
97 }
98 else {
99 mStatsEnabled = true;
100 bgfx::setDebug(BGFX_DEBUG_STATS);
101 }
102 }
103 ParentViewer::onKeyPress(key, modifiers);
104 }
105
106 void onMouseDoubleClick(
107 MouseButton::Enum button,
108 double x,
109 double y,
110 const KeyModifiers& modifiers) override
111 {
112 ParentViewer::onMouseDoubleClick(button, x, y, modifiers);
113
114 if (button == MouseButton::LEFT) {
115 const bool homogeneousNDC =
116 Context::instance().capabilites().homogeneousDepth;
117
118 ParentViewer::readDepthRequest(x, y, homogeneousNDC);
119 }
120 }
121};
122
123} // namespace vcl
124
125#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
A class representing a box in N-dimensional space.
Definition box.h:46
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:371
The DirectionalLightUniforms class manages the uniforms which describe a directional light that can b...
Definition directional_light_uniforms.h:49
void updateLight(const vcl::DirectionalLight< S > &light)
updateLight
Definition directional_light_uniforms.h:66
Definition viewer_drawer_bgfx.h:36