Skip to content

Commit 8d0c237

Browse files
committed
add createGrid to geometry creator
1 parent e05478d commit 8d0c237

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

include/nbl/asset/utils/CGeometryCreator.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,15 @@ class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
115115
\param subdivision Specifies subdivision level of the icosphere.
116116
\param smooth Specifies whether vertecies should be built for smooth or flat shading.
117117
*/
118-
119118
core::smart_refctd_ptr<ICPUPolygonGeometry> createIcoSphere(float radius=1.f, uint32_t subdivision=1, bool smooth=false) const;
120119

120+
//! Create a grid geometry
121+
/**
122+
No vertex buffer, only index in triangle strip topology without reset, "snake" with degenerates
123+
\param "resolution" Specifies resolution of grid
124+
*/
125+
core::smart_refctd_ptr<ICPUPolygonGeometry> createGrid(const hlsl::uint16_t2 resolution = { 128u, 128u }) const;
126+
121127
private:
122128
SCreationParams m_params;
123129
};

src/nbl/asset/utils/CGeometryCreator.cpp

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,15 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createSphere(float
498498
memcpy(normals + vertex_i, &quantizedBottomNormal, sizeof(quantizedBottomNormal));
499499
}
500500

501+
for (auto i = 0u; i < vertexCount; ++i)
502+
{
503+
auto position = positions[i];
504+
auto len = glm::length(position);
505+
506+
auto ok = len >= 1.f - 0.01f;
507+
assert(ok);
508+
}
509+
501510
CPolygonGeometryManipulator::recomputeContentHashes(retval.get());
502511
return retval;
503512
}
@@ -1889,5 +1898,93 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createIcoSphere(fl
18891898
return retval;
18901899
}
18911900

1901+
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createGrid(const hlsl::uint16_t2 resolution) const
1902+
{
1903+
if (resolution.x < 2 || resolution.y < 2)
1904+
return nullptr;
1905+
1906+
auto retval = core::make_smart_refctd_ptr<ICPUPolygonGeometry>();
1907+
retval->setIndexing(IPolygonGeometryBase::TriangleStrip());
1908+
1909+
//! Create indices
1910+
/*
1911+
i \in [0, resolution.x - 1], j \in [0, resolution.y - 1]
1912+
logical vertex id : V(i, j)
1913+
1914+
Eg. resolution = {5u, 4u}:
1915+
1916+
j=3 15--16--17--18--19
1917+
| \ | \ | \ | \ |
1918+
j=2 10--11--12--13--14
1919+
| \ | \ | \ | \ |
1920+
j=1 5-- 6-- 7-- 8-- 9
1921+
| \ | \ | \ | \ |
1922+
j=0 0-- 1-- 2-- 3-- 4
1923+
i=0 1 2 3 4
1924+
1925+
Strip order (one draw), rows linked by 2 degenerate indices:
1926+
row 0 -> 1 (L->R): 0,5, 1,6, 2,7, 3,8, 4,9, 9,9
1927+
row 1 -> 2 (R->L): 9,14, 8,13, 7,12, 6,11, 5,10, 5,5
1928+
row 2 -> 3 (L->R): 5,10, 6,11, 7,12, 8,13, 9,14, 14,14
1929+
*/
1930+
1931+
const size_t indexCount = 2ull * resolution.x * (resolution.y - 1) + 2ull * (resolution.y - 2);
1932+
const size_t maxIndex = resolution.x * resolution.y - 1u;
1933+
1934+
auto createIndices = [&]<typename IndexT>() -> void
1935+
{
1936+
auto indexView = createIndexView<IndexT>(indexCount, maxIndex);
1937+
1938+
auto V = [&](IndexT i, IndexT j) { return IndexT(j * resolution.x + i); };
1939+
auto* index = static_cast<IndexT*>(indexView.src.buffer->getPointer());
1940+
#define PUSH_INDEX(value) *index = value; ++index;
1941+
1942+
for (IndexT j = 0u; j < resolution.y - 1; ++j)
1943+
{
1944+
if ((j & 1u) == 0)
1945+
{
1946+
for (IndexT i = 0u; i < resolution.x; ++i)
1947+
{
1948+
PUSH_INDEX(V(i, j))
1949+
PUSH_INDEX(V(i, j + 1))
1950+
}
1951+
1952+
if (j + 1 < resolution.y - 1)
1953+
{
1954+
IndexT last = V(resolution.x - 1, j + 1);
1955+
PUSH_INDEX(last)
1956+
PUSH_INDEX(last)
1957+
}
1958+
}
1959+
else
1960+
{
1961+
for (int i = int(resolution.x) - 1; i >= 0; --i)
1962+
{
1963+
PUSH_INDEX(V(uint32_t(i), j))
1964+
PUSH_INDEX(V(uint32_t(i), j + 1))
1965+
}
1966+
1967+
if (j + 1 < resolution.y - 1)
1968+
{
1969+
IndexT first = V(0, j + 1);
1970+
PUSH_INDEX(first)
1971+
PUSH_INDEX(first)
1972+
}
1973+
}
1974+
}
1975+
retval->setIndexView(std::move(indexView));
1976+
};
1977+
1978+
if (maxIndex <= std::numeric_limits<uint16_t>::max())
1979+
createIndices.template operator() < uint16_t > ();
1980+
else if (maxIndex <= std::numeric_limits<uint32_t>::max())
1981+
createIndices.template operator() < uint32_t > ();
1982+
else
1983+
return nullptr;
1984+
1985+
CPolygonGeometryManipulator::recomputeContentHashes(retval.get());
1986+
return retval;
1987+
}
1988+
18921989
} // end namespace nbl::asset
18931990

0 commit comments

Comments
 (0)