-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrism.h
More file actions
197 lines (184 loc) · 5.39 KB
/
Prism.h
File metadata and controls
197 lines (184 loc) · 5.39 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#pragma once
#include "IndexedTriangleList.h"
#include <DirectXMath.h>
#include "ChiliMath.h"
class Prism
{
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 auto offset = dx::XMVectorSet(0.0f, 0.0f, 2.0f, 0.0f);
const float longitudeAngle = 2.0f * PI / longDiv;
// near center
std::vector<V> vertices;
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,-1.0f };
const auto iCenterNear = (unsigned short)(vertices.size() - 1);
// far center
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,1.0f };
const auto iCenterFar = (unsigned short)(vertices.size() - 1);
// base vertices
for (int iLong = 0; iLong < longDiv; iLong++)
{
// near base
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
dx::XMStoreFloat3(&vertices.back().pos, v);
}
// far base
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
v = dx::XMVectorAdd(v, offset);
dx::XMStoreFloat3(&vertices.back().pos, v);
}
}
// side indices
std::vector<unsigned short> indices;
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
const auto i = iLong * 2;
const auto mod = longDiv * 2;
indices.push_back(i + 2);
indices.push_back((i + 2) % mod + 2);
indices.push_back(i + 1 + 2);
indices.push_back((i + 2) % mod + 2);
indices.push_back((i + 3) % mod + 2);
indices.push_back(i + 1 + 2);
}
// base indices
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
const auto i = iLong * 2;
const auto mod = longDiv * 2;
indices.push_back(i + 2);
indices.push_back(iCenterNear);
indices.push_back((i + 2) % mod + 2);
indices.push_back(iCenterFar);
indices.push_back(i + 1 + 2);
indices.push_back((i + 3) % mod + 2);
}
return { std::move(vertices),std::move(indices) };
}
template<class V>
static IndexTriangleList<V> MakeTesselatedIndependentCapNormals(int longDiv) {
namespace dx = DirectX;
assert(longDiv >= 3);
const auto base = dx::XMVectorSet(1.0f, 0.0f, -1.0f, 0.0f);
const auto offset = dx::XMVectorSet(0.0f, 0.0f, 2.0f, 0.0f);
const float longitudeAngle = 2.0f * PI / longDiv;
std::vector<V> vertices;
// near center
const auto iCenterNear = (unsigned short)vertices.size();
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,-1.0f };
vertices.back().norm = { 0.0f,0.0f,-1.0f };
// near base vertices
const auto iBaseNear = (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);
vertices.back().norm = { 0.0f,0.0f,-1.0f };
}
// far center
const auto iCenterFar = (unsigned short)vertices.size();
vertices.emplace_back();
vertices.back().pos = { 0.0f,0.0f,1.0f };
vertices.back().norm = { 0.0f,0.0f,1.0f };
// far base vertices
const auto iBaseFar = (unsigned short)vertices.size();
for (int iLong = 0; iLong < longDiv; iLong++)
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
v = dx::XMVectorAdd(v, offset);
dx::XMStoreFloat3(&vertices.back().pos, v);
vertices.back().norm = { 0.0f,0.0f,1.0f };
}
// fusilage vertices
const auto iFusilage = (unsigned short)vertices.size();
for (int iLong = 0; iLong < longDiv; iLong++)
{
// near base
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
dx::XMStoreFloat3(&vertices.back().pos, v);
vertices.back().norm = { vertices.back().pos.x,vertices.back().pos.y,0.0f };
}
// far base
{
vertices.emplace_back();
auto v = dx::XMVector3Transform(
base,
dx::XMMatrixRotationZ(longitudeAngle * iLong)
);
v = dx::XMVectorAdd(v, offset);
dx::XMStoreFloat3(&vertices.back().pos, v);
vertices.back().norm = { vertices.back().pos.x,vertices.back().pos.y,0.0f };
}
}
std::vector<unsigned short> indices;
// near base indices
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
const auto i = iLong;
const auto mod = longDiv;
// near
indices.push_back(i + iBaseNear);
indices.push_back(iCenterNear);
indices.push_back((i + 1) % mod + iBaseNear);
}
// far base indices
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
const auto i = iLong;
const auto mod = longDiv;
// far
indices.push_back(iCenterFar);
indices.push_back(i + iBaseFar);
indices.push_back((i + 1) % mod + iBaseFar);
}
// fusilage indices
for (unsigned short iLong = 0; iLong < longDiv; iLong++)
{
const auto i = iLong * 2;
const auto mod = longDiv * 2;
indices.push_back(i + iFusilage);
indices.push_back((i + 2) % mod + iFusilage);
indices.push_back(i + 1 + iFusilage);
indices.push_back((i + 2) % mod + iFusilage);
indices.push_back((i + 3) % mod + iFusilage);
indices.push_back(i + 1 + iFusilage);
}
return { std::move(vertices),std::move(indices) };
}
template<class V>
static IndexTriangleList<V> Make()
{
return MakeTesselated<V>(24);
}
};