-
Notifications
You must be signed in to change notification settings - Fork 378
/
DiscGenerators.cs
100 lines (83 loc) · 3.78 KB
/
DiscGenerators.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace g3
{
// generate a triangle fan, no subdvisions
public class TrivialDiscGenerator : MeshGenerator
{
public float Radius = 1.0f;
public float StartAngleDeg = 0.0f;
public float EndAngleDeg = 360.0f;
public int Slices = 32;
override public void Generate()
{
vertices = new VectorArray3d(Slices + 1);
uv = new VectorArray2f(Slices + 1);
normals = new VectorArray3f(Slices + 1);
triangles = new IndexArray3i(Slices);
int vi = 0;
vertices[vi] = Vector3d.Zero;
uv[vi] = new Vector2f(0.5f, 0.5f);
normals[vi] = Vector3f.AxisY;
vi++;
bool bFullDisc = ((EndAngleDeg - StartAngleDeg) > 359.99f);
float fTotalRange = (EndAngleDeg - StartAngleDeg) * MathUtil.Deg2Radf;
float fStartRad = StartAngleDeg * MathUtil.Deg2Radf;
float fDelta = (bFullDisc) ? fTotalRange / Slices : fTotalRange / (Slices - 1);
for (int k = 0; k < Slices; ++k) {
float a = fStartRad + (float)k * fDelta;
double cosa = Math.Cos(a), sina = Math.Sin(a);
vertices[vi] = new Vector3d(Radius * cosa, 0, Radius * sina);
uv[vi] = new Vector2f(0.5f * (1.0f + cosa), 0.5f * (1 + sina));
normals[vi] = Vector3f.AxisY;
vi++;
}
int ti = 0;
for (int k = 1; k < Slices; ++k)
triangles.Set(ti++, k, 0, k + 1, Clockwise);
if (bFullDisc) // close disc if we went all the way
triangles.Set(ti++, Slices, 0, 1, Clockwise);
}
}
// generate a triangle fan, no subdvisions
public class PuncturedDiscGenerator : MeshGenerator
{
public float OuterRadius = 1.0f;
public float InnerRadius = 0.5f;
public float StartAngleDeg = 0.0f;
public float EndAngleDeg = 360.0f;
public int Slices = 32;
override public void Generate()
{
vertices = new VectorArray3d(2*Slices);
uv = new VectorArray2f(2*Slices);
normals = new VectorArray3f(2*Slices);
triangles = new IndexArray3i(2*Slices);
bool bFullDisc = ((EndAngleDeg - StartAngleDeg) > 359.99f);
float fTotalRange = (EndAngleDeg - StartAngleDeg) * MathUtil.Deg2Radf;
float fStartRad = StartAngleDeg * MathUtil.Deg2Radf;
float fDelta = (bFullDisc) ? fTotalRange / Slices : fTotalRange / (Slices - 1);
float fUVRatio = InnerRadius / OuterRadius;
for (int k = 0; k < Slices; ++k) {
float angle = fStartRad + (float)k * fDelta;
double cosa = Math.Cos(angle), sina = Math.Sin(angle);
vertices[k] = new Vector3d(InnerRadius * cosa, 0, InnerRadius * sina);
vertices[Slices+k] = new Vector3d(OuterRadius * cosa, 0, OuterRadius * sina);
uv[k] = new Vector2f(0.5f * (1.0f + fUVRatio * cosa), 0.5f * (1.0f + fUVRatio * sina));
uv[Slices + k] = new Vector2f(0.5f * (1.0f + cosa), 0.5f * (1.0f + sina));
normals[k] = normals[Slices + k] = Vector3f.AxisY;
}
int ti = 0;
for (int k = 0; k < Slices-1; ++k) {
triangles.Set(ti++, k, k + 1, Slices + k + 1, Clockwise);
triangles.Set(ti++, k, Slices + k + 1, Slices + k, Clockwise);
}
if (bFullDisc) { // close disc if we went all the way
triangles.Set(ti++, Slices - 1, 0, Slices, Clockwise);
triangles.Set(ti++, Slices - 1, Slices, 2 * Slices - 1, Clockwise);
}
}
}
}