-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCone.h
More file actions
126 lines (112 loc) · 3.14 KB
/
Cone.h
File metadata and controls
126 lines (112 loc) · 3.14 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#pragma once
#include "IndexedTriangleList.h"
#include <DirectXMath.h>
#include "ChiliMath.h"
class Cone
{
public:
template<class V>
static IndexTriangleList<V> MakeTesselated(int longDiv)
{
namespace dx = DirectX;
assert(longDiv >= 3);
const auto base = dx::XMVectorSet(1.0f, 0.0f, -1.0f, 0.0f);
const float longitudeAngle = 2.0f * PI / longDiv;
// base vertices
std::vector<V> vertices;
for (int iLong = 0; iLong < longDiv; iLong++)
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
dx::XMStoreFloat3(&vertices.back().pos, v);
}
// the center
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,-1.0f };
const auto iCenter = (unsigned short)(vertices.size() - 1);
// the tip :darkness:
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,1.0f };
const auto iTip = (unsigned short)(vertices.size() - 1);
// base indices
std::vector<unsigned short> indices;
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
indices.push_back(iCenter);
indices.push_back((iLong + 1) % longDiv);
indices.push_back(iLong);
}
// cone indices
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
indices.push_back(iLong);
indices.push_back((iLong + 1) % longDiv);
indices.push_back(iTip);
}
return { std::move(vertices),std::move(indices) };
}
template<class V>
static IndexTriangleList<V> MakeTesselatedIndependentFaces(int longDiv) {
namespace dx = DirectX;
assert(longDiv >= 3);
const auto base = dx::XMVectorSet(1.0f, 0.0f, -1.0f, 0.0f);
const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices;
// cone vertices
const auto iCone = (unsigned short)vertices.size();
for (int iLong = 0; iLong < longDiv; iLong++)
{
const float thetas[] = {
longitudeAngle * iLong,
longitudeAngle * (((iLong + 1) == longDiv) ? 0 : (iLong + 1))
};
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,1.0f };
for (auto theta : thetas)
{
vertices.emplace_back();
const auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(theta)
);
dx::XMStoreFloat3(&vertices.back().pos, v);
}
}
// base vertices
const auto iBaseCenter = (unsigned short)vertices.size();
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,-1.0f };
const auto iBaseEdge = (unsigned short)vertices.size();
for (int iLong = 0; iLong < longDiv; iLong++)
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
dx::XMStoreFloat3(&vertices.back().pos, v);
}
std::vector<unsigned short> indices;
// cone indices
for (unsigned short i = 0; i < longDiv * 3; i++)
{
indices.push_back(i + iCone);
}
// base indices
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
indices.push_back(iBaseCenter);
indices.push_back((iLong + 1) % longDiv + iBaseEdge);
indices.push_back(iLong + iBaseEdge);
}
return { std::move(vertices),std::move(indices) };
}
template<class V>
static IndexTriangleList<V> Make()
{
return MakeTesselated<V>(24);
}
};