-
Notifications
You must be signed in to change notification settings - Fork 2
/
BatchRenderable.cs
107 lines (86 loc) · 2.12 KB
/
BatchRenderable.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
using OpenTK;
namespace WarriorsSnuggery.Graphics
{
public abstract class BatchRenderable
{
protected readonly Vertex[] Vertices;
protected Vertex[] Calculated;
public bool Visible = true;
protected Vector4 Position = Vector4.Zero;
protected Vector4 Rotation = Vector4.Zero;
protected Vector3 Scale = Vector3.One;
protected Color Color = Color.White;
protected bool MatrixChanged;
public BatchRenderable(Vertex[] vertices, Color color)
{
Vertices = vertices;
Color = color;
MatrixChanged = true;
}
public virtual void SetPosition(CPos position)
{
SetPosition(position.ToVector());
}
public virtual void SetPosition(Vector position)
{
Vector4 vec4 = position;
if (Position == vec4)
return;
Position = vec4;
MatrixChanged = true;
}
public virtual void SetRotation(VAngle rotation)
{
SetRotation((Vector4)rotation);
}
public virtual void SetRotation(Vector4 rotation)
{
Vector4 rot4 = rotation;
if (Rotation == rot4)
return;
Rotation = rot4;
MatrixChanged = true;
}
public virtual void SetScale(float scale)
{
SetScale(new Vector3(scale));
}
public virtual void SetScale(Vector scale)
{
SetScale(((Vector4)scale).Xyz);
}
public virtual void SetScale(Vector3 scale)
{
if (Scale == scale)
return;
Scale = scale;
MatrixChanged = true;
}
public virtual void SetColor(Color color)
{
if (Color == color)
return;
Color = color;
MatrixChanged = true;
}
public virtual void PushToBatchRenderer()
{
if (!Visible)
return;
if (MatrixChanged)
{
var t2 = Matrix4.CreateTranslation(Position.X, Position.Y, Position.Z);
var r1 = Matrix4.CreateRotationX(Rotation.X);
var r2 = Matrix4.CreateRotationY(Rotation.Y);
var r3 = Matrix4.CreateRotationZ(Rotation.Z);
var s1 = Matrix4.CreateScale(Scale);
var matrix = r1 * r2 * r3 * s1 * t2;
Calculated = new Vertex[Vertices.Length];
for (int i = 0; i < Vertices.Length; i++)
Calculated[i] = Vertices[i].Apply(matrix, Color);
MatrixChanged = false;
}
MasterRenderer.BatchRenderer.Add(Calculated);
}
}
}