-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndexedTriangleList.h
More file actions
57 lines (50 loc) · 1.33 KB
/
IndexedTriangleList.h
File metadata and controls
57 lines (50 loc) · 1.33 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
#pragma once
#include <vector>
#include <DirectXMath.h>
template<class T>
class IndexTriangleList
{
public:
IndexTriangleList() = default;
IndexTriangleList(std::vector<T> verts_in, std::vector<unsigned short> indices_in)
:
vertices(std::move(verts_in)),
indices(std::move(indices_in))
{
assert(vertices.size() > 2);
assert(indices.size() % 3 == 0);
}
void Transform(DirectX::FXMMATRIX matrix)
{
for (auto& v : vertices)
{
const DirectX::XMVECTOR pos = DirectX::XMLoadFloat3(&v.pos);
DirectX::XMStoreFloat3(
&v.pos,
DirectX::XMVector3Transform(pos, matrix)
);
}
}
// asserts face-independent vertices w/ normals cleared to zero
void SetNormalsIndependentFlat() noexcept(!IS_DEBUG)
{
using namespace DirectX;
assert(indices.size() % 3 == 0 && indices.size() > 0);
for (size_t i = 0; i < indices.size(); i += 3)
{
auto& v0 = vertices[indices[i]];
auto& v1 = vertices[indices[i + 1]];
auto& v2 = vertices[indices[i + 2]];
const auto p0 = XMLoadFloat3(&v0.pos);
const auto p1 = XMLoadFloat3(&v1.pos);
const auto p2 = XMLoadFloat3(&v2.pos);
const auto n = XMVector3Normalize(XMVector3Cross((p1 - p0), (p2 - p0)));
XMStoreFloat3(&v0.norm, n);
XMStoreFloat3(&v1.norm, n);
XMStoreFloat3(&v2.norm, n);
}
}
public:
std::vector<T> vertices;
std::vector<unsigned short> indices;
};