Visual Computing Library
Loading...
Searching...
No Matches
viewer_drawer.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_H
24#define VCL_BGFX_DRAWERS_VIEWER_DRAWER_H
25
26#include <vclib/render/drawers/abstract_viewer_drawer.h>
27
28#include <vclib/bgfx/drawable/drawable_axis.h>
29#include <vclib/bgfx/drawable/drawable_directional_light.h>
30#include <vclib/bgfx/drawable/drawable_trackball.h>
31#include <vclib/bgfx/drawable/uniforms/camera_uniforms.h>
32#include <vclib/bgfx/drawable/uniforms/directional_light_uniforms.h>
33#include <vclib/bgfx/drawable/uniforms/mesh_render_settings_uniforms.h>
34
35namespace vcl {
36
37template<typename DerivedRenderApp>
38class ViewerDrawerBGFX : public AbstractViewerDrawer<DerivedRenderApp>
39{
41 using DTB = ParentViewer::DTB;
42
43 CameraUniforms mCameraUniforms;
44 DirectionalLightUniforms mDirectionalLightUniforms;
45
46 DrawableAxis mAxis;
47 DrawableDirectionalLight mDirectionalLight;
48 DrawableTrackBall mDrawTrackBall;
49
50 // flags
51 bool mStatsEnabled = false;
52
53public:
54 ViewerDrawerBGFX(uint width = 1024, uint height = 768) :
55 ParentViewer(width, height)
56 {
57 mCameraUniforms.updateCamera(DTB::camera());
58 mDirectionalLightUniforms.updateLight(DTB::light());
59 }
60
62 const std::shared_ptr<DrawableObjectVector>& v,
63 uint width = 1024,
64 uint height = 768) : ViewerDrawerBGFX(width, height)
65 {
66 ParentViewer::setDrawableObjectVector(v);
67 }
68
69 void onInit(uint viewId) override
70 {
71 ParentViewer::onInit(viewId);
72 mAxis.init();
73 mDirectionalLight.init();
74 mDrawTrackBall.init();
75 }
76
77 void onDraw(uint viewId) override
78 {
79 onDrawContent(viewId);
80
81 if (mAxis.isVisible()) {
82 mAxis.draw(viewId);
83 }
84
85 if (mDirectionalLight.isVisible()) {
86 mDirectionalLight.draw(viewId);
87 }
88
89 if (mDrawTrackBall.isVisible()) {
90 mDrawTrackBall.draw(viewId);
91 }
92 }
93
94 void onDrawContent(uint viewId) override
95 {
96 setDirectionalLightVisibility(
97 DTB::currentMotion() == DTB::TrackBallType::DIR_LIGHT_ARC);
98 updateDirectionalLight();
99 updateDrawableTrackball();
100
101 bgfx::setViewTransform(
102 viewId, DTB::viewMatrix().data(), DTB::projectionMatrix().data());
103
104 mCameraUniforms.updateCamera(DTB::camera());
105 mCameraUniforms.bind();
106
107 mDirectionalLightUniforms.bind();
108
109 ParentViewer::drawableObjectVector().draw(viewId);
110 }
111
112 void onKeyPress(Key::Enum key, const KeyModifiers& modifiers) override
113 {
114 if (key == Key::F1) {
115 if (mStatsEnabled) {
116 mStatsEnabled = false;
117 bgfx::setDebug(BGFX_DEBUG_NONE);
118 }
119 else {
120 mStatsEnabled = true;
121 bgfx::setDebug(BGFX_DEBUG_STATS);
122 }
123 }
124 ParentViewer::onKeyPress(key, modifiers);
125 }
126
127 void onMouseDoubleClick(
128 MouseButton::Enum button,
129 double x,
130 double y,
131 const KeyModifiers& modifiers) override
132 {
133 const bool homogeneousNDC =
134 Context::instance().capabilites().homogeneousDepth;
135
136 ParentViewer::readRequest(button, x, y, modifiers, homogeneousNDC);
137 }
138
139 void toggleAxisVisibility() override
140 {
141 mAxis.setVisibility(!mAxis.isVisible());
142 }
143
144 void toggleTrackBallVisibility() override
145 {
146 mDrawTrackBall.setVisibility(!mDrawTrackBall.isVisible());
147 }
148
149private:
150 bool isDirectionalLightVisible() const
151 {
152 return mDirectionalLight.isVisible();
153 }
154
155 void setDirectionalLightVisibility(bool b)
156 {
157 mDirectionalLight.setVisibility(b);
158 }
159
160 void updateDirectionalLight()
161 {
162 auto v = DTB::lightGizmoMatrix();
163 mDirectionalLight.updateRotation(v);
164 mDirectionalLightUniforms.updateLight(DTB::light());
165 }
166
167 void updateDrawableTrackball()
168 {
169 auto v = DTB::gizmoMatrix();
170 mDrawTrackBall.setTransform(v);
171 mDrawTrackBall.updateDragging(DTB::isDragging());
172 }
173};
174
175} // namespace vcl
176
177#endif // VCL_BGFX_DRAWERS_VIEWER_DRAWER_H
The AbstractViewerDrawer class is a base class for all viewer drawers implementations.
Definition abstract_viewer_drawer.h:49
Definition camera_uniforms.h:34
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:365
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 drawable_axis.h:41
void setVisibility(bool vis) override
This member function is used to set the visibility of the object.
Definition drawable_axis.h:124
void draw(uint viewId) const override
This member function must draw the object. It will be called at every frame.
Definition drawable_axis.cpp:41
bool isVisible() const override
This member function is used to check if the object is visible.
Definition drawable_axis.h:122
Definition drawable_directional_light.h:40
void setVisibility(bool vis) override
This member function is used to set the visibility of the object.
Definition drawable_directional_light.h:95
bool isVisible() const override
This member function is used to check if the object is visible.
Definition drawable_directional_light.h:93
void draw(uint viewId) const override
This member function must draw the object. It will be called at every frame.
Definition drawable_directional_light.cpp:111
virtual void init()
This member function is called after the initialization of the Context. It must initialize and bind d...
Definition drawable_object.h:71
Definition drawable_trackball.h:39
bool isVisible() const override
This member function is used to check if the object is visible.
Definition drawable_trackball.h:153
void setVisibility(bool vis) override
This member function is used to set the visibility of the object.
Definition drawable_trackball.h:155
void updateDragging(bool isDragging)
Update the dragging status of the trackball.
Definition drawable_trackball.h:107
void draw(uint viewId) const override
This member function must draw the object. It will be called at every frame.
Definition drawable_trackball.h:120
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
Definition viewer_drawer.h:39