-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolidVisualizer.cpp
More file actions
63 lines (53 loc) · 1.29 KB
/
SolidVisualizer.cpp
File metadata and controls
63 lines (53 loc) · 1.29 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
#include "SolidVisualizer.h"
namespace Forth
{
SolidVisualizer::SolidVisualizer() {}
void SolidVisualizer::Initialize(Buffer3 &buffer)
{
buff = &buffer;
buff->Clear();
buff->simplex = SimplexMode::SM_Triangle;
}
void SolidVisualizer::Render(const Vector4 *buffer, int count)
{
Buffer3 &b = *buff;
int o = b.vertices_count;
b.AddVert(buffer[0].ToVec3());
b.AddVert(buffer[1].ToVec3());
for (int i = 2; i < count; i++)
{
b.AddVert(buffer[i].ToVec3());
b.AddTris(0 + o, i - 1 + o, i + o);
}
}
void SolidVisualizer::End()
{
// Not needed. Much easier to use double-sided shader instead.
// RefineTriangleOrder();
}
void SolidVisualizer::RefineTriangleOrder()
{
int *t = buff->indices;
Vector3 *v = buff->vertices;
Vector3 median = GetAverage(buff->vertices, buff->vertices_count) - Vector3(.1e-4f);
for (int i = 0; i < buff->indices_count; i += 3)
{
int a = t[i], b = t[i + 1], c = t[i + 2];
Vector3 &va = v[a];
Vector3 N = Cross(v[c] - va, v[b] - va);
if (Dot(N, median - va) < 0)
{
// Flip
t[i] = c;
t[i + 2] = a;
}
}
}
Vector3 SolidVisualizer::GetAverage(const Vector3 *v, const int size)
{
Vector3 median = Vector3();
for (size_t i = size; i-- > 0;)
median += v[i];
return median / (float)size;
}
} // namespace Forth