3#include "MeshGeneratorCommon.h"
7 template<ShapeType subtype>
8 void sphereFactoryHelper(std::vector<PositionNormalTexVertex>& V,
9 std::vector<uint32_t>& I,
10 const glm::vec3& p001,
11 const glm::vec3& p010,
12 const glm::vec3& p100,
17 for (
int j = 0; j < N; j++) {
18 float v = float(j) / float(N - 1);
21 for (
int i = 0; i < (N - j - (j == 0 ? 1 : 0)); i++) {
22 float u = float(i) / float(N - 1);
25 glm::vec3 P = glm::normalize(v*p010 + u*p001 + (1.f - u - v)*p100);
28 float t = 1.f / std::sqrt(1.f - P.z*P.z);
29 float t_u = float(0.5 * glm::one_over_pi<float>())*std::acos(std::min(1.f, P.y*t));
30 float t_v = subtype == ShapeType::SphereGeoTexSph
31 ? glm::one_over_pi<float>() * std::acos(-P.z)
33 V.push_back({ P, P, glm::vec2(t_u, t_v) });
37 V.push_back({ p001, p001, glm::vec2(0.125f, 1.f) });
43 for (uint32_t j = 0; j < samples; j++) {
44 for (uint32_t i = 0; i < samples - j; i++) {
46 I.push_back(j + k + samples - j + 1);
47 I.push_back(j + k + 1);
48 if (i < samples - j - 1) {
49 I.push_back(j + k + samples - j + 1);
50 I.push_back(j + k + samples - j + 2);
51 I.push_back(j + k + 1);
61 void sphereFactoryCloneOctant(std::vector<PositionNormalTexVertex>& V,
62 std::vector<uint32_t>& I,
63 const std::vector<PositionNormalTexVertex>& V_t,
64 const std::vector<uint32_t>& I_t,
65 const glm::vec3& P100,
66 const glm::vec3& P010,
67 const glm::vec3& P001,
68 const glm::vec2& transform_u,
69 const glm::vec2& transform_v,
72 uint32_t offset = uint32_t(V.size());
73 int Vn = ((samples + 1)*(samples + 2)) / 2;
74 for (
int i = 0; i < Vn; i++) {
75 glm::vec3 pw = V_t[i].position;
76 glm::vec3 p = P001 * pw.x + P010*pw.y + P100*pw.z;
77 glm::vec2 tw = V_t[i].tex;
78 glm::vec2 t = glm::vec2(transform_u.x*tw.x + transform_u.y,
79 transform_v.x*tw.y + transform_v.y);
80 V.push_back({ p, p, t });
83 int In = ((3 * samples*(samples + 1)) / 2 + (3 * (samples - 1)*samples) / 2);
84 for (
int i = 0; i < In; i++) {
85 I.push_back(offset + I_t[i]);
89 template<ShapeType subtype>
90 void sphereGeoTexSphFactory(
Mesh * m,
const ShapeDefinition & definition,
int kRefinementLevels,
int kMaxSamples)
92 int n = (samplesFromLevel(definition.refinement, kRefinementLevels, kMaxSamples) + 3) / 4;
94 std::vector<PositionNormalTexVertex> V_t;
95 V_t.reserve(((n + 1)*(n + 2)) / 2);
97 std::vector<uint32_t> I_t;
98 I_t.reserve(((3 * n*(n + 1)) / 2 + (3 * (n - 1)*n) / 2));
102 sphereFactoryHelper<subtype>(V_t, I_t, glm::vec3(0.f, 0.f, 1), glm::vec3(0.f, 1, 0.f), glm::vec3(1, 0.f, 0.f), n);
104 std::vector<PositionNormalTexVertex> V;
105 V.reserve(8 * ((n + 1)*(n + 2)) / 2);
107 std::vector<uint32_t> I;
108 I.reserve(8 * ((3 * n*(n + 1)) / 2 + (3 * (n - 1)*n) / 2));
112 auto & r = definition.size.x;
114 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, r), glm::vec3( r, 0.f, 0.f), glm::vec3(0.f, -r, 0.f), glm::vec2( 1.f, 0.25f), glm::vec2( 1.f, 0.f), n);
115 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, r), glm::vec3(0.f, -r, 0.f), glm::vec3( -r, 0.f, 0.f), glm::vec2( 1.f, 0.50f), glm::vec2( 1.f, 0.f), n);
116 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, r), glm::vec3( -r, 0.f, 0.f), glm::vec3(0.f, r, 0.f), glm::vec2( 1.f, 0.75f), glm::vec2( 1.f, 0.f), n);
117 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, r), glm::vec3(0.f, r, 0.f), glm::vec3( r, 0.f, 0.f), glm::vec2( 1.f, 0.0f ), glm::vec2( 1.f, 0.f), n);
119 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, -r), glm::vec3( r, 0.f, 0.f), glm::vec3(0.f, r, 0.f), glm::vec2(-1.f, 0.25f), glm::vec2(-1.f, 1.0f), n);
120 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, -r), glm::vec3(0.f, -r, 0.f), glm::vec3( r, 0.f, 0.f), glm::vec2(-1.f, 0.50f), glm::vec2(-1.f, 1.0f), n);
121 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, -r), glm::vec3( -r, 0.f, 0.f), glm::vec3(0.f, -r, 0.f), glm::vec2(-1.f, 0.75f), glm::vec2(-1.f, 1.0f), n);
122 sphereFactoryCloneOctant(V, I, V_t, I_t, glm::vec3(0.f, 0.f, -r), glm::vec3(0.f, r, 0.f), glm::vec3( -r, 0.f, 0.f), glm::vec2(-1.f, 1.0f ), glm::vec2(-1.f, 1.0f), n);
126 m->
setBounds(computeBoundingBoxFromVertexFormat(V.begin(), V.end()));
Meshes contain streams of vertex data in addition to index data and options defining geometry used fo...
void setIndexData(const uint32_t *data, size_t count)
Convenience method for setting index data from a raw pointer to data and count number of elements.
void setBounds(Geometry::BoundingBox box)
Set custom bounds for the mesh.
void setVertexData(Element *elements, size_t count)
Set vertex data.
Defines creation values for a unique shape.