Visual Computing Library
Loading...
Searching...
No Matches
drawable_trackball.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_DRAWABLE_DRAWABLE_TRACKBALL_H
24#define VCL_BGFX_DRAWABLE_DRAWABLE_TRACKBALL_H
25
26#include "uniforms/drawable_trackball_uniforms.h"
27
28#include <vclib/algorithms/core/create.h>
29#include <vclib/bgfx/buffers.h>
30#include <vclib/math/transform.h>
31#include <vclib/render/drawable/drawable_object.h>
32#include <vclib/space/core/matrix.h>
33
34#include <vclib/bgfx/context.h>
35
36namespace vcl {
37
39{
40 inline static const uint N_POINTS = 128;
41 inline static const auto TRACKBALL_DATA =
43
44 bool mVisible = true;
45
46 VertexBuffer mVertexCoordsBuffer;
47 IndexBuffer mEdgeIndexBuffer;
48
49 // TODO: can we be sure that this is called after the context initialization
50 // triggered by a window?
51 bgfx::ProgramHandle mProgram =
53 .programManager()
54 .getProgram<VertFragProgram::DRAWABLE_TRACKBALL>();
55
57
58 vcl::Matrix44f mTransform = vcl::Matrix44f::Identity();
59
60public:
62 {
63 mUniforms.setNumberOfVerticesPerAxis(N_POINTS);
64
65 createBuffers();
66 }
67
69 mVisible(other.mVisible), mProgram(other.mProgram),
70 mUniforms(other.mUniforms), mTransform(other.mTransform)
71 {
72 // copy all the members that can be copied, and then re-create the
73 // buffers
74 createBuffers();
75 }
76
77 // default move constructor - buffers can be moved
79
80 // default destructor - buffers are destroyed by their destructor
81 ~DrawableTrackBall() = default;
82
90 {
91 using std::swap;
92 swap(mVisible, other.mVisible);
93 swap(mVertexCoordsBuffer, other.mVertexCoordsBuffer);
94 swap(mEdgeIndexBuffer, other.mEdgeIndexBuffer);
95 swap(mProgram, other.mProgram);
96 swap(mUniforms, other.mUniforms);
97 swap(mTransform, other.mTransform);
98 }
99
100 friend void swap(DrawableTrackBall& a, DrawableTrackBall& b) { a.swap(b); }
101
107 void updateDragging(bool isDragging) { mUniforms.setDragging(isDragging); }
108
109 void setTransform(const vcl::Matrix44f& mtx) { mTransform = mtx; }
110
111 // copy and swap idiom
112 DrawableTrackBall& operator=(DrawableTrackBall other)
113 {
114 swap(other);
115 return *this;
116 }
117
118 // DrawableObject interface
119
120 void draw(uint viewId) const override
121 {
122 if (isVisible()) {
123 if (bgfx::isValid(mProgram)) {
124 bgfx::setState(
128
129 mVertexCoordsBuffer.bind(0);
130 mEdgeIndexBuffer.bind();
131
132 bgfx::setTransform(mTransform.data());
133
134 mUniforms.bind();
135
136 bgfx::submit(viewId, mProgram);
137 }
138 }
139 }
140
141 Box3d boundingBox() const override { return Box3d(); }
142
143 std::shared_ptr<DrawableObject> clone() const& override
144 {
145 return std::make_shared<DrawableTrackBall>(*this);
146 }
147
148 std::shared_ptr<DrawableObject> clone() && override
149 {
150 return std::make_shared<DrawableTrackBall>(std::move(*this));
151 }
152
153 bool isVisible() const override { return mVisible; }
154
155 void setVisibility(bool vis) override { mVisible = vis; }
156
157private:
158 void createBuffers()
159 {
160 // vertex buffer
161 mVertexCoordsBuffer.create(
162 TRACKBALL_DATA.first.data(),
163 TRACKBALL_DATA.first.size(),
164 bgfx::Attrib::Position,
165 3,
166 PrimitiveType::FLOAT);
167
168 // edge index buffer
169 mEdgeIndexBuffer.create(
170 TRACKBALL_DATA.second.data(), TRACKBALL_DATA.second.size(), false);
171 }
172};
173
174} // namespace vcl
175
176#endif // VCL_BGFX_DRAWABLE_DRAWABLE_TRACKBALL_H
static Context & instance(void *windowHandle=nullptr, void *displayHandle=nullptr)
Return the context instance.
Definition context.cpp:365
The DrawableObject class is the base class for all the objects that can be drawn in a 3D viewer.
Definition drawable_object.h:55
Definition drawable_trackball.h:39
std::shared_ptr< DrawableObject > clone() const &override
This member function is used to create a new copy of the DrawableObject. Each derived class must impl...
Definition drawable_trackball.h:143
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
std::shared_ptr< DrawableObject > clone() &&override
This member function is used to create a new DrawableObject that is a moved from the current one....
Definition drawable_trackball.h:148
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
Box3d boundingBox() const override
This member function is used to find a good camera position to render object. It should return the th...
Definition drawable_trackball.h:141
void swap(DrawableTrackBall &other)
Swap the content of this object with another DrawableTrackBall object.
Definition drawable_trackball.h:89
Definition drawable_trackball_uniforms.h:32
The IndexBuffer manages the lifetime of a bgfx::IndexBufferHandle.
Definition index_buffer.h:43
void create(const void *bufferIndices, const uint bufferSize, bool is32Bit=true, bgfx::ReleaseFn releaseFn=nullptr)
Creates the index buffer and sets the data for rendering.
Definition index_buffer.h:103
void bind(uint stage=UINT_NULL, bgfx::Access::Enum access=bgfx::Access::Read) const
Bind the index buffer to the rendering pipeline.
Definition index_buffer.h:193
Definition matrix.h:36
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43
The VertexBuffer manages the lifetime of a bgfx::VertexBufferHandle.
Definition vertex_buffer.h:43
void create(const void *bufferData, uint vertNum, bgfx::Attrib::Enum attrib, uint attribNumPerVertex, PrimitiveType attribType, bool normalize=false, bgfx::ReleaseFn releaseFn=nullptr)
Creates the vertex buffer and sets the data for rendering.
Definition vertex_buffer.h:106
void bind(uint stream, bgfx::Access::Enum access=bgfx::Access::Read) const
Bind the vertex buffer to the rendering pipeline.
Definition vertex_buffer.h:231