@@ -42,20 +42,14 @@ Uint32 GetGeometryPrimitiveVertexSize(GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlag
4242 ((VertexFlags & GEOMETRY_PRIMITIVE_VERTEX_FLAG_TEXCOORD) ? sizeof (float2) : 0 ));
4343}
4444
45- void CreateCubeGeometry (const CubeGeometryPrimitiveAttributes& Attribs,
46- IDataBlob** ppVertices,
47- IDataBlob** ppIndices,
48- GeometryPrimitiveInfo* pInfo)
45+ template <typename VertexHandlerType>
46+ void CreateCubeGeometryInternal (Uint32 NumSubdivisions,
47+ GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags,
48+ IDataBlob** ppVertices,
49+ IDataBlob** ppIndices,
50+ GeometryPrimitiveInfo* pInfo,
51+ VertexHandlerType&& HandleVertex)
4952{
50- const float Size = Attribs.Size ;
51- const Uint32 NumSubdivisions = Attribs.NumSubdivisions ;
52- const GEOMETRY_PRIMITIVE_VERTEX_FLAGS VertexFlags = Attribs.VertexFlags ;
53-
54- if (Size <= 0 )
55- {
56- UNEXPECTED (" Size must be positive" );
57- return ;
58- }
5953 if (NumSubdivisions == 0 )
6054 {
6155 UNEXPECTED (" NumSubdivisions must be positive" );
@@ -123,7 +117,6 @@ void CreateCubeGeometry(const CubeGeometryPrimitiveAttributes& Attribs,
123117 {
124118 if (pVert != nullptr )
125119 {
126- const float3& Normal = FaceNormals[FaceIndex];
127120 // 6 ______7______ 8
128121 // | .'| .'|
129122 // | .' | .' |
@@ -158,9 +151,11 @@ void CreateCubeGeometry(const CubeGeometryPrimitiveAttributes& Attribs,
158151 case 5 : Pos = float3{+XY.x , XY.y , -0 .5f }; break ;
159152 }
160153
154+ float3 Normal = FaceNormals[FaceIndex];
155+ HandleVertex (Pos, Normal, UV);
156+
161157 if (VertexFlags & GEOMETRY_PRIMITIVE_VERTEX_FLAG_POSITION)
162158 {
163- Pos *= Size;
164159 memcpy (pVert, &Pos, sizeof (Pos));
165160 pVert += sizeof (Pos);
166161 }
@@ -214,6 +209,54 @@ void CreateCubeGeometry(const CubeGeometryPrimitiveAttributes& Attribs,
214209 VERIFY_EXPR (pIdx == nullptr || pIdx == pIndexData->GetConstDataPtr <Uint32>() + IndexDataSize / sizeof (Uint32));
215210}
216211
212+ void CreateCubeGeometry (const CubeGeometryPrimitiveAttributes& Attribs,
213+ IDataBlob** ppVertices,
214+ IDataBlob** ppIndices,
215+ GeometryPrimitiveInfo* pInfo)
216+ {
217+ const float Size = Attribs.Size ;
218+ if (Size <= 0 )
219+ {
220+ UNEXPECTED (" Size must be positive" );
221+ return ;
222+ }
223+
224+ CreateCubeGeometryInternal (Attribs.NumSubdivisions ,
225+ Attribs.VertexFlags ,
226+ ppVertices,
227+ ppIndices,
228+ pInfo,
229+ [&](float3& Pos, float3& Normal, float2& UV) {
230+ Pos *= Size;
231+ });
232+ }
233+
234+ void CreateSphereGeometry (const SphereGeometryPrimitiveAttributes& Attribs,
235+ IDataBlob** ppVertices,
236+ IDataBlob** ppIndices,
237+ GeometryPrimitiveInfo* pInfo)
238+ {
239+ const float Radius = Attribs.Radius ;
240+ if (Radius <= 0 )
241+ {
242+ UNEXPECTED (" Radius must be positive" );
243+ return ;
244+ }
245+
246+ CreateCubeGeometryInternal (Attribs.NumSubdivisions ,
247+ Attribs.VertexFlags ,
248+ ppVertices,
249+ ppIndices,
250+ pInfo,
251+ [&](float3& Pos, float3& Normal, float2& UV) {
252+ Normal = normalize (Pos);
253+ Pos = Normal * Radius;
254+
255+ UV.x = 0 .5f + atan2 (Normal.z , Normal.x ) / (2 * PI_F);
256+ UV.y = 0 .5f - asin (Normal.y ) / PI_F;
257+ });
258+ }
259+
217260void CreateGeometryPrimitive (const GeometryPrimitiveAttributes& Attribs,
218261 IDataBlob** ppVertices,
219262 IDataBlob** ppIndices,
@@ -222,7 +265,7 @@ void CreateGeometryPrimitive(const GeometryPrimitiveAttributes& Attribs,
222265 DEV_CHECK_ERR (ppVertices == nullptr || *ppVertices == nullptr , " *ppVertices is not null which may cause memory leaks" );
223266 DEV_CHECK_ERR (ppIndices == nullptr || *ppIndices == nullptr , " *ppIndices is not null which may cause memory leaks" );
224267
225- static_assert (GEOMETRY_PRIMITIVE_TYPE_LAST == 1 , " Please update the switch below to handle the new geometry primitive type" );
268+ static_assert (GEOMETRY_PRIMITIVE_TYPE_COUNT == 3 , " Please update the switch below to handle the new geometry primitive type" );
226269 switch (Attribs.Type )
227270 {
228271 case GEOMETRY_PRIMITIVE_TYPE_UNDEFINED:
@@ -233,6 +276,10 @@ void CreateGeometryPrimitive(const GeometryPrimitiveAttributes& Attribs,
233276 CreateCubeGeometry (static_cast <const CubeGeometryPrimitiveAttributes&>(Attribs), ppVertices, ppIndices, pInfo);
234277 break ;
235278
279+ case GEOMETRY_PRIMITIVE_TYPE_SPHERE:
280+ CreateSphereGeometry (static_cast <const SphereGeometryPrimitiveAttributes&>(Attribs), ppVertices, ppIndices, pInfo);
281+ break ;
282+
236283 default :
237284 UNEXPECTED (" Unknown geometry primitive type" );
238285 }
0 commit comments