Skip to content

Commit 00a8777

Browse files
Geometry Primitives: replaced CreateCubeGeometry with generic CreateGeometryPrimitive function
1 parent 8320517 commit 00a8777

File tree

2 files changed

+128
-38
lines changed

2 files changed

+128
-38
lines changed

Common/interface/GeometryPrimitives.h

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131

3232
DILIGENT_BEGIN_NAMESPACE(Diligent)
3333

34+
#include "../../Primitives/interface/DefineRefMacro.h"
35+
3436
/// Geometry primitive vertex flags.
3537
// clang-format off
3638
DILIGENT_TYPED_ENUM(GEOMETRY_PRIMITIVE_VERTEX_FLAGS, Uint32)
@@ -60,37 +62,105 @@ DILIGENT_TYPED_ENUM(GEOMETRY_PRIMITIVE_VERTEX_FLAGS, Uint32)
6062
GEOMETRY_PRIMITIVE_VERTEX_FLAG_TEXCOORD,
6163
};
6264
// clang-format on
63-
6465
DEFINE_FLAG_ENUM_OPERATORS(GEOMETRY_PRIMITIVE_VERTEX_FLAGS)
6566

67+
/// Geometry primitive types.
68+
// clang-format off
69+
DILIGENT_TYPED_ENUM(GEOMETRY_PRIMITIVE_TYPE, Uint32)
70+
{
71+
/// Geometry primitive type is undefined.
72+
GEOMETRY_PRIMITIVE_TYPE_UNDEFINED = 0u,
73+
74+
/// Cube geometry primitive type.
75+
GEOMETRY_PRIMITIVE_TYPE_CUBE = 1u,
76+
77+
GEOMETRY_PRIMITIVE_TYPE_LAST = GEOMETRY_PRIMITIVE_TYPE_CUBE
78+
};
79+
// clang-format on
80+
81+
/// Geometry primitive attributes.
82+
struct GeometryPrimitiveAttributes
83+
{
84+
/// The geometry primitive type, see Diligent::GEOMETRY_PRIMITIVE_TYPE.
85+
GEOMETRY_PRIMITIVE_TYPE Type DEFAULT_INITIALIZER(GEOMETRY_PRIMITIVE_TYPE_UNDEFINED);
86+
87+
/// Vertex flags that specify which vertex components to include in the output vertices,
88+
/// see Diligent::GEOMETRY_PRIMITIVE_VERTEX_FLAGS.
89+
GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags DEFAULT_INITIALIZER(GEOMETRY_PRIMITIVE_VERTEX_FLAG_ALL);
90+
91+
/// The number of subdivisions.
92+
///
93+
/// \remarks This parameter defines the fidelity of the geometry primitive.
94+
/// For example, for a cube geometry primitive, the cube faces are subdivided
95+
/// into Subdivision x Subdivision quads, producing (Subdivision + 1)^2 vertices
96+
/// per face.
97+
Uint32 NumSubdivisions DEFAULT_INITIALIZER(0);
98+
99+
#if DILIGENT_CPP_INTERFACE
100+
GeometryPrimitiveAttributes() noexcept = default;
101+
102+
explicit GeometryPrimitiveAttributes(GEOMETRY_PRIMITIVE_TYPE _Type,
103+
GEOMETRY_PRIMITIVE_VERTEX_FLAGS _VertexFlags = GEOMETRY_PRIMITIVE_VERTEX_FLAG_ALL,
104+
Uint32 _NumSubdivision = 1) noexcept :
105+
Type{_Type},
106+
VertexFlags{_VertexFlags},
107+
NumSubdivisions{_NumSubdivision}
108+
{}
109+
#endif
110+
};
111+
typedef struct GeometryPrimitiveAttributes GeometryPrimitiveAttributes;
112+
113+
/// Cube geometry primitive attributes.
114+
// clang-format off
115+
struct CubeGeometryPrimitiveAttributes DILIGENT_DERIVE(GeometryPrimitiveAttributes)
116+
117+
/// The size of the cube.
118+
/// The cube is centered at (0, 0, 0) and has the size of Size x Size x Size.
119+
/// If the cube size is 1, the coordinates of the cube vertices are in the range [-0.5, 0.5].
120+
float Size DEFAULT_INITIALIZER(1.f);
121+
122+
#if DILIGENT_CPP_INTERFACE
123+
explicit CubeGeometryPrimitiveAttributes(float _Size = 1,
124+
GEOMETRY_PRIMITIVE_VERTEX_FLAGS _VertexFlags = GEOMETRY_PRIMITIVE_VERTEX_FLAG_ALL,
125+
Uint32 _NumSubdivision = 1) noexcept :
126+
GeometryPrimitiveAttributes{GEOMETRY_PRIMITIVE_TYPE_CUBE, _VertexFlags, _NumSubdivision},
127+
Size{_Size}
128+
{}
129+
#endif
130+
};
131+
// clang-format on
132+
133+
/// Geometry primitive info.
134+
struct GeometryPrimitiveInfo
135+
{
136+
/// The number of vertices.
137+
Uint32 NumVertices DEFAULT_INITIALIZER(0);
138+
139+
/// The number of indices.
140+
Uint32 NumIndices DEFAULT_INITIALIZER(0);
141+
142+
/// The size of the vertex in bytes.
143+
Uint32 VertexSize DEFAULT_INITIALIZER(0);
144+
};
145+
66146
/// Returns the size of the geometry primitive vertex in bytes.
67147
Uint32 GetGeometryPrimitiveVertexSize(GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags);
68148

