-
Notifications
You must be signed in to change notification settings - Fork 41
/
PicoGK_Viewer.cs
646 lines (545 loc) · 21.1 KB
/
PicoGK_Viewer.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//
// SPDX-License-Identifier: Apache-2.0
//
// PicoGK ("peacock") is a compact software kernel for computational geometry,
// specifically for use in Computational Engineering Models (CEM).
//
// For more information, please visit https://picogk.org
//
// PicoGK is developed and maintained by LEAP 71 - © 2023-2024 by LEAP 71
// https://leap71.com
//
// Computational Engineering will profoundly change our physical world in the
// years ahead. Thank you for being part of the journey.
//
// We have developed this library to be used widely, for both commercial and
// non-commercial projects alike. Therefore, we have released it under a
// permissive open-source license.
//
// The foundation of PicoGK is a thin layer on top of the powerful open-source
// OpenVDB project, which in turn uses many other Free and Open Source Software
// libraries. We are grateful to be able to stand on the shoulders of giants.
//
// LEAP 71 licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, THE SOFTWARE IS
// PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
using System.Diagnostics;
using System.IO.Compression;
using System.Numerics;
namespace PicoGK
{
public partial class Viewer
{
public Viewer( string strTitle,
Vector2 vecSize,
LogFile oLog)
{
m_oLog = oLog;
m_iMainThreadID = Environment.CurrentManagedThreadId;
m_oHandler.AddAction(new KeyAction(
new RotateToNextRoundAngleAction(
RotateToNextRoundAngleAction.EDir.Dir_Down),
EKeys.Key_Down));
m_oHandler.AddAction(new KeyAction(
new RotateToNextRoundAngleAction(
RotateToNextRoundAngleAction.EDir.Dir_Up),
EKeys.Key_Up));
m_oHandler.AddAction(new KeyAction(
new RotateToNextRoundAngleAction(
RotateToNextRoundAngleAction.EDir.Dir_Left),
EKeys.Key_Left));
m_oHandler.AddAction(new KeyAction(
new RotateToNextRoundAngleAction(
RotateToNextRoundAngleAction.EDir.Dir_Right),
EKeys.Key_Right));
AddKeyHandler(m_oHandler);
// Assign the delegate functions, to make sure the
// garbage collector doesn't destroy them during the
// lifetime of the objects
m_fnInfoCB = InfoCB;
m_fnUpdateCB = UpdateCB;
m_fnKeyPressedCB = KeyPressedCB;
m_fnMouseMovedCB = MouseMovedCB;
m_fnMouseButtonCB = MouseButtonCB;
m_fnScrollWheelCB = ScrollWheelCB;
m_fnWindowSizeCB = WindowSizeCB;
m_hThis = _hCreate( strTitle,
vecSize,
m_fnInfoCB,
m_fnUpdateCB,
m_fnKeyPressedCB,
m_fnMouseMovedCB,
m_fnMouseButtonCB,
m_fnScrollWheelCB,
m_fnWindowSizeCB);
Debug.Assert(m_hThis != IntPtr.Zero);
}
public bool bPoll()
{
Debug.Assert(m_iMainThreadID == Environment.CurrentManagedThreadId);
// Call this function only from the main() thread of the application
Thread.Yield();
bool bUpdateNeeded = false;
lock (m_oAnims)
{
if (m_oAnims.bPulse())
bUpdateNeeded = true;
}
lock (m_oActions)
{
// Set idle flag, if we did not encounter
// any actions for one entire poll period
if (m_oActions.Count() == 0)
m_bIdle = true;
while (m_oActions.Count > 0)
{
IViewerAction oAction = m_oActions.Dequeue();
oAction.Do(this);
bUpdateNeeded = true;
}
}
lock (m_oTLLock)
{
if (m_oTimeLapse != null)
{
if (m_oTimeLapse.bDue(out string strScreenShotPath))
{
_RequestScreenShot(m_hThis, strScreenShotPath);
bUpdateNeeded = true;
}
}
}
if (bUpdateNeeded)
_RequestUpdate(m_hThis);
Debug.Assert(_bIsValid(m_hThis));
return _bPoll(m_hThis);
}
public void RequestUpdate()
{
lock (m_oActions)
{
m_oActions.Enqueue(new RequestUpdateAction());
}
}
public void LoadLightSetup(string strFilePath)
{
using (ZipArchive oZip = ZipFile.OpenRead(strFilePath))
{
// Find the entry for the diffTexture
ZipArchiveEntry? oDiffuseEntry = oZip.GetEntry("Diffuse.dds");
if (oDiffuseEntry == null)
{
throw new FileNotFoundException("Diffuse.dds texture entry not found in the ZIP archive.");
}
// Find the entry for the specTexture
ZipArchiveEntry? oSpecularEntry = oZip.GetEntry("Specular.dds");
if (oSpecularEntry == null)
{
throw new FileNotFoundException("SpecTexture entry not found in the ZIP archive.");
}
byte[] abyDiffuseData;
using (Stream oDiffuseStream = oDiffuseEntry.Open())
{
using (MemoryStream oDiffuseMemStream = new MemoryStream())
{
oDiffuseStream.CopyTo(oDiffuseMemStream);
abyDiffuseData = oDiffuseMemStream.ToArray();
}
}
byte[] abySpecularData;
using (Stream oSpecularStream = oSpecularEntry.Open())
{
using (MemoryStream oSpecularMemStream = new MemoryStream())
{
oSpecularStream.CopyTo(oSpecularMemStream);
abySpecularData = oSpecularMemStream.ToArray();
}
}
lock (m_oActions)
{
m_oActions.Enqueue(new LoadLightSetupAction( m_oLog,
abyDiffuseData,
abySpecularData));
}
}
}
public void Add( in Voxels vox,
int nGroupID = 0)
{
lock (m_oActions)
{
m_oActions.Enqueue(new AddVoxelsAction(vox, nGroupID));
}
}
public void Remove(Voxels vox)
{
lock (m_oActions)
{
m_oActions.Enqueue(new RemoveVoxelsAction(vox));
}
}
public void Add( Mesh msh,
int nGroupID = 0)
{
lock (m_oActions)
{
m_oActions.Enqueue(new AddMeshAction(msh, nGroupID));
}
}
public void Remove(Mesh msh)
{
lock (m_oActions)
{
m_oActions.Enqueue(new RemoveMeshAction(msh));
}
}
public void Add( PolyLine oPoly,
int nGroupID = 0)
{
lock (m_oActions)
{
m_oActions.Enqueue(new AddPolyLineAction(oPoly, nGroupID));
}
}
public void Remove(PolyLine oPoly)
{
lock (m_oActions)
{
m_oActions.Enqueue(new RemovePolyLineAction(oPoly));
}
}
public void RemoveAllObjects()
{
lock (m_oActions)
{
m_oActions.Enqueue(new RemoveAllObjectsAction());
}
}
public void RequestScreenShot(string strScreenShotPath)
{
lock (m_oActions)
{
m_oActions.Enqueue(new RequestScreenShotAction(strScreenShotPath));
}
}
public void SetGroupVisible(int nGroupID,
bool bVisible)
{
lock (m_oActions)
{
m_oActions.Enqueue(new SetGroupVisibleAction(nGroupID, bVisible));
}
}
public void SetGroupStatic( int nGroupID,
bool bStatic)
{
lock (m_oActions)
{
m_oActions.Enqueue(new SetGroupStaticAction(nGroupID, bStatic));
}
}
public void SetGroupMaterial (int nGroupID,
ColorFloat clr,
float fMetallic,
float fRoughness)
{
lock (m_oActions)
{
m_oActions.Enqueue(new SetGroupMaterialAction( nGroupID,
clr,
fMetallic,
fRoughness));
}
}
public void SetGroupMatrix( int nGroupID,
Matrix4x4 mat)
{
lock (m_oActions)
{
m_oActions.Enqueue(new SetGroupMatrixAction( nGroupID,
mat));
}
}
public void SetBackgroundColor(ColorFloat clr)
{
m_clrBackground = clr;
RequestUpdate();
}
public void AdjustViewAngles( float fOrbitRelative,
float fElevationRelative)
{
SetViewAngles( m_fOrbit + fOrbitRelative,
m_fElevation + fElevationRelative);
}
public void SetViewAngles( float fOrbit,
float fElevation)
{
m_fElevation = fElevation;
if (m_fElevation > 180.0f)
fElevation = 90.0f;
else if (m_fElevation < 180.0f)
fElevation = -90.0f;
m_fOrbit = fOrbit;
while (m_fOrbit > 360.0f)
m_fOrbit -= 360.0f;
while (m_fOrbit < 0.0f)
m_fOrbit += 360.0f;
RequestUpdate();
}
public void SetFov(float fAngle)
{
m_fFov = fAngle;
RequestUpdate();
}
public void LogStatistics()
{
lock (m_oActions)
{
m_oActions.Enqueue(new LogStatisticsAction(m_oLog));
}
}
/// <summary>
/// Allows you to query if all viewer actions are complete
/// </summary>
/// <returns>t
/// true: No more viewer actions pending
/// false: Viewer actions still pending (busy)</returns>
public bool bIsIdle()
{
lock (m_oActions)
{
if (m_oActions.Count() > 0)
m_bIdle = false;
}
return m_bIdle;
}
bool m_bIdle = false;
public float m_fElevation = 30.0f;
public float m_fOrbit = 45.0f;
float m_fFov = 45.0f;
float m_fZoom = 1.0f;
bool m_bPerspective = true;
int m_iMainThreadID = -1;
ColorFloat m_clrBackground = new(0.3f);
List<Mesh> m_oMeshes = new();
List<PolyLine> m_oPolyLines = new();
Dictionary<Voxels, Mesh> m_oVoxels = new();
BBox3 m_oBBox = new();
void RecalculateBoundingBox()
{
Debug.Assert(m_iMainThreadID == Environment.CurrentManagedThreadId);
// Call this function only from the main() thread of the application
// start from fresh
m_oBBox = new();
lock (m_oMeshes)
{
foreach (Mesh msh in m_oMeshes)
{
m_oBBox.Include(msh.oBoundingBox());
}
}
lock (m_oPolyLines)
{
foreach (PolyLine poly in m_oPolyLines)
{
m_oBBox.Include(poly.oBoundingBox());
}
}
}
void DoAdd(Mesh msh, int nGroupID)
{
m_oBBox.Include(msh.oBoundingBox());
lock (m_oMeshes)
{
m_oMeshes.Add(msh);
}
_AddMesh( m_hThis,
nGroupID,
msh.m_hThis);
}
void DoRemove(Mesh msh)
{
Debug.Assert(m_iMainThreadID == Environment.CurrentManagedThreadId);
// Call this function only from the main() thread of the application
lock (m_oMeshes)
{
if (!m_oMeshes.Contains(msh))
throw new Exception("Tried to remove mesh that was never added");
m_oMeshes.Remove(msh);
_RemoveMesh(m_hThis,
msh.m_hThis);
}
RecalculateBoundingBox();
RequestUpdate();
}
void DoRemove(PolyLine poly)
{
Debug.Assert(m_iMainThreadID == Environment.CurrentManagedThreadId);
// Call this function only from the main() thread of the application
lock (m_oPolyLines)
{
if (!m_oPolyLines.Contains(poly))
throw new Exception("Tried to remove mesh that was never added");
m_oPolyLines.Remove(poly);
_RemovePolyLine( m_hThis,
poly.m_hThis);
}
RecalculateBoundingBox();
RequestUpdate();
}
Matrix4x4 m_matModelTrans = Matrix4x4.Identity;
Matrix4x4 m_matModelViewProjection = Matrix4x4.Identity;
Matrix4x4 m_matModelViewStatic = Matrix4x4.Identity;
Matrix4x4 m_matProjectionStatic = Matrix4x4.Identity;
Matrix4x4 m_matStatic = Matrix4x4.Identity;
Vector3 m_vecEye = new Vector3(1.0f);
Vector3 m_vecEyeStatic = new Vector3(0f, 10f, 0f);
Vector2 m_vecPrevPos = new();
bool m_bOrbit = false;
LogFile m_oLog;
///////// Internals
void InfoCB( string strMessage,
bool bFatalError)
{
m_oLog.Log(strMessage);
}
void UpdateCB( IntPtr hViewer,
in Vector2 vecViewport,
ref ColorFloat clrBackground,
ref Matrix4x4 matModelViewProjection,
ref Matrix4x4 matModelTransform,
ref Matrix4x4 matStatic,
ref Vector3 vecEyePosition,
ref Vector3 vecEyeStatic)
{
try
{
Debug.Assert(hViewer == m_hThis);
if (!m_oBBox.bIsEmpty())
{
Vector3 vecSceneCenter = m_oBBox.vecCenter();
double fR = ((m_oBBox.vecMax - vecSceneCenter).Length() * 3.0f) * m_fZoom;
double fRElev = Math.Cos((double)m_fElevation * Math.PI / 180.0f) * fR;
m_vecEye.X = (float)(Math.Cos((double)m_fOrbit * Math.PI / 180.0) * fRElev);
m_vecEye.Y = (float)(Math.Sin((double)m_fOrbit * Math.PI / 180.0) * fRElev);
m_vecEye.Z = (float)(Math.Sin((double)m_fElevation * Math.PI / 180.0) * fR);
float fFar = (vecSceneCenter - m_vecEye).Length() * 2.0f;
Matrix4x4 matModelView = Utils.matLookAt(m_vecEye, vecSceneCenter);
Matrix4x4 matProjection;
if (m_bPerspective)
{
matProjection = Matrix4x4.CreatePerspectiveFieldOfView(
(float)(m_fFov * Math.PI / 180.0),
vecViewport.X / vecViewport.Y,
0.1f,
fFar);
}
else
{
matProjection = Matrix4x4.CreateOrthographic( m_oBBox.vecSize().X * 2,
m_oBBox.vecSize().Y * 2,
0.1f,
fFar);
}
m_matModelViewStatic = Utils.matLookAt(m_vecEyeStatic, new Vector3(0, 0, 0));
m_matProjectionStatic = Matrix4x4.CreateOrthographic(100f * vecViewport.X / vecViewport.Y, 100f, 0.1f, 100f);
m_matModelViewProjection = matModelView * matProjection;
m_matStatic = m_matModelViewStatic * m_matProjectionStatic;
}
vecEyeStatic = m_vecEyeStatic;
vecEyePosition = m_vecEye;
matStatic = m_matStatic;
matModelViewProjection = m_matModelViewProjection;
matModelTransform = m_matModelTrans;
clrBackground = m_clrBackground;
}
catch (Exception e)
{
m_oLog.Log($"Caught exception in Viewer update callback:\n{e.ToString()}\n");
}
}
void KeyPressedCB(IntPtr hViewer,
int iKey,
int iScancode,
int iAction,
int iModifiers)
{
Debug.Assert(hViewer == m_hThis);
EKeys eKey = (EKeys)iKey;
if ((iAction == 0) || (iAction == 1)) // ignore 2, which is repeat, only down and up
{
foreach (IKeyHandler xHandler in m_oKeyHandlers)
{
if (xHandler.bHandleEvent(this,
eKey, iAction == 1, // Pressed
(iModifiers & 0x0001) != 0,
(iModifiers & 0x0002) != 0,
(iModifiers & 0x0004) != 0,
(iModifiers & 0x0008) != 0))
return; // Handled
}
}
}
void MouseMovedCB(IntPtr hViewer,
in Vector2 vecMousePos)
{
Debug.Assert(hViewer == m_hThis);
if (m_bOrbit)
{
Vector2 vecDist = vecMousePos - m_vecPrevPos;
AdjustViewAngles(-vecDist.X / 2.0f, vecDist.Y / 2);
m_vecPrevPos = vecMousePos;
lock (m_oAnims)
{
m_oAnims.Clear();
}
}
}
void MouseButtonCB(IntPtr hViewer,
int iButton,
int iAction,
int iModifiers,
in Vector2 vecMousePos)
{
Debug.Assert(hViewer == m_hThis);
if (iAction == 1)
{
m_bOrbit = true;
m_vecPrevPos = vecMousePos;
lock (m_oAnims)
{
m_oAnims.Clear();
}
}
else if (iAction == 0)
{
m_bOrbit = false;
}
}
void ScrollWheelCB( IntPtr hViewer,
in Vector2 vecScrollWheel,
in Vector2 vecMousePos)
{
Debug.Assert(hViewer == m_hThis);
m_fZoom -= vecScrollWheel.Y / 50f;
if (m_fZoom < 0.1f)
m_fZoom = 0.1f;
RequestUpdate();
}
void WindowSizeCB( IntPtr hViewer,
in Vector2 vecWindowSize)
{
Debug.Assert(hViewer == m_hThis);
RequestUpdate();
}
}
}