-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSphere.h
More file actions
99 lines (92 loc) · 2.87 KB
/
Sphere.h
File metadata and controls
99 lines (92 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#pragma once
#include "IndexedTriangleList.h"
#include <DirectXMath.h>
#include "ChiliMath.h"
class Sphere
{
public:
template<class V>
static IndexTriangleList<V> MakeTesselated(int latDiv, int longDiv)
{
namespace dx = DirectX;
assert(latDiv >= 3);
assert(longDiv >= 3);
constexpr float radius = 1.0f;
const auto base = dx::XMVectorSet(0.0f, 0.0f, radius, 0.0f);
const float lattitudeAngle = PI / latDiv;
const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices;
for (int iLat = 1; iLat < latDiv; iLat++)
{
const auto latBase = dx::XMVector3Transform(
base,
dx::XMMatrixRotationX(lattitudeAngle * iLat)
);
for (int iLong = 0; iLong < longDiv; iLong++)
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
latBase,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
dx::XMStoreFloat3(&vertices.back().pos, v);
}
}
// add the cap vertices
const auto iNorthPole = (unsigned short)vertices.size();
vertices.emplace_back();
dx::XMStoreFloat3(&vertices.back().pos, base);
const auto iSouthPole = (unsigned short)vertices.size();
vertices.emplace_back();
dx::XMStoreFloat3(&vertices.back().pos, dx::XMVectorNegate(base));
const auto calcIdx = [latDiv, longDiv](unsigned short iLat, unsigned short iLong)
{ return iLat * longDiv + iLong; };
std::vector<unsigned short> indices;
for (unsigned short iLat = 0; iLat < latDiv - 2; iLat++)
{
for (unsigned short iLong = 0; iLong < longDiv - 1; iLong++)
{
indices.push_back(calcIdx(iLat, iLong));
indices.push_back(calcIdx(iLat + 1, iLong));
indices.push_back(calcIdx(iLat, iLong + 1));
indices.push_back(calcIdx(iLat, iLong + 1));
indices.push_back(calcIdx(iLat + 1, iLong));
indices.push_back(calcIdx(iLat + 1, iLong + 1));
}
// wrap band
indices.push_back(calcIdx(iLat, longDiv - 1));
indices.push_back(calcIdx(iLat + 1, longDiv - 1));
indices.push_back(calcIdx(iLat, 0));
indices.push_back(calcIdx(iLat, 0));
indices.push_back(calcIdx(iLat + 1, longDiv - 1));
indices.push_back(calcIdx(iLat + 1, 0));
}
// cap fans
for (unsigned short iLong = 0; iLong < longDiv - 1; iLong++)
{
// north
indices.push_back(iNorthPole);
indices.push_back(calcIdx(0, iLong));
indices.push_back(calcIdx(0, iLong + 1));
// south
indices.push_back(calcIdx(latDiv - 2, iLong + 1));
indices.push_back(calcIdx(latDiv - 2, iLong));
indices.push_back(iSouthPole);
}
// wrap triangles
// north
indices.push_back(iNorthPole);
indices.push_back(calcIdx(0, longDiv - 1));
indices.push_back(calcIdx(0, 0));
// south
indices.push_back(calcIdx(latDiv - 2, 0));
indices.push_back(calcIdx(latDiv - 2, longDiv - 1));
indices.push_back(iSouthPole);
return { std::move(vertices),std::move(indices) };
}
template<class V>
static IndexTriangleList<V> Make()
{
return MakeTesselated<V>(12, 24);
}
};