-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecondaryForm.cs
More file actions
285 lines (231 loc) · 9.23 KB
/
SecondaryForm.cs
File metadata and controls
285 lines (231 loc) · 9.23 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
using Khronos;
using OpenGL;
using System.Diagnostics;
using System.Text;
namespace Licenta_ver1
{
public partial class SecondaryForm : Form
{
//Renderer testBodyGL320;
SolarSystem solarSystem;
//float zoom = 1e10f;//2e10f;
//Vertex3f translate = new(0, 0, 0);
int simulation_speed = 1;
Vertex3f cameraPos = new(0, 0, 2e8f);
Vertex3f cameraTarget = new(0, 0, 0);
const float solarSystemRadius_m = 1e9f; // max distance (Pluto)
// idk what it it it works if the value is 1e6f to 1e9f
// maybe distance to pluto in mickey mouse
//private CelestialBodyCollection bodies = new();
float yaw = 0f; // degrees
float pitch = 0f; // degrees (clamped –90..+90)
float camerayaw = 0f;
float actualcamerayaw = 0.0f;
float actualcamerapitch = 0.0f;
bool isDragging = false;
Point lastMousePos;
public SecondaryForm()
{
InitializeComponent();
// OpenGL context is not created when this function is called;
// cannot do anything OpenGl here.
comboBox1.SelectedValue = "Sun"; // default selected planet
comboBox2.SelectedValue = "None"; // default second focus
}
/// <summary>
/// Update function called after rendering, before swapping buffers.
/// updatefunc
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RenderControl_ContextUpdate(object sender, GlControlEventArgs e)
{
/*if (!paused)
for (int i = 1; i < simulation_speed; i++)
bodies.UpdateForces(simulation_speed);//////*/
//Thread.Sleep(1);
//Thread.Sleep(1);/////
const float cameraSpeed = 0.37f; // speed of camera movement
// one iteration of smoothly approaching actual camera yaw to the target yaw
if (Math.Abs(actualcamerayaw - camerayaw) > 0.01f)
{
float deltaYaw = camerayaw - actualcamerayaw;
if (deltaYaw > 0)
{
actualcamerayaw += (float)Math.Log(deltaYaw + 1) * cameraSpeed; // logarithmic approach to avoid overshooting
}
else
{
actualcamerayaw -= (float)Math.Log(-deltaYaw + 1) * cameraSpeed; // logarithmic approach to avoid overshooting
}
}
else
{
actualcamerayaw = camerayaw; // snap to target
}
// same for pitch
if (Math.Abs(actualcamerapitch - pitch) > 0.01f)
{
float deltaPitch = pitch - actualcamerapitch;
if (deltaPitch > 0)
{
actualcamerapitch += (float)Math.Log(deltaPitch + 1) * cameraSpeed; // logarithmic approach to avoid overshooting
}
else
{
actualcamerapitch -= (float)Math.Log(-deltaPitch + 1) * cameraSpeed; // logarithmic approach to avoid overshooting
}
}
else
{
actualcamerapitch = pitch; // snap to target
}
for (int i = 0; i < simulation_speed; i++)
{
if (!paused)
solarSystem.Simulate(simulation_speed * 1e-3); // simulate step with speed factor
}
solarSystem.UpdateRenderer();
}
private void RenderControl_RenderGL320()
{
Gl.Enable(EnableCap.DepthTest);
Gl.DepthFunc(DepthFunction.Lequal); // default = Less
Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit | ClearBufferMask.DepthBufferBit);
solarSystem.Render(
Width, Height,
comboBox1.SelectedItem?.ToString() ?? "None",
comboBox2.SelectedItem?.ToString(),
actualcamerayaw, actualcamerapitch,
trackBar4.Value / 100f
);
}
private void RenderControl_ContextDestroying(object sender, GlControlEventArgs e) { }
private void RenderControl_Render(object sender, GlControlEventArgs e)
{
// Common GL commands
Control senderControl = (Control)sender;
Gl.Viewport(0, 0, senderControl.ClientSize.Width, senderControl.ClientSize.Height);
Gl.Clear(ClearBufferMask.ColorBufferBit);
RenderControl_RenderGL320();
}
/// <summary>
/// Allocate GL resources or GL states.
/// </summary>
/// <param name="sender">
/// The <see cref="object"/> that has rasied the event.
/// </param>
/// <param name="e">
/// The <see cref="GlControlEventArgs"/> that specifies the event arguments.
/// </param>
private void RenderControl_ContextCreated(object sender, GlControlEventArgs e)
{
GlControl glControl = (GlControl)sender;
// GL Debugging
if (Gl.CurrentExtensions != null && Gl.CurrentExtensions.DebugOutput_ARB)
{
Gl.DebugMessageCallback(GLDebugProc, IntPtr.Zero);
Gl.DebugMessageControl(DebugSource.DontCare, DebugType.DontCare, DebugSeverity.DontCare, 0, null, true);
}
RenderControl_CreateGL320();
// Uses multisampling, if available
if (Gl.CurrentVersion != null && Gl.CurrentVersion.Api == KhronosVersion.ApiGl && glControl.MultisampleBits > 0)
Gl.Enable(EnableCap.Multisample);
}
/// <summary>
/// initfunc
/// </summary>
private void RenderControl_CreateGL320()
{
RenderControl.MouseMove += OnMouseMove;
RenderControl.MouseDown += OnMouseDown;
RenderControl.MouseUp += OnMouseUp;
solarSystem = new SolarSystem("../../../JSON_Data/Planets.json");
Gl.Enable(EnableCap.DepthTest);
Gl.DepthMask(true);
Gl.DepthFunc(DepthFunction.Greater);
Gl.Disable(EnableCap.CullFace);
}
private void RenderControl_Load(object sender, EventArgs e)
{
}
private void trackBar3_Scroll(object sender, EventArgs e)
{
simulation_speed = trackBar3.Value;
}
private void trackBar4_Scroll(object sender, EventArgs e)
{
cameraPos.y = trackBar4.Value * 3e6f;
cameraPos.z = trackBar4.Value * 3e7f;
Debug.WriteLine($"Camera: {cameraPos}");
}
bool paused = false;
void OnMouseDown(object? s, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = true;
lastMousePos = e.Location;
this.Cursor = Cursors.SizeAll;
}
}
void OnMouseMove(object? s, MouseEventArgs e)
{
if (!isDragging) return;
var dx = e.X - lastMousePos.X; // +right
var dy = e.Y - lastMousePos.Y; // +down
const float sensitivity = 0.3f;
camerayaw += dx * sensitivity; // left = –yaw, right = +yaw
pitch += dy * sensitivity; // up = –pitch, down = +pitch
pitch = Math.Clamp(pitch, -89f, 89f); // avoid gimbal-flip
lastMousePos = e.Location;
Invalidate();
}
void OnMouseUp(object? s, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = false;
this.Cursor = Cursors.Default;
}
}
private void Button1_Click(object sender, EventArgs e)
{
paused = !paused;
if (paused)
{
button1.Text = "Resume";
}
else
{
button1.Text = "Pause";
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
camerayaw = 0f; // reset yaw when changing focus
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// do not change yaw here
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
solarSystem.SunRadiusScale = float.TryParse(textBox1.Text, out float scale) ? scale : 1.0e-7f;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
solarSystem.PlanetRadiusScale = float.TryParse(textBox2.Text, out float scale) ? scale : 1.0e-7f;
}
private static void GLDebugProc(DebugSource source, DebugType type, uint id, DebugSeverity severity, int length, IntPtr message, IntPtr userParam)
{
string strMessage;
// Decode message string
unsafe
{
strMessage = Encoding.ASCII.GetString((byte*)message.ToPointer(), length);
}
Console.WriteLine($"{source}, {type}, {severity}: {strMessage}");
}
}
}