-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFluidSimulation.cs
1018 lines (908 loc) · 37.4 KB
/
FluidSimulation.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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Numerics;
using System.Diagnostics;
using FluidSim.Abstractions;
using FluidSim.Utilities;
using Silk.NET.Maths;
using Silk.NET.Windowing;
using Silk.NET.Input;
using ImGuiNET;
using System.Runtime.InteropServices;
#if OPENGLES
using Silk.NET.OpenGLES;
using Silk.NET.OpenGLES.Extensions.ImGui;
#else
using Silk.NET.OpenGL;
using Silk.NET.OpenGL.Extensions.ImGui;
#endif
namespace FluidSim;
class FluidSimulation
{
#region Simulation params
private int _simResolution;
private int _dyeResolution;
private int _bloomResolution;
private int _sunraysResolution;
private int _bloomIterations;
private float _bloomThreshold;
private float _bloomSoftKnee;
private float _bloomIntensity;
private float _sunraysWeight = 1.0f;
private int _pressureIterations;
private float _curl;
private float _pressure;
private float _velocityDissipation;
private float _densityDissipation;
private float _splatRadius;
private float _splatForce = 6000f;
private float _splatDx;
private float _splatDy;
private bool _enableBloom = true;
private bool _enableShading = true;
private bool _enableSunrays = true;
private bool _paused;
private bool _steppingMode = true;
private bool _showGui = true;
private bool _screenshotRequested = false;
private int _vizModeSelected = (int)VisualizationMode.Dye;
private Vector4 _splatColor = new(1, 1, 0, 1);
private float _fixedDt = 1.0f / 60f;
private readonly Stopwatch _simStopwatch = new();
private int _simResolutionIndex = SimResolutionValues.Length - 2;
private int _dyeResolutionIndex = 0;
private static readonly string[] DyeResolutionLabels = new string[] { "high (1024)", "medium (512)", "low (256)", "very low (128)" };
private static readonly int[] DyeResolutionValues = new int[] { 1024, 512, 256, 128 };
private static readonly string[] SimResolutionLabels = new string[] { "very low (32)", "low (64)", "medium (128)", "high (256)", "very high(512)", "ultra (1024)" };
private static readonly int[] SimResolutionValues = new int[] { 32, 64, 128, 256, 512, 1024 };
private string _screenshotFolder;
private readonly IList<Pointer> _pointers = new List<Pointer>
{
new Pointer {Id = 0}
};
#endregion
#region Windowing
private IWindow _window;
private IGLBufferFactory _bufferFactory;
private IInputContext _input;
private ImGuiController _controller;
private float _aspectRatio;
private float _actualWindowWidth;
private bool _resized;
#endregion
#region GL
private GL _gl;
private BufferObject<float> _vbo;
private BufferObject<ushort> _ebo;
private VertexArrayObject<float, ushort> _vao;
private readonly static float[] Vertices = new float[] { -1, -1, -1, 1, 1, 1, 1, -1 };
private readonly static ushort[] Indices = new ushort[] { 0, 1, 2, 0, 2, 3 };
private bool _linearFiltering = false;
private bool _halfFloat = true;
private readonly static InternalFormat Rgba32f = InternalFormat.Rgba32f;
private readonly static InternalFormat Rg32f = InternalFormat.RG32f;
private readonly static InternalFormat R32f = InternalFormat.R32f;
private readonly static InternalFormat Rgba16f = InternalFormat.Rgba16f;
private readonly static InternalFormat Rg16f = InternalFormat.RG16f;
private readonly static InternalFormat R16f = InternalFormat.R16f;
#endregion
#region Framebuffers
private FrameBufferObject _curlBuff;
private FrameBufferObject _divergenceBuff;
private FrameBufferObject _bloom;
private FrameBufferObject _sunrays;
private FrameBufferObject _sunraysTemp;
private DoubleFrameBuffer _velocityBuff;
private DoubleFrameBuffer _pressureBuff;
private DoubleFrameBuffer _dyeBuff;
private IDictionary<string, ShaderProgram> Shaders = new Dictionary<string, ShaderProgram>();
private IList<FrameBufferObject> _bloomFramebuffers = new List<FrameBufferObject>();
private Abstractions.Texture _ditheringTexture;
private Vector2D<float> _ditherScale;
#endregion
#region GUI
private bool _guiCollapsed = false;
private Vector2 _guiPanelSize;
private readonly static string[] VizModes = Enum.GetNames<VisualizationMode>();
// to flip texture drawed with ImgGui
private readonly static Vector2 FboPrevieSize = new(64, 64);
private readonly static Vector2 FboPreviewUv0 = new(0, 1);
private readonly static Vector2 FboPreviewUv1 = new(1, 0);
private readonly static Vector4 FboPreviewBorderColor = new(1, 0, 0, 1);
private readonly static Vector4 GuiSectionTitleColor = new Vector4(0.86f, 0.6f, 0f, 1);
#endregion
private float CorrectDeltaX(float delta)
{
if (_aspectRatio < 1) return delta * _aspectRatio;
return delta;
}
private float CorrectDeltaY(float delta)
{
if (_aspectRatio > 1) return delta / _aspectRatio;
return delta;
}
private record Pointer
{
public int Id { get; init; }
public float TexCoordX { get; set; }
public float TexCoordY { get; set; }
public float PrevTexCoordX { get; set; }
public float PrevTexCoordY { get; set; }
public float DeltaX { get; set; }
public float DeltaY { get; set; }
public bool IsDown { get; set; }
public bool Moved { get; set; }
public (float r, float g, float b) Color { get; set; }
}
public FluidSimulation(SimulationConfiguration config)
{
_paused = config.Paused;
_steppingMode = config.Stepping;
_pressure = config.Pressure;
_pressureIterations = config.PressureIterations;
_curl = config.Curl;
_velocityDissipation = config.VelocityDissipation;
_densityDissipation = config.DensityDissipation;
_splatDx = config.DefaultSplatDx;
_splatDy = config.DefaultSplatDy;
_splatRadius = config.SplatRadius;
_splatForce = 6000f;
_simResolution = config.SimResolution;
_dyeResolution = config.DyeResolution;
_vizModeSelected = (int)config.ActiveVisualizationMode;
_halfFloat = config.HalfFloat;
_linearFiltering = config.LinearFiltering;
_bloomIterations = config.BloomIterations;
_bloomResolution = config.BloomResolution;
_bloomThreshold = config.BloomThreshold;
_bloomSoftKnee = config.BloomSoftKnee;
_bloomIntensity = config.BloomIntensity;
_enableBloom = config.Bloom;
_enableSunrays = config.Sunrays;
_sunraysResolution = config.SunraysResolution;
_sunraysWeight = config.SunraysWeight;
_screenshotFolder = string.IsNullOrEmpty(config.ScreenshotsFolder)
? Path.GetDirectoryName(Environment.ProcessPath)
: config.ScreenshotsFolder;
var options = WindowOptions.Default;
options.Size = new Vector2D<int>(config.WindowWidth, config.WindowHeight);
options.Title = "Fluidsim";
#if OPENGLES
options.API = new GraphicsAPI(
ContextAPI.OpenGLES,
ContextProfile.Core,
ContextFlags.Default,
new APIVersion(3, 0));
#endif
_window = Window.Create(options);
_guiPanelSize = new Vector2(400, config.WindowHeight);
_actualWindowWidth = _window.Size.X;// - _guiPanelSize.X;
_aspectRatio = _actualWindowWidth / _window.Size.Y;
_window.Load += OnLoad;
_window.Render += OnRender;
_window.Closing += OnClosing;
_window.Resize += OnResize;
}
private void OnResize(Vector2D<int> newSize)
{
_actualWindowWidth = newSize.X/* - _guiPanelSize.X*/;
_aspectRatio = _actualWindowWidth / newSize.Y;
_guiPanelSize.Y = newSize.Y;
_resized = true;
}
private void InitDrawingBuffers()
{
_vbo = _bufferFactory.CreateArrayBuffer(Vertices.AsSpan());
_ebo = _bufferFactory.CreateElementArrayBuffer(Indices.AsSpan());
_vao = _bufferFactory.CreateVertexArrayObject(_vbo, _ebo);
_vao.VertexAttributePointer(0, 2, VertexAttribPointerType.Float, 2, 0);
}
public void Run()
{
_window.Run();
}
private void InitGLContext()
{
#if OPENGLES
_gl = _window.CreateOpenGLES();
#else
_gl = _window.CreateOpenGL();
#endif
_bufferFactory = new GLBufferFactory(_gl);
_controller = new ImGuiController(
_gl,
_window,
_input
);
}
private (uint, uint) GetFboSizeFromResolution(float resolution)
{
var width = _window.FramebufferSize.X - _guiPanelSize.X;
var height = _window.FramebufferSize.Y;
var aspectRatio = width / height;
if (aspectRatio < 1.0f)
{
aspectRatio = 1.0f / aspectRatio;
}
var min = (uint)Math.Round(resolution);
var max = (uint)Math.Round(resolution * aspectRatio);
return width > height ? (max, min) : (min, max);
}
private void InitFrameBuffers()
{
var (dyeX, dyeY) = GetFboSizeFromResolution(_dyeResolution);
var (x, y) = GetFboSizeFromResolution(_simResolution);
var filtering = _linearFiltering ? GLEnum.Linear : GLEnum.Nearest;
var rgba = _halfFloat ? Rgba16f : Rgba32f;
var rg = _halfFloat ? Rg16f : Rg32f;
var r = _halfFloat ? R16f : R32f;
var type = _halfFloat ? GLEnum.HalfFloat : GLEnum.Float;
_dyeBuff = _bufferFactory.CreateDoubleFrameBuffer(dyeX, dyeY, rgba, PixelFormat.Rgba, type, filtering);
_velocityBuff = _bufferFactory.CreateDoubleFrameBuffer(x, y, rg, PixelFormat.RG, type, filtering);
_curlBuff = _bufferFactory.CreateFrameBuffer(x, y, r, PixelFormat.Red, type, GLEnum.Nearest);
_divergenceBuff = _bufferFactory.CreateFrameBuffer(x, y, r, PixelFormat.Red, type, GLEnum.Nearest);
_pressureBuff = _bufferFactory.CreateDoubleFrameBuffer(x, y, r, PixelFormat.Red, type, GLEnum.Nearest);
_ditheringTexture = new Abstractions.Texture(
_gl,
"Resources\\LDR_LLL1_0.png",
InternalFormat.Rgba,
PixelFormat.Rgba,
PixelType.UnsignedByte);
_ditherScale = new Vector2D<float>(
_actualWindowWidth / (float) _ditheringTexture.Width,
_window.FramebufferSize.Y / (float) _ditheringTexture.Height
);
InitBloomFrameBuffers();
InitSunraysFrameBuffers();
}
private void InitBloomFrameBuffers()
{
Console.WriteLine("BLOOM_FILTERS");
var (w, h) = GetFboSizeFromResolution(_bloomResolution);
var filtering = _linearFiltering ? GLEnum.Linear : GLEnum.Nearest;
var rgba = _halfFloat ? Rgba16f : Rgba32f;
var type = _halfFloat ? GLEnum.HalfFloat : GLEnum.Float;
_bloom = _bufferFactory.CreateFrameBuffer(w, h, rgba, PixelFormat.Rgba, type, filtering);
_bloomFramebuffers.Clear();
for (var i = 0; i < _bloomIterations; i++)
{
var width = w >> (i + 1);
var height = h >> (i + 1);
if (width < 2 || height < 2) break;
var fbo = _bufferFactory.CreateFrameBuffer(width, height, rgba, PixelFormat.Rgba, type, filtering);
_bloomFramebuffers.Add(fbo);
}
}
private void InitSunraysFrameBuffers()
{
var (w, h) = GetFboSizeFromResolution(_sunraysResolution);
var filtering = _linearFiltering ? GLEnum.Linear : GLEnum.Nearest;
var r = _halfFloat ? R16f : R32f;
var type = _halfFloat ? GLEnum.HalfFloat : GLEnum.Float;
_sunrays = _bufferFactory.CreateFrameBuffer(w, h, r, PixelFormat.Red, type, filtering);
_sunraysTemp = _bufferFactory.CreateFrameBuffer(w, h, r, PixelFormat.Red, type, filtering);
}
private void ResizeFrameBuffers()
{
var (dyeX, dyeY) = GetFboSizeFromResolution(_dyeResolution);
var (x, y) = GetFboSizeFromResolution(_simResolution);
_velocityBuff.Resize(x, y);
_dyeBuff.Resize(dyeX, dyeY);
_curlBuff.Resize(x, y);
_divergenceBuff.Resize(x, y);
_pressureBuff.Resize(x, y);
_bloom.Dispose();
foreach(var bloomFbo in _bloomFramebuffers)
{
bloomFbo.Dispose();
}
InitBloomFrameBuffers();
_sunrays.Dispose();
_sunraysTemp.Dispose();
InitSunraysFrameBuffers();
}
private void ApplySunrays()
{
var source = _dyeBuff.Read;
var mask = _dyeBuff.Write;
var destination = _sunrays;
_gl.Disable(EnableCap.Blend);
var sunraysMaskProgram = Shaders["sunrays_mask"];
sunraysMaskProgram.Bind();
sunraysMaskProgram.SetUniform("uTexture", source.Attach(TextureUnit.Texture0));
Blit(mask);
var sunraysProgram = Shaders["sunrays"];
sunraysProgram.Bind();
sunraysProgram.SetUniform("weight", _sunraysWeight);
sunraysProgram.SetUniform("uTexture", mask.Attach(TextureUnit.Texture0));
Blit(destination);
}
private void ApplyBloom()
{
var source = _dyeBuff.Read;
var destination = _bloom;
if (_bloomFramebuffers.Count < 2) return;
var last = destination;
_gl.Disable(EnableCap.Blend);
var bloomPrefilterProgram = Shaders["blur_prefilter"];
bloomPrefilterProgram.Bind();
var knee = _bloomThreshold * _bloomSoftKnee + 0.0001;
var curve0 = _bloomThreshold - knee;
var curve1 = knee * 2;
var curve2 = 0.25 / knee;
bloomPrefilterProgram.SetUniform("curve", curve0, curve1, curve2);
bloomPrefilterProgram.SetUniform("threshold", _bloomThreshold);
bloomPrefilterProgram.SetUniform("uTexture", source.Attach(TextureUnit.Texture0));
Blit(last);
var bloomBlurProgram = Shaders["bloom_blur"];
bloomBlurProgram.Bind();
for (var i = 0; i < _bloomFramebuffers.Count; i++)
{
var dest = _bloomFramebuffers[i];
bloomBlurProgram.SetUniform("texelSize", last.TexelSizeX, last.TexelSizeY);
bloomBlurProgram.SetUniform("uTexture", last.Attach(TextureUnit.Texture0));
Blit(dest);
last = dest;
}
_gl.BlendFunc(BlendingFactor.One, BlendingFactor.One);
_gl.Enable(EnableCap.Blend);
for (var i = _bloomFramebuffers.Count - 2; i >= 0; i--)
{
var baseTex = _bloomFramebuffers[i];
bloomBlurProgram.SetUniform("texelSize", last.TexelSizeX, last.TexelSizeY);
bloomBlurProgram.SetUniform("uTexture", last.Attach(TextureUnit.Texture0));
Blit(baseTex);
last = baseTex;
}
_gl.Disable(EnableCap.Blend);
var bloomFinalProgram = Shaders["bloom_final"];
bloomFinalProgram.Bind();
bloomFinalProgram.SetUniform("texelSize", last.TexelSizeX, last.TexelSizeY);
bloomFinalProgram.SetUniform("uTexture", last.Attach(TextureUnit.Texture0));
bloomFinalProgram.SetUniform("intensity", _bloomIntensity);
Blit(destination);
}
private void Blur(FrameBufferObject target, FrameBufferObject temp, int iterations)
{
var blurProgram = Shaders["blur"];
blurProgram.Bind();
for (var i = 0; i < iterations; i++)
{
blurProgram.SetUniform("texelSize", target.TexelSizeX, 0.0);
blurProgram.SetUniform("uTexture", target.Attach(TextureUnit.Texture0));
Blit(temp);
blurProgram.SetUniform("texelSize", 0.0, target.TexelSizeY);
blurProgram.SetUniform("uTexture", temp.Attach(TextureUnit.Texture0));
Blit(target);
}
}
private unsafe void CaptureWindowFramebuffer()
{
var (w, h) = (_window.FramebufferSize.X, _window.FramebufferSize.Y);
var data = NativeMemory.Alloc((nuint)(w * h * 3));
try
{
var span = new Span<byte>(data, (int)(w * h * 3));
_gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, 0);
_gl.PixelStore(PixelStoreParameter.PackAlignment, 1);
_gl.ReadPixels(0, 0, (uint)w, (uint)h, GLEnum.Rgb, GLEnum.UnsignedByte, span);
var mem = new Memory<byte>(span.ToArray());
var img = Image.WrapMemory<Rgb24>(mem, (int)w, (int)h);
img.Mutate(x => x.Flip(FlipMode.Vertical));
var path = Path.Join(
_screenshotFolder,
"capture_" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".png");
img.SaveAsPng(path);
}
finally
{
NativeMemory.Free(data);
}
}
private unsafe void FrameBufferToImage(FrameBufferObject fbo)
{
var (w, h) = (fbo.Width, fbo.Height);
var data = NativeMemory.Alloc((nuint)w * h * 3);
try
{
using var capture = _bufferFactory.CreateFrameBuffer(w, h, InternalFormat.Rgb, PixelFormat.Rgb, GLEnum.UnsignedByte, GLEnum.Nearest);
fbo.BlitTo(capture);
var span = new Span<byte>(data, (int)(w * h * 3));
_gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, capture.Handle);
_gl.PixelStore(PixelStoreParameter.PackAlignment, 1);
_gl.ReadPixels(0, 0, (uint)w, (uint)h, GLEnum.Rgb, GLEnum.UnsignedByte, span);
var mem = new Memory<byte>(span.ToArray());
var img = Image.WrapMemory<Rgb24>(mem, (int)w, (int)h);
img.Mutate(x => x.Flip(FlipMode.Vertical));
var path = Path.Join(
_screenshotFolder,
"capture_" + DateTime.Now.ToString("yyyyMMddTHHmmss") + ".png");
img.SaveAsPng(path);
}
finally
{
NativeMemory.Free(data);
}
}
private IEnumerable<string> GetShaderDefines()
{
var defines = new List<string>();
if (_enableBloom) defines.Add("BLOOM");
if (_enableShading) defines.Add("SHADING");
if (_enableSunrays) defines.Add("SUNRAYS");
return defines;
}
private void InitShaders()
{
var baseVertex = Path.Join("Shaders", "base.vert");
var blurVertex = Path.Join("Shaders", "blur.vert");
foreach (var frag in Directory.EnumerateFiles("Shaders"))
{
if (frag.EndsWith(".vert")) continue;
var name = Path.GetFileNameWithoutExtension(frag);
var vertext = name == "blur" ? blurVertex : baseVertex;
Console.WriteLine($"COMPILING: {frag} {vertext}");
IEnumerable<string>? defines = name == "display" ? GetShaderDefines() : null;
var program = new ShaderProgram(_gl, vertext, frag, defines);
Shaders.Add(name, program);
}
if (_linearFiltering == false)
{
Shaders["advection"] = Shaders["advection_manual_filtering"];
}
Console.WriteLine($"Loaded {Shaders.Count} shader programs.");
}
private Pointer GetPointerFromMouse(IMouse mouse)
{
var pointer = _pointers.FirstOrDefault(p => p.Id == mouse.Index);
if (pointer == null)
{
pointer = new Pointer() { Id = mouse.Index };
_pointers.Add(pointer);
}
return pointer;
}
private bool IsOverGui(Vector2 position)
{
if (position.X < (_window.Size.X - _guiPanelSize.X)) return false;
return _guiCollapsed == false;
}
private void InitInputs()
{
_input = _window.CreateInput();
foreach(var mouse in _input.Mice)
{
mouse.MouseDown += (mouse, _) =>
{
var pos = mouse.Position;
if (IsOverGui(pos)) return;
var p = GetPointerFromMouse(mouse);
p.IsDown = true;
p.Moved = false;
p.TexCoordX = mouse.Position.X / _actualWindowWidth;
p.TexCoordY = 1.0f - mouse.Position.Y / _window.Size.Y;
p.PrevTexCoordX = p.TexCoordX;
p.PrevTexCoordY = p.TexCoordY;
p.DeltaX = 0;
p.DeltaY = 0;
p.Color = Random.Shared.NextColorRgb();
};
mouse.MouseMove += (mouse, _) =>
{
var pos = mouse.Position;
var p = GetPointerFromMouse(mouse);
if (p.IsDown == false) return;
p.PrevTexCoordX = p.TexCoordX;
p.PrevTexCoordY = p.TexCoordY;
p.TexCoordX = mouse.Position.X / _actualWindowWidth;
p.TexCoordY = 1.0f - mouse.Position.Y / _window.Size.Y;
p.DeltaX = CorrectDeltaX(p.TexCoordX - p.PrevTexCoordX);
p.DeltaY = CorrectDeltaY(p.TexCoordY - p.PrevTexCoordY);
p.Moved = Math.Abs(p.DeltaX) > 0 || Math.Abs(p.DeltaY) > 0;
};
mouse.MouseUp += (mouse, _) =>
{
var pos = mouse.Position;
var p = GetPointerFromMouse(mouse);
p.IsDown = false;
};
}
_input.Keyboards.First().KeyDown += (_, k, _) =>
{
switch (k)
{
case Key.Number1:
_vizModeSelected = (int)VisualizationMode.Velocity;
break;
case Key.Number2:
_vizModeSelected = (int)VisualizationMode.Curl;
break;
case Key.Number3:
_vizModeSelected = (int)VisualizationMode.Divergence;
break;
case Key.Number4:
_vizModeSelected = (int)VisualizationMode.Pressure;
break;
case Key.Number5:
_vizModeSelected = (int)VisualizationMode.Dye;
break;
case Key.Space:
Splat(0.5f, 0.5f,
_splatDx,
_splatDy,
_splatColor.X, _splatColor.Y, _splatColor.Z,
_splatRadius);
break;
case Key.P:
_paused = !_paused;
break;
case Key.S:
_steppingMode = !_steppingMode;
break;
case Key.I:
_showGui = !_showGui;
break;
}
};
}
private unsafe void Blit(FrameBufferObject? target)
{
if (target == null)
{
_gl.Viewport(0, 0, (uint)_actualWindowWidth, (uint)_window.FramebufferSize.Y);
_gl.BindFramebuffer(GLEnum.Framebuffer, 0);
_gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f);
_gl.Clear(ClearBufferMask.ColorBufferBit);
}
else
{
_gl.Viewport(0, 0, target.Width, target.Height);
_gl.BindFramebuffer(GLEnum.Framebuffer, target.Handle);
}
_gl.DrawElements(PrimitiveType.Triangles, (uint)Indices.Length, DrawElementsType.UnsignedShort, null);
}
private double CorrectRadius(double radius)
{
if (_aspectRatio > 1)
{
return radius * _aspectRatio;
}
return radius;
}
private void RandomSplats(uint numSplats)
{
for (var i = 0; i < numSplats; i++)
{
var (r, g, b) = Random.Shared.NextColorRgb();
var x = (float)Random.Shared.NextDouble();
var y = (float)Random.Shared.NextDouble();
var dx = 1000 * (float)(Random.Shared.NextDouble() - 0.5);
var dy = 1000 * (float)(Random.Shared.NextDouble() - 0.5);
var splatRadius = Math.Min(Math.Max(0.01f, (float)Random.Shared.NextDouble()), 1f);
Splat(x, y, dx, dy, r, g, b, splatRadius);
}
}
private void Splat(float x, float y, float dx, float dy, float r, float g, float b, float radius = 0.25f)
{
var splatProgram = Shaders["splat"];
splatProgram.Bind();
splatProgram.SetUniform("uTarget", _velocityBuff.Read.Attach(TextureUnit.Texture0));
splatProgram.SetUniform("aspectRatio", _aspectRatio);
splatProgram.SetUniform("point", x, y);
splatProgram.SetUniform("color", dx, dy, 1.0f);
splatProgram.SetUniform("radius", CorrectRadius(radius / 100.0));
Blit(_velocityBuff.Write);
_velocityBuff.Swap();
_dyeBuff.Read.Attach(0);
splatProgram.SetUniform("uTarget", 0);
splatProgram.SetUniform("color", r, g, b);
Blit(_dyeBuff.Write);
_dyeBuff.Swap();
}
private void Splat(Pointer pointer)
{
var (r, g, b) = pointer.Color;
var x = pointer.TexCoordX;
var y = pointer.TexCoordY;
var (dx, dy) =
(
pointer.DeltaX * _splatForce,
pointer.DeltaY * _splatForce
);
Splat(x, y, dx, dy, r, g, b, _splatRadius);
}
private void OnLoad()
{
InitInputs();
InitGLContext();
InitShaders();
InitFrameBuffers();
InitDrawingBuffers();
}
private void RenderGui()
{
ImGui.SetWindowSize(_guiPanelSize, ImGuiCond.Always);
ImGui.SetWindowPos(pos: new Vector2(_window.Size.X - _guiPanelSize.X, 0), ImGuiCond.Always);
ImGui.SetNextWindowSizeConstraints(_guiPanelSize, _guiPanelSize);
static void SectionText(string text)
{
ImGui.Spacing();
ImGui.PushStyleColor(ImGuiCol.Text, GuiSectionTitleColor);
ImGui.Text(text);
ImGui.PopStyleColor();
ImGui.Separator();
}
static void TextSameLine(params string[] text)
{
foreach (var t in text)
{
ImGui.TextDisabled(t);
ImGui.SameLine(0, -1);
}
}
SectionText("Parameters");
ImGui.SliderFloat("vorticity", ref _curl, 0, 50);
ImGui.SliderFloat("dens. diffusion", ref _densityDissipation, 0, 4f);
ImGui.SliderFloat("vel. diffusion", ref _velocityDissipation, 0, 4f);
ImGui.SliderFloat("pressure", ref _pressure, 0, 1f);
ImGui.SliderInt("press. iters.", ref _pressureIterations, 20, 120);
if(ImGui.Combo("sim. resolution.", ref _simResolutionIndex, SimResolutionLabels, SimResolutionLabels.Length))
{
var newSimResolution = SimResolutionValues[_simResolutionIndex];
if (newSimResolution != _simResolution)
{
_simResolution = newSimResolution;
_resized = true;
}
}
if (ImGui.Combo("dye. resolution.", ref _dyeResolutionIndex, DyeResolutionLabels, DyeResolutionLabels.Length))
{
var newDyeResolution = DyeResolutionValues[_dyeResolutionIndex];
if (newDyeResolution != _dyeResolution)
{
_dyeResolution = newDyeResolution;
_resized = true;
}
}
SectionText("Simulation");
float framerate = ImGui.GetIO().Framerate;
ImGui.Text($"Application average {1000.0f / framerate:0.##} ms/frame ({framerate:0.#} FPS)");
var simElapsedMs = (float)_simStopwatch.Elapsed.TotalMilliseconds;
ImGui.Text($"Simulation average {simElapsedMs:0.##} ms/frame");
ImGui.Checkbox("paused", ref _paused);
ImGui.SameLine(0, -1);
ImGui.Checkbox("stepping", ref _steppingMode);
if (_steppingMode)
{
ImGui.SameLine(0, -1);
if (ImGui.Button("step"))
{
_paused = false;
}
}
SectionText("Interaction");
ImGui.SliderFloat("splat radius", ref _splatRadius, 0.01f, 1f);
ImGui.SliderFloat("splat dx", ref _splatDx, 0.0f, 10000f);
ImGui.SliderFloat("splat dy", ref _splatDy, 0.0f, 10000f);
ImGui.ColorEdit4("splat color", ref _splatColor);
if (ImGui.Button("splat"))
{
Splat(0.25f, 0.5f,
_splatDx, _splatDy,
_splatColor.X, _splatColor.Y, _splatColor.Z, _splatRadius);
}
ImGui.SameLine(0, -1);
if (ImGui.Button("random splats"))
{
RandomSplats(10);
}
SectionText("Display");
ImGui.Combo("viz. mode", ref _vizModeSelected, VizModes, VizModes.Length);
if (ImGui.Checkbox("shading", ref _enableShading))
{
Shaders["display"].UpdateDefines(GetShaderDefines());
}
if (ImGui.Checkbox("bloom", ref _enableBloom))
{
Shaders["display"].UpdateDefines(GetShaderDefines());
}
ImGui.SliderFloat("bloom intensity", ref _bloomIntensity, 0.1f, 2.0f);
ImGui.SliderFloat("bloom threshold", ref _bloomThreshold, 0.0f, 0.1f);
if (ImGui.Checkbox("sunrays", ref _enableSunrays))
{
Shaders["display"].UpdateDefines(GetShaderDefines());
}
ImGui.SliderFloat("sunrays weight", ref _sunraysWeight, 0.3f, 1.0f);
SectionText("FBOs");
TextSameLine(" vel ", " curl", " div ", " pre ", " dye");
ImGui.Spacing();
ImGui.Image((nint)_velocityBuff.Read.TextureHandle, FboPrevieSize, FboPreviewUv0, FboPreviewUv1);
ImGui.SameLine(0, -1);
ImGui.Image((nint)_curlBuff.TextureHandle, FboPrevieSize, FboPreviewUv0, FboPreviewUv1);
ImGui.SameLine(0, -1);
ImGui.Image((nint)_divergenceBuff.TextureHandle, FboPrevieSize, FboPreviewUv0, FboPreviewUv1);
ImGui.SameLine(0, -1);
ImGui.Image((nint)_pressureBuff.Read.TextureHandle, FboPrevieSize, FboPreviewUv0, FboPreviewUv1);
ImGui.SameLine(0, -1);
ImGui.Image((nint)_dyeBuff.Read.TextureHandle, FboPrevieSize, FboPreviewUv0, FboPreviewUv1, Vector4.One, FboPreviewBorderColor);
ImGui.SameLine(0, -1);
SectionText("Captures");
ImGui.InputText(string.Empty, ref _screenshotFolder, 260);
if (ImGui.Button("take screenshot"))
{
_screenshotRequested = true;
}
_guiCollapsed = ImGui.IsWindowCollapsed();
}
private void OnRender(double dt)
{
if (_showGui)
{
_controller.Update((float)dt);
}
if (_resized)
{
_resized = false;
ResizeFrameBuffers();
}
if (_paused == false)
{
_simStopwatch.Restart();
_gl.Disable(EnableCap.Blend);
for(var i = 0; i < _pointers.Count; i++)
{
var p = _pointers[i];
if (p.Moved)
{
p.Moved = false;
Splat(p);
}
}
var curlProgram = Shaders["curl"];
curlProgram.Bind();
curlProgram.SetUniform("texelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
curlProgram.SetUniform("uVelocity", _velocityBuff.Read.Attach(TextureUnit.Texture0));
Blit(_curlBuff);
var vorticityProgram = Shaders["vorticity"];
vorticityProgram.Bind();
vorticityProgram.SetUniform("texelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
vorticityProgram.SetUniform("uVelocity", _velocityBuff.Read.Attach(TextureUnit.Texture0));
vorticityProgram.SetUniform("uCurl", _curlBuff.Attach(TextureUnit.Texture1));
vorticityProgram.SetUniform("curl", _curl);
vorticityProgram.SetUniform("dt", _fixedDt);
Blit(_velocityBuff.Write);
_velocityBuff.Swap();
var divergenceProgram = Shaders["divergence"];
divergenceProgram.Bind();
divergenceProgram.SetUniform("texelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
divergenceProgram.SetUniform("uVelocity", _velocityBuff.Read.Attach(TextureUnit.Texture0));
Blit(_divergenceBuff);
var clearProgram = Shaders["clear"];
clearProgram.Bind();
clearProgram.SetUniform("uTexture", _pressureBuff.Read.Attach(TextureUnit.Texture0));
clearProgram.SetUniform("value", _pressure); // pressure
Blit(_pressureBuff.Write);
_pressureBuff.Swap();
var pressureProgram = Shaders["pressure"];
pressureProgram.Bind();
pressureProgram.SetUniform("texelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
pressureProgram.SetUniform("uDivergence", _divergenceBuff.Attach(TextureUnit.Texture0));
for (var i = 0; i < _pressureIterations; i++)
{
pressureProgram.SetUniform("uPressure", _pressureBuff.Read.Attach(TextureUnit.Texture1));
Blit(_pressureBuff.Write);
_pressureBuff.Swap();
}
var gradientSubtractProgram = Shaders["gradient_subtract"];
gradientSubtractProgram.Bind();
gradientSubtractProgram.SetUniform("texelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
gradientSubtractProgram.SetUniform("uPressure", _pressureBuff.Read.Attach(TextureUnit.Texture0));
gradientSubtractProgram.SetUniform("uVelocity", _velocityBuff.Read.Attach(TextureUnit.Texture1));
Blit(_velocityBuff.Write);
_velocityBuff.Swap();
var advectionProgram = Shaders["advection"];
advectionProgram.Bind();
advectionProgram.SetUniform("texelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
if (_linearFiltering == false)
{
advectionProgram.SetUniform("dyeTexelSize", _velocityBuff.TexelSizeX, _velocityBuff.TexelSizeY);
}
var velocityId = _velocityBuff.Read.Attach(TextureUnit.Texture0);
advectionProgram.SetUniform("uVelocity", velocityId);
advectionProgram.SetUniform("uSource", velocityId);
advectionProgram.SetUniform("dt", _fixedDt);
advectionProgram.SetUniform("dissipation", _velocityDissipation);
Blit(_velocityBuff.Write);
_velocityBuff.Swap();
if (_linearFiltering == false)
{
advectionProgram.SetUniform("dyeTexelSize", _dyeBuff.TexelSizeX, _dyeBuff.TexelSizeY);
}
advectionProgram.SetUniform("uVelocity", _velocityBuff.Read.Attach(TextureUnit.Texture0));
advectionProgram.SetUniform("uSource", _dyeBuff.Read.Attach(TextureUnit.Texture1));
advectionProgram.SetUniform("dissipation", _densityDissipation);
Blit(_dyeBuff.Write);
_dyeBuff.Swap();
_simStopwatch.Stop();
}
if (_steppingMode)
{
_paused = true;
}
var simStatus = _paused ? "PAUSED" : "RUNNING";
var vizMode = (VisualizationMode)_vizModeSelected;
_window.Title = $"status={simStatus} mode={vizMode} stepping={_steppingMode}";
switch (vizMode)
{
case VisualizationMode.Velocity:
_velocityBuff.Read.BlitToScreen(_window.FramebufferSize); break;
case VisualizationMode.Curl:
_curlBuff.BlitToScreen(_window.FramebufferSize); break;
case VisualizationMode.Divergence:
_divergenceBuff.BlitToScreen(_window.FramebufferSize); break;
case VisualizationMode.Pressure:
_pressureBuff.Read.BlitToScreen(_window.FramebufferSize); break;
case VisualizationMode.Dye:
_dyeBuff.Read.BlitToScreen(new Vector2D<int>((int)_actualWindowWidth, _window.Size.Y));
break;
case VisualizationMode.Fancy:
if (_enableBloom)
{
ApplyBloom();
}
if (_enableSunrays)
{
ApplySunrays();
Blur(_sunrays, _sunraysTemp, 1);
}
_gl.BlendFunc(BlendingFactor.One, BlendingFactor.OneMinusSrcAlpha);
_gl.Enable(EnableCap.Blend);
var displayProgram = Shaders["display"];
displayProgram.Bind();
var (width, height) = ((uint)_actualWindowWidth, (uint)_window.FramebufferSize.Y);
if (_enableShading)
{
displayProgram.SetUniform("texelSize", 1.0f / width, 1.0f / height);
}
displayProgram.SetUniform("uTexture", _dyeBuff.Read.Attach(TextureUnit.Texture0));
if (_enableBloom)
{
displayProgram.SetUniform("uBloom", _bloom.Attach(TextureUnit.Texture1));
displayProgram.SetUniform("uDithering", _ditheringTexture.Attach(TextureUnit.Texture2));
displayProgram.SetUniform("ditherScale", _ditherScale.X, _ditherScale.Y);
}
if (_enableSunrays)
{
displayProgram.SetUniform("uSunrays", _sunrays.Attach(TextureUnit.Texture3));
}
Blit(null);
break;
default:
break;
}
if (_screenshotRequested)
{
// FrameBufferToImage(_dyeBuff.Read);
CaptureWindowFramebuffer();
_screenshotRequested = false;
}
if (_showGui)
{
RenderGui();
_gl.UseProgram(0);
_gl.Viewport(0, 0, (uint)_window.Size.X, (uint)_window.Size.Y);