/
GLUtils.cs
248 lines (201 loc) · 9.1 KB
/
GLUtils.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System.Collections.Generic;
using UnityEngine;
using static MechJebLib.Statics;
namespace MuMech
{
public static class GLUtils
{
private static Material _material;
private static Material material
{
get
{
if (_material == null) _material = new Material(Shader.Find("Legacy Shaders/Particles/Additive"));
return _material;
}
}
public static void DrawMapViewGroundMarker(CelestialBody body, double latitude, double longitude, Color c, double rotation = 0,
double radius = 0)
{
DrawGroundMarker(body, latitude, longitude, c, true, rotation, radius);
}
public static void DrawGroundMarker(CelestialBody body, double latitude, double longitude, Color c, bool map, double rotation = 0,
double radius = 0)
{
Vector3d up = body.GetSurfaceNVector(latitude, longitude);
double height = body.pqsController.GetSurfaceHeight(QuaternionD.AngleAxis(longitude, Vector3d.down) *
QuaternionD.AngleAxis(latitude, Vector3d.forward) * Vector3d.right);
if (height < body.Radius) { height = body.Radius; }
Vector3d center = body.position + height * up;
Vector3d camPos = map
? ScaledSpace.ScaledToLocalSpace(PlanetariumCamera.Camera.transform.position)
: (Vector3d)FlightCamera.fetch.mainCamera.transform.position;
if (IsOccluded(center, body, camPos)) return;
Vector3d north = Vector3d.Exclude(up, body.transform.up).normalized;
if (radius <= 0) { radius = map ? body.Radius / 15 : 5; }
if (!map)
{
Vector3 centerPoint = FlightCamera.fetch.mainCamera.WorldToViewportPoint(center);
if (centerPoint.z < 0)
return;
}
GLTriangle(
center,
center + radius * (QuaternionD.AngleAxis(rotation - 10, up) * north),
center + radius * (QuaternionD.AngleAxis(rotation + 10, up) * north)
, c, map);
GLTriangle(
center,
center + radius * (QuaternionD.AngleAxis(rotation + 110, up) * north),
center + radius * (QuaternionD.AngleAxis(rotation + 130, up) * north)
, c, map);
GLTriangle(
center,
center + radius * (QuaternionD.AngleAxis(rotation - 110, up) * north),
center + radius * (QuaternionD.AngleAxis(rotation - 130, up) * north)
, c, map);
}
public static void GLTriangle(Vector3d worldVertices1, Vector3d worldVertices2, Vector3d worldVertices3, Color c, bool map)
{
GL.PushMatrix();
material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.TRIANGLES);
GL.Color(c);
GLVertex(worldVertices1, map);
GLVertex(worldVertices2, map);
GLVertex(worldVertices3, map);
GL.End();
GL.PopMatrix();
}
public static void GLVertex(Vector3d worldPosition, bool map = false)
{
Vector3 screenPoint =
map
? PlanetariumCamera.Camera.WorldToViewportPoint(ScaledSpace.LocalToScaledSpace(worldPosition))
: FlightCamera.fetch.mainCamera.WorldToViewportPoint(worldPosition);
GL.Vertex3(screenPoint.x, screenPoint.y, 0);
}
public static void GLPixelLine(Vector3d worldPosition1, Vector3d worldPosition2, bool map)
{
Vector3 screenPoint1, screenPoint2;
if (map)
{
screenPoint1 = PlanetariumCamera.Camera.WorldToScreenPoint(ScaledSpace.LocalToScaledSpace(worldPosition1));
screenPoint2 = PlanetariumCamera.Camera.WorldToScreenPoint(ScaledSpace.LocalToScaledSpace(worldPosition2));
}
else
{
screenPoint1 = FlightCamera.fetch.mainCamera.WorldToScreenPoint(worldPosition1);
screenPoint2 = FlightCamera.fetch.mainCamera.WorldToScreenPoint(worldPosition2);
}
if (screenPoint1.z > 0 && screenPoint2.z > 0)
{
GL.Vertex3(screenPoint1.x, screenPoint1.y, 0);
GL.Vertex3(screenPoint2.x, screenPoint2.y, 0);
}
}
//Tests if byBody occludes worldPosition, from the perspective of the planetarium camera
// https://cesiumjs.org/2013/04/25/Horizon-culling/
public static bool IsOccluded(Vector3d worldPosition, CelestialBody byBody, Vector3d camPos)
{
Vector3d VC = (byBody.position - camPos) / (byBody.Radius - 100);
Vector3d VT = (worldPosition - camPos) / (byBody.Radius - 100);
double VT_VC = Vector3d.Dot(VT, VC);
// In front of the horizon plane
if (VT_VC < VC.sqrMagnitude - 1) return false;
return VT_VC * VT_VC / VT.sqrMagnitude > VC.sqrMagnitude - 1;
}
//If dashed = false, draws 0-1-2-3-4-5...
//If dashed = true, draws 0-1 2-3 4-5...
public static void DrawPath(CelestialBody mainBody, List<Vector3d> points, Color c, bool map, bool dashed = false)
{
GL.PushMatrix();
material.SetPass(0);
GL.LoadPixelMatrix();
GL.Begin(GL.LINES);
GL.Color(c);
Vector3d camPos = map
? ScaledSpace.ScaledToLocalSpace(PlanetariumCamera.Camera.transform.position)
: (Vector3d)FlightCamera.fetch.mainCamera.transform.position;
int step = dashed ? 2 : 1;
for (int i = 0; i < points.Count - 1; i += step)
{
if (!IsOccluded(points[i], mainBody, camPos) && !IsOccluded(points[i + 1], mainBody, camPos))
{
GLPixelLine(points[i], points[i + 1], map);
}
}
GL.End();
GL.PopMatrix();
}
public static void DrawBoundingBox(CelestialBody mainBody, Vessel vessel, MechJebModuleDockingAutopilot.Box3d box, Color c)
{
//Vector3d origin = vessel.GetWorldPos3D() - vessel.GetTransform().rotation * box.center ;
//Vector3d origin = vessel.GetTransform().TransformPoint(box.center);
Vector3d origin = vessel.transform.TransformPoint(box.center);
Vector3d A1 = origin + vessel.transform.rotation * new Vector3d(+box.size.x, +box.size.y, +box.size.z);
Vector3d A2 = origin + vessel.transform.rotation * new Vector3d(+box.size.x, -box.size.y, +box.size.z);
Vector3d A3 = origin + vessel.transform.rotation * new Vector3d(-box.size.x, -box.size.y, +box.size.z);
Vector3d A4 = origin + vessel.transform.rotation * new Vector3d(-box.size.x, +box.size.y, +box.size.z);
Vector3d B1 = origin + vessel.transform.rotation * new Vector3d(+box.size.x, +box.size.y, -box.size.z);
Vector3d B2 = origin + vessel.transform.rotation * new Vector3d(+box.size.x, -box.size.y, -box.size.z);
Vector3d B3 = origin + vessel.transform.rotation * new Vector3d(-box.size.x, -box.size.y, -box.size.z);
Vector3d B4 = origin + vessel.transform.rotation * new Vector3d(-box.size.x, +box.size.y, -box.size.z);
GL.PushMatrix();
material.SetPass(0);
GL.LoadOrtho();
GL.Begin(GL.LINES);
GL.Color(c);
GLVertex(A1);
GLVertex(A2);
GLVertex(A2);
GLVertex(A3);
GLVertex(A3);
GLVertex(A4);
GLVertex(A4);
GLVertex(A1);
GLVertex(B1);
GLVertex(B2);
GLVertex(B2);
GLVertex(B3);
GLVertex(B3);
GLVertex(B4);
GLVertex(B4);
GLVertex(B1);
GLVertex(A1);
GLVertex(B1);
GLVertex(A2);
GLVertex(B2);
GLVertex(A3);
GLVertex(B3);
GLVertex(A4);
GLVertex(B4);
GL.End();
GL.PopMatrix();
}
private static readonly List<Vector3d> _points = new List<Vector3d>();
public static void DrawOrbit(Orbit o, Color c)
{
_points.Clear();
if (o.eccentricity < 1)
{
//elliptical orbits:
for (int trueAnomaly = 0; trueAnomaly < 360; trueAnomaly += 1)
{
_points.Add(o.WorldPositionAtUT(o.TimeOfTrueAnomaly(Deg2Rad(trueAnomaly), 0)));
}
_points.Add(_points[0]); //close the loop
}
else
{
//hyperbolic orbits:
for (int meanAnomaly = -1000; meanAnomaly <= 1000; meanAnomaly += 5)
{
_points.Add(o.WorldPositionAtUT(o.UTAtMeanAnomaly(meanAnomaly * UtilMath.Deg2Rad, 0)));
}
}
DrawPath(o.referenceBody, _points, c, true);
}
}
}