Skip to content

Commit

Permalink
Merge branch 'main' into buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 25, 2025
2 parents 043b144 + 86ec990 commit cab4b16
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 129 deletions.
2 changes: 1 addition & 1 deletion examples/render/common/default_viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions vclib/core/include/vclib/algorithms/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,76 +20,68 @@
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
****************************************************************************/

#include <vclib/render/drawable/trackball/trackball_render_data.h>
#ifndef VCL_ALGORITHMS_CORE_CREATE_H
#define VCL_ALGORITHMS_CORE_CREATE_H

#include <vclib/algorithms/core/polygon/create.h>
#include <vclib/math/transform.h>
#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<typename ScalarType = float, std::integral UintType = uint16_t>
std::pair<std::vector<vcl::Point3<ScalarType>>, std::vector<UintType>>
createTrackBall(ScalarType scale = 1.0, uint pointsPerCircle = 64)
{
vcl::Polygon2f circle =
vcl::createCircle<vcl::Polygon2f>(pointsPerCircle, 1.0f);
using PointType = vcl::Point3<ScalarType>;

mVertices.reserve(pointsPerCircle * 3);
std::vector<PointType> vertices;
std::vector<UintType> edges;

vcl::Polygon2<ScalarType> circle =
vcl::createCircle<vcl::Polygon2<ScalarType>>(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
14 changes: 6 additions & 8 deletions vclib/core/include/vclib/algorithms/mesh/create/axis.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshType>(cylRadius, cylLength);
MeshType cylinder = vcl::createCylinder<MeshType>(cylRadius, cylLength);

if (fromOrigin) {
vcl::translate(cylinder, vcl::Point3d(0, unitLength * 0.5, 0));
Expand All @@ -66,22 +65,21 @@ MeshType createAxisConeSpheres(double unitLength, bool fromOrigin = false)
const double firstSphereRadius = unitLength * 0.02;
const double commonSphereRadius = unitLength * 0.008;

MeshType coneSpheres =
vcl::createCone<MeshType>(coneRadius, 0, coneLength);
double transl = unitLength + (coneLength * 0.5);
MeshType coneSpheres = vcl::createCone<MeshType>(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<MeshType>(s);
MeshType sp = vcl::createSphere<MeshType>(s);
coneSpheres.append(sp);
}

for (uint i = 0; i < 9; ++i) {
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<MeshType>(s);
MeshType sp = vcl::createSphere<MeshType>(s);
coneSpheres.append(sp);
if (!fromOrigin) {
s.center().y() = -x;
Expand All @@ -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<MeshType>(s);
MeshType sp = vcl::createSphere<MeshType>(s);
coneSpheres.append(sp);

vcl::updatePerVertexNormals(coneSpheres);
Expand Down
18 changes: 9 additions & 9 deletions vclib/core/include/vclib/types/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
14 changes: 10 additions & 4 deletions vclib/render/include/vclib/bgfx/drawable/drawable_trackball.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@

#include "uniforms/drawable_trackball_uniforms.h"

#include <vclib/algorithms/core/create.h>
#include <vclib/render/drawable/drawable_object.h>
#include <vclib/render/drawable/trackball/trackball_render_data.h>
#include <vclib/space/core/matrix.h>

#include <vclib/bgfx/context.h>

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<float, uint16_t>(1.0, N_POINTS);

bool mVisible = true;

bgfx::VertexBufferHandle mVertexCoordBH = BGFX_INVALID_HANDLE;
Expand All @@ -48,16 +52,18 @@ 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();

~DrawableTrackBall();

void updateDragging(bool isDragging);

void setTransform(const vcl::Matrix44f& mtx);

// DrawableObject interface

void draw(uint viewId) const override;
Expand Down

This file was deleted.

20 changes: 14 additions & 6 deletions vclib/render/src/vclib/bgfx/drawable/drawable_trackball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

namespace vcl {

DrawableTrackBall::DrawableTrackBall() : TrackballRenderData(128)
DrawableTrackBall::DrawableTrackBall()
{
mUniforms.setNumberOfVerticesPerAxis(128);
mUniforms.setNumberOfVerticesPerAxis(N_POINTS);

createBuffers();
}
Expand All @@ -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()) {
Expand All @@ -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();

Expand All @@ -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

0 comments on commit cab4b16

Please sign in to comment.