-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathTorus.cs
134 lines (106 loc) · 4.65 KB
/
Torus.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using CompiledMaterial.GBufferPNC;
using Core;
using Core.MaterialBase;
using Core.Primitive;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Mathematics;
namespace Engine
{
public class Torus : GameObject
{
public Torus(float radius1, float radius2, int sampleCount)
{
Debug.Assert(radius1 > 0 && radius1 > radius2 && sampleCount > 8);
Radius1 = radius1;
Radius2 = radius2;
SampleCount = sampleCount;
Initialize();
}
protected override void PrepareRenderingData()
{
}
public override void Initialize()
{
GenerateVertices();
RenderingThread.Instance.ExecuteImmediatelyIfRenderingThread
(
()=>
{
drawable = new DrawableBase<PNC_VertexAttribute>();
var vertexArray = VertexList.ToArray();
drawable.SetupVertexData(ref vertexArray);
defaultMaterial = ShaderManager.Instance.GetMaterial<GBufferPNC>();
VertexList.Clear();
bReadyToDraw = true;
});
}
public override void Render()
{
if (bReadyToDraw)
{
defaultMaterial.BindAndExecute
(
() => { drawable.DrawArrays(PrimitiveType.Triangles); }
);
}
}
protected void GenerateVertices()
{
for(int i = 0; i < SampleCount; ++i)
{
var degree1 = OpenTK.Mathematics.MathHelper.DegreesToRadians(((double)360.0 / SampleCount) * i);
var degree2 = OpenTK.Mathematics.MathHelper.DegreesToRadians(((double)360.0 / SampleCount) * (i + 1));
var x1 = Radius1 * (float)Math.Cos(degree1);
var z1 = Radius1 * (float)Math.Sin(degree1);
var x2 = Radius1 * (float)Math.Cos(degree2);
var z2 = Radius1 * (float)Math.Sin(degree2);
var vCenter1 = new Vector3(x1, 0, z1);
var vDir1 = vCenter1.Normalized();
var vCenter2 = new Vector3(x2, 0, z2);
var vDir2 = vCenter2.Normalized();
for (int j =0; j < SampleCount; ++j)
{
var innerDegree1 = OpenTK.Mathematics.MathHelper.DegreesToRadians(((double)360.0 / SampleCount) * j);
var innerDegree2 = OpenTK.Mathematics.MathHelper.DegreesToRadians(((double)360.0 / SampleCount) * (j+1));
var offsetX1 = Radius2 * (float)Math.Cos(innerDegree1);
var offsetY1 = Radius2 * (float)Math.Sin(innerDegree1);
var offsetX2 = Radius2 * (float)Math.Cos(innerDegree2);
var offsetY2 = Radius2 * (float)Math.Sin(innerDegree2);
var v1 = vCenter1 + vDir1 * offsetX1 + new Vector3(0, offsetY1, 0);
var v2 = vCenter1 + vDir1 * offsetX2 + new Vector3(0, offsetY2, 0);
var v3 = vCenter2 + vDir2 * offsetX1 + new Vector3(0, offsetY1, 0);
var v4 = vCenter2 + vDir2 * offsetX2 + new Vector3(0, offsetY2, 0);
var d1 = (v3 - v4).Normalized();
var d2 = (v2 - v4).Normalized();
var norm1 = Vector3.Cross(d2, d1).Normalized();
var d3 = (v3 - v1).Normalized();
var d4 = (v2 - v1).Normalized();
var norm2 = Vector3.Cross(d3, d4).Normalized();
// v4 - v3
// | / |
// v2 - v1
VertexList.Add(new PNC_VertexAttribute(v4, norm1, Color));
VertexList.Add(new PNC_VertexAttribute(v3, norm1, Color));
VertexList.Add(new PNC_VertexAttribute(v2, norm1, Color));
VertexList.Add(new PNC_VertexAttribute(v2, norm2, Color));
VertexList.Add(new PNC_VertexAttribute(v3, norm2, Color));
VertexList.Add(new PNC_VertexAttribute(v1, norm2, Color));
}
}
}
protected float Radius1 = 10;
protected float Radius2 = 3;
protected int SampleCount = 10;
protected Vector3 Color = new Vector3(1, 0, 0);
protected int VertexCount = 0;
protected List<PNC_VertexAttribute> VertexList = new List<PNC_VertexAttribute>();
protected DrawableBase<PNC_VertexAttribute> drawable = null;
}
}