diff --git a/examples/render/common/default_viewer.h b/examples/render/common/default_viewer.h index 34c4cc784..58929602d 100644 --- a/examples/render/common/default_viewer.h +++ b/examples/render/common/default_viewer.h @@ -54,7 +54,7 @@ auto defaultViewer() #ifdef VCLIB_RENDER_EXAMPLES_WITH_QT return vcl::qt::MeshViewer(); #elif VCLIB_RENDER_EXAMPLES_WITH_GLFW - return vcl::glfw::ViewerWindow ; + return vcl::glfw::ViewerWindow; #endif } diff --git a/vclib/core/include/vclib/algorithms/core.h b/vclib/core/include/vclib/algorithms/core.h index 88527ad36..0a909f440 100644 --- a/vclib/core/include/vclib/algorithms/core.h +++ b/vclib/core/include/vclib/algorithms/core.h @@ -25,6 +25,7 @@ #include "core/bounding_box.h" #include "core/box.h" +#include "core/create.h" #include "core/distance.h" #include "core/fitting.h" #include "core/intersection.h" diff --git a/vclib/render/src/vclib/render/drawable/trackball/trackball_render_data.cpp b/vclib/core/include/vclib/algorithms/core/create.h similarity index 56% rename from vclib/render/src/vclib/render/drawable/trackball/trackball_render_data.cpp rename to vclib/core/include/vclib/algorithms/core/create.h index 6c73cad9a..8749514dd 100644 --- a/vclib/render/src/vclib/render/drawable/trackball/trackball_render_data.cpp +++ b/vclib/core/include/vclib/algorithms/core/create.h @@ -20,76 +20,68 @@ * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * ****************************************************************************/ -#include +#ifndef VCL_ALGORITHMS_CORE_CREATE_H +#define VCL_ALGORITHMS_CORE_CREATE_H -#include -#include +#include "polygon/create.h" namespace vcl { -TrackballRenderData::TrackballRenderData(uint pointsPerCircle) +/** + * @brief Returns a pair of vectors containing the vertices and edges of a 3D + * Trackball, composed of three circles in the x, y, and z planes. + * + * @tparam ScalarType: the type of scalar used for the 3D vertices. + * @tparam UintType: the type of integer used for the edge indices. + * + * @param[in] scale: the scale of the trackball. + * @param[in] pointsPerCircle: the number of points per circle. + * @return a pair of vectors containing the vertices and edges of the trackball. + */ +template +std::pair>, std::vector> +createTrackBall(ScalarType scale = 1.0, uint pointsPerCircle = 64) { - vcl::Polygon2f circle = - vcl::createCircle(pointsPerCircle, 1.0f); + using PointType = vcl::Point3; - mVertices.reserve(pointsPerCircle * 3); + std::vector vertices; + std::vector edges; + + vcl::Polygon2 circle = + vcl::createCircle>(pointsPerCircle, 1.0); + + vertices.reserve(pointsPerCircle * 3); // x uint first = 0; for (uint i = 0; i < circle.size(); ++i) { const auto& p = circle.point(i); - mVertices.push_back(vcl::Point3f(0, p.x(), p.y())); - mEdges.push_back(i + first); - mEdges.push_back((i + 1) % circle.size() + first); + vertices.push_back(PointType(0, p.x(), p.y())); + edges.push_back(i + first); + edges.push_back((i + 1) % circle.size() + first); } // y first = circle.size(); for (uint i = 0; i < circle.size(); ++i) { const auto& p = circle.point(i); - mVertices.push_back(vcl::Point3f(p.x(), 0, p.y())); - mEdges.push_back(i + first); - mEdges.push_back((i + 1) % circle.size() + first); + vertices.push_back(PointType(p.x(), 0, p.y())); + edges.push_back(i + first); + edges.push_back((i + 1) % circle.size() + first); } // z first = 2 * circle.size(); for (uint i = 0; i < circle.size(); ++i) { const auto& p = circle.point(i); - mVertices.push_back(vcl::Point3f(p.x(), p.y(), 0)); - mEdges.push_back(i + first); - mEdges.push_back((i + 1) % circle.size() + first); + vertices.push_back(PointType(p.x(), p.y(), 0)); + edges.push_back(i + first); + edges.push_back((i + 1) % circle.size() + first); } -} - -uint TrackballRenderData::vertexNumber() const -{ - return mVertices.size(); -} - -uint TrackballRenderData::edgeNumber() const -{ - return mEdges.size(); -} - -const float* TrackballRenderData::vertexBufferData() const -{ - return mVertices.front().data(); -} - -const uint16_t* TrackballRenderData::edgeBufferData() const -{ - return mEdges.data(); -} -const float* TrackballRenderData::transformData() const -{ - return mTransform.data(); -} - -void TrackballRenderData::setTransform(const Matrix44f& mtx) -{ - mTransform = mtx; + return std::make_pair(std::move(vertices), std::move(edges)); } } // namespace vcl + +#endif // VCL_ALGORITHMS_CORE_CREATE_H diff --git a/vclib/core/include/vclib/algorithms/mesh/create/axis.h b/vclib/core/include/vclib/algorithms/mesh/create/axis.h index fd5d5dc52..2420db543 100644 --- a/vclib/core/include/vclib/algorithms/mesh/create/axis.h +++ b/vclib/core/include/vclib/algorithms/mesh/create/axis.h @@ -42,8 +42,7 @@ MeshType createAxisCylinder(double unitLength, bool fromOrigin = false) const double firstSphereRadius = unitLength * 0.02; const double commonSphereRadius = unitLength * 0.008; - MeshType cylinder = - vcl::createCylinder(cylRadius, cylLength); + MeshType cylinder = vcl::createCylinder(cylRadius, cylLength); if (fromOrigin) { vcl::translate(cylinder, vcl::Point3d(0, unitLength * 0.5, 0)); @@ -66,14 +65,13 @@ MeshType createAxisConeSpheres(double unitLength, bool fromOrigin = false) const double firstSphereRadius = unitLength * 0.02; const double commonSphereRadius = unitLength * 0.008; - MeshType coneSpheres = - vcl::createCone(coneRadius, 0, coneLength); - double transl = unitLength + (coneLength * 0.5); + MeshType coneSpheres = vcl::createCone(coneRadius, 0, coneLength); + double transl = unitLength + (coneLength * 0.5); vcl::translate(coneSpheres, vcl::Point3d(0, transl, 0)); if (!fromOrigin) { vcl::Sphered s(vcl::Point3d(0, -1, 0), firstSphereRadius); - MeshType sp = vcl::createSphere(s); + MeshType sp = vcl::createSphere(s); coneSpheres.append(sp); } @@ -81,7 +79,7 @@ MeshType createAxisConeSpheres(double unitLength, bool fromOrigin = false) const double step = unitLength * 0.1; const double x = step + i * step; vcl::Sphered s(vcl::Point3d(0, x, 0), commonSphereRadius); - MeshType sp = vcl::createSphere(s); + MeshType sp = vcl::createSphere(s); coneSpheres.append(sp); if (!fromOrigin) { s.center().y() = -x; @@ -92,7 +90,7 @@ MeshType createAxisConeSpheres(double unitLength, bool fromOrigin = false) const double rad = fromOrigin ? firstSphereRadius : commonSphereRadius; vcl::Sphered s = vcl::Sphered(vcl::Point3d(0, 0, 0), rad); - MeshType sp = vcl::createSphere(s); + MeshType sp = vcl::createSphere(s); coneSpheres.append(sp); vcl::updatePerVertexNormals(coneSpheres); diff --git a/vclib/core/include/vclib/types/base.h b/vclib/core/include/vclib/types/base.h index a40bcb302..21832d8d0 100644 --- a/vclib/core/include/vclib/types/base.h +++ b/vclib/core/include/vclib/types/base.h @@ -84,15 +84,15 @@ enum class MatrixStorageType { ROW_MAJOR, COLUMN_MAJOR }; constexpr int sizeOf(PrimitiveType type) noexcept { switch (type) { - case PrimitiveType::CHAR: return sizeof(char); - case PrimitiveType::UCHAR: return sizeof(unsigned char); - case PrimitiveType::SHORT: return sizeof(int16_t); - case PrimitiveType::USHORT: return sizeof(uint16_t); - case PrimitiveType::INT: return sizeof(int32_t); - case PrimitiveType::UINT: return sizeof(uint32_t); - case PrimitiveType::FLOAT: return sizeof(float); - case PrimitiveType::DOUBLE: return sizeof(double); - default: return 0; + case PrimitiveType::CHAR: return sizeof(char); + case PrimitiveType::UCHAR: return sizeof(unsigned char); + case PrimitiveType::SHORT: return sizeof(int16_t); + case PrimitiveType::USHORT: return sizeof(uint16_t); + case PrimitiveType::INT: return sizeof(int32_t); + case PrimitiveType::UINT: return sizeof(uint32_t); + case PrimitiveType::FLOAT: return sizeof(float); + case PrimitiveType::DOUBLE: return sizeof(double); + default: return 0; } } diff --git a/vclib/render/include/vclib/bgfx/drawable/drawable_trackball.h b/vclib/render/include/vclib/bgfx/drawable/drawable_trackball.h index 93f7cac43..6e023d4eb 100644 --- a/vclib/render/include/vclib/bgfx/drawable/drawable_trackball.h +++ b/vclib/render/include/vclib/bgfx/drawable/drawable_trackball.h @@ -25,16 +25,20 @@ #include "uniforms/drawable_trackball_uniforms.h" +#include #include -#include #include #include namespace vcl { -class DrawableTrackBall : public DrawableObject, protected TrackballRenderData +class DrawableTrackBall : public DrawableObject { + inline static const uint N_POINTS = 128; + inline static const auto TRACKBALL_DATA = + createTrackBall(1.0, N_POINTS); + bool mVisible = true; bgfx::VertexBufferHandle mVertexCoordBH = BGFX_INVALID_HANDLE; @@ -48,9 +52,9 @@ class DrawableTrackBall : public DrawableObject, protected TrackballRenderData DrawableTrackballUniforms mUniforms; -public: - using TrackballRenderData::setTransform; + vcl::Matrix44f mTransform = vcl::Matrix44f::Identity(); +public: // TODO: manage copy and swap DrawableTrackBall(); @@ -58,6 +62,8 @@ class DrawableTrackBall : public DrawableObject, protected TrackballRenderData void updateDragging(bool isDragging); + void setTransform(const vcl::Matrix44f& mtx); + // DrawableObject interface void draw(uint viewId) const override; diff --git a/vclib/render/include/vclib/render/drawable/trackball/trackball_render_data.h b/vclib/render/include/vclib/render/drawable/trackball/trackball_render_data.h deleted file mode 100644 index bdcb583ce..000000000 --- a/vclib/render/include/vclib/render/drawable/trackball/trackball_render_data.h +++ /dev/null @@ -1,56 +0,0 @@ -/***************************************************************************** - * VCLib * - * Visual Computing Library * - * * - * Copyright(C) 2021-2025 * - * Visual Computing Lab * - * ISTI - Italian National Research Council * - * * - * All rights reserved. * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the Mozilla Public License Version 2.0 as published * - * by the Mozilla Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * Mozilla Public License Version 2.0 * - * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * - ****************************************************************************/ - -#ifndef VCL_RENDER_DRAWABLE_TRACKBALL_TRACKBALL_RENDER_DATA_H -#define VCL_RENDER_DRAWABLE_TRACKBALL_TRACKBALL_RENDER_DATA_H - -#include -#include - -namespace vcl { - -class TrackballRenderData -{ - std::vector mVertices; - std::vector mEdges; - - vcl::Matrix44f mTransform = vcl::Matrix44f::Identity(); - -public: - TrackballRenderData(uint pointsPerCircle = 64); - - uint vertexNumber() const; - - uint edgeNumber() const; - - const float* vertexBufferData() const; - - const uint16_t* edgeBufferData() const; - - const float* transformData() const; - - void setTransform(const vcl::Matrix44f& mtx); -}; - -} // namespace vcl - -#endif // VCL_RENDER_DRAWABLE_TRACKBALL_TRACKBALL_RENDER_DATA_H diff --git a/vclib/render/src/vclib/bgfx/drawable/drawable_trackball.cpp b/vclib/render/src/vclib/bgfx/drawable/drawable_trackball.cpp index 134b4de12..da266e949 100644 --- a/vclib/render/src/vclib/bgfx/drawable/drawable_trackball.cpp +++ b/vclib/render/src/vclib/bgfx/drawable/drawable_trackball.cpp @@ -27,9 +27,9 @@ namespace vcl { -DrawableTrackBall::DrawableTrackBall() : TrackballRenderData(128) +DrawableTrackBall::DrawableTrackBall() { - mUniforms.setNumberOfVerticesPerAxis(128); + mUniforms.setNumberOfVerticesPerAxis(N_POINTS); createBuffers(); } @@ -46,6 +46,11 @@ void DrawableTrackBall::updateDragging(bool isDragging) mUniforms.setDragging(isDragging); } +void DrawableTrackBall::setTransform(const Matrix44f& mtx) +{ + mTransform = mtx; +} + void DrawableTrackBall::draw(uint viewId) const { if (isVisible()) { @@ -58,7 +63,7 @@ void DrawableTrackBall::draw(uint viewId) const bgfx::setVertexBuffer(0, mVertexCoordBH); bgfx::setIndexBuffer(mEdgeIndexBH); - bgfx::setTransform(transformData()); + bgfx::setTransform(mTransform.data()); mUniforms.bind(); @@ -76,11 +81,14 @@ void DrawableTrackBall::createBuffers() .end(); mVertexCoordBH = bgfx::createVertexBuffer( - bgfx::makeRef(vertexBufferData(), vertexNumber() * 3 * sizeof(float)), + bgfx::makeRef( + TRACKBALL_DATA.first.data(), + TRACKBALL_DATA.first.size() * 3 * sizeof(float)), layout); - mEdgeIndexBH = bgfx::createIndexBuffer( - bgfx::makeRef(edgeBufferData(), edgeNumber() * sizeof(uint16_t))); + mEdgeIndexBH = bgfx::createIndexBuffer(bgfx::makeRef( + TRACKBALL_DATA.second.data(), + TRACKBALL_DATA.second.size() * sizeof(uint16_t))); } } // namespace vcl