69-
/// Creates a cube geometry.
149+
/// Creates a geometry primitive
70150
///
71-
/// \param [in] Size The size of the cube.
72-
/// The cube is centered at (0, 0, 0) and has the size of Size x Size x Size.
73-
/// If the cube size is 1, the coordinates of the cube vertices are in the range [-0.5, 0.5].
74-
/// \param [in] NumSubdivisions The number of subdivisions.
75-
/// The cube faces are subdivided into Subdivision x Subdivision quads.
76-
/// \param [in] VertexFlags Flags that specify which vertex components to include in the output vertices.
151+
/// \param [in] Attribs Geometry primitive attributes, see Diligent::GeometryPrimitiveAttributes.
77152
/// \param [out] ppVertices Address of the memory location where the pointer to the output vertex data blob will be stored.
78153
/// The vertex components are stored as interleaved floating-point values.
79154
/// For example, if VertexFlags = GEOMETRY_PRIMITIVE_VERTEX_FLAG_POS_NORM, the vertex data will
80155
/// be stored as follows:
81156
/// P0, N0, P1, N1, ..., Pn, Nn.
82157
/// \param [out] ppIndices Address of the memory location where the pointer to the output index data blob will be stored.
83158
/// Index data is stored as 32-bit unsigned integers representing the triangle list.
84-
/// \param [out] pNumVertices Address of the memory location where the number of vertices will be stored.
85-
/// This parameter can be null.
86-
/// \param [out] pNumIndices Address of the memory location where the number of indices will be stored.
87-
/// This parameter can be null.
88-
void DILIGENT_GLOBAL_FUNCTION(CreateCubeGeometry)(float Size,
89-
Uint32 NumSubdivisions,
90-
GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags,
91-
IDataBlob** ppVertices,
92-
IDataBlob** ppIndices,
93-
Uint32* pNumVertices DEFAULT_VALUE(nullptr),
94-
Uint32* pNumIndices DEFAULT_VALUE(nullptr));
159+
/// \param [out] pInfo Address of the memory location where the pointer to the output geometry primitive info will be stored.
160+
void DILIGENT_GLOBAL_FUNCTION(CreateGeometryPrimitive)(const GeometryPrimitiveAttributes REF Attribs,
161+
IDataBlob** ppVertices,
162+
IDataBlob** ppIndices,
163+
GeometryPrimitiveInfo* pInfo DEFAULT_VALUE(nullptr));
164+
#include "../../Primitives/interface/UndefRefMacro.h"
95165

96166
DILIGENT_END_NAMESPACE // namespace Diligent

Common/src/GeometryPrimitives.cpp

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ Uint32 GetGeometryPrimitiveVertexSize(GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlag
4242
((VertexFlags & GEOMETRY_PRIMITIVE_VERTEX_FLAG_TEXCOORD) ? sizeof(float2) : 0));
4343
}
4444

