forked from arookas/Demolisher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DemolisherForm.cs
500 lines (451 loc) · 12.7 KB
/
DemolisherForm.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
using Arookas.Collections;
using Arookas.Math;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using AQuaternion = Arookas.Math.Quaternion;
namespace Arookas.Demolisher
{
internal partial class DemolisherForm : Form
{
// state
Matrix4 CameraMatrix { get { return Matrix4.LookAt(camera.pos.X, camera.pos.Y, camera.pos.Z, camera.pos.X, camera.pos.Y, camera.pos.Z + 100.0f, 0.0f, 1.0f, 0.0f); } }
Viewpoint camera = new Viewpoint() { pos = Vector3D.UnitZ + new Vector3D(0.0f, 0.75f, 0.0f), rot = new Vector3D(30.0f, 180.0f, 0.0f) };
ADictionary<Keys, bool> input = new ADictionary<Keys, bool>();
List<Bin> loadedModels = new List<Bin>();
bool glLoaded = false;
Timer timer;
// settings
bool alphaTest = true;
bool showGrid = true;
bool invertLook = true;
bool showBoundingBoxes = false;
bool showCeilings = true;
bool showFourthWalls = true;
bool showNBT = false;
PolygonMode polygonMode = PolygonMode.Fill;
// util
bool HasModelsLoaded { get { return (loadedModels.Count > 0); } }
string TitleSuffix { get { return String.Format("Demolisher v{0} arookas", Program.Version); } }
public DemolisherForm()
{
InitializeComponent();
SetTitle();
timer = new Timer()
{
Interval = 28,
};
timer.Tick += timer_Tick;
timer.Start();
}
public void LoadModel(string fileName)
{
LoadModel(fileName, Vector3.Zero, Vector3.Zero, Vector3.One);
}
public void LoadModel(string fileName, Vector3 pos, Vector3 rot, Vector3 scale)
{
if (fileName.EndsWith(".bin", StringComparison.InvariantCultureIgnoreCase))
{
Bin bin = new Bin(fileName, pos, rot, scale);
TreeNode binNode = new TreeNode()
{
Checked = true,
Text = bin.Name,
Tag = bin,
};
LoadBINNode(binNode.Nodes, bin, 0);
tvw_models.BeginUpdate();
tvw_models.Nodes.Add(binNode);
tvw_models.EndUpdate();
gl_frame.Invalidate();
loadedModels.Add(bin);
}
SetTitle();
}
public void CloseModel(Bin bin)
{
bin.Dispose();
loadedModels.Remove(bin);
tvw_models.Nodes.Remove(tvw_models.Nodes.Cast<TreeNode>().First(node => node.Tag == bin));
if (tvw_models.Nodes.Count == 0)
{
tvw_models_AfterSelect(null, new TreeViewEventArgs(null));
}
gl_frame.Invalidate();
SetTitle();
}
public void CloseModels()
{
if (HasModelsLoaded)
{
loadedModels.ForEach(model => model.Dispose());
loadedModels.Clear();
SetTitle();
Invalidate();
}
}
void LoadBINNode(TreeNodeCollection nodes, Bin bin, int index)
{
for (int childIndex = bin[index].ChildIndex; childIndex >= 0; childIndex = bin[childIndex].NextIndex)
{
TreeNode node = new TreeNode()
{
Checked = true,
Text = String.Format("Object {0}", childIndex),
Tag = bin[childIndex],
};
if (bin[childIndex].ChildIndex >= 0)
{
LoadBINNode(node.Nodes, bin, bin[childIndex].ChildIndex);
}
nodes.Add(node);
}
}
public void SetTitle()
{
Text = TitleSuffix;
}
void InitViewport()
{
const float nearClip = 0.01f;
const float farClip = 1000.0f;
GL.Viewport(gl_frame.ClientRectangle.Size);
GL.MatrixMode(MatrixMode.Projection);
Matrix4 perspectiveMatrix = Matrix4.CreatePerspectiveFieldOfView(60.0f * (float)MathUtility.DegreesToRadians, (float)gl_frame.ClientRectangle.Width / gl_frame.ClientRectangle.Height, nearClip, farClip);
GL.LoadMatrix(ref perspectiveMatrix);
}
void Render()
{
// Refresh.
GL.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
// Enable crap.
GL.Enable(EnableCap.Blend);
GL.Enable(EnableCap.CullFace);
GL.Enable(EnableCap.DepthTest);
GL.Enable(EnableCap.LineSmooth);
GLUtility.EnableTexture2D();
GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.FrontFace(FrontFaceDirection.Cw);
GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest);
GL.LineWidth(1.0f);
GL.PolygonMode(MaterialFace.Front, polygonMode);
GL.ShadeModel(ShadingModel.Smooth);
// Set the viewpoint.
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Matrix4 cameraMatrix = CameraMatrix;
GL.Rotate(camera.rot.Z, Vector3.UnitZ);
GL.Rotate(camera.rot.X, Vector3.UnitX);
GL.Rotate(camera.rot.Y, Vector3.UnitY);
GL.Scale(1.0f, 1.0f, 1.0f);
GL.MultMatrix(ref cameraMatrix);
// Set up lighting.
GL.Enable(EnableCap.Lighting);
GL.Light(LightName.Light0, LightParameter.Ambient, new Color(127).ToColor4());
GL.Light(LightName.Light0, LightParameter.Position, new Vector4(camera.pos.ToVector3(), 1.0f));
GL.Enable(EnableCap.Light0);
// Render models (opaque first, then translucent).
if (HasModelsLoaded)
{
RenderFlags flags = RenderFlags.None;
if (showBoundingBoxes)
{
flags |= RenderFlags.BoundingBox;
}
if (showCeilings)
{
flags |= RenderFlags.Ceilings;
}
if (showFourthWalls)
{
flags |= RenderFlags.FourthWalls;
}
if (showNBT)
{
flags |= RenderFlags.NBT;
}
loadedModels.ForEach(model => model.Render(RenderFlags.Opaque | flags));
loadedModels.ForEach(model => model.Render(RenderFlags.Translucent | flags));
}
// Render grid.
if (showGrid)
{
RenderGrid(20);
}
// Swap the front and back buffers.
gl_frame.SwapBuffers();
}
void RenderGrid(int gridSize)
{
GL.Disable(EnableCap.Lighting);
GLUtility.DisableTexture2D();
GL.Begin(BeginMode.Lines);
for (int z = -gridSize; z <= gridSize; z++)
{
GL.Color3(Color.White);
GL.Vertex3(-gridSize, 0.0f, z);
GL.Vertex3(gridSize, 0.0f, z);
}
for (int x = -gridSize; x <= gridSize; x++)
{
GL.Color3(Color.White);
GL.Vertex3(x, 0.0f, -gridSize);
GL.Vertex3(x, 0.0f, gridSize);
}
GL.End();
GLUtility.EnableTexture2D();
GL.Enable(EnableCap.Lighting);
}
// timer
void timer_Tick(object sender, EventArgs e)
{
if (glLoaded)
{
const float moveSpeed = 0.05f;
const float runSpeed = 0.5f;
const float rotSpeed = 5.0f;
bool invalidate = false;
// Movement.
Vector3D pos = new Vector3D();
if (input[Keys.E])
{
pos += Vector3D.UnitZ; // forward
invalidate = true;
}
if (input[Keys.D])
{
pos -= Vector3D.UnitZ; // backward
invalidate = true;
}
if (input[Keys.S])
{
pos += Vector3D.UnitX; // left
invalidate = true;
}
if (input[Keys.F])
{
pos -= Vector3D.UnitX; // right
invalidate = true;
}
camera.pos += AQuaternion.FromEulerAngles(camera.rot * new Vector3D(1.0f, -1.0f, 1.0f) * (float)MathUtility.DegreesToRadians) * pos * (input[Keys.ShiftKey] ? runSpeed : moveSpeed);
// Look.
if (input[invertLook ? Keys.Down : Keys.Up]) // up
{
camera.rot -= Vector3D.UnitX * rotSpeed;
invalidate = true;
}
if (input[invertLook ? Keys.Up : Keys.Down]) // down
{
camera.rot += Vector3D.UnitX * rotSpeed;
invalidate = true;
}
if (input[Keys.Left]) // left
{
camera.rot -= Vector3D.UnitY * rotSpeed;
invalidate = true;
}
if (input[Keys.Right]) // right
{
camera.rot += Vector3D.UnitY * rotSpeed;
invalidate = true;
}
if (invalidate)
{
gl_frame.Invalidate();
}
}
}
// men_file
void btn_open_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog()
{
Title = "Open BIN model",
Filter = "BIN models (*.bin)|*.bin",
Multiselect = true,
})
{
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string model in openFileDialog.FileNames)
{
LoadModel(model);
}
}
}
}
// men_view
void btn_alphatest_CheckedChanged(object sender, EventArgs e)
{
alphaTest = btn_alphatest.Checked;
gl_frame.Invalidate();
}
void btn_bbox_CheckedChanged(object sender, EventArgs e)
{
showBoundingBoxes = btn_bbox.Checked;
gl_frame.Invalidate();
}
void btn_grid_CheckedChanged(object sender, EventArgs e)
{
showGrid = btn_grid.Checked;
gl_frame.Invalidate();
}
void btn_invertLook_Click(object sender, EventArgs e)
{
invertLook = btn_invertLook.Checked;
gl_frame.Invalidate();
}
void btn_wireframe_CheckedChanged(object sender, EventArgs e)
{
polygonMode = (btn_wireframe.Checked ? PolygonMode.Line : PolygonMode.Fill);
gl_frame.Invalidate();
}
void btn_ceilings_CheckedChanged(object sender, EventArgs e)
{
showCeilings = btn_ceilings.Checked;
gl_frame.Invalidate();
}
void btn_fourthwalls_CheckedChanged(object sender, EventArgs e)
{
showFourthWalls = btn_fourthwalls.Checked;
gl_frame.Invalidate();
}
void btn_nbt_CheckedChanged(object sender, EventArgs e)
{
showNBT = btn_nbt.Checked;
gl_frame.Invalidate();
}
// men_main
void btn_import_Click(object sender, EventArgs e)
{
btn_open_Click(sender, e);
}
void btn_unload_Click(object sender, EventArgs e)
{
CloseModel(tvw_models.SelectedNode.Tag as Bin);
}
// tvw_models
void tvw_models_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Node.Tag is IRenderable)
{
IRenderable renderable = (e.Node.Tag as IRenderable);
renderable.Visible = !renderable.Visible;
}
gl_frame.Invalidate();
}
// gl_frame
void gl_frame_DragDrop(object sender, DragEventArgs e)
{
foreach (string model in (e.Data.GetData(DataFormats.FileDrop) as string[]))
{
LoadModel(model);
}
}
void gl_frame_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
void gl_frame_KeyDown(object sender, KeyEventArgs e)
{
input[e.KeyCode] = true;
}
void gl_frame_KeyUp(object sender, KeyEventArgs e)
{
input[e.KeyCode] = false;
}
void gl_frame_Load(object sender, EventArgs e)
{
glLoaded = true;
Program.LoadShaders();
}
void gl_frame_Paint(object sender, PaintEventArgs e)
{
Render();
}
void gl_frame_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Up:
case Keys.Down:
case Keys.Left:
case Keys.Right:
case Keys.Shift | Keys.Up:
case Keys.Shift | Keys.Down:
case Keys.Shift | Keys.Left:
case Keys.Shift | Keys.Right:
e.IsInputKey = true;
break;
}
}
void gl_frame_Resize(object sender, EventArgs e)
{
InitViewport();
gl_frame.Invalidate();
}
private void tvw_models_AfterSelect(object sender, TreeViewEventArgs e)
{
btn_unload.Enabled = !(e.Node == null || !(e.Node.Tag is Bin));
btn_export.Enabled = (btn_unload.Enabled = (e.Node != null && e.Node.Tag is Bin));
btn_info.Enabled = (e.Node != null && (e.Node.Tag is Bin || e.Node.Tag is GraphObject));
}
private void btn_export_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog
{
Description = "Select a folder in which to export the OBJ and texture files.",
ShowNewFolderButton = true
})
{
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
using (OBJExportDialog objexportDialog = new OBJExportDialog())
{
if (objexportDialog.ShowDialog() == DialogResult.OK)
{
(tvw_models.SelectedNode.Tag as Bin).Export(folderBrowserDialog.SelectedPath, objexportDialog.ExportTextures, objexportDialog.TextureFormat, objexportDialog.ExportGeometry, objexportDialog.OnlyVisible, objexportDialog.IgnoreTransforms, objexportDialog.SwapU, objexportDialog.SwapV);
}
}
}
}
}
private void btn_info_Click(object sender, EventArgs e)
{
if (tvw_models.SelectedNode.Tag is Bin)
{
Bin binMdl = (tvw_models.SelectedNode.Tag as Bin);
MessageBox.Show($"Name: {binMdl.Name}\n"
+ $"Visible: {binMdl.Visible}\n"
+ $"Position: {binMdl.Position}\n"
+ $"Rotation: {binMdl.Rotation}\n"
+ $"Scale: {binMdl.Scale}",
$"Information of BIN model '{binMdl.Name}'", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
GraphObject gObj = (tvw_models.SelectedNode.Tag as GraphObject);
string rFlags = Enum.GetName(typeof(GraphObjectRenderFlags), gObj.RenderFlags);
MessageBox.Show($"Visible: {gObj.Visible}\n"
+ $"RenderFlags: {rFlags ?? "Unknown"}\n"
+ $"PartCount: {gObj.PartCount}\n"
+ $"BoundingBox: Min: {gObj.BoundingBox.Min}, Max: {gObj.BoundingBox.Max}\n"
+ $"Position: {gObj.Position}\n"
+ $"Rotation: {gObj.Rotation}\n"
+ $"Scale: {gObj.Scale}",
$"Information of BIN Graph{tvw_models.SelectedNode.Text}", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}