MCPcopy Create free account
hub / github.com/davideberly/GeometricTools / CreateSphere

Method CreateSphere

GTE/Graphics/MeshFactory.cpp:564–730  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

562}
563
564std::shared_ptr<Visual> MeshFactory::CreateSphere(uint32_t numZSamples,
565 uint32_t numRadialSamples, float radius)
566{
567 // Quantities derived from inputs.
568 uint32_t zsm1 = numZSamples - 1;
569 uint32_t zsm2 = numZSamples - 2;
570 uint32_t zsm3 = numZSamples - 3;
571 uint32_t rsp1 = numRadialSamples + 1;
572 float invRS = 1.0f / static_cast<float>(numRadialSamples);
573 float zFactor = 2.0f / static_cast<float>(zsm1);
574 uint32_t numVertices = zsm2 * rsp1 + 2;
575 uint32_t numTriangles = 2 * zsm2 * numRadialSamples;
576
577 // Generate geometry.
578 auto vbuffer = CreateVBuffer(numVertices);
579 if (!vbuffer)
580 {
581 return nullptr;
582 }
583
584 Vector3<float> pos{}, nor{};
585 std::array<Vector3<float>, 3> basis{};
586 Vector2<float> tcd{};
587
588 // Generate points on the unit circle to be used in computing the mesh
589 // points on a sphere slice.
590 std::vector<float> cs(rsp1), sn(rsp1);
591 for (uint32_t r = 0; r < numRadialSamples; ++r)
592 {
593 float angle = invRS * r * static_cast<float>(GTE_C_TWO_PI);
594 cs[r] = std::cos(angle);
595 sn[r] = std::sin(angle);
596 }
597 cs[numRadialSamples] = cs[0];
598 sn[numRadialSamples] = sn[0];
599
600 // Generate the sphere itself.
601 uint32_t i = 0;
602 for (uint32_t z = 1; z < zsm1; ++z)
603 {
604 float zFraction = -1.0f + zFactor*static_cast<float>(z); // in (-1,1)
605 float zValue = radius*zFraction;
606
607 // Compute center of slice.
608 Vector3<float> sliceCenter{ 0.0f, 0.0f, zValue };
609
610 // Compute radius of slice.
611 float sliceRadius = std::sqrt(std::max(radius * radius - zValue * zValue, 0.0f));
612
613 // Compute slice vertices with duplication at endpoint.
614 for (uint32_t r = 0; r <= numRadialSamples; ++r, ++i)
615 {
616 float radialFraction = r * invRS; // in [0,1)
617 Vector3<float> radial{ cs[r], sn[r], 0.0f };
618 pos = sliceCenter + sliceRadius * radial;
619 nor = pos;
620 Normalize(nor);
621 if (!mOutside)

Callers

nothing calls this directly

Calls 9

UpdateModelBoundMethod · 0.80
SetCenterMethod · 0.80
SetRadiusMethod · 0.80
cosFunction · 0.50
sinFunction · 0.50
sqrtFunction · 0.50
NormalizeFunction · 0.50
SetTriangleMethod · 0.45

Tested by

no test coverage detected