45-
void CreateCubeGeometry(float Size,
46-
Uint32 NumSubdivisions,
47-
GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags,
48-
IDataBlob** ppVertices,
49-
IDataBlob** ppIndices,
50-
Uint32* pNumVertices,
51-
Uint32* pNumIndices)
45+
void CreateCubeGeometry(const CubeGeometryPrimitiveAttributes& Attribs,
46+
IDataBlob** ppVertices,
47+
IDataBlob** ppIndices,
48+
GeometryPrimitiveInfo* pInfo)
5249
{
50+
const float Size = Attribs.Size;
51+
const Uint32 NumSubdivisions = Attribs.NumSubdivisions;
52+
const GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags = Attribs.VertexFlags;
53+
5354
if (Size <= 0)
5455
{
5556
UNEXPECTED("Size must be positive");
@@ -82,10 +83,12 @@ void CreateCubeGeometry(float Size,
8283
const Uint32 VertexDataSize = NumFaceVertices * NumFaces * VertexSize;
8384
const Uint32 IndexDataSize = NumFaceIndices * NumFaces * sizeof(Uint32);
8485

85-
if (pNumVertices != nullptr)
86-
*pNumVertices = NumFaceVertices * NumFaces;
87-
if (pNumIndices != nullptr)
88-
*pNumIndices = NumFaceIndices * NumFaces;
86+
if (pInfo != nullptr)
87+
{
88+
pInfo->NumVertices = NumFaceVertices * NumFaces;
89+
pInfo->NumIndices = NumFaceIndices * NumFaces;
90+
pInfo->VertexSize = VertexSize;
91+
}
8992

9093
RefCntAutoPtr<DataBlobImpl> pVertexData;
9194
Uint8* pVert = nullptr;
@@ -211,6 +214,26 @@ void CreateCubeGeometry(float Size,
211214
VERIFY_EXPR(pIdx == nullptr || pIdx == pIndexData->GetConstDataPtr<Uint32>() + IndexDataSize / sizeof(Uint32));
212215
}
213216

217+
void CreateGeometryPrimitive(const GeometryPrimitiveAttributes& Attribs,
218+
IDataBlob** ppVertices,
219+
IDataBlob** ppIndices,
220+
GeometryPrimitiveInfo* pInfo)
221+
{
222+
switch (Attribs.Type)
223+
{
224+
case GEOMETRY_PRIMITIVE_TYPE_UNDEFINED:
225+
UNEXPECTED("Undefined geometry primitive type");
226+
break;
227+
228+
case GEOMETRY_PRIMITIVE_TYPE_CUBE:
229+
CreateCubeGeometry(static_cast<const CubeGeometryPrimitiveAttributes&>(Attribs), ppVertices, ppIndices, pInfo);
230+
break;
231+
232+
default:
233+
UNEXPECTED("Unknown geometry primitive type");
234+
}
235+
}
236+
214237
} // namespace Diligent
215238

216239
extern "C"
@@ -220,14 +243,11 @@ extern "C"
220243
return Diligent::GetGeometryPrimitiveVertexSize(VertexFlags);
221244
}
222245

223-
void Diligent_CreateCubeGeometry(float Size,
224-
Diligent::Uint32 SubdivisionLevel,
225-
Diligent::GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags,
226-
Diligent::IDataBlob** ppVertices,
227-
Diligent::IDataBlob** ppIndices,
228-
Diligent::Uint32* pNumVertices,
229-
Diligent::Uint32* pNumIndices)
246+
void Diligent_CreateGeometryPrimitive(const Diligent::GeometryPrimitiveAttributes& Attribs,
247+
Diligent::IDataBlob** ppVertices,
248+
Diligent::IDataBlob** ppIndices,
249+
Diligent::GeometryPrimitiveInfo* pInfo)
230250
{
231-
Diligent::CreateCubeGeometry(Size, SubdivisionLevel, VertexFlags, ppVertices, ppIndices, pNumVertices, pNumIndices);
251+
Diligent::CreateGeometryPrimitive(Attribs, ppVertices, ppIndices, pInfo);
232252
}
233253
}

0 commit comments

Comments
 (0)