Skip to content

Commit 9966670

Browse files
author
devsh
committed
Merge remote-tracking branch 'remotes/origin/smooth_normal_calculation'
2 parents 61a4bd2 + ac25a95 commit 9966670

File tree

8 files changed

+581
-1804
lines changed

8 files changed

+581
-1804
lines changed

include/nbl/asset/ICPUPolygonGeometry.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class NBL_API2 ICPUPolygonGeometry final : public IPolygonGeometry<ICPUBuffer>
3131
{
3232
const auto nextDepth = _depth ? (_depth-1):0;
3333
auto retval = core::smart_refctd_ptr<ICPUPolygonGeometry>(new ICPUPolygonGeometry());
34+
retval->m_indexing = m_indexing;
3435
retval->m_positionView = m_positionView.clone(nextDepth);
3536
retval->m_jointOBBView = m_jointOBBView.clone(nextDepth);
3637
retval->m_indexView = m_indexView.clone(nextDepth);
@@ -96,6 +97,14 @@ class NBL_API2 ICPUPolygonGeometry final : public IPolygonGeometry<ICPUBuffer>
9697
}
9798
return false;
9899
}
100+
inline void* getNormalPtr()
101+
{
102+
if (isMutable())
103+
{
104+
return m_normalView.getPointer();
105+
}
106+
return nullptr;
107+
}
99108

100109
//
101110
template<typename Visitor>

include/nbl/asset/utils/CGeometryCreator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
103103
core::smart_refctd_ptr<ICPUPolygonGeometry> createCone(float radius, float length, uint16_t tesselation,
104104
float oblique=0.f, CQuantNormalCache* const quantNormalCacheOverride=nullptr) const;
105105

106+
core::smart_refctd_ptr<ICPUPolygonGeometry> createPrism(float radius, float length, uint16_t sideCount) const;
107+
106108
core::smart_refctd_ptr<ICPUPolygonGeometry> createRectangle(const hlsl::float32_t2 size={0.5f,0.5f}) const;
107109

108110
core::smart_refctd_ptr<ICPUPolygonGeometry> createDisk(const float radius, const uint32_t tesselation) const;

include/nbl/asset/utils/CPolygonGeometryManipulator.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@
99

1010
#include "nbl/asset/ICPUPolygonGeometry.h"
1111
#include "nbl/asset/utils/CGeometryManipulator.h"
12-
#include "nbl/asset/utils/CQuantNormalCache.h"
13-
#include "nbl/asset/utils/CQuantQuaternionCache.h"
1412

1513
namespace nbl::asset
1614
{
1715

1816
//! An interface for easy manipulation of polygon geometries.
19-
class CPolygonGeometryManipulator
17+
class NBL_API2 CPolygonGeometryManipulator
2018
{
2119
public:
20+
//vertex data needed for CSmoothNormalGenerator
21+
struct SSNGVertexData
22+
{
23+
uint32_t index; //offset of the vertex into index buffer
24+
uint32_t hash; //
25+
float wage; //angle wage of the vertex
26+
hlsl::float32_t3 position; //position of the vertex in 3D space
27+
hlsl::float32_t3 parentTriangleFaceNormal; //
28+
};
29+
30+
using VxCmpFunction = std::function<bool(const SSNGVertexData&, const SSNGVertexData&, const ICPUPolygonGeometry*)>;
31+
2232
static inline void recomputeContentHashes(ICPUPolygonGeometry* geo)
2333
{
2434
if (!geo)
@@ -230,6 +240,16 @@ class CPolygonGeometryManipulator
230240
EEM_QUATERNION,
231241
EEM_COUNT
232242
};
243+
244+
static core::smart_refctd_ptr<ICPUPolygonGeometry> createUnweldedList(const ICPUPolygonGeometry* inGeo);
245+
246+
static core::smart_refctd_ptr<ICPUPolygonGeometry> createSmoothVertexNormal(const ICPUPolygonGeometry* inbuffer, bool enableWelding = false, float epsilon = 1.525e-5f,
247+
VxCmpFunction vxcmp = [](const CPolygonGeometryManipulator::SSNGVertexData& v0, const CPolygonGeometryManipulator::SSNGVertexData& v1, const ICPUPolygonGeometry* buffer)
248+
{
249+
static constexpr float cosOf45Deg = 0.70710678118f;
250+
return dot(v0.parentTriangleFaceNormal,v1.parentTriangleFaceNormal) > cosOf45Deg;
251+
});
252+
233253
#if 0 // TODO: REDO
234254
//! Struct used to pass chosen comparison method and epsilon to functions performing error metrics.
235255
/**

src/nbl/asset/utils/CGeometryCreator.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,72 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
662662
return retval;
663663
}
664664

665+
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createPrism(
666+
float radius, float length, uint16_t sideCount) const
667+
{
668+
using namespace hlsl;
669+
670+
if (sideCount < 3) return nullptr;
671+
672+
const auto halfIx = sideCount;
673+
const uint32_t u32_vertexCount = 2 * sideCount;
674+
if (u32_vertexCount > std::numeric_limits<uint16_t>::max())
675+
return nullptr;
676+
const auto vertexCount = static_cast<uint16_t>(u32_vertexCount);
677+
678+
auto retval = core::make_smart_refctd_ptr<ICPUPolygonGeometry>();
679+
retval->setIndexing(IPolygonGeometryBase::TriangleList());
680+
681+
// Create indices
682+
using index_t = uint16_t;
683+
{
684+
constexpr uint32_t RowCount = 2u;
685+
const auto IndexCount = RowCount * 3 * sideCount;
686+
auto indexView = createIndexView<index_t>(IndexCount, vertexCount - 1);
687+
auto u = reinterpret_cast<index_t*>(indexView.src.buffer->getPointer());
688+
689+
for (uint16_t i = 0u, j = 0u; i < halfIx; ++i)
690+
{
691+
u[j++] = i;
692+
u[j++] = (i + 1u) != halfIx ? (i + 1u):0u;
693+
u[j++] = i + halfIx;
694+
u[j++] = i + halfIx;
695+
u[j++] = (i + 1u)!= halfIx ? (i + 1u):0u;
696+
u[j++] = (i + 1u)!= halfIx ? (i + 1u + halfIx) : halfIx;
697+
}
698+
699+
retval->setIndexView(std::move(indexView));
700+
}
701+
702+
// Create vertex attributes with NONE usage because we have no clue how they'll be used
703+
hlsl::float32_t3* positions;
704+
705+
{
706+
shapes::AABB<4, float32_t> aabb;
707+
aabb.maxVx = float32_t4(radius, radius, length, 0.0f);
708+
aabb.minVx = float32_t4(-radius, -radius, 0.0f, 0.0f);
709+
auto positionView = createPositionView(vertexCount, aabb);
710+
positions = reinterpret_cast<decltype(positions)>(positionView.src.buffer->getPointer());
711+
retval->setPositionView(std::move(positionView));
712+
}
713+
714+
const float invSideCount = 1.f / static_cast<float>(sideCount);
715+
const float step = 2.f * numbers::pi<float32_t> * invSideCount;
716+
for (uint32_t i = 0u; i < sideCount; ++i)
717+
{
718+
const auto f_i = static_cast<float>(i);
719+
hlsl::float32_t3 p(std::cos(f_i * step), std::sin(f_i * step), 0.f);
720+
p *= radius;
721+
722+
positions[i] = { p.x, p.y, p.z };
723+
724+
positions[i + halfIx] = { p.x, p.y, length };
725+
}
726+
727+
CPolygonGeometryManipulator::recomputeContentHashes(retval.get());
728+
return retval;
729+
}
730+
665731
core::smart_refctd_ptr<ICPUGeometryCollection> CGeometryCreator::createArrow(
666732
const uint16_t tesselationCylinder,
667733
const uint16_t tesselationCone,

0 commit comments

Comments
 (0)