├── Screenshots
├── img1.png
├── img2.png
├── img3.png
└── Samples_68510.png
├── .editorconfig
├── OpenTK-PathTracer
├── res
│ ├── textures
│ │ └── EnvironmentMap
│ │ │ ├── negX.png
│ │ │ ├── negY.png
│ │ │ ├── negZ.png
│ │ │ ├── posX.png
│ │ │ ├── posY.png
│ │ │ └── posZ.png
│ ├── imgui.ini
│ └── shaders
│ │ ├── final.glsl
│ │ ├── screenQuad.glsl
│ │ ├── PostProcessing
│ │ └── fragment.glsl
│ │ ├── AtmosphericScattering
│ │ ├── computeMy.glsl
│ │ └── compute.glsl
│ │ └── PathTracing
│ │ ├── fragCompute.glsl
│ │ └── compute.glsl
├── Properties
│ └── launchSettings.json
├── src
│ ├── GameObjects
│ │ ├── BaseGameObject.cs
│ │ ├── Sphere.cs
│ │ └── Cuboid.cs
│ ├── BaseSTD140Compatible.cs
│ ├── Ray.cs
│ ├── Program.cs
│ ├── KeyboardManager.cs
│ ├── Render
│ │ ├── ScreenEffect.cs
│ │ ├── Objects
│ │ │ ├── VAO.cs
│ │ │ ├── TimerQuery.cs
│ │ │ ├── Framebuffer.cs
│ │ │ ├── BufferObject.cs
│ │ │ ├── ShaderProgram.cs
│ │ │ └── Texture.cs
│ │ ├── PathTracer.cs
│ │ ├── AtmosphericScatterer.cs
│ │ └── Gui.cs
│ ├── MouseManager.cs
│ ├── Material.cs
│ ├── Camera.cs
│ ├── Helper.cs
│ ├── ImGui
│ │ └── ImGuiController.cs
│ └── MainWindow.cs
└── OpenTK-PathTracer.csproj
├── OpenTK-PathTracer.sln
├── README.md
├── .gitattributes
└── .gitignore
/Screenshots/img1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/Screenshots/img1.png
--------------------------------------------------------------------------------
/Screenshots/img2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/Screenshots/img2.png
--------------------------------------------------------------------------------
/Screenshots/img3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/Screenshots/img3.png
--------------------------------------------------------------------------------
/Screenshots/Samples_68510.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/Screenshots/Samples_68510.png
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | [*.cs]
2 |
3 | # IDE0090: Use 'new(...)'
4 | csharp_style_implicit_object_creation_when_type_is_apparent = false
5 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/textures/EnvironmentMap/negX.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/OpenTK-PathTracer/res/textures/EnvironmentMap/negX.png
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/textures/EnvironmentMap/negY.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/OpenTK-PathTracer/res/textures/EnvironmentMap/negY.png
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/textures/EnvironmentMap/negZ.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/OpenTK-PathTracer/res/textures/EnvironmentMap/negZ.png
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/textures/EnvironmentMap/posX.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/OpenTK-PathTracer/res/textures/EnvironmentMap/posX.png
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/textures/EnvironmentMap/posY.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/OpenTK-PathTracer/res/textures/EnvironmentMap/posY.png
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/textures/EnvironmentMap/posZ.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/BoyBaykiller/OpenTK-PathTracer/HEAD/OpenTK-PathTracer/res/textures/EnvironmentMap/posZ.png
--------------------------------------------------------------------------------
/OpenTK-PathTracer/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "OpenTK-PathTracer": {
4 | "commandName": "Project",
5 | "workingDirectory": "$(ProjectDir)"
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/imgui.ini:
--------------------------------------------------------------------------------
1 | [Window][Debug##Default]
2 | Pos=411,77
3 | Size=400,400
4 | Collapsed=0
5 |
6 | [Window][Overview]
7 | Pos=0,-1
8 | Size=389,330
9 | Collapsed=0
10 |
11 | [Window][GameObjectProperties]
12 | Pos=494,21
13 | Size=354,255
14 | Collapsed=1
15 |
16 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/final.glsl:
--------------------------------------------------------------------------------
1 | #version 450 core
2 | layout(location = 0) out vec4 FragColor;
3 |
4 | layout(binding = 0) uniform sampler2D SamplerTexture;
5 |
6 | in InOutVars
7 | {
8 | vec2 TexCoord;
9 | } inData;
10 |
11 | void main()
12 | {
13 | FragColor = texture(SamplerTexture, inData.TexCoord);
14 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/GameObjects/BaseGameObject.cs:
--------------------------------------------------------------------------------
1 | using OpenTK;
2 |
3 | namespace OpenTK_PathTracer.GameObjects
4 | {
5 | abstract class BaseGameObject : BaseSTD140Compatible
6 | {
7 | public Material Material;
8 | public Vector3 Position;
9 |
10 | public abstract bool IntersectsRay(Ray ray, out float t1, out float t2);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/BaseSTD140Compatible.cs:
--------------------------------------------------------------------------------
1 | using OpenTK;
2 | using OpenTK_PathTracer.Render.Objects;
3 |
4 | namespace OpenTK_PathTracer
5 | {
6 | abstract class BaseSTD140Compatible
7 | {
8 | public abstract int BufferOffset { get; }
9 |
10 | public abstract Vector4[] GetGPUFriendlyData();
11 |
12 | public void Upload(BufferObject buffer)
13 | {
14 | Vector4[] data = GetGPUFriendlyData();
15 | buffer.SubData(BufferOffset, Vector4.SizeInBytes * data.Length, data);
16 | }
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/screenQuad.glsl:
--------------------------------------------------------------------------------
1 | #version 450 core
2 |
3 | const vec4 data[6] = vec4[]
4 | (
5 | vec4( -1.0, 1.0, 0.0, 1.0 ),
6 | vec4( -1.0, -1.0, 0.0, 0.0 ),
7 | vec4( 1.0, -1.0, 1.0, 0.0 ),
8 | vec4( -1.0, 1.0, 0.0, 1.0 ),
9 | vec4( 1.0, -1.0, 1.0, 0.0 ),
10 | vec4( 1.0, 1.0, 1.0, 1.0 )
11 | );
12 |
13 | out InOutVars
14 | {
15 | vec2 TexCoord;
16 | } outData;
17 |
18 | void main()
19 | {
20 | vec4 vertex = data[gl_VertexID];
21 |
22 | gl_Position = vec4(vertex.xy, 0.0, 1.0);
23 | outData.TexCoord = vertex.zw;
24 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Ray.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using OpenTK;
3 |
4 | namespace OpenTK_PathTracer
5 | {
6 | struct Ray
7 | {
8 | public Vector3 Origin, Direction;
9 |
10 | public Vector3 GetPoint(float deltaTime)
11 | {
12 | return Origin + Direction * deltaTime;
13 | }
14 |
15 | public static Ray GetWorldSpaceRay(Matrix4 inverseProjection, Matrix4 inverseView, Vector3 worldPosition, Vector2 normalizedDeviceCoords)
16 | {
17 | Vector4 rayEye = new Vector4(normalizedDeviceCoords.X, normalizedDeviceCoords.Y, -1.0f, 1.0f) * inverseProjection; rayEye.Z = -1.0f; rayEye.W = 0.0f; // vector * matrix, because OpenTK...
18 | return new Ray { Origin = worldPosition, Direction = (rayEye * inverseView).Xyz.Normalized() };
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 |
4 | namespace OpenTK_PathTracer
5 | {
6 | class Program
7 | {
8 | static void Main()
9 | {
10 | try
11 | {
12 | using MainWindow mainWindow = new MainWindow();
13 | mainWindow.Run(Math.Min(OpenTK.DisplayDevice.Default.RefreshRate, 144));
14 | }
15 | catch (Exception ex)
16 | {
17 | StackFrame frame = new StackTrace(ex, true).GetFrame(0);
18 | Console.WriteLine("\n====== Exception ======");
19 | Console.WriteLine($"Type: {ex.GetType().Name}");
20 | Console.WriteLine($"Filename: {System.IO.Path.GetFileName(frame.GetFileName())}");
21 | Console.WriteLine($"Line: {frame.GetFileLineNumber()}");
22 | Console.WriteLine($"Message: {ex.Message}");
23 | Console.WriteLine("===== Enter to exit =====");
24 | Console.ReadLine();
25 | }
26 | }
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/KeyboardManager.cs:
--------------------------------------------------------------------------------
1 | using OpenTK.Input;
2 |
3 | namespace OpenTK_PathTracer
4 | {
5 | static class KeyboardManager
6 | {
7 | private static KeyboardState lastKeyboardState;
8 | private static KeyboardState thisKeyboardState;
9 | public static void Update()
10 | {
11 | lastKeyboardState = thisKeyboardState;
12 | thisKeyboardState = Keyboard.GetState();
13 | }
14 |
15 | ///
16 | ///
17 | ///
18 | /// True if key is down this update but not last one
19 | public static bool IsKeyTouched(Key key) => thisKeyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key);
20 |
21 | ///
22 | ///
23 | ///
24 | /// True if key is down
25 | public static bool IsKeyDown(Key key) => thisKeyboardState.IsKeyDown(key);
26 |
27 | ///
28 | ///
29 | ///
30 | /// True if key is up this update but not last one
31 | public static bool IsKeyUp(Key key) => thisKeyboardState.IsKeyUp(key) && lastKeyboardState.IsKeyDown(key);
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/PostProcessing/fragment.glsl:
--------------------------------------------------------------------------------
1 | #version 450 core
2 | layout(location = 0) out vec4 FragColor;
3 |
4 | layout(binding = 0) uniform sampler2D Sampler0;
5 | layout(binding = 1) uniform sampler2D Sampler1;
6 | layout(binding = 2) uniform sampler2D Sampler2;
7 |
8 | vec3 LinearToInverseGamma(vec3 rgb, float gamma);
9 | vec3 ACESFilm(vec3 x);
10 |
11 |
12 | in InOutVars
13 | {
14 | vec2 TexCoord;
15 | } inData;
16 |
17 | void main()
18 | {
19 | vec3 color = texture(Sampler0, inData.TexCoord).rgb;
20 | color += texture(Sampler1, inData.TexCoord).rgb;
21 | //color += texture(Sampler2, inData.TexCoord).rgb;
22 |
23 | color = ACESFilm(color);
24 | color = LinearToInverseGamma(color, 2.4);
25 | FragColor = vec4(color, 1.0);
26 | }
27 |
28 | vec3 LinearToInverseGamma(vec3 rgb, float gamma)
29 | {
30 | //rgb = clamp(rgb, 0.0, 1.0);
31 | return mix(pow(rgb, vec3(1.0 / gamma)) * 1.055 - 0.055, rgb * 12.92, vec3(lessThan(rgb, 0.0031308.xxx)));
32 | }
33 |
34 | // ACES tone mapping curve fit to go from HDR to LDR
35 | // https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
36 | vec3 ACESFilm(vec3 x)
37 | {
38 | float a = 2.51;
39 | float b = 0.03;
40 | float c = 2.43;
41 | float d = 0.59;
42 | float e = 0.14;
43 | return clamp((x * (a * x + b)) / (x * (c * x + d) + e), 0.0, 1.0);
44 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.0.32014.148
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenTK-PathTracer", "OpenTK-PathTracer\OpenTK-PathTracer.csproj", "{A84EF8C7-EA68-4C27-A669-CF765BA3A6C0}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7EC3E980-F7A8-4276-92BE-5A4A9E71E3AD}"
9 | ProjectSection(SolutionItems) = preProject
10 | .editorconfig = .editorconfig
11 | EndProjectSection
12 | EndProject
13 | Global
14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
15 | Debug|x64 = Debug|x64
16 | Release|x64 = Release|x64
17 | EndGlobalSection
18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
19 | {A84EF8C7-EA68-4C27-A669-CF765BA3A6C0}.Debug|x64.ActiveCfg = Debug|x64
20 | {A84EF8C7-EA68-4C27-A669-CF765BA3A6C0}.Debug|x64.Build.0 = Debug|x64
21 | {A84EF8C7-EA68-4C27-A669-CF765BA3A6C0}.Release|x64.ActiveCfg = Release|x64
22 | {A84EF8C7-EA68-4C27-A669-CF765BA3A6C0}.Release|x64.Build.0 = Release|x64
23 | EndGlobalSection
24 | GlobalSection(SolutionProperties) = preSolution
25 | HideSolutionNode = FALSE
26 | EndGlobalSection
27 | GlobalSection(ExtensibilityGlobals) = postSolution
28 | SolutionGuid = {A29A2783-CEA4-4196-B5F5-95BC170097CA}
29 | EndGlobalSection
30 | EndGlobal
31 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/OpenTK-PathTracer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net5.0
6 | OpenTK_PathTracer
7 | AnyCPU;x64
8 | Always
9 | OpenTK-PathTracer
10 |
11 |
12 |
13 |
14 |
15 | true
16 |
17 |
18 |
19 | true
20 |
21 |
22 |
23 | true
24 |
25 |
26 |
27 | true
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/ScreenEffect.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using OpenTK.Graphics.OpenGL4;
3 | using OpenTK_PathTracer.Render.Objects;
4 |
5 | namespace OpenTK_PathTracer.Render
6 | {
7 | class ScreenEffect
8 | {
9 | private readonly Framebuffer framebuffer;
10 | public readonly Texture Result;
11 | private readonly ShaderProgram shaderProgram;
12 | private static readonly Shader vertexShader = new Shader(ShaderType.VertexShader, File.ReadAllText("res/shaders/screenQuad.glsl"));
13 | public ScreenEffect(Shader fragmentShader, int width, int height)
14 | {
15 | if (fragmentShader.ShaderType != ShaderType.FragmentShader)
16 | throw new System.ArgumentException($"Only pass in shaders of type {ShaderType.FragmentShader}");
17 |
18 | framebuffer = new Framebuffer();
19 |
20 | Result = new Texture(TextureTarget2d.Texture2D);
21 | Result.SetFilter(TextureMinFilter.Nearest, TextureMagFilter.Nearest);
22 | Result.MutableAllocate(width, height, 1, PixelInternalFormat.Rgba8);
23 |
24 | framebuffer.AddRenderTarget(FramebufferAttachment.ColorAttachment0, Result);
25 |
26 | shaderProgram = new ShaderProgram(vertexShader, fragmentShader);
27 | }
28 |
29 | public void Render(params Texture[] textures)
30 | {
31 | framebuffer.Bind();
32 | shaderProgram.Use();
33 |
34 | for (int i = 0; i < textures.Length; i++)
35 | textures[i].AttachSampler(i);
36 | GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
37 | }
38 |
39 | public void SetSize(int width, int height)
40 | {
41 | Result.MutableAllocate(width, height, 1, Result.PixelInternalFormat);
42 | }
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/GameObjects/Sphere.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using OpenTK;
3 |
4 | namespace OpenTK_PathTracer.GameObjects
5 | {
6 | class Sphere : BaseGameObject
7 | {
8 | public const int GPU_INSTANCE_SIZE = 16 + Material.GPU_INSTANCE_SIZE;
9 |
10 | public int Instance;
11 | public float Radius;
12 | public Sphere(Vector3 position, float radius, int instance, Material material)
13 | {
14 | Position = position;
15 | Radius = radius;
16 | Material = material;
17 | Instance = instance;
18 | }
19 |
20 | public override int BufferOffset => 0 + Instance * GPU_INSTANCE_SIZE;
21 |
22 | private readonly Vector4[] gpuData = new Vector4[GPU_INSTANCE_SIZE / Vector4.SizeInBytes];
23 | public override Vector4[] GetGPUFriendlyData()
24 | {
25 | gpuData[0].Xyz = Position;
26 | gpuData[0].W = Radius;
27 |
28 | Array.Copy(Material.GetGPUFriendlyData(), 0, gpuData, 1, gpuData.Length - 1);
29 |
30 | return gpuData;
31 | }
32 |
33 | // Source: https://antongerdelan.net/opengl/raycasting.html
34 | public override bool IntersectsRay(Ray ray, out float t1, out float t2)
35 | {
36 | t1 = t2 = 0;
37 |
38 | Vector3 sphereToRay = ray.Origin - this.Position;
39 | float b = Vector3.Dot(ray.Direction, sphereToRay);
40 | float c = Vector3.Dot(sphereToRay, sphereToRay) - this.Radius * this.Radius;
41 | float discriminant = b * b - c;
42 | if (discriminant < 0)
43 | return false; // only imaginary collision
44 |
45 | float squareRoot = MathF.Sqrt(discriminant);
46 | t1 = -b - squareRoot;
47 | t2 = -b + squareRoot;
48 |
49 | return true;
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # C# OpenGL OpenTK Path Tracer
2 |
3 | I am presenting a noisy, yet fully [Path Traced](https://de.wikipedia.org/wiki/Path_Tracing) renderer written in C#.
4 |
5 | The calculations and rendering are done in real time using OpenGL.
6 | I upload the whole Scene to a UBO which is then accessed in a Compute Shader where all the Path Tracing happens.
7 | Due to the realistic nature of Path Tracers various effects like Soft Shadows, Reflections or Ambient Occlusion emerge automatically without explicitly adding code for any of these effects like you would have to do in a traditional rasterizer.
8 |
9 | The renderer also features [Depth of Field](https://en.wikipedia.org/wiki/Depth_of_field), which can be controlled with two variables at runtime through [ImGui](https://github.com/mellinoe/ImGui.NET).
10 | `FocalLength` is the distance an object appears in focus.
11 | `ApertureDiameter` controlls how strongly objects out of focus are blured.
12 |
13 | If a ray does not hit any object the color is retrieved from a cubemap which can either be 6 images inside the `Res` folder or a precomputed skybox.
14 | The atmospheric scattering in this skybox gets calculated in yet an other Compute Shader at startup.
15 |
16 | Screenshots taken via the screenshot feature are saved in the local execution folder `Screenshots`.
17 |
18 | Also see https://youtu.be/XcIToi0fh5c.
19 |
20 | ---
21 |
22 | ## **Controls**
23 |
24 | ### **KeyBoard:**
25 | * W, A, S, D => Movment
26 | * E => Toggle cursor visibility
27 | * R => Reset scene
28 | * V => Toggle VSync
29 | * F11 => Toggle fullscreen
30 | * LShift => Faster movment speed
31 | * LControl => Slower movment speed
32 | * Esc => Close
33 |
34 | ### **Mouse:**
35 | * LButton => Select object if cursor is visible
36 |
37 | ---
38 |
39 | ## **Render Samples**
40 |
41 | 
42 |
43 | 
44 |
45 | 
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Objects/VAO.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using OpenTK.Graphics.OpenGL4;
3 |
4 | namespace OpenTK_PathTracer.Render.Objects
5 | {
6 | class VAO : IDisposable
7 | {
8 | private static int lastBindedID = -1;
9 |
10 | public readonly int ID;
11 | public VAO()
12 | {
13 | GL.CreateVertexArrays(1, out ID);
14 | }
15 |
16 | public VAO(BufferObject elementArrayBuffer)
17 | {
18 | GL.CreateVertexArrays(1, out ID);
19 | GL.VertexArrayElementBuffer(ID, elementArrayBuffer.ID);
20 | }
21 |
22 | public void AddSourceBuffer(BufferObject sourceBuffer, int bindingIndex, int vertexSize, int bufferOffset = 0)
23 | {
24 | GL.VertexArrayVertexBuffer(ID, bindingIndex, sourceBuffer.ID, (IntPtr)bufferOffset, vertexSize);
25 | }
26 |
27 | public void SetAttribFormat(int bindingIndex, int index, int attribTypeElements, VertexAttribType vertexAttribType, int offset, bool normalize = false, int divisor = 0)
28 | {
29 | GL.EnableVertexArrayAttrib(ID, index);
30 | GL.VertexArrayAttribFormat(ID, index, attribTypeElements, vertexAttribType, normalize, offset);
31 | GL.VertexArrayAttribBinding(ID, index, bindingIndex);
32 | GL.VertexArrayBindingDivisor(ID, bindingIndex, divisor);
33 | }
34 |
35 | public void Bind()
36 | {
37 | if (lastBindedID != ID)
38 | {
39 | GL.BindVertexArray(ID);
40 | lastBindedID = ID;
41 | }
42 | }
43 |
44 | public static void Bind(int id)
45 | {
46 | if (lastBindedID != id)
47 | {
48 | GL.BindVertexArray(id);
49 | lastBindedID = id;
50 | }
51 | }
52 |
53 | public void Dispose()
54 | {
55 | GL.DeleteVertexArray(ID);
56 | }
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Objects/TimerQuery.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Diagnostics;
3 | using OpenTK.Graphics.OpenGL4;
4 |
5 | namespace OpenTK_PathTracer.Render.Objects
6 | {
7 | class TimerQuery : IDisposable
8 | {
9 | public float ElapsedMilliseconds { get; private set; }
10 |
11 | private readonly Stopwatch timer = new Stopwatch();
12 | private bool doStopAndReset = false;
13 |
14 | public readonly int ID;
15 | public int UpdateRate;
16 | public TimerQuery(int updatePeriodInMs)
17 | {
18 | GL.CreateQueries(QueryTarget.TimeElapsed, 1, out ID);
19 | UpdateRate = updatePeriodInMs;
20 | }
21 |
22 |
23 | ///
24 | /// If milliseconds are elapsed since the last , a new one on will be issued, which measures all render commands from now until .
25 | ///
26 | public void Start()
27 | {
28 | if (!timer.IsRunning || timer.ElapsedMilliseconds >= UpdateRate)
29 | {
30 | GL.BeginQuery(QueryTarget.TimeElapsed, ID);
31 | doStopAndReset = true;
32 | timer.Restart();
33 | }
34 | }
35 |
36 | ///
37 | /// Resets the and stores the result in
38 | ///
39 | public void StopAndReset()
40 | {
41 | if (doStopAndReset)
42 | {
43 | GL.EndQuery(QueryTarget.TimeElapsed);
44 | GL.GetQueryObject(ID, GetQueryObjectParam.QueryResult, out int elapsedTime);
45 | ElapsedMilliseconds = elapsedTime / 1000000f;
46 | doStopAndReset = false;
47 | }
48 | }
49 |
50 | public void Dispose()
51 | {
52 | GL.DeleteQuery(ID);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/GameObjects/Cuboid.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using OpenTK;
3 |
4 | namespace OpenTK_PathTracer.GameObjects
5 | {
6 | class Cuboid : BaseGameObject
7 | {
8 | public const int GPU_INSTANCE_SIZE = 16 * 2 + Material.GPU_INSTANCE_SIZE;
9 |
10 | public int Instance;
11 | public Vector3 Dimensions;
12 | public Cuboid(Vector3 position, Vector3 dimensions, int instance, Material material)
13 | {
14 | Position = position;
15 | Dimensions = dimensions;
16 | Material = material;
17 | Instance = instance;
18 | }
19 |
20 |
21 | public override int BufferOffset => Sphere.GPU_INSTANCE_SIZE * MainWindow.MAX_GAMEOBJECTS_SPHERES + Instance * GPU_INSTANCE_SIZE;
22 |
23 | public Vector3 Min => Position - Dimensions * 0.5f;
24 | public Vector3 Max => Position + Dimensions * 0.5f;
25 |
26 | private readonly Vector4[] gpuData = new Vector4[GPU_INSTANCE_SIZE / Vector4.SizeInBytes];
27 | public override Vector4[] GetGPUFriendlyData()
28 | {
29 | gpuData[0].Xyz = Min;
30 | gpuData[1].Xyz = Max;
31 |
32 | Array.Copy(Material.GetGPUFriendlyData(), 0, gpuData, 2, gpuData.Length - 2);
33 |
34 | return gpuData;
35 | }
36 |
37 | // Source: https://medium.com/@bromanz/another-view-on-the-classic-ray-aabb-intersection-algorithm-for-bvh-traversal-41125138b525
38 | public override bool IntersectsRay(Ray ray, out float t1, out float t2)
39 | {
40 | t1 = float.MinValue;
41 | t2 = float.MaxValue;
42 |
43 | Vector3 t0s = Vector3.Divide((this.Min - ray.Origin), ray.Direction);
44 | Vector3 t1s = Vector3.Divide((this.Max - ray.Origin), ray.Direction);
45 |
46 | Vector3 tsmaller = Vector3.ComponentMin(t0s, t1s);
47 | Vector3 tbigger = Vector3.ComponentMax(t0s, t1s);
48 |
49 | t1 = Math.Max(t1, Math.Max(tsmaller.X, Math.Max(tsmaller.Y, tsmaller.Z)));
50 | t2 = Math.Min(t2, Math.Min(tbigger.X, Math.Min(tbigger.Y, tbigger.Z)));
51 | return t1 <= t2;
52 | }
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/MouseManager.cs:
--------------------------------------------------------------------------------
1 | using OpenTK;
2 | using OpenTK.Input;
3 |
4 | namespace OpenTK_PathTracer
5 | {
6 | public static class MouseManager
7 | {
8 | private static MouseState lastMouseState;
9 | private static MouseState thisMouseState;
10 | private static MouseState thisMouseCursorState;
11 | public static void Update()
12 | {
13 | lastMouseState = thisMouseState;
14 | thisMouseState = Mouse.GetState();
15 | thisMouseCursorState = Mouse.GetCursorState();
16 | }
17 |
18 |
19 | public static int WindowPositionX => thisMouseCursorState.X;
20 | public static int WindowPositionY => thisMouseCursorState.Y;
21 | public static int PositionX => thisMouseState.X;
22 | public static int PositionY => thisMouseState.Y;
23 | public static Vector2 DeltaPosition => new Vector2(thisMouseState.X - lastMouseState.X, thisMouseState.Y - lastMouseState.Y);
24 | public static float DeltaScrollX => thisMouseState.Scroll.X - lastMouseState.Scroll.X;
25 | public static float DeltaScrollY => thisMouseState.Scroll.Y - lastMouseState.Scroll.Y;
26 |
27 | public static ButtonState LeftButton => thisMouseState.LeftButton;
28 | public static ButtonState RightButton => thisMouseState.RightButton;
29 | public static ButtonState MiddleButton => thisMouseState.MiddleButton;
30 |
31 | ///
32 | ///
33 | ///
34 | /// True if mouseButton is down this update but not last one
35 | public static bool IsButtonTouched(MouseButton mouseButton) => thisMouseState.IsButtonDown(mouseButton) && lastMouseState.IsButtonUp(mouseButton);
36 |
37 | ///
38 | ///
39 | ///
40 | /// True if mouseButton is down
41 | public static bool IsButtonDown(MouseButton mouseButton) => thisMouseState.IsButtonDown(mouseButton);
42 |
43 | ///
44 | ///
45 | ///
46 | /// True if mouseButton is up this update but not last one
47 | public static bool IsButtonUp(MouseButton mouseButton) => thisMouseState.IsButtonUp(mouseButton) && lastMouseState.IsButtonDown(mouseButton);
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Material.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using OpenTK;
3 |
4 | namespace OpenTK_PathTracer
5 | {
6 | class Material : BaseSTD140Compatible
7 | {
8 | public static Material Zero => new Material(albedo: Vector3.One, emissiv: Vector3.Zero, refractionColor: Vector3.Zero, specularChance: 0.0f, specularRoughness: 0.0f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f);
9 | public const int GPU_INSTANCE_SIZE = 16 * 4;
10 |
11 | public Vector3 Albedo;
12 | public Vector3 Emissiv;
13 | public Vector3 AbsorbanceColor;
14 | public float SpecularChance;
15 | public float SpecularRoughness;
16 | public float IOR;
17 | public float RefractionChance;
18 | public float RefractionRoughnes;
19 | public Material(Vector3 albedo, Vector3 emissiv, Vector3 refractionColor, float specularChance, float specularRoughness, float indexOfRefraction, float refractionChance, float refractionRoughnes)
20 | {
21 | // Note: diffuse chance is 1.0f - (SpecularChance + RefractionChance). So must add up to 1.0
22 |
23 | Albedo = albedo;
24 | Emissiv = emissiv;
25 | AbsorbanceColor = refractionColor;
26 | SpecularChance = Math.Clamp(specularChance, 0.0f, 1.0f);
27 | SpecularRoughness = specularRoughness;
28 | IOR = Math.Max(indexOfRefraction, 1.0f);
29 | RefractionChance = Math.Clamp(refractionChance, 0.0f, 1.0f - SpecularChance);
30 | RefractionRoughnes = refractionRoughnes;
31 | }
32 |
33 | public override int BufferOffset => throw new NotSupportedException("Material is not meant to be directly uploaded to the GPU");
34 |
35 | private readonly Vector4[] gpuData = new Vector4[GPU_INSTANCE_SIZE / Vector4.SizeInBytes];
36 | public override Vector4[] GetGPUFriendlyData()
37 | {
38 | gpuData[0].Xyz = Albedo;
39 | gpuData[0].W = SpecularChance;
40 |
41 | gpuData[1].Xyz = Emissiv;
42 | gpuData[1].W = SpecularRoughness;
43 |
44 | gpuData[2].Xyz = AbsorbanceColor;
45 | gpuData[2].W = RefractionChance;
46 |
47 | gpuData[3].X = RefractionRoughnes;
48 | gpuData[3].Y = IOR;
49 |
50 | return gpuData;
51 | }
52 |
53 | private readonly static Random rnd = new Random();
54 | public static Material GetRndMaterial()
55 | {
56 | bool isEmissiv = rnd.NextDouble() < 0.2;
57 | return new Material(albedo: RndVector3(), emissiv: isEmissiv ? RndVector3() : Vector3.Zero, refractionColor: RndVector3() * 2.0f, specularChance: (float)rnd.NextDouble() * 0.5f, specularRoughness: (float)rnd.NextDouble(), indexOfRefraction: (float)rnd.NextDouble() + 1, refractionChance: (float)rnd.NextDouble() * 0.5f, refractionRoughnes: (float)rnd.NextDouble());
58 | }
59 |
60 | private static Vector3 RndVector3() => new Vector3((float)rnd.NextDouble(), (float)rnd.NextDouble(), (float)rnd.NextDouble());
61 | }
62 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Objects/Framebuffer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SixLabors.ImageSharp;
3 | using SixLabors.ImageSharp.Processing;
4 | using SixLabors.ImageSharp.PixelFormats;
5 | using OpenTK.Graphics.OpenGL4;
6 |
7 | namespace OpenTK_PathTracer.Render.Objects
8 | {
9 | class Framebuffer : IDisposable
10 | {
11 | private static int lastBindedID = -1;
12 |
13 | public readonly int ID;
14 | public Framebuffer()
15 | {
16 | GL.CreateFramebuffers(1, out ID);
17 | }
18 |
19 | public void Clear(ClearBufferMask clearBufferMask)
20 | {
21 | Bind();
22 | GL.Clear(clearBufferMask);
23 | }
24 |
25 | public void AddRenderTarget(FramebufferAttachment framebufferAttachment, Texture texture)
26 | {
27 | GL.NamedFramebufferTexture(ID, framebufferAttachment, texture.ID, 0);
28 | }
29 | public void SetRenderTarget(params DrawBuffersEnum[] drawBuffersEnums)
30 | {
31 | GL.NamedFramebufferDrawBuffers(ID, drawBuffersEnums.Length, drawBuffersEnums);
32 | }
33 | public void SetReadTarget(ReadBufferMode readBufferMode)
34 | {
35 | GL.NamedFramebufferReadBuffer(ID, readBufferMode);
36 | }
37 |
38 | public void Bind(FramebufferTarget framebufferTarget = FramebufferTarget.Framebuffer)
39 | {
40 | if (lastBindedID != ID)
41 | {
42 | GL.BindFramebuffer(framebufferTarget, ID);
43 | lastBindedID = ID;
44 | }
45 | }
46 |
47 | public FramebufferStatus GetFBOStatus()
48 | {
49 | return GL.CheckNamedFramebufferStatus(ID, FramebufferTarget.Framebuffer);
50 | }
51 |
52 | public static void Bind(int id, FramebufferTarget framebufferTarget = FramebufferTarget.Framebuffer)
53 | {
54 | if (lastBindedID != id)
55 | {
56 | GL.BindFramebuffer(framebufferTarget, id);
57 | lastBindedID = id;
58 | }
59 | }
60 |
61 | public static void Clear(int id, ClearBufferMask clearBufferMask)
62 | {
63 | Framebuffer.Bind(id);
64 | GL.Clear(clearBufferMask);
65 | }
66 |
67 | public static unsafe Image GetBitmapFramebufferAttachment(int id, FramebufferAttachment framebufferAttachment, int width, int height, int x = 0, int y = 0)
68 | {
69 | Image image = new Image(width, height);
70 | GL.NamedFramebufferReadBuffer(id, id == 0 ? ReadBufferMode.Front : (ReadBufferMode)framebufferAttachment);
71 |
72 | Bind(id, FramebufferTarget.ReadFramebuffer);
73 | fixed (void* ptr = image.GetPixelRowSpan(0))
74 | {
75 | GL.ReadPixels(x, y, width, height, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)ptr);
76 | }
77 | GL.Finish();
78 |
79 | image.Mutate(p => p.Flip(FlipMode.Vertical));
80 |
81 | return image;
82 | }
83 |
84 | public void Dispose()
85 | {
86 | GL.DeleteFramebuffer(ID);
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Camera.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using OpenTK;
3 | using OpenTK.Input;
4 |
5 | namespace OpenTK_PathTracer
6 | {
7 | class Camera
8 | {
9 | public Vector3 Position;
10 | public Vector3 ViewDir;
11 | public Vector3 Up;
12 | public Vector3 Velocity;
13 | public float MovmentSpeed;
14 | public float MouseSensitivity;
15 | public Matrix4 View { get; private set; }
16 | public Camera(Vector3 position, Vector3 up, float lookX = -90.0f, float lookY = 0.0f, float mouseSensitivity = 0.1f, float speed = 10)
17 | {
18 | LookX = lookX;
19 | LookY = lookY;
20 |
21 | ViewDir.X = MathF.Cos(MathHelper.DegreesToRadians(LookX)) * MathF.Cos(MathHelper.DegreesToRadians(LookY));
22 | ViewDir.Y = MathF.Sin(MathHelper.DegreesToRadians(LookY));
23 | ViewDir.Z = MathF.Sin(MathHelper.DegreesToRadians(LookX)) * MathF.Cos(MathHelper.DegreesToRadians(LookY));
24 |
25 | View = GenerateMatrix(position, ViewDir, up);
26 | Position = position;
27 | Up = up;
28 | MovmentSpeed = speed;
29 | MouseSensitivity = mouseSensitivity;
30 | }
31 |
32 |
33 | public float LookX { get; private set; }
34 | public float LookY { get; private set; }
35 | public void ProcessInputs(float dT, out bool frameChanged)
36 | {
37 | frameChanged = false;
38 |
39 | Vector2 mouseDelta = MouseManager.DeltaPosition;
40 | if (mouseDelta.X != 0 || mouseDelta.Y != 0)
41 | frameChanged = true;
42 |
43 | LookX += mouseDelta.X * MouseSensitivity;
44 | LookY -= mouseDelta.Y * MouseSensitivity;
45 |
46 | if (LookY >= 90) LookY = 89.999f;
47 | if (LookY <= -90) LookY = -89.999f;
48 |
49 | ViewDir.X = MathF.Cos(MathHelper.DegreesToRadians(LookX)) * MathF.Cos(MathHelper.DegreesToRadians(LookY));
50 | ViewDir.Y = MathF.Sin(MathHelper.DegreesToRadians(LookY));
51 | ViewDir.Z = MathF.Sin(MathHelper.DegreesToRadians(LookX)) * MathF.Cos(MathHelper.DegreesToRadians(LookY));
52 |
53 | Vector3 acceleration = Vector3.Zero;
54 | if (KeyboardManager.IsKeyDown(Key.W))
55 | acceleration += ViewDir;
56 |
57 | if (KeyboardManager.IsKeyDown(Key.S))
58 | acceleration -= ViewDir;
59 |
60 | if (KeyboardManager.IsKeyDown(Key.D))
61 | acceleration += Vector3.Cross(ViewDir, Up).Normalized();
62 |
63 | if (KeyboardManager.IsKeyDown(Key.A))
64 | acceleration -= Vector3.Cross(ViewDir, Up).Normalized();
65 |
66 | Velocity += KeyboardManager.IsKeyDown(Key.LShift) ? acceleration * 5.0f : (KeyboardManager.IsKeyDown(Key.LControl) ? acceleration * 0.35f : acceleration);
67 | if (acceleration != Vector3.Zero || Velocity != Vector3.Zero)
68 | frameChanged = true;
69 |
70 | if (Vector3.Dot(Velocity, Velocity) < 0.01f)
71 | Velocity = Vector3.Zero;
72 |
73 | Velocity *= 0.95f;
74 | Velocity += acceleration * dT;
75 | Position += Velocity * dT;
76 | View = GenerateMatrix(Position, ViewDir, Up);
77 | }
78 |
79 | public static Matrix4 GenerateMatrix(Vector3 position, Vector3 viewDir, Vector3 up)
80 | {
81 | return Matrix4.LookAt(position, position + viewDir, up);
82 | }
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Helper.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Linq;
4 | using System.Threading.Tasks;
5 | using System.Collections.Generic;
6 | using SixLabors.ImageSharp;
7 | using SixLabors.ImageSharp.PixelFormats;
8 | using OpenTK.Graphics.OpenGL4;
9 | using OpenTK_PathTracer.Render.Objects;
10 |
11 | namespace OpenTK_PathTracer
12 | {
13 | static class Helper
14 | {
15 | public const string SHADER_DIRECTORY_PATH = "res/shaders/";
16 | public static readonly double APIVersion = Convert.ToDouble($"{GL.GetInteger(GetPName.MajorVersion)}{GL.GetInteger(GetPName.MinorVersion)}") / 10.0;
17 |
18 | public static unsafe void ParallelLoadCubemapImages(Texture texture, string[] paths, SizedInternalFormat sizedInternalFormat)
19 | {
20 | if (texture.Target != TextureTarget.TextureCubeMap)
21 | throw new ArgumentException($"texture must be {TextureTarget.TextureCubeMap}");
22 |
23 | if (paths.Length != 6)
24 | throw new ArgumentException($"Number of images must be equal to six");
25 |
26 | if (!paths.All(p => File.Exists(p)))
27 | throw new FileNotFoundException($"At least on of the specified paths is invalid");
28 |
29 | Image[] images = new Image[6];
30 | Task.Run(() =>
31 | {
32 | Parallel.For(0, 6, i =>
33 | {
34 | images[i] = Image.Load(paths[i]);
35 | });
36 | }).Wait();
37 | if (!images.All(i => i.Width == i.Height && i.Width == images[0].Width))
38 | throw new ArgumentException($"Individual cubemap textures must be squares and every texture must be of the same size");
39 |
40 | int size = images[0].Width;
41 | texture.ImmutableAllocate(size, size, 1, sizedInternalFormat);
42 | for (int i = 0; i < 6; i++)
43 | {
44 | fixed (void* ptr = images[i].GetPixelRowSpan(0))
45 | {
46 | texture.SubTexture3D(size, size, 1, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)ptr, 0, 0, 0, i);
47 | images[i].Dispose();
48 | }
49 | }
50 | }
51 |
52 | private static HashSet GetExtensions()
53 | {
54 | HashSet hashSet = new HashSet();
55 | for (int i = 0; i < GL.GetInteger(GetPName.NumExtensions); i++)
56 | hashSet.Add(GL.GetString(StringNameIndexed.Extensions, i));
57 |
58 | return hashSet;
59 | }
60 |
61 | private static readonly HashSet glExtensions = new HashSet(GetExtensions());
62 |
63 |
64 | ///
65 | ///
66 | /// The extension to check against. Examples: GL_ARB_bindless_texture or WGL_EXT_swap_control
67 | /// True if the extension is available
68 | public static bool IsExtensionsAvailable(string extension)
69 | {
70 | return glExtensions.Contains(extension);
71 | }
72 |
73 | ///
74 | ///
75 | /// Extension to check against. Examples: GL_ARB_direct_state_access or GL_ARB_compute_shader
76 | /// API version the extension became part of the core profile
77 | /// True if this GL version >= or the extension is otherwise available
78 | public static bool IsCoreExtensionAvailable(string extension, double first)
79 | {
80 | return (APIVersion >= first) || IsExtensionsAvailable(extension);
81 | }
82 | }
83 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Objects/BufferObject.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Runtime.InteropServices;
3 | using OpenTK.Graphics.OpenGL4;
4 |
5 | namespace OpenTK_PathTracer.Render.Objects
6 | {
7 | public class BufferObject : IDisposable
8 | {
9 | public readonly int ID;
10 | public int Size { get; private set; }
11 |
12 | public BufferObject()
13 | {
14 | GL.CreateBuffers(1, out ID);
15 | }
16 |
17 | public void BindRange(BufferRangeTarget bufferRangeTarget, int index, int offset, int size)
18 | {
19 | GL.BindBufferRange(bufferRangeTarget, index, ID, (IntPtr)offset, size);
20 | }
21 |
22 | public void Bind(BufferTarget bufferTarget)
23 | {
24 | GL.BindBuffer(bufferTarget, ID);
25 | }
26 |
27 | ///
28 | /// Sets the content of this buffer to 0
29 | ///
30 | public void Reset()
31 | {
32 | IntPtr intPtr = Marshal.AllocHGlobal(Size);
33 | GL.NamedBufferSubData(ID, IntPtr.Zero, Size, intPtr);
34 | Marshal.FreeHGlobal(intPtr);
35 | }
36 |
37 | public void SubData(int offset, int size, T data) where T : struct
38 | {
39 | GL.NamedBufferSubData(ID, (IntPtr)offset, size, ref data);
40 | }
41 | public void SubData(int offset, int size, T[] data) where T : struct
42 | {
43 | GL.NamedBufferSubData(ID, (IntPtr)offset, size, data);
44 | }
45 | public void SubData(int offset, int size, IntPtr data)
46 | {
47 | GL.NamedBufferSubData(ID, (IntPtr)offset, size, data);
48 | }
49 |
50 | public void MutableAllocate(int size, T data, BufferUsageHint bufferUsageHint) where T : struct
51 | {
52 | GL.NamedBufferData(ID, size, ref data, bufferUsageHint);
53 | Size = size;
54 | }
55 | public void MutableAllocate(int size, T[] data, BufferUsageHint bufferUsageHint) where T : struct
56 | {
57 | GL.NamedBufferData(ID, size, data, bufferUsageHint);
58 | Size = size;
59 | }
60 | public void MutableAllocate(int size, IntPtr data, BufferUsageHint bufferUsageHint)
61 | {
62 | GL.NamedBufferData(ID, size, data, bufferUsageHint);
63 | Size = size;
64 | }
65 |
66 | public void ImmutableAllocate(int size, T data, BufferStorageFlags bufferStorageFlags) where T : struct
67 | {
68 | GL.NamedBufferStorage(ID, size, ref data, bufferStorageFlags);
69 | Size = size;
70 | }
71 | public void ImmutableAllocate(int size, T[] data, BufferStorageFlags bufferStorageFlags) where T : struct
72 | {
73 | GL.NamedBufferStorage(ID, size, data, bufferStorageFlags);
74 | Size = size;
75 | }
76 | public void ImmutableAllocate(int size, IntPtr data, BufferStorageFlags bufferStorageFlags)
77 | {
78 | GL.NamedBufferStorage(ID, size, data, bufferStorageFlags);
79 | Size = size;
80 | }
81 |
82 | public void GetSubData(int offset, int size, out T data) where T : struct
83 | {
84 | data = new T();
85 | GL.GetNamedBufferSubData(ID, (IntPtr)offset, size, ref data);
86 | }
87 | public void GetSubData(int offset, int size, T[] data) where T : struct
88 | {
89 | GL.GetNamedBufferSubData(ID, (IntPtr)offset, size, data);
90 | }
91 | public void GetSubData(int offset, int size, out IntPtr data)
92 | {
93 | data = Marshal.AllocHGlobal(size);
94 | GL.GetNamedBufferSubData(ID, (IntPtr)offset, size, data);
95 | }
96 |
97 | public void Dispose()
98 | {
99 | GL.DeleteBuffer(ID);
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/PathTracer.cs:
--------------------------------------------------------------------------------
1 | #define _USE_COMPUTE
2 | using System.IO;
3 | using OpenTK;
4 | using OpenTK.Graphics.OpenGL4;
5 | using OpenTK_PathTracer.Render.Objects;
6 |
7 | namespace OpenTK_PathTracer
8 | {
9 | class PathTracer
10 | {
11 | private int _numSpheres;
12 | public int NumSpheres
13 | {
14 | get => _numSpheres;
15 |
16 | set
17 | {
18 | _numSpheres = value;
19 | shaderProgram.Upload("uboGameObjectsSize", new Vector2(value, NumCuboids));
20 | }
21 | }
22 |
23 |
24 | private int _numCuboids;
25 | public int NumCuboids
26 | {
27 | get => _numCuboids;
28 |
29 | set
30 | {
31 | _numCuboids = value;
32 | shaderProgram.Upload("uboGameObjectsSize", new Vector2(NumSpheres, value));
33 | }
34 | }
35 |
36 |
37 | private int _rayDepth;
38 | public int RayDepth
39 | {
40 | get => _rayDepth;
41 |
42 | set
43 | {
44 | _rayDepth = value;
45 | shaderProgram.Upload("rayDepth", value);
46 | }
47 | }
48 |
49 | private int _spp;
50 | public int SPP
51 | {
52 | get => _spp;
53 |
54 | set
55 | {
56 | _spp = value;
57 | shaderProgram.Upload("SPP", value);
58 | }
59 | }
60 |
61 | private float _focalLength;
62 | public float FocalLength
63 | {
64 | get => _focalLength;
65 |
66 | set
67 | {
68 | _focalLength = value;
69 | shaderProgram.Upload("focalLength", value);
70 | }
71 | }
72 |
73 | private float _apertureDiameter;
74 | public float ApertureDiameter
75 | {
76 | get => _apertureDiameter;
77 |
78 | set
79 | {
80 | _apertureDiameter = value;
81 | shaderProgram.Upload("apertureDiameter", value);
82 | }
83 | }
84 |
85 | public Texture EnvironmentMap;
86 | public readonly Texture Result;
87 | #if USE_COMPUTE
88 | private static readonly ShaderProgram shaderProgram = new ShaderProgram(new Shader(ShaderType.ComputeShader, File.ReadAllText("res/shaders/PathTracing/compute.glsl")));
89 | #else
90 | private readonly Framebuffer framebuffer;
91 | private static readonly ShaderProgram shaderProgram = new ShaderProgram(
92 | new Shader(ShaderType.VertexShader, File.ReadAllText("res/shaders/screenQuad.glsl")),
93 | new Shader(ShaderType.FragmentShader, File.ReadAllText("res/shaders/PathTracing/fragCompute.glsl")));
94 | #endif
95 | public PathTracer(Texture environmentMap, int width, int height, int rayDepth, int spp, float focalLength, float apertureDiamater)
96 | {
97 | Result = new Texture(TextureTarget2d.Texture2D);
98 | Result.SetFilter(TextureMinFilter.Linear, TextureMagFilter.Linear);
99 | Result.MutableAllocate(width, height, 1, PixelInternalFormat.Rgba32f);
100 | #if !USE_COMPUTE
101 | framebuffer = new Framebuffer();
102 | framebuffer.AddRenderTarget(FramebufferAttachment.ColorAttachment0, Result);
103 | #endif
104 |
105 | RayDepth = rayDepth;
106 | SPP = spp;
107 | FocalLength = focalLength;
108 | ApertureDiameter = apertureDiamater;
109 | EnvironmentMap = environmentMap;
110 | }
111 |
112 | public int Samples => thisRenderNumFrame * SPP;
113 | private int thisRenderNumFrame;
114 | public void Render()
115 | {
116 | shaderProgram.Use();
117 | shaderProgram.Upload(0, thisRenderNumFrame++);
118 | EnvironmentMap.AttachSampler(1);
119 | #if USE_COMPUTE
120 | Result.AttachImage(0, 0, false, 0, TextureAccess.ReadWrite, SizedInternalFormat.Rgba32f);
121 | GL.DispatchCompute((Result.Width + 8 - 1) / 8, (Result.Height + 8 - 1) / 8, 1);
122 |
123 | GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
124 | #else
125 | framebuffer.Bind();
126 | Result.AttachSampler(0);
127 | GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
128 | #endif
129 | }
130 |
131 | public void SetSize(int width, int height)
132 | {
133 | thisRenderNumFrame = 0;
134 | Result.MutableAllocate(width, height, 1, Result.PixelInternalFormat);
135 | }
136 |
137 | public void ResetRenderer()
138 | {
139 | thisRenderNumFrame = 0;
140 | }
141 | }
142 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/AtmosphericScatterer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using OpenTK;
4 | using OpenTK.Graphics.OpenGL4;
5 | using OpenTK_PathTracer.Render.Objects;
6 |
7 | namespace OpenTK_PathTracer.Render
8 | {
9 | class AtmosphericScatterer
10 | {
11 | private int _iSteps;
12 | public int ISteps
13 | {
14 | set
15 | {
16 | _iSteps = value;
17 | shaderProgram.Upload("iSteps", _iSteps);
18 | }
19 |
20 | get => _iSteps;
21 | }
22 |
23 | private int _jSteps;
24 | public int JSteps
25 | {
26 | set
27 | {
28 | _jSteps = value;
29 | shaderProgram.Upload("jSteps", _jSteps);
30 | }
31 |
32 | get => _jSteps;
33 | }
34 |
35 | private float _time;
36 | public float Time
37 | {
38 | set
39 | {
40 | _time = value;
41 | shaderProgram.Upload("lightPos", new Vector3(0.0f, MathF.Sin(MathHelper.DegreesToRadians(_time * 360.0f)), MathF.Cos(MathHelper.DegreesToRadians(_time * 360.0f))) * 149600000e3f);
42 | }
43 |
44 | get => _time;
45 | }
46 |
47 | private float _lightIntensity;
48 | public float LightIntensity
49 | {
50 | set
51 | {
52 | _lightIntensity = Math.Max(value, 0.0f);
53 | shaderProgram.Upload("lightIntensity", _lightIntensity);
54 | }
55 |
56 | get => _lightIntensity;
57 | }
58 |
59 | public readonly TimerQuery Timer;
60 | public readonly Texture Result;
61 | private static readonly ShaderProgram shaderProgram = new ShaderProgram(new Shader(ShaderType.ComputeShader, File.ReadAllText("res/shaders/AtmosphericScattering/compute.glsl")));
62 | private readonly BufferObject bufferObject;
63 | public AtmosphericScatterer(int size)
64 | {
65 | Timer = new TimerQuery(600);
66 |
67 | Result = new Texture(TextureTarget2d.TextureCubeMap);
68 | Result.SetFilter(TextureMinFilter.Nearest, TextureMagFilter.Linear);
69 | Result.MutableAllocate(size, size, 1, PixelInternalFormat.Rgba32f);
70 |
71 | bufferObject = new BufferObject();
72 | bufferObject.ImmutableAllocate(Vector4.SizeInBytes * 4 * 7 + Vector4.SizeInBytes, IntPtr.Zero, BufferStorageFlags.DynamicStorageBit);
73 | bufferObject.BindRange(BufferRangeTarget.UniformBuffer, 2, 0, bufferObject.Size);
74 |
75 | Matrix4 invProjection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(90.0f), 1, 0.1f, 10f).Inverted();
76 | Matrix4[] invViews = new Matrix4[]
77 | {
78 | Camera.GenerateMatrix(Vector3.Zero, new Vector3(1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)).Inverted(), // PositiveX
79 | Camera.GenerateMatrix(Vector3.Zero, new Vector3(-1.0f, 0.0f, 0.0f), new Vector3(0.0f, -1.0f, 0.0f)).Inverted(), // NegativeX
80 |
81 | Camera.GenerateMatrix(Vector3.Zero, new Vector3(0.0f, 1.0f, 0.0f), new Vector3(0.0f, 0.0f, 1.0f)).Inverted(), // PositiveY
82 | Camera.GenerateMatrix(Vector3.Zero, new Vector3(0.0f, -1.0f, 0.0f), new Vector3(0.0f, 0.0f, -1.0f)).Inverted(), // NegativeY
83 |
84 | Camera.GenerateMatrix(Vector3.Zero, new Vector3(0.0f, 0.0f, 1.0f), new Vector3(0.0f, -1.0f, 0.0f)).Inverted(), // PositiveZ
85 | Camera.GenerateMatrix(Vector3.Zero, new Vector3(0.0f, 0.0f, -1.0f), new Vector3(0.0f, -1.0f, 0.0f)).Inverted(), // NegativeZ
86 | };
87 |
88 | bufferObject.SubData(0, Vector4.SizeInBytes * 4, invProjection);
89 | bufferObject.SubData(Vector4.SizeInBytes * 4, Vector4.SizeInBytes * 4 * invViews.Length, invViews);
90 |
91 | Time = 0.5f;
92 | ISteps = 50;
93 | JSteps = 15;
94 | LightIntensity = 15.0f;
95 | }
96 |
97 |
98 | ///
99 | /// This method computes a whole cubemap rather than just whats visible. It is meant for precomputation and should not be called frequently for performance reasons
100 | ///
101 | ///
102 | public void Render()
103 | {
104 | Timer.Start();
105 |
106 | Result.AttachImage(0, 0, true, 0, TextureAccess.WriteOnly, SizedInternalFormat.Rgba32f);
107 | shaderProgram.Use();
108 |
109 | GL.DispatchCompute((Result.Width + 8 - 1) / 8, (Result.Width + 8 - 1) / 8, 6);
110 | GL.MemoryBarrier(MemoryBarrierFlags.TextureFetchBarrierBit);
111 |
112 | Timer.StopAndReset();
113 | }
114 |
115 | public void SetSize(int size)
116 | {
117 | Result.MutableAllocate(size, size, 1, Result.PixelInternalFormat);
118 | }
119 | }
120 | }
121 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/AtmosphericScattering/computeMy.glsl:
--------------------------------------------------------------------------------
1 | #version 450 core
2 | #define FLOAT_MAX 3.4028235e+38
3 | #define FLOAT_MIN -3.4028235e+38
4 | #define EPSILON 0.0001
5 | #define PI 3.14159265
6 |
7 | layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
8 |
9 | layout(binding = 0, rgba32f) uniform writeonly restrict imageCube ImgResult;
10 |
11 | struct Ray
12 | {
13 | vec3 Origin;
14 | vec3 Direction;
15 | };
16 |
17 | layout (std140, binding = 2) uniform AtmosphericDataUBO
18 | {
19 | mat4 InvProjection;
20 | mat4[6] InvView;
21 | } atmoDataUBO;
22 |
23 | vec3 CalculateScattering(Ray ray, int samples);
24 | float AvgDensityOver(vec3 start, vec3 end, int samples);
25 | float DensityAtPoint(vec3 point);
26 | bool RaySphereIntersect(Ray ray, vec3 position, float radius, out float t1, out float t2);
27 | Ray GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec3 viewPos, vec2 normalizedDeviceCoors);
28 | bool IsInside(vec2 pos, vec2 size);
29 |
30 |
31 | const vec3 PlanetPos = vec3(0, 0, 0);
32 | const float PlanetRad = 600;
33 |
34 | const vec3 LightPos = vec3(0.0, 500.0 + 800.0, 0.0);
35 | const vec3 ViewPos = vec3(20.43, -201.99 + 800.0, -20.67);
36 | const vec3 WaveLengths = vec3(680.0, 550.0, 440.0);
37 |
38 | const float AtmosphereRad = 0.01;
39 | const float DensityFallOff = 35.0;
40 | const float ScatteringStrength = 2.1;
41 |
42 | const int ISteps = 100;
43 | const int JSteps = 8;
44 |
45 | void main()
46 | {
47 | ivec2 imgResultSize = imageSize(ImgResult);
48 | ivec3 imgCoord = ivec3(gl_GlobalInvocationID);
49 | if (!IsInside(imgCoord.xy, imgResultSize))
50 | return;
51 |
52 | vec2 ndc = vec2(imgCoord.xy) / imgResultSize * 2.0 - 1.0;
53 |
54 | Ray rayEyeToWorld = GetWorldSpaceRay(atmoDataUBO.InvProjection, atmoDataUBO.InvView[imgCoord.z], ViewPos, ndc);
55 | vec3 scattered = CalculateScattering(rayEyeToWorld, ISteps);
56 |
57 | imageStore(ImgResult, imgCoord, vec4(scattered, 1.0));
58 | }
59 |
60 |
61 | vec3 CalculateScattering(Ray ray, int samples)
62 | {
63 | vec3 ScatteringCoefficients = vec3(pow(400 / max(WaveLengths.x, EPSILON), 4), pow(400 / max(WaveLengths.y, EPSILON), 4), pow(400 / max(WaveLengths.z, EPSILON), 4)) * ScatteringStrength;
64 | vec3 color = vec3(0);
65 | float t1, t2;
66 | if (!(RaySphereIntersect(ray, PlanetPos, PlanetRad + AtmosphereRad, t1, t2) && t2 > 0))
67 | return color;
68 |
69 | float planetT1, planetT2;
70 | RaySphereIntersect(ray, PlanetPos, PlanetRad, planetT1, planetT2);
71 |
72 | t2 = min(planetT1, t2); // if also hit planet set t2 to planetT1
73 |
74 |
75 | vec3 ViewPos = t1 < 0 ? ray.Origin : (ray.Origin + ray.Direction * t1);
76 | ray.Origin = ViewPos + EPSILON;
77 |
78 | vec3 deltaStep = ((ray.Origin + ray.Direction * t2) - ray.Origin) / samples;
79 |
80 | vec3 scatteredLight = vec3(0);
81 | for (int i = 0; i < samples; i++)
82 | {
83 | ray.Direction = normalize(LightPos - ray.Origin);
84 | RaySphereIntersect(ray, PlanetPos, PlanetRad + AtmosphereRad, t1, t2);
85 |
86 | float avgDensityAlongRay = AvgDensityOver(ray.Origin, ray.Origin + ray.Direction * t2, JSteps);
87 | float avgDensityAlongViewRay = AvgDensityOver(ViewPos, ray.Origin, JSteps);
88 | vec3 transmitted = exp((-avgDensityAlongRay - avgDensityAlongViewRay) * ScatteringCoefficients); // combines transmittance from Densityray and ViewRay
89 |
90 | float localDensity = DensityAtPoint(ray.Origin);
91 |
92 | scatteredLight += localDensity * transmitted * ScatteringCoefficients;
93 |
94 | ray.Origin += deltaStep;
95 | }
96 | return scatteredLight / samples;
97 | }
98 |
99 | float AvgDensityOver(vec3 start, vec3 end, int samples) // Physics terminology: "Optical Depth"
100 | {
101 | // Take integral over DensityAtPoint() from start to end. I dont think there exists a closed-form solution so we are simply going to make an approxiamtion using riemann sum
102 |
103 | vec3 rayPos = start;
104 | vec3 deltaStep = (end - start) / samples;
105 | float density = 0.0;
106 |
107 | for (int i = 0; i < samples; i++)
108 | {
109 | density += DensityAtPoint(rayPos);
110 | rayPos += deltaStep;
111 | }
112 |
113 | return density / samples;
114 | }
115 |
116 | float DensityAtPoint(vec3 point)
117 | {
118 | float height = length(point - PlanetPos) - PlanetRad;
119 | float height01 = height / (AtmosphereRad - PlanetRad); // 0 at Planetshell, 1 at outer atmosphere
120 |
121 | return exp(-height01 * DensityFallOff) * (1 - height01); // 1 at Planetshell, 0 at outer atmosphere
122 | }
123 |
124 | // Source: https://antongerdelan.net/opengl/raycasting.html
125 | bool RaySphereIntersect(Ray ray, vec3 position, float radius, out float t1, out float t2)
126 | {
127 | t1 = t2 = FLOAT_MAX;
128 |
129 | vec3 sphereToRay = ray.Origin - position;
130 | float b = dot(ray.Direction, sphereToRay);
131 | float c = dot(sphereToRay, sphereToRay) - radius * radius;
132 | float discriminant = b * b - c;
133 | if (discriminant < 0)
134 | return false;
135 |
136 | float squareRoot = sqrt(discriminant);
137 | t1 = -b - squareRoot;
138 | t2 = -b + squareRoot;
139 |
140 | return true;
141 | }
142 |
143 | bool IsInside(vec2 pos, vec2 size)
144 | {
145 | return pos.x < size.x && pos.y < size.y;
146 | }
147 |
148 | Ray GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec3 viewPos, vec2 normalizedDeviceCoords)
149 | {
150 | vec4 rayEye = inverseProj * vec4(normalizedDeviceCoords.xy, -1.0, 0.0);
151 | rayEye.zw = vec2(-1.0, 0.0);
152 | return Ray(viewPos, normalize((inverseView * rayEye).xyz));
153 | }
154 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/AtmosphericScattering/compute.glsl:
--------------------------------------------------------------------------------
1 | // I only adapted his code to a compute shader.
2 | // The actual atmospheric scattering code is copied from the given source
3 | // Source: https://github.com/wwwtyro/glsl-atmosphere
4 |
5 | #version 450 core
6 | #define PI 3.14159265
7 |
8 | layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
9 |
10 | layout(binding = 0, rgba32f) uniform writeonly restrict imageCube ImgResult;
11 |
12 | layout (std140, binding = 2) uniform AtmosphericDataUBO
13 | {
14 | mat4 InvProjection;
15 | mat4[6] InvView;
16 | } atmoDataUBO;
17 |
18 | vec2 Rsi(vec3 r0, vec3 rd, float sr);
19 | vec3 Atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g);
20 | bool IsInside(vec2 pos, vec2 size);
21 | vec3 GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec2 normalizedDeviceCoords);
22 |
23 | uniform vec3 lightPos;
24 |
25 | uniform float lightIntensity;
26 |
27 | uniform int iSteps;
28 | uniform int jSteps;
29 |
30 | void main()
31 | {
32 | ivec2 imgResultSize = imageSize(ImgResult);
33 | ivec3 imgCoord = ivec3(gl_GlobalInvocationID);
34 | if (!IsInside(imgCoord.xy, imgResultSize))
35 | return;
36 |
37 | vec2 ndc = vec2(imgCoord.xy) / imgResultSize * 2.0 - 1.0;
38 |
39 | vec3 eyeToWorld = GetWorldSpaceRay(atmoDataUBO.InvProjection, atmoDataUBO.InvView[imgCoord.z], ndc);
40 |
41 | vec3 color = Atmosphere(
42 | eyeToWorld, // normalized ray direction
43 | vec3(0, 6376e3, 0), // ray origin
44 | lightPos, // position of the sun
45 | lightIntensity, // intensity of the sun
46 | 6371e3, // radius of the planet in meters
47 | 6471e3, // radius of the atmosphere in meters
48 | vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient
49 | 21e-6, // Mie scattering coefficient
50 | 8e3, // Rayleigh scale height
51 | 1.2e3, // Mie scale height
52 | 0.758 // Mie preferred scattering direction
53 | );
54 |
55 | imageStore(ImgResult, imgCoord, vec4(color, 1.0));
56 | }
57 |
58 | vec2 Rsi(vec3 r0, vec3 rd, float sr) {
59 | // ray-sphere intersection that assumes
60 | // the sphere is centered at the origin.
61 | // No intersection when result.x > result.y
62 | float a = dot(rd, rd);
63 | float b = 2.0 * dot(rd, r0);
64 | float c = dot(r0, r0) - (sr * sr);
65 | float d = (b*b) - 4.0*a*c;
66 | if (d < 0.0) return vec2(1e5,-1e5);
67 | return vec2(
68 | (-b - sqrt(d))/(2.0*a),
69 | (-b + sqrt(d))/(2.0*a)
70 | );
71 | }
72 |
73 | vec3 Atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {
74 | // Normalize the sun and view directions.
75 | pSun = normalize(pSun);
76 | r = normalize(r);
77 |
78 | // Calculate the step size of the primary ray.
79 | vec2 p = Rsi(r0, r, rAtmos);
80 | if (p.x > p.y) return vec3(0,0,0);
81 | p.y = min(p.y, Rsi(r0, r, rPlanet).x);
82 | float iStepSize = (p.y - p.x) / float(iSteps);
83 |
84 | // Initialize the primary ray time.
85 | float iTime = 0.0;
86 |
87 | // Initialize accumulators for Rayleigh and Mie scattering.
88 | vec3 totalRlh = vec3(0,0,0);
89 | vec3 totalMie = vec3(0,0,0);
90 |
91 | // Initialize optical depth accumulators for the primary ray.
92 | float iOdRlh = 0.0;
93 | float iOdMie = 0.0;
94 |
95 | // Calculate the Rayleigh and Mie phases.
96 | float mu = dot(r, pSun);
97 | float mumu = mu * mu;
98 | float gg = g * g;
99 | float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
100 | float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
101 |
102 | // Sample the primary ray.
103 | for (int i = 0; i < iSteps; i++) {
104 |
105 | // Calculate the primary ray sample position.
106 | vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);
107 |
108 | // Calculate the height of the sample.
109 | float iHeight = length(iPos) - rPlanet;
110 |
111 | // Calculate the optical depth of the Rayleigh and Mie scattering for this step.
112 | float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
113 | float odStepMie = exp(-iHeight / shMie) * iStepSize;
114 |
115 | // Accumulate optical depth.
116 | iOdRlh += odStepRlh;
117 | iOdMie += odStepMie;
118 |
119 | // Calculate the step size of the secondary ray.
120 | float jStepSize = Rsi(iPos, pSun, rAtmos).y / float(jSteps);
121 |
122 | // Initialize the secondary ray time.
123 | float jTime = 0.0;
124 |
125 | // Initialize optical depth accumulators for the secondary ray.
126 | float jOdRlh = 0.0;
127 | float jOdMie = 0.0;
128 |
129 | // Sample the secondary ray.
130 | for (int j = 0; j < jSteps; j++) {
131 |
132 | // Calculate the secondary ray sample position.
133 | vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
134 |
135 | // Calculate the height of the sample.
136 | float jHeight = length(jPos) - rPlanet;
137 |
138 | // Accumulate the optical depth.
139 | jOdRlh += exp(-jHeight / shRlh) * jStepSize;
140 | jOdMie += exp(-jHeight / shMie) * jStepSize;
141 |
142 | // Increment the secondary ray time.
143 | jTime += jStepSize;
144 | }
145 |
146 | // Calculate attenuation.
147 | vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));
148 |
149 | // Accumulate scattering.
150 | totalRlh += odStepRlh * attn;
151 | totalMie += odStepMie * attn;
152 |
153 | // Increment the primary ray time.
154 | iTime += iStepSize;
155 | }
156 |
157 | // Calculate and return the final color.
158 | return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);
159 | }
160 |
161 | bool IsInside(vec2 pos, vec2 size)
162 | {
163 | return pos.x < size.x && pos.y < size.y;
164 | }
165 |
166 | vec3 GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec2 normalizedDeviceCoords)
167 | {
168 | vec4 rayEye = inverseProj * vec4(normalizedDeviceCoords.xy, -1.0, 0.0);
169 | rayEye.zw = vec2(-1.0, 0.0);
170 | return normalize((inverseView * rayEye).xyz);
171 | }
172 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | [Aa][Rr][Mm]/
24 | [Aa][Rr][Mm]64/
25 | bld/
26 | [Bb]in/
27 | [Oo]bj/
28 | [Ll]og/
29 |
30 | # Visual Studio 2015/2017 cache/options directory
31 | .vs/
32 | # Uncomment if you have tasks that create the project's static files in wwwroot
33 | #wwwroot/
34 |
35 | # Visual Studio 2017 auto generated files
36 | Generated\ Files/
37 |
38 | # MSTest test Results
39 | [Tt]est[Rr]esult*/
40 | [Bb]uild[Ll]og.*
41 |
42 | # NUNIT
43 | *.VisualState.xml
44 | TestResult.xml
45 |
46 | # Build Results of an ATL Project
47 | [Dd]ebugPS/
48 | [Rr]eleasePS/
49 | dlldata.c
50 |
51 | # Benchmark Results
52 | BenchmarkDotNet.Artifacts/
53 |
54 | # .NET Core
55 | project.lock.json
56 | project.fragment.lock.json
57 | artifacts/
58 |
59 | # StyleCop
60 | StyleCopReport.xml
61 |
62 | # Files built by Visual Studio
63 | *_i.c
64 | *_p.c
65 | *_h.h
66 | *.ilk
67 | *.meta
68 | *.obj
69 | *.iobj
70 | *.pch
71 | *.pdb
72 | *.ipdb
73 | *.pgc
74 | *.pgd
75 | *.rsp
76 | *.sbr
77 | *.tlb
78 | *.tli
79 | *.tlh
80 | *.tmp
81 | *.tmp_proj
82 | *_wpftmp.csproj
83 | *.log
84 | *.vspscc
85 | *.vssscc
86 | .builds
87 | *.pidb
88 | *.svclog
89 | *.scc
90 |
91 | # Chutzpah Test files
92 | _Chutzpah*
93 |
94 | # Visual C++ cache files
95 | ipch/
96 | *.aps
97 | *.ncb
98 | *.opendb
99 | *.opensdf
100 | *.sdf
101 | *.cachefile
102 | *.VC.db
103 | *.VC.VC.opendb
104 |
105 | # Visual Studio profiler
106 | *.psess
107 | *.vsp
108 | *.vspx
109 | *.sap
110 |
111 | # Visual Studio Trace Files
112 | *.e2e
113 |
114 | # TFS 2012 Local Workspace
115 | $tf/
116 |
117 | # Guidance Automation Toolkit
118 | *.gpState
119 |
120 | # ReSharper is a .NET coding add-in
121 | _ReSharper*/
122 | *.[Rr]e[Ss]harper
123 | *.DotSettings.user
124 |
125 | # JustCode is a .NET coding add-in
126 | .JustCode
127 |
128 | # TeamCity is a build add-in
129 | _TeamCity*
130 |
131 | # DotCover is a Code Coverage Tool
132 | *.dotCover
133 |
134 | # AxoCover is a Code Coverage Tool
135 | .axoCover/*
136 | !.axoCover/settings.json
137 |
138 | # Visual Studio code coverage results
139 | *.coverage
140 | *.coveragexml
141 |
142 | # NCrunch
143 | _NCrunch_*
144 | .*crunch*.local.xml
145 | nCrunchTemp_*
146 |
147 | # MightyMoose
148 | *.mm.*
149 | AutoTest.Net/
150 |
151 | # Web workbench (sass)
152 | .sass-cache/
153 |
154 | # Installshield output folder
155 | [Ee]xpress/
156 |
157 | # DocProject is a documentation generator add-in
158 | DocProject/buildhelp/
159 | DocProject/Help/*.HxT
160 | DocProject/Help/*.HxC
161 | DocProject/Help/*.hhc
162 | DocProject/Help/*.hhk
163 | DocProject/Help/*.hhp
164 | DocProject/Help/Html2
165 | DocProject/Help/html
166 |
167 | # Click-Once directory
168 | publish/
169 |
170 | # Publish Web Output
171 | *.[Pp]ublish.xml
172 | *.azurePubxml
173 | # Note: Comment the next line if you want to checkin your web deploy settings,
174 | # but database connection strings (with potential passwords) will be unencrypted
175 | *.pubxml
176 | *.publishproj
177 |
178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
179 | # checkin your Azure Web App publish settings, but sensitive information contained
180 | # in these scripts will be unencrypted
181 | PublishScripts/
182 |
183 | # NuGet Packages
184 | *.nupkg
185 | # The packages folder can be ignored because of Package Restore
186 | **/[Pp]ackages/*
187 | # except build/, which is used as an MSBuild target.
188 | !**/[Pp]ackages/build/
189 | # Uncomment if necessary however generally it will be regenerated when needed
190 | #!**/[Pp]ackages/repositories.config
191 | # NuGet v3's project.json files produces more ignorable files
192 | *.nuget.props
193 | *.nuget.targets
194 |
195 | # Microsoft Azure Build Output
196 | csx/
197 | *.build.csdef
198 |
199 | # Microsoft Azure Emulator
200 | ecf/
201 | rcf/
202 |
203 | # Windows Store app package directories and files
204 | AppPackages/
205 | BundleArtifacts/
206 | Package.StoreAssociation.xml
207 | _pkginfo.txt
208 | *.appx
209 |
210 | # Visual Studio cache files
211 | # files ending in .cache can be ignored
212 | *.[Cc]ache
213 | # but keep track of directories ending in .cache
214 | !?*.[Cc]ache/
215 |
216 | # Others
217 | ClientBin/
218 | ~$*
219 | *~
220 | *.dbmdl
221 | *.dbproj.schemaview
222 | *.jfm
223 | *.pfx
224 | *.publishsettings
225 | orleans.codegen.cs
226 |
227 | # Including strong name files can present a security risk
228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
229 | #*.snk
230 |
231 | # Since there are multiple workflows, uncomment next line to ignore bower_components
232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
233 | #bower_components/
234 |
235 | # RIA/Silverlight projects
236 | Generated_Code/
237 |
238 | # Backup & report files from converting an old project file
239 | # to a newer Visual Studio version. Backup files are not needed,
240 | # because we have git ;-)
241 | _UpgradeReport_Files/
242 | Backup*/
243 | UpgradeLog*.XML
244 | UpgradeLog*.htm
245 | ServiceFabricBackup/
246 | *.rptproj.bak
247 |
248 | # SQL Server files
249 | *.mdf
250 | *.ldf
251 | *.ndf
252 |
253 | # Business Intelligence projects
254 | *.rdl.data
255 | *.bim.layout
256 | *.bim_*.settings
257 | *.rptproj.rsuser
258 | *- Backup*.rdl
259 |
260 | # Microsoft Fakes
261 | FakesAssemblies/
262 |
263 | # GhostDoc plugin setting file
264 | *.GhostDoc.xml
265 |
266 | # Node.js Tools for Visual Studio
267 | .ntvs_analysis.dat
268 | node_modules/
269 |
270 | # Visual Studio 6 build log
271 | *.plg
272 |
273 | # Visual Studio 6 workspace options file
274 | *.opt
275 |
276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
277 | *.vbw
278 |
279 | # Visual Studio LightSwitch build output
280 | **/*.HTMLClient/GeneratedArtifacts
281 | **/*.DesktopClient/GeneratedArtifacts
282 | **/*.DesktopClient/ModelManifest.xml
283 | **/*.Server/GeneratedArtifacts
284 | **/*.Server/ModelManifest.xml
285 | _Pvt_Extensions
286 |
287 | # Paket dependency manager
288 | .paket/paket.exe
289 | paket-files/
290 |
291 | # FAKE - F# Make
292 | .fake/
293 |
294 | # JetBrains Rider
295 | .idea/
296 | *.sln.iml
297 |
298 | # CodeRush personal settings
299 | .cr/personal
300 |
301 | # Python Tools for Visual Studio (PTVS)
302 | __pycache__/
303 | *.pyc
304 |
305 | # Cake - Uncomment if you are using it
306 | # tools/**
307 | # !tools/packages.config
308 |
309 | # Tabs Studio
310 | *.tss
311 |
312 | # Telerik's JustMock configuration file
313 | *.jmconfig
314 |
315 | # BizTalk build output
316 | *.btp.cs
317 | *.btm.cs
318 | *.odx.cs
319 | *.xsd.cs
320 |
321 | # OpenCover UI analysis results
322 | OpenCover/
323 |
324 | # Azure Stream Analytics local run output
325 | ASALocalRun/
326 |
327 | # MSBuild Binary and Structured Log
328 | *.binlog
329 |
330 | # NVidia Nsight GPU debugger configuration file
331 | *.nvuser
332 |
333 | # MFractors (Xamarin productivity tool) working folder
334 | .mfractor/
335 |
336 | # Local History for Visual Studio
337 | .localhistory/
338 |
339 | # BeatPulse healthcheck temp database
340 | healthchecksdb
341 |
342 | imgui.ini
343 | OpenTK-PathTracer/res/imgui.ini
344 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Objects/ShaderProgram.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Linq;
4 | using System.Text;
5 | using OpenTK;
6 | using OpenTK.Graphics.OpenGL4;
7 |
8 | namespace OpenTK_PathTracer.Render.Objects
9 | {
10 | struct Shader : IDisposable
11 | {
12 | public readonly int ID;
13 | public readonly ShaderType ShaderType;
14 |
15 | public Shader(ShaderType shaderType, string sourceCode)
16 | {
17 | ShaderType = shaderType;
18 |
19 | ID = GL.CreateShader(shaderType);
20 |
21 | sourceCode = PreProcessIncludes(sourceCode);
22 | GL.ShaderSource(ID, sourceCode);
23 | GL.CompileShader(ID);
24 |
25 | string compileInfo = GL.GetShaderInfoLog(ID);
26 | if (compileInfo != string.Empty)
27 | Console.WriteLine(compileInfo);
28 | }
29 |
30 | ///
31 | /// Searches the string for #include and includes the specified Path. Example: #include PathTracing/fragCompute
32 | ///
33 | ///
34 | ///
35 | private static string PreProcessIncludes(string s)
36 | {
37 | StringBuilder includedContent = new StringBuilder(s.Length + 2000);
38 | using StringReader stringReader = new StringReader(s);
39 |
40 | string line;
41 | while ((line = stringReader.ReadLine()) is not null) // dont use != because it could be overriden
42 | {
43 | string trimmed = line.Trim();
44 | if (trimmed.Length > 9 && trimmed.Substring(0, 9) == "#include ")
45 | {
46 | string filePath = $"{Helper.SHADER_DIRECTORY_PATH}{trimmed.Substring(9, trimmed.Length - 9)}.glsl";
47 | includedContent.Append(PreProcessIncludes(File.ReadAllText(filePath)));
48 | }
49 | else
50 | {
51 | includedContent.AppendLine(line);
52 | }
53 | }
54 | return includedContent.ToString();
55 | }
56 |
57 | public void Dispose()
58 | {
59 | GL.DeleteShader(ID);
60 | }
61 | }
62 |
63 | class ShaderProgram : IDisposable
64 | {
65 | private static int lastBindedID = -1;
66 |
67 | public readonly int ID;
68 | public ShaderProgram(params Shader[] shaders)
69 | {
70 | if (shaders is null || shaders.Length == 0 || !shaders.All(s => s.ID != 0))
71 | throw new IndexOutOfRangeException($"Shader array is empty or null. Or at least one shader has ID 0");
72 |
73 | if(!shaders.All(s => shaders.All(s1 => s.ID == s1.ID || s1.ShaderType != s.ShaderType)))
74 | throw new Exception($"A ShaderProgram can only hold one instance of every ShaderType. Validate the shader array.");
75 |
76 | ID = GL.CreateProgram();
77 |
78 | for (int i = 0; i < shaders.Length; i++)
79 | GL.AttachShader(ID, shaders[i].ID);
80 |
81 | GL.LinkProgram(ID);
82 | for (int i = 0; i < shaders.Length; i++)
83 | {
84 | GL.DetachShader(ID, shaders[i].ID);
85 | shaders[i].Dispose();
86 | }
87 | }
88 |
89 | public void Use()
90 | {
91 | if (lastBindedID != ID)
92 | {
93 | GL.UseProgram(ID);
94 | lastBindedID = ID;
95 | }
96 | }
97 |
98 | public static void Use(int id)
99 | {
100 | if (lastBindedID != id)
101 | {
102 | GL.UseProgram(id);
103 | lastBindedID = id;
104 | }
105 | }
106 |
107 | public static void UploadToProgram(int id, int location, Matrix4 matrix4, bool transpose = false)
108 | {
109 | GL.ProgramUniformMatrix4(id, location, transpose, ref matrix4);
110 | }
111 | public void Upload(int location, Matrix4 matrix4, bool transpose = false)
112 | {
113 | GL.ProgramUniformMatrix4(ID, location, transpose, ref matrix4);
114 | }
115 | public void Upload(string name, Matrix4 matrix4, bool transpose = false)
116 | {
117 | GL.ProgramUniformMatrix4(ID, GetUniformLocation(name), transpose, ref matrix4);
118 | }
119 |
120 | public static void UploadToProgram(int id, int location, Vector4 vector4)
121 | {
122 | GL.ProgramUniform4(id, location, vector4);
123 | }
124 | public void Upload(int location, Vector4 vector4)
125 | {
126 | GL.ProgramUniform4(ID, location, vector4);
127 | }
128 | public void Upload(string name, Vector4 vector4)
129 | {
130 | GL.ProgramUniform4(ID, GetUniformLocation(name), vector4);
131 | }
132 |
133 | public static void UploadToProgram(int id, int location, Vector3 vector3)
134 | {
135 | GL.ProgramUniform3(id, location, vector3);
136 | }
137 | public void Upload(int location, Vector3 vector3)
138 | {
139 | GL.ProgramUniform3(ID, location, vector3);
140 | }
141 | public void Upload(string name, Vector3 vector3)
142 | {
143 | GL.ProgramUniform3(ID, GetUniformLocation(name), vector3);
144 | }
145 |
146 | public static void UploadToProgram(int id, int location, Vector2 vector2)
147 | {
148 | GL.ProgramUniform2(id, location, vector2);
149 | }
150 | public void Upload(int location, Vector2 vector2)
151 | {
152 | GL.ProgramUniform2(ID, location, vector2);
153 | }
154 | public void Upload(string name, Vector2 vector2)
155 | {
156 | GL.ProgramUniform2(ID, GetUniformLocation(name), vector2);
157 | }
158 |
159 | public static void UploadToProgram(int id, int location, float x)
160 | {
161 | GL.ProgramUniform1(id, location, x);
162 | }
163 | public void Upload(int location, float x)
164 | {
165 | GL.ProgramUniform1(ID, location, x);
166 | }
167 | public void Upload(string name, float x)
168 | {
169 | GL.ProgramUniform1(ID, GetUniformLocation(name), x);
170 | }
171 |
172 | public static void UploadToProgram(int id, int location, int x)
173 | {
174 | GL.ProgramUniform1(id, location, x);
175 | }
176 | public void Upload(int location, int x)
177 | {
178 | GL.ProgramUniform1(ID, location, x);
179 | }
180 | public void Upload(string name, int x)
181 | {
182 | GL.ProgramUniform1(ID, GetUniformLocation(name), x);
183 | }
184 |
185 | public static void UploadToProgram(int id, int location, uint x)
186 | {
187 | GL.ProgramUniform1(id, location, x);
188 | }
189 | public void Upload(int location, uint x)
190 | {
191 | GL.ProgramUniform1(ID, location, x);
192 | }
193 | public void Upload(string name, uint x)
194 | {
195 | GL.ProgramUniform1(ID, GetUniformLocation(name), x);
196 | }
197 |
198 | public static void UploadToProgram(int id, int location, bool x)
199 | {
200 | GL.ProgramUniform1(id, location, x ? 1 : 0);
201 | }
202 | public void Upload(int location, bool x)
203 | {
204 | GL.ProgramUniform1(ID, location, x ? 1 : 0);
205 | }
206 | public void Upload(string name, bool x)
207 | {
208 | GL.ProgramUniform1(ID, GetUniformLocation(name), x ? 1 : 0);
209 | }
210 |
211 | public int GetUniformLocation(string name)
212 | {
213 | return GL.GetUniformLocation(ID, name);
214 | }
215 |
216 |
217 | public void Dispose()
218 | {
219 | GL.DeleteProgram(ID);
220 | }
221 | }
222 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/ImGui/ImGuiController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Runtime.CompilerServices;
4 | using OpenTK;
5 | using OpenTK.Graphics.OpenGL4;
6 | using OpenTK.Input;
7 | using OpenTK_PathTracer.Render.Objects;
8 | using ImGuiNET;
9 |
10 | namespace OpenTK_PathTracer.GUI
11 | {
12 | ///
13 | /// This ImGui wrapper is from here.
14 | /// I modified it to make it work tighter with my project
15 | ///
16 | public class ImGuiController : IDisposable
17 | {
18 | private bool frameBegun;
19 |
20 | private VAO vao;
21 | private ShaderProgram shaderProgram;
22 | private Texture fontTexture;
23 | private BufferObject vbo;
24 | private BufferObject ebo;
25 |
26 | private int Width;
27 | private int Height;
28 |
29 | private System.Numerics.Vector2 scaleFactor = System.Numerics.Vector2.One;
30 | public ImGuiController(int width, int height)
31 | {
32 | Width = width;
33 | Height = height;
34 |
35 | IntPtr context = ImGui.CreateContext();
36 | ImGui.SetCurrentContext(context);
37 |
38 | ImGuiIOPtr io = ImGui.GetIO();
39 | io.Fonts.AddFontDefault();
40 | io.BackendFlags |= ImGuiBackendFlags.RendererHasVtxOffset;
41 |
42 | CreateDeviceResources();
43 | SetKeyMappings();
44 |
45 | SetPerFrameImGuiData(1f / 60f);
46 |
47 | ImGui.NewFrame();
48 | frameBegun = true;
49 | }
50 |
51 | public void WindowResized(int width, int height)
52 | {
53 | Width = width;
54 | Height = height;
55 | }
56 |
57 | private void CreateDeviceResources()
58 | {
59 | vbo = new BufferObject();
60 | vbo.MutableAllocate(10000, IntPtr.Zero, BufferUsageHint.DynamicDraw);
61 | ebo = new BufferObject();
62 | ebo.MutableAllocate(2000, IntPtr.Zero, BufferUsageHint.DynamicDraw);
63 |
64 | CreateFontDeviceTexture();
65 |
66 | string vertexSource = @"#version 330 core
67 |
68 | uniform mat4 projection_matrix;
69 |
70 | layout(location = 0) in vec2 in_position;
71 | layout(location = 1) in vec2 in_texCoord;
72 | layout(location = 2) in vec4 in_color;
73 |
74 | out vec4 color;
75 | out vec2 texCoord;
76 |
77 | void main()
78 | {
79 | gl_Position = projection_matrix * vec4(in_position, 0, 1);
80 | color = in_color;
81 | texCoord = in_texCoord;
82 | }";
83 | string fragmentSource = @"#version 330 core
84 |
85 | uniform sampler2D in_fontTexture;
86 |
87 | in vec4 color;
88 | in vec2 texCoord;
89 |
90 | out vec4 outputColor;
91 |
92 | void main()
93 | {
94 | outputColor = color * texture(in_fontTexture, texCoord);
95 | }";
96 |
97 | shaderProgram = new ShaderProgram(new Shader(ShaderType.VertexShader, vertexSource), new Shader(ShaderType.FragmentShader, fragmentSource));
98 |
99 | vao = new VAO(ebo);
100 | vao.AddSourceBuffer(vbo, 0, Unsafe.SizeOf());
101 | vao.SetAttribFormat(0, 0, 2, VertexAttribType.Float, 0 * sizeof(float));
102 | vao.SetAttribFormat(0, 1, 2, VertexAttribType.Float, 2 * sizeof(float));
103 | vao.SetAttribFormat(0, 2, 4, VertexAttribType.UnsignedByte, 4 * sizeof(float), true);
104 | }
105 |
106 | private void CreateFontDeviceTexture()
107 | {
108 | ImGuiIOPtr io = ImGui.GetIO();
109 | io.Fonts.GetTexDataAsRGBA32(out IntPtr pixels, out int width, out int height, out _);
110 |
111 | fontTexture = new Texture(TextureTarget2d.Texture2D);
112 | fontTexture.SetFilter(TextureMinFilter.Nearest, TextureMagFilter.Nearest);
113 | fontTexture.ImmutableAllocate(width, height, 1, SizedInternalFormat.Rgba8);
114 | fontTexture.SubTexture2D(width, height, PixelFormat.Bgra, PixelType.UnsignedByte, pixels);
115 |
116 |
117 | io.Fonts.SetTexID((IntPtr)fontTexture.ID);
118 | io.Fonts.ClearTexData();
119 | }
120 |
121 | ///
122 | /// Renders the ImGui draw list data.
123 | /// This method requires a because it may create new DeviceBuffers if the size of vertex
124 | /// or index data has increased beyond the capacity of the existing buffers.
125 | /// A is needed to submit drawing and resource update commands.
126 | ///
127 | public void Render()
128 | {
129 | if (frameBegun)
130 | {
131 | frameBegun = false;
132 | ImGui.Render();
133 | RenderImDrawData(ImGui.GetDrawData());
134 | }
135 | }
136 |
137 | ///
138 | /// Updates ImGui input and IO configuration state.
139 | ///
140 | public void Update(GameWindow wnd, float deltaSeconds)
141 | {
142 | if (frameBegun)
143 | ImGui.Render();
144 |
145 | SetPerFrameImGuiData(deltaSeconds);
146 | UpdateImGuiInput(wnd);
147 |
148 | frameBegun = true;
149 | ImGui.NewFrame();
150 | }
151 |
152 | ///
153 | /// Sets per-frame data based on the associated window.
154 | /// This is called by Update(float).
155 | ///
156 | private void SetPerFrameImGuiData(float deltaSeconds)
157 | {
158 | ImGuiIOPtr io = ImGui.GetIO();
159 | io.DisplaySize = new System.Numerics.Vector2(Width / scaleFactor.X, Height / scaleFactor.Y);
160 | io.DisplayFramebufferScale = scaleFactor;
161 | io.DeltaTime = deltaSeconds;
162 | }
163 |
164 | private readonly List pressedChars = new List();
165 | private void UpdateImGuiInput(GameWindow wnd)
166 | {
167 | ImGuiIOPtr io = ImGui.GetIO();
168 |
169 | io.MouseDown[0] = MouseManager.IsButtonDown(MouseButton.Left);
170 | io.MouseDown[1] = MouseManager.IsButtonDown(MouseButton.Right);
171 | io.MouseDown[2] = MouseManager.IsButtonDown(MouseButton.Middle);
172 |
173 | System.Drawing.Point screenPoint = new System.Drawing.Point(MouseManager.WindowPositionX, MouseManager.WindowPositionY);
174 | System.Drawing.Point point = wnd.PointToClient(screenPoint);
175 | io.MousePos = new System.Numerics.Vector2(point.X, point.Y);
176 |
177 | io.MouseWheel = MouseManager.DeltaScrollY;
178 | io.MouseWheelH = MouseManager.DeltaScrollX;
179 |
180 | foreach (Key key in Enum.GetValues(typeof(Key)))
181 | io.KeysDown[(int)key] = KeyboardManager.IsKeyDown(key);
182 |
183 | for (int i = 0; i < pressedChars.Count; i++)
184 | io.AddInputCharacter(pressedChars[i]);
185 |
186 | pressedChars.Clear();
187 |
188 | io.KeyCtrl = KeyboardManager.IsKeyDown(Key.ControlLeft) || KeyboardManager.IsKeyDown(Key.ControlRight);
189 | io.KeyAlt = KeyboardManager.IsKeyDown(Key.AltLeft) || KeyboardManager.IsKeyDown(Key.AltRight);
190 | io.KeyShift = KeyboardManager.IsKeyDown(Key.ShiftLeft) || KeyboardManager.IsKeyDown(Key.ShiftRight);
191 | io.KeySuper = KeyboardManager.IsKeyDown(Key.WinLeft) || KeyboardManager.IsKeyDown(Key.WinRight);
192 | }
193 |
194 | public void PressChar(char keyChar)
195 | {
196 | pressedChars.Add(keyChar);
197 | }
198 |
199 | private static void SetKeyMappings()
200 | {
201 | ImGuiIOPtr io = ImGui.GetIO();
202 | io.KeyMap[(int)ImGuiKey.Tab] = (int)Key.Tab;
203 | io.KeyMap[(int)ImGuiKey.LeftArrow] = (int)Key.Left;
204 | io.KeyMap[(int)ImGuiKey.RightArrow] = (int)Key.Right;
205 | io.KeyMap[(int)ImGuiKey.UpArrow] = (int)Key.Up;
206 | io.KeyMap[(int)ImGuiKey.DownArrow] = (int)Key.Down;
207 | io.KeyMap[(int)ImGuiKey.PageUp] = (int)Key.PageUp;
208 | io.KeyMap[(int)ImGuiKey.PageDown] = (int)Key.PageDown;
209 | io.KeyMap[(int)ImGuiKey.Home] = (int)Key.Home;
210 | io.KeyMap[(int)ImGuiKey.End] = (int)Key.End;
211 | io.KeyMap[(int)ImGuiKey.Delete] = (int)Key.Delete;
212 | io.KeyMap[(int)ImGuiKey.Backspace] = (int)Key.BackSpace;
213 | io.KeyMap[(int)ImGuiKey.Enter] = (int)Key.Enter;
214 | io.KeyMap[(int)ImGuiKey.Escape] = (int)Key.Escape;
215 | io.KeyMap[(int)ImGuiKey.A] = (int)Key.A;
216 | io.KeyMap[(int)ImGuiKey.C] = (int)Key.C;
217 | io.KeyMap[(int)ImGuiKey.V] = (int)Key.V;
218 | io.KeyMap[(int)ImGuiKey.X] = (int)Key.X;
219 | io.KeyMap[(int)ImGuiKey.Y] = (int)Key.Y;
220 | io.KeyMap[(int)ImGuiKey.Z] = (int)Key.Z;
221 | }
222 |
223 | private void RenderImDrawData(ImDrawDataPtr drawData)
224 | {
225 | if (drawData.CmdListsCount == 0)
226 | return;
227 |
228 | for (int i = 0; i < drawData.CmdListsCount; i++)
229 | {
230 | ImDrawListPtr cmdList = drawData.CmdListsRange[i];
231 | int vertexSize = cmdList.VtxBuffer.Size * Unsafe.SizeOf();
232 | if (vertexSize > vbo.Size)
233 | {
234 | int newSize = (int)Math.Max(vbo.Size * 1.5f, vertexSize);
235 | vbo.MutableAllocate(newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
236 | }
237 |
238 | int indexSize = cmdList.IdxBuffer.Size * sizeof(ushort);
239 | if (indexSize > ebo.Size)
240 | {
241 | int newSize = (int)Math.Max(ebo.Size * 1.5f, indexSize);
242 | ebo.MutableAllocate(newSize, IntPtr.Zero, BufferUsageHint.DynamicDraw);
243 | }
244 | }
245 |
246 | ImGuiIOPtr io = ImGui.GetIO();
247 | Matrix4 mvp = Matrix4.CreateOrthographicOffCenter(0.0f, io.DisplaySize.X, io.DisplaySize.Y, 0.0f, -1.0f, 1.0f);
248 |
249 | shaderProgram.Use();
250 | shaderProgram.Upload("projection_matrix", mvp);
251 | shaderProgram.Upload("in_fontTexture", 0);
252 |
253 | vao.Bind();
254 |
255 | drawData.ScaleClipRects(io.DisplayFramebufferScale);
256 |
257 | GL.Enable(EnableCap.Blend);
258 | GL.Enable(EnableCap.ScissorTest);
259 | GL.BlendEquation(BlendEquationMode.FuncAdd);
260 | GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
261 | //GL.BlendFuncSeparate(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha, BlendingFactorSrc.One, BlendingFactorDest.One);
262 |
263 | for (int i = 0; i < drawData.CmdListsCount; i++)
264 | {
265 | ImDrawListPtr cmd_list = drawData.CmdListsRange[i];
266 |
267 | vbo.SubData(0, cmd_list.VtxBuffer.Size * Unsafe.SizeOf(), cmd_list.VtxBuffer.Data);
268 | ebo.SubData(0, cmd_list.IdxBuffer.Size * sizeof(ushort), cmd_list.IdxBuffer.Data);
269 |
270 | int idx_offset = 0;
271 |
272 | for (int cmd_i = 0; cmd_i < cmd_list.CmdBuffer.Size; cmd_i++)
273 | {
274 | ImDrawCmdPtr pcmd = cmd_list.CmdBuffer[cmd_i];
275 | if (pcmd.UserCallback != IntPtr.Zero)
276 | {
277 | throw new NotImplementedException();
278 | }
279 | else
280 | {
281 | GL.BindTextureUnit(0, (int)pcmd.TextureId);
282 |
283 | // We do _windowHeight - (int)clip.W instead of (int)clip.Y because gl has flipped Y when it comes to these coordinates
284 | var clip = pcmd.ClipRect;
285 | GL.Scissor((int)clip.X, Height - (int)clip.W, (int)(clip.Z - clip.X), (int)(clip.W - clip.Y));
286 |
287 | if ((io.BackendFlags & ImGuiBackendFlags.RendererHasVtxOffset) != 0)
288 | GL.DrawElementsBaseVertex(PrimitiveType.Triangles, (int)pcmd.ElemCount, DrawElementsType.UnsignedShort, (IntPtr)(idx_offset * sizeof(ushort)), 0);
289 | else
290 | GL.DrawElements(BeginMode.Triangles, (int)pcmd.ElemCount, DrawElementsType.UnsignedShort, (int)pcmd.IdxOffset * sizeof(ushort));
291 | }
292 |
293 | idx_offset += (int)pcmd.ElemCount;
294 | }
295 | }
296 | GL.Disable(EnableCap.Blend);
297 | GL.Disable(EnableCap.ScissorTest);
298 | }
299 |
300 | ///
301 | /// Frees all graphics resources used by the renderer.
302 | ///
303 | public void Dispose()
304 | {
305 | fontTexture.Dispose();
306 | shaderProgram.Dispose();
307 | vao.Dispose();
308 | }
309 | }
310 | }
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/PathTracing/fragCompute.glsl:
--------------------------------------------------------------------------------
1 | #version 450 core
2 | #define FLOAT_MAX 3.4028235e+38
3 | #define FLOAT_MIN -3.4028235e+38
4 | #define EPSILON 0.001
5 | #define PI 3.14159265
6 | // Example shader include: #include PathTracing/fragCompute
7 |
8 | layout(location = 0) out vec4 FragColor;
9 |
10 | layout(binding = 0) uniform sampler2D SamplerLastFrame;
11 | layout(binding = 1) uniform samplerCube SamplerEnvironment;
12 |
13 | struct Material
14 | {
15 | vec3 Albedo; // Base color
16 | float SpecularChance; // How reflective
17 |
18 | vec3 Emissiv; // How much light is emitted
19 | float SpecularRoughness; // How rough reflections are
20 |
21 | vec3 Absorbance; // How strongly light is absorbed
22 | float RefractionChance; // How transparent
23 |
24 | float RefractionRoughness; // How rough refractions are
25 | float IOR; // How strongly light gets refracted and the amout of light that is reflected
26 | };
27 |
28 | struct Cuboid
29 | {
30 | vec3 Min;
31 | vec3 Max;
32 |
33 | Material Material;
34 | };
35 |
36 | struct Sphere
37 | {
38 | vec3 Position;
39 | float Radius;
40 |
41 | Material Material;
42 | };
43 |
44 | struct HitInfo
45 | {
46 | float T;
47 | bool FromInside;
48 | vec3 NearHitPos;
49 | vec3 Normal;
50 | Material Material;
51 | };
52 |
53 | struct Ray
54 | {
55 | vec3 Origin;
56 | vec3 Direction;
57 | };
58 |
59 | layout(std140, binding = 0) uniform BasicDataUBO
60 | {
61 | mat4 InvProjection;
62 | mat4 InvView;
63 | vec3 ViewPos;
64 | } basicDataUBO;
65 |
66 | layout(std140, binding = 1) uniform GameObjectsUBO
67 | {
68 | Sphere Spheres[256];
69 | Cuboid Cuboids[64];
70 | } gameObjectsUBO;
71 |
72 | in InOutVars
73 | {
74 | vec2 TexCoord;
75 | } inData;
76 |
77 | vec3 Radiance(Ray ray);
78 | float BSDF(inout Ray ray, HitInfo hitInfo, out bool isRefractive);
79 | bool RayTrace(Ray ray, out HitInfo hitInfo);
80 | bool RaySphereIntersect(Ray ray, Sphere sphere, out float t1, out float t2);
81 | bool RayCuboidIntersect(Ray ray, Cuboid cuboid, out float t1, out float t2);
82 | vec3 CosineSampleHemisphere(vec3 normal);
83 | vec2 UniformSampleUnitCircle();
84 | vec3 GetNormal(Sphere sphere, vec3 surfacePosition);
85 | vec3 GetNormal(Cuboid cuboid, vec3 surfacePosition);
86 | uint GetPCGHash(inout uint seed);
87 | float GetRandomFloat01();
88 | float GetSmallestPositive(float t1, float t2);
89 | Ray GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec3 viewPos, vec2 normalizedDeviceCoords);
90 | float FresnelSchlick(float cosTheta, float n1, float n2);
91 | vec3 InverseGammaToLinear(vec3 rgb);
92 |
93 | uniform vec2 uboGameObjectsSize;
94 |
95 | uniform int rayDepth;
96 | uniform int SPP;
97 |
98 | uniform float focalLength;
99 | uniform float apertureDiameter;
100 |
101 | layout(location = 0) uniform int thisRendererFrame;
102 |
103 | uint rndSeed;
104 | void main()
105 | {
106 | vec2 txtResultSize = vec2(textureSize(SamplerLastFrame, 0));
107 |
108 | rndSeed = uint(gl_FragCoord.x) * 1973 + uint(gl_FragCoord.y) * 9277 + thisRendererFrame * 2699 | 1;
109 | //rndSeed = thisRendererFrame;
110 |
111 | vec3 irradiance = vec3(0.0);
112 | for (int i = 0; i < SPP; i++)
113 | {
114 | // add random offset to lower left corner of pixel to effectively integrate over whole pixel and eliminate aliasing
115 | vec2 subPixelOffset = (vec2(GetRandomFloat01(), GetRandomFloat01()) - 0.5) / txtResultSize;
116 | vec2 ndc = (inData.TexCoord + subPixelOffset) * 2.0 - 1.0;
117 | Ray rayEyeToWorld = GetWorldSpaceRay(basicDataUBO.InvProjection, basicDataUBO.InvView, basicDataUBO.ViewPos, ndc);
118 |
119 | vec3 focalPoint = rayEyeToWorld.Origin + rayEyeToWorld.Direction * focalLength;
120 | vec2 offset = apertureDiameter * 0.5 * UniformSampleUnitCircle();
121 |
122 | rayEyeToWorld.Origin = (basicDataUBO.InvView * vec4(offset, 0.0, 1.0)).xyz;
123 | rayEyeToWorld.Direction = normalize(focalPoint - rayEyeToWorld.Origin);
124 |
125 | irradiance += Radiance(rayEyeToWorld);
126 | }
127 | irradiance /= SPP;
128 | vec3 lastFrameColor = texture(SamplerLastFrame, inData.TexCoord).rgb;
129 |
130 | irradiance = mix(lastFrameColor, irradiance, 1.0 / (thisRendererFrame + 1));
131 | FragColor = vec4(irradiance, 1.0);
132 | }
133 |
134 | vec3 Radiance(Ray ray)
135 | {
136 | vec3 throughput = vec3(1.0);
137 | vec3 radiance = vec3(0.0);
138 |
139 | HitInfo hitInfo;
140 | bool isRefractive;
141 | float rayProbability;
142 | for (int i = 0; i < rayDepth; i++)
143 | {
144 | if (RayTrace(ray, hitInfo))
145 | {
146 | // If ray did just pass through medium apply Beer's law
147 | if (hitInfo.FromInside)
148 | {
149 | hitInfo.Normal *= -1.0;
150 | throughput *= exp(-hitInfo.Material.Absorbance * hitInfo.T);
151 | }
152 |
153 | // Evaluating BSDF gives a new ray based on the hitPoints properties and the incomming ray,
154 | // the probability this ray would take its path
155 | // and a bool indicating wheter the ray penetrates into the medium
156 | rayProbability = BSDF(ray, hitInfo, isRefractive);
157 |
158 | radiance += hitInfo.Material.Emissiv * throughput;
159 | if (!isRefractive)
160 | {
161 | // The cosine term is already taken into account by the CosineSampleHemisphere function. Its weighting the random rays to a cosine distibution
162 | // throughput *= hitInfo.Material.Albedo * dot(ray.Direction, hitInfo.Normal);
163 |
164 | throughput *= hitInfo.Material.Albedo;
165 | }
166 | throughput /= rayProbability;
167 |
168 | // Russian Roulette, unbiased method to terminate rays and therefore lower render times (also reduces fireflies)
169 | {
170 | float p = max(throughput.x, max(throughput.y, throughput.z));
171 | if (GetRandomFloat01() > p)
172 | break;
173 |
174 | throughput /= p;
175 | }
176 | }
177 | else
178 | {
179 | radiance += texture(SamplerEnvironment, ray.Direction).rgb * throughput;
180 | break;
181 | }
182 | }
183 | return radiance;
184 | }
185 |
186 | float BSDF(inout Ray ray, HitInfo hitInfo, out bool isRefractive)
187 | {
188 | isRefractive = false;
189 |
190 | float specularChance = hitInfo.Material.SpecularChance;
191 | float refractionChance = hitInfo.Material.RefractionChance;
192 | if (specularChance > 0.0)
193 | {
194 | specularChance = mix(specularChance, 1.0, FresnelSchlick(dot(-ray.Direction, hitInfo.Normal), hitInfo.FromInside ? hitInfo.Material.IOR : 1.0, !hitInfo.FromInside ? hitInfo.Material.IOR : 1.0));
195 | float diffuseChance = 1.0 - specularChance - refractionChance;
196 | refractionChance = 1.0 - specularChance - diffuseChance;
197 | }
198 |
199 | vec3 diffuseRay = CosineSampleHemisphere(hitInfo.Normal);
200 | float rayProbability = 1.0;
201 |
202 | float raySelectRoll = GetRandomFloat01();
203 | if (specularChance > raySelectRoll)
204 | {
205 | vec3 reflectionRayDir = reflect(ray.Direction, hitInfo.Normal);
206 | reflectionRayDir = normalize(mix(reflectionRayDir, diffuseRay, hitInfo.Material.SpecularRoughness * hitInfo.Material.SpecularRoughness));
207 | ray.Direction = reflectionRayDir;
208 | rayProbability = specularChance;
209 | }
210 | else if (specularChance + refractionChance > raySelectRoll)
211 | {
212 | vec3 refractionRayDir = refract(ray.Direction, hitInfo.Normal, hitInfo.FromInside ? (hitInfo.Material.IOR / 1.0) : (1.0 / hitInfo.Material.IOR));
213 | refractionRayDir = normalize(mix(refractionRayDir, CosineSampleHemisphere(-hitInfo.Normal), hitInfo.Material.RefractionRoughness * hitInfo.Material.RefractionRoughness));
214 | ray.Direction = refractionRayDir;
215 | rayProbability = refractionChance;
216 | isRefractive = true;
217 | }
218 | else
219 | {
220 | ray.Direction = diffuseRay;
221 | rayProbability = 1.0 - specularChance - refractionChance;
222 | }
223 |
224 | ray.Origin = hitInfo.NearHitPos + ray.Direction * EPSILON;
225 | return max(rayProbability, EPSILON);
226 | }
227 |
228 | bool RayTrace(Ray ray, out HitInfo hitInfo)
229 | {
230 | hitInfo.T = FLOAT_MAX;
231 | float t1, t2;
232 |
233 | for (int i = 0; i < uboGameObjectsSize.x; i++)
234 | {
235 | Sphere sphere = gameObjectsUBO.Spheres[i];
236 | if (RaySphereIntersect(ray, sphere, t1, t2) && t2 > 0.0 && t1 < hitInfo.T)
237 | {
238 | hitInfo.T = GetSmallestPositive(t1, t2);
239 | hitInfo.FromInside = hitInfo.T == t2;
240 | hitInfo.Material = gameObjectsUBO.Spheres[i].Material;
241 | hitInfo.NearHitPos = ray.Origin + ray.Direction * hitInfo.T;
242 | hitInfo.Normal = GetNormal(sphere, hitInfo.NearHitPos);
243 | }
244 | }
245 |
246 | for (int i = 0; i < uboGameObjectsSize.y; i++)
247 | {
248 | Cuboid cuboid = gameObjectsUBO.Cuboids[i];
249 | if (RayCuboidIntersect(ray, cuboid, t1, t2) && t2 > 0.0 && t1 < hitInfo.T)
250 | {
251 | hitInfo.T = GetSmallestPositive(t1, t2);
252 | hitInfo.FromInside = hitInfo.T == t2;
253 | hitInfo.Material = gameObjectsUBO.Cuboids[i].Material;
254 | hitInfo.NearHitPos = ray.Origin + ray.Direction * hitInfo.T;
255 | hitInfo.Normal = GetNormal(cuboid, hitInfo.NearHitPos);
256 | }
257 | }
258 |
259 | return hitInfo.T != FLOAT_MAX;
260 | }
261 |
262 | // Source: https://antongerdelan.net/opengl/raycasting.html
263 | bool RaySphereIntersect(Ray ray, Sphere sphere, out float t1, out float t2)
264 | {
265 | t1 = t2 = FLOAT_MAX;
266 |
267 | vec3 sphereToRay = ray.Origin - sphere.Position;
268 | float b = dot(ray.Direction, sphereToRay);
269 | float c = dot(sphereToRay, sphereToRay) - sphere.Radius * sphere.Radius;
270 | float discriminant = b * b - c;
271 | if (discriminant < 0.0)
272 | return false;
273 |
274 | float squareRoot = sqrt(discriminant);
275 | t1 = -b - squareRoot;
276 | t2 = -b + squareRoot;
277 |
278 | return t1 <= t2;
279 | }
280 |
281 | // Source: https://medium.com/@bromanz/another-view-on-the-classic-ray-aabb-intersection-algorithm-for-bvh-traversal-41125138b525
282 | bool RayCuboidIntersect(Ray ray, Cuboid cuboid, out float t1, out float t2)
283 | {
284 | t1 = FLOAT_MIN;
285 | t2 = FLOAT_MAX;
286 |
287 | vec3 t0s = (cuboid.Min - ray.Origin) / ray.Direction;
288 | vec3 t1s = (cuboid.Max - ray.Origin) / ray.Direction;
289 |
290 | vec3 tsmaller = min(t0s, t1s);
291 | vec3 tbigger = max(t0s, t1s);
292 |
293 | t1 = max(t1, max(tsmaller.x, max(tsmaller.y, tsmaller.z)));
294 | t2 = min(t2, min(tbigger.x, min(tbigger.y, tbigger.z)));
295 | return t1 <= t2;
296 | }
297 |
298 | // Source: https://blog.demofox.org/2020/05/25/casual-shadertoy-path-tracing-1-basic-camera-diffuse-emissive/
299 | vec3 CosineSampleHemisphere(vec3 normal)
300 | {
301 |
302 | float z = GetRandomFloat01() * 2.0 - 1.0;
303 | float a = GetRandomFloat01() * 2.0 * PI;
304 | float r = sqrt(1.0 - z * z);
305 | float x = r * cos(a);
306 | float y = r * sin(a);
307 |
308 | // Convert unit vector in sphere to a cosine weighted vector in hemisphere
309 | return normalize(normal + vec3(x, y, z));
310 | }
311 |
312 | vec2 UniformSampleUnitCircle()
313 | {
314 | float angle = GetRandomFloat01() * 2.0 * PI;
315 | float r = sqrt(GetRandomFloat01());
316 | return vec2(cos(angle), sin(angle)) * r;
317 | }
318 |
319 | vec3 GetNormal(Sphere sphere, vec3 surfacePosition)
320 | {
321 | return (surfacePosition - sphere.Position) / sphere.Radius;
322 | }
323 |
324 | // Source: https://gist.github.com/Shtille/1f98c649abeeb7a18c5a56696546d3cf
325 | vec3 GetNormal(Cuboid cuboid, vec3 surfacePosition)
326 | {
327 | vec3 halfSize = (cuboid.Max - cuboid.Min) * 0.5;
328 | vec3 centerSurface = surfacePosition - (cuboid.Max + cuboid.Min) * 0.5;
329 |
330 | vec3 normal = vec3(0.0);
331 | normal += vec3(sign(centerSurface.x), 0.0, 0.0) * step(abs(abs(centerSurface.x) - halfSize.x), EPSILON);
332 | normal += vec3(0.0, sign(centerSurface.y), 0.0) * step(abs(abs(centerSurface.y) - halfSize.y), EPSILON);
333 | normal += vec3(0.0, 0.0, sign(centerSurface.z)) * step(abs(abs(centerSurface.z) - halfSize.z), EPSILON);
334 | return normalize(normal);
335 | }
336 |
337 | uint GetPCGHash(inout uint seed)
338 | {
339 | seed = seed * 747796405u + 2891336453u;
340 | uint word = ((seed >> ((seed >> 28u) + 4u)) ^ seed) * 277803737u;
341 | return (word >> 22u) ^ word;
342 | }
343 |
344 | float GetRandomFloat01()
345 | {
346 | return float(GetPCGHash(rndSeed)) / 4294967296.0;
347 | }
348 |
349 | // Assumes t2 > t1 && t2 > 0.0
350 | float GetSmallestPositive(float t1, float t2)
351 | {
352 | return t1 < 0 ? t2 : t1;
353 | }
354 |
355 | Ray GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec3 viewPos, vec2 normalizedDeviceCoords)
356 | {
357 | vec4 rayEye = inverseProj * vec4(normalizedDeviceCoords, -1.0, 0.0);
358 | rayEye.zw = vec2(-1.0, 0.0);
359 | return Ray(viewPos, normalize((inverseView * rayEye).xyz));
360 | }
361 |
362 | float FresnelSchlick(float cosTheta, float n1, float n2)
363 | {
364 | float r0 = (n1 - n2) / (n1 + n2);
365 | r0 *= r0;
366 | return r0 + (1.0 - r0) * pow(1.0 - cosTheta, 5.0);
367 | }
368 |
369 | vec3 InverseGammaToLinear(vec3 rgb)
370 | {
371 | return mix(pow(((rgb + 0.055) / 1.055), vec3(2.4)), rgb / 12.92, vec3(lessThan(rgb, vec3(0.04045))));
372 | }
373 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/res/shaders/PathTracing/compute.glsl:
--------------------------------------------------------------------------------
1 | #version 450 core
2 | #define FLOAT_MAX 3.4028235e+38
3 | #define FLOAT_MIN -3.4028235e+38
4 | #define EPSILON 0.001
5 | #define PI 3.14159265
6 | // Example shader include: #include PathTracing/fragCompute
7 |
8 | layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
9 |
10 | layout(binding = 0, rgba32f) restrict uniform image2D ImgResult;
11 | layout(binding = 1) uniform samplerCube SamplerEnvironment;
12 |
13 | struct Material
14 | {
15 | vec3 Albedo; // Base color
16 | float SpecularChance; // How reflective
17 |
18 | vec3 Emissiv; // How much light is emitted
19 | float SpecularRoughness; // How rough reflections are
20 |
21 | vec3 Absorbance; // How strongly light is absorbed
22 | float RefractionChance; // How transparent
23 |
24 | float RefractionRoughness; // How rough refractions are
25 | float IOR; // How strongly light gets refracted and the amout of light that is reflected
26 | };
27 |
28 | struct Cuboid
29 | {
30 | vec3 Min;
31 | vec3 Max;
32 |
33 | Material Material;
34 | };
35 |
36 | struct Sphere
37 | {
38 | vec3 Position;
39 | float Radius;
40 |
41 | Material Material;
42 | };
43 |
44 | struct HitInfo
45 | {
46 | float T;
47 | bool FromInside;
48 | vec3 NearHitPos;
49 | vec3 Normal;
50 | Material Material;
51 | };
52 |
53 | struct Ray
54 | {
55 | vec3 Origin;
56 | vec3 Direction;
57 | };
58 |
59 | layout(std140, binding = 0) uniform BasicDataUBO
60 | {
61 | mat4 InvProjection;
62 | mat4 InvView;
63 | vec3 ViewPos;
64 | } basicDataUBO;
65 |
66 | layout(std140, binding = 1) uniform GameObjectsUBO
67 | {
68 | Sphere Spheres[256];
69 | Cuboid Cuboids[64];
70 | } gameObjectsUBO;
71 |
72 | vec3 Radiance(Ray ray);
73 | float BSDF(inout Ray ray, HitInfo hitInfo, out bool isRefractive);
74 | bool RayTrace(Ray ray, out HitInfo hitInfo);
75 | bool RaySphereIntersect(Ray ray, Sphere sphere, out float t1, out float t2);
76 | bool RayCuboidIntersect(Ray ray, Cuboid cuboid, out float t1, out float t2);
77 | vec3 CosineSampleHemisphere(vec3 normal);
78 | vec2 UniformSampleUnitCircle();
79 | vec3 GetNormal(Sphere sphere, vec3 surfacePosition);
80 | vec3 GetNormal(Cuboid cuboid, vec3 surfacePosition);
81 | uint GetPCGHash(inout uint seed);
82 | float GetRandomFloat01();
83 | float GetSmallestPositive(float t1, float t2);
84 | Ray GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec3 viewPos, vec2 normalizedDeviceCoords);
85 | float FresnelSchlick(float cosTheta, float n1, float n2);
86 | vec3 InverseGammaToLinear(vec3 rgb);
87 |
88 | uniform vec2 uboGameObjectsSize;
89 |
90 | uniform int rayDepth;
91 | uniform int SPP;
92 |
93 | uniform float focalLength;
94 | uniform float apertureDiameter;
95 |
96 | layout(location = 0) uniform int thisRendererFrame;
97 |
98 | uint rndSeed;
99 |
100 |
101 | void main()
102 | {
103 | ivec2 imgResultSize = imageSize(ImgResult);
104 | ivec2 imgCoord = ivec2(gl_GlobalInvocationID.xy);
105 |
106 | rndSeed = gl_GlobalInvocationID.x * 1973 + gl_GlobalInvocationID.y * 9277 + thisRendererFrame * 2699 | 1;
107 | //rndSeed = thisRendererFrame;
108 |
109 | vec3 irradiance = vec3(0.0);
110 | for (int i = 0; i < SPP; i++)
111 | {
112 | // add random offset to lower left corner of pixel to effectively integrate over whole pixel and eliminate aliasing
113 | vec2 subPixelOffset = vec2(GetRandomFloat01(), GetRandomFloat01());
114 | vec2 ndc = (imgCoord + subPixelOffset) / imgResultSize * 2.0 - 1.0;
115 | Ray rayEyeToWorld = GetWorldSpaceRay(basicDataUBO.InvProjection, basicDataUBO.InvView, basicDataUBO.ViewPos, ndc);
116 |
117 | vec3 focalPoint = rayEyeToWorld.Origin + rayEyeToWorld.Direction * focalLength;
118 | vec2 offset = apertureDiameter * 0.5 * UniformSampleUnitCircle();
119 |
120 | rayEyeToWorld.Origin = (basicDataUBO.InvView * vec4(offset, 0.0, 1.0)).xyz;
121 | rayEyeToWorld.Direction = normalize(focalPoint - rayEyeToWorld.Origin);
122 |
123 | irradiance += Radiance(rayEyeToWorld);
124 | }
125 | irradiance /= SPP;
126 | vec3 lastFrameColor = imageLoad(ImgResult, imgCoord).rgb;
127 |
128 | irradiance = mix(lastFrameColor, irradiance, 1.0 / (thisRendererFrame + 1));
129 | imageStore(ImgResult, imgCoord, vec4(irradiance, 1.0));
130 | }
131 |
132 | vec3 Radiance(Ray ray)
133 | {
134 | vec3 throughput = vec3(1.0);
135 | vec3 radiance = vec3(0.0);
136 |
137 | HitInfo hitInfo;
138 | bool isRefractive;
139 | float rayProbability;
140 | for (int i = 0; i < rayDepth; i++)
141 | {
142 | if (RayTrace(ray, hitInfo))
143 | {
144 | // If ray did just pass through medium apply Beer's law
145 | if (hitInfo.FromInside)
146 | {
147 | hitInfo.Normal *= -1.0;
148 | throughput *= exp(-hitInfo.Material.Absorbance * hitInfo.T);
149 | }
150 |
151 | // Evaluating BSDF gives a new ray based on the hitPoints properties and the incomming ray,
152 | // the probability this ray would take its path
153 | // and a bool indicating wheter the ray penetrates into the medium
154 | rayProbability = BSDF(ray, hitInfo, isRefractive);
155 |
156 | radiance += hitInfo.Material.Emissiv * throughput;
157 | if (!isRefractive)
158 | {
159 | // The cosine term is already taken into account by the CosineSampleHemisphere function. Its weighting the random rays to a cosine distibution
160 | // throughput *= hitInfo.Material.Albedo * dot(ray.Direction, hitInfo.Normal);
161 |
162 | throughput *= hitInfo.Material.Albedo;
163 | }
164 | throughput /= rayProbability;
165 |
166 | // Russian Roulette, unbiased method to terminate rays and therefore lower render times (also reduces fireflies)
167 | {
168 | float p = max(throughput.x, max(throughput.y, throughput.z));
169 | if (GetRandomFloat01() > p)
170 | break;
171 |
172 | throughput /= p;
173 | }
174 | }
175 | else
176 | {
177 | radiance += texture(SamplerEnvironment, ray.Direction).rgb * throughput;
178 | break;
179 | }
180 | }
181 | return radiance;
182 | }
183 |
184 | float BSDF(inout Ray ray, HitInfo hitInfo, out bool isRefractive)
185 | {
186 | isRefractive = false;
187 |
188 | float specularChance = hitInfo.Material.SpecularChance;
189 | float refractionChance = hitInfo.Material.RefractionChance;
190 | if (specularChance > 0.0)
191 | {
192 | specularChance = mix(specularChance, 1.0, FresnelSchlick(dot(-ray.Direction, hitInfo.Normal), hitInfo.FromInside ? hitInfo.Material.IOR : 1.0, !hitInfo.FromInside ? hitInfo.Material.IOR : 1.0));
193 | float diffuseChance = 1.0 - specularChance - refractionChance;
194 | refractionChance = 1.0 - specularChance - diffuseChance;
195 | }
196 |
197 | vec3 diffuseRay = CosineSampleHemisphere(hitInfo.Normal);
198 | float rayProbability = 1.0;
199 |
200 | float raySelectRoll = GetRandomFloat01();
201 | if (specularChance > raySelectRoll)
202 | {
203 | vec3 reflectionRayDir = reflect(ray.Direction, hitInfo.Normal);
204 | reflectionRayDir = normalize(mix(reflectionRayDir, diffuseRay, hitInfo.Material.SpecularRoughness * hitInfo.Material.SpecularRoughness));
205 | ray.Direction = reflectionRayDir;
206 | rayProbability = specularChance;
207 | }
208 | else if (specularChance + refractionChance > raySelectRoll)
209 | {
210 | vec3 refractionRayDir = refract(ray.Direction, hitInfo.Normal, hitInfo.FromInside ? (hitInfo.Material.IOR / 1.0) : (1.0 / hitInfo.Material.IOR));
211 | refractionRayDir = normalize(mix(refractionRayDir, CosineSampleHemisphere(-hitInfo.Normal), hitInfo.Material.RefractionRoughness * hitInfo.Material.RefractionRoughness));
212 | ray.Direction = refractionRayDir;
213 | rayProbability = refractionChance;
214 | isRefractive = true;
215 | }
216 | else
217 | {
218 | ray.Direction = diffuseRay;
219 | rayProbability = 1.0 - specularChance - refractionChance;
220 | }
221 |
222 | ray.Origin = hitInfo.NearHitPos + ray.Direction * EPSILON;
223 | return max(rayProbability, EPSILON);
224 | }
225 |
226 | bool RayTrace(Ray ray, out HitInfo hitInfo)
227 | {
228 | hitInfo.T = FLOAT_MAX;
229 | float t1, t2;
230 |
231 | for (int i = 0; i < uboGameObjectsSize.x; i++)
232 | {
233 | Sphere sphere = gameObjectsUBO.Spheres[i];
234 | if (RaySphereIntersect(ray, sphere, t1, t2) && t2 > 0.0 && t1 < hitInfo.T)
235 | {
236 | hitInfo.T = GetSmallestPositive(t1, t2);
237 | hitInfo.FromInside = hitInfo.T == t2;
238 | hitInfo.Material = gameObjectsUBO.Spheres[i].Material;
239 | hitInfo.NearHitPos = ray.Origin + ray.Direction * hitInfo.T;
240 | hitInfo.Normal = GetNormal(sphere, hitInfo.NearHitPos);
241 | }
242 | }
243 |
244 | for (int i = 0; i < uboGameObjectsSize.y; i++)
245 | {
246 | Cuboid cuboid = gameObjectsUBO.Cuboids[i];
247 | if (RayCuboidIntersect(ray, cuboid, t1, t2) && t2 > 0.0 && t1 < hitInfo.T)
248 | {
249 | hitInfo.T = GetSmallestPositive(t1, t2);
250 | hitInfo.FromInside = hitInfo.T == t2;
251 | hitInfo.Material = gameObjectsUBO.Cuboids[i].Material;
252 | hitInfo.NearHitPos = ray.Origin + ray.Direction * hitInfo.T;
253 | hitInfo.Normal = GetNormal(cuboid, hitInfo.NearHitPos);
254 | }
255 | }
256 |
257 | return hitInfo.T != FLOAT_MAX;
258 | }
259 |
260 | // Source: https://antongerdelan.net/opengl/raycasting.html
261 | bool RaySphereIntersect(Ray ray, Sphere sphere, out float t1, out float t2)
262 | {
263 | t1 = t2 = FLOAT_MAX;
264 |
265 | vec3 sphereToRay = ray.Origin - sphere.Position;
266 | float b = dot(ray.Direction, sphereToRay);
267 | float c = dot(sphereToRay, sphereToRay) - sphere.Radius * sphere.Radius;
268 | float discriminant = b * b - c;
269 | if (discriminant < 0.0)
270 | return false;
271 |
272 | float squareRoot = sqrt(discriminant);
273 | t1 = -b - squareRoot;
274 | t2 = -b + squareRoot;
275 |
276 | return t1 <= t2;
277 | }
278 |
279 | // Source: https://medium.com/@bromanz/another-view-on-the-classic-ray-aabb-intersection-algorithm-for-bvh-traversal-41125138b525
280 | bool RayCuboidIntersect(Ray ray, Cuboid cuboid, out float t1, out float t2)
281 | {
282 | t1 = FLOAT_MIN;
283 | t2 = FLOAT_MAX;
284 |
285 | vec3 t0s = (cuboid.Min - ray.Origin) / ray.Direction;
286 | vec3 t1s = (cuboid.Max - ray.Origin) / ray.Direction;
287 |
288 | vec3 tsmaller = min(t0s, t1s);
289 | vec3 tbigger = max(t0s, t1s);
290 |
291 | t1 = max(t1, max(tsmaller.x, max(tsmaller.y, tsmaller.z)));
292 | t2 = min(t2, min(tbigger.x, min(tbigger.y, tbigger.z)));
293 | return t1 <= t2;
294 | }
295 |
296 | // Source: https://blog.demofox.org/2020/05/25/casual-shadertoy-path-tracing-1-basic-camera-diffuse-emissive/
297 | vec3 CosineSampleHemisphere(vec3 normal)
298 | {
299 | float z = GetRandomFloat01() * 2.0 - 1.0;
300 | float a = GetRandomFloat01() * 2.0 * PI;
301 | float r = sqrt(1.0 - z * z);
302 | float x = r * cos(a);
303 | float y = r * sin(a);
304 |
305 | // Convert unit vector in sphere to a cosine weighted vector in hemisphere
306 | return normalize(normal + vec3(x, y, z));
307 | }
308 |
309 | vec2 UniformSampleUnitCircle()
310 | {
311 | float angle = GetRandomFloat01() * 2.0 * PI;
312 | float r = sqrt(GetRandomFloat01());
313 | return vec2(cos(angle), sin(angle)) * r;
314 | }
315 |
316 | vec3 GetNormal(Sphere sphere, vec3 surfacePosition)
317 | {
318 | return (surfacePosition - sphere.Position) / sphere.Radius;
319 | }
320 |
321 | // Source: https://gist.github.com/Shtille/1f98c649abeeb7a18c5a56696546d3cf
322 | vec3 GetNormal(Cuboid cuboid, vec3 surfacePosition)
323 | {
324 | vec3 halfSize = (cuboid.Max - cuboid.Min) * 0.5;
325 | vec3 centerSurface = surfacePosition - (cuboid.Max + cuboid.Min) * 0.5;
326 |
327 | vec3 normal = vec3(0.0);
328 | normal += vec3(sign(centerSurface.x), 0.0, 0.0) * step(abs(abs(centerSurface.x) - halfSize.x), EPSILON);
329 | normal += vec3(0.0, sign(centerSurface.y), 0.0) * step(abs(abs(centerSurface.y) - halfSize.y), EPSILON);
330 | normal += vec3(0.0, 0.0, sign(centerSurface.z)) * step(abs(abs(centerSurface.z) - halfSize.z), EPSILON);
331 | return normalize(normal);
332 | }
333 |
334 | uint GetPCGHash(inout uint seed)
335 | {
336 | seed = seed * 747796405u + 2891336453u;
337 | uint word = ((seed >> ((seed >> 28u) + 4u)) ^ seed) * 277803737u;
338 | return (word >> 22u) ^ word;
339 | }
340 |
341 | float GetRandomFloat01()
342 | {
343 | return float(GetPCGHash(rndSeed)) / 4294967296.0;
344 | }
345 |
346 | // Assumes t2 > t1 && t2 > 0.0
347 | float GetSmallestPositive(float t1, float t2)
348 | {
349 | return t1 < 0 ? t2 : t1;
350 | }
351 |
352 | Ray GetWorldSpaceRay(mat4 inverseProj, mat4 inverseView, vec3 viewPos, vec2 normalizedDeviceCoords)
353 | {
354 | vec4 rayEye = inverseProj * vec4(normalizedDeviceCoords, -1.0, 0.0);
355 | rayEye.zw = vec2(-1.0, 0.0);
356 | return Ray(viewPos, normalize((inverseView * rayEye).xyz));
357 | }
358 |
359 | float FresnelSchlick(float cosTheta, float n1, float n2)
360 | {
361 | float r0 = (n1 - n2) / (n1 + n2);
362 | r0 *= r0;
363 | return r0 + (1.0 - r0) * pow(1.0 - cosTheta, 5.0);
364 | }
365 |
366 | vec3 InverseGammaToLinear(vec3 rgb)
367 | {
368 | return mix(pow(((rgb + 0.055) / 1.055), vec3(2.4)), rgb / 12.92, vec3(lessThan(rgb, vec3(0.04045))));
369 | }
370 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Gui.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using ImGuiNET;
3 | using SixLabors.ImageSharp;
4 | using OpenTK;
5 | using OpenTK.Input;
6 | using OpenTK_PathTracer.GUI;
7 | using OpenTK_PathTracer.GameObjects;
8 | using OpenTK_PathTracer.Render.Objects;
9 |
10 | namespace OpenTK_PathTracer.Render
11 | {
12 | static class Gui
13 | {
14 | public static ImGuiController ImGuiController = new ImGuiController(0, 0);
15 | private static BaseGameObject pickedObject;
16 |
17 | public static bool IsEnvironmentAtmosphere;
18 |
19 | public static void Render(MainWindow mainWindow, float frameTime, out bool frameChanged)
20 | {
21 | ImGuiController.Update(mainWindow, frameTime);
22 |
23 | float windowAlpha = mainWindow.CursorVisible ? 0.8f : 0.3f;
24 | frameChanged = false;
25 | ImGui.SetNextWindowBgAlpha(windowAlpha);
26 | ImGui.Begin("Overview");
27 | {
28 | if (ImGui.Button("Screenshot"))
29 | {
30 | System.IO.Directory.CreateDirectory("Screenshots");
31 | using Image img = Framebuffer.GetBitmapFramebufferAttachment(0, OpenTK.Graphics.OpenGL4.FramebufferAttachment.ColorAttachment0, mainWindow.Width, mainWindow.Height);
32 | img.SaveAsPng($@"Screenshots\Samples_{mainWindow.PathTracer.Samples}.png");
33 | }
34 | if (ImGui.CollapsingHeader("PathTracing"))
35 | {
36 | ImGui.Text($"VSync: {mainWindow.VSync}");
37 | ImGui.Text($"FPS: {mainWindow.FPS}"); ImGui.SameLine(); ImGui.Text($"UPS: {mainWindow.UPS}"); ImGui.SameLine(); ImGui.Text($"Samples/Pixel/Second: {mainWindow.FPS * mainWindow.PathTracer.SPP}");
38 | ImGui.Checkbox("RenderInBackground", ref mainWindow.IsRenderInBackground);
39 | int temp = mainWindow.PathTracer.SPP;
40 | if (ImGui.SliderInt("Samples/Pixel", ref temp, 1, 10))
41 | {
42 | frameChanged = true;
43 | mainWindow.PathTracer.SPP = temp;
44 | }
45 |
46 |
47 | temp = mainWindow.PathTracer.RayDepth;
48 | if (ImGui.SliderInt("MaxRayDepth", ref temp, 1, 50))
49 | {
50 | frameChanged = true;
51 | mainWindow.PathTracer.RayDepth = temp;
52 | }
53 |
54 | float floatTemp = mainWindow.PathTracer.FocalLength;
55 | if (ImGui.InputFloat("FocalLength", ref floatTemp, 0.1f))
56 | {
57 | frameChanged = true;
58 | mainWindow.PathTracer.FocalLength = MathF.Max(floatTemp, 0);
59 | }
60 |
61 | floatTemp = mainWindow.PathTracer.ApertureDiameter;
62 | if (ImGui.InputFloat("ApertureDiameter", ref floatTemp, 0.002f))
63 | {
64 | frameChanged = true;
65 | mainWindow.PathTracer.ApertureDiameter = MathF.Max(floatTemp, 0);
66 | }
67 | ImGui.Text($"f-number: f/{mainWindow.PathTracer.FocalLength / mainWindow.PathTracer.ApertureDiameter}");
68 |
69 | if (ImGui.Button("SpheresRandomMaterial"))
70 | {
71 | frameChanged = true;
72 | mainWindow.SetGameObjectsRandomMaterial(36);
73 | }
74 | }
75 | if (ImGui.CollapsingHeader("EnvironmentMap"))
76 | {
77 | bool hadInput = false;
78 |
79 | IsEnvironmentAtmosphere = mainWindow.PathTracer.EnvironmentMap == mainWindow.AtmosphericScatterer.Result;
80 | if (ImGui.Checkbox("Atmosphere", ref IsEnvironmentAtmosphere))
81 | {
82 | hadInput = true;
83 | if (!IsEnvironmentAtmosphere)
84 | mainWindow.PathTracer.EnvironmentMap = mainWindow.SkyBox;
85 | else
86 | mainWindow.PathTracer.EnvironmentMap = mainWindow.AtmosphericScatterer.Result;
87 | }
88 |
89 | if (IsEnvironmentAtmosphere)
90 | {
91 | ImGui.Text($"Computation time: {MathF.Round(mainWindow.AtmosphericScatterer.Timer.ElapsedMilliseconds, 2)} ms");
92 |
93 | string[] resolutions = new string[] { "2048", "1024", "512", "256", "128", "64", "32" };
94 | string current = mainWindow.AtmosphericScatterer.Result.Width.ToString();
95 | if (ImGui.BeginCombo("##combo", current))
96 | {
97 | for (int i = 0; i < resolutions.Length; i++)
98 | {
99 | bool isSelected = current == resolutions[i];
100 | if (ImGui.Selectable(resolutions[i], isSelected))
101 | {
102 | hadInput = true;
103 | current = resolutions[i];
104 | mainWindow.AtmosphericScatterer.SetSize(Convert.ToInt32(current));
105 | mainWindow.AtmosphericScatterer.Render();
106 | }
107 |
108 | if (isSelected)
109 | ImGui.SetItemDefaultFocus();
110 | }
111 | ImGui.EndCombo();
112 | }
113 |
114 | int tempInt = mainWindow.AtmosphericScatterer.ISteps;
115 | if (ImGui.SliderInt("InScatteringSamples", ref tempInt, 1, 100))
116 | {
117 | hadInput = true;
118 | mainWindow.AtmosphericScatterer.ISteps = tempInt;
119 | mainWindow.AtmosphericScatterer.Render();
120 | }
121 |
122 | tempInt = mainWindow.AtmosphericScatterer.JSteps;
123 | if (ImGui.SliderInt("DensitySamples", ref tempInt, 1, 40))
124 | {
125 | hadInput = true;
126 | mainWindow.AtmosphericScatterer.JSteps = tempInt;
127 | mainWindow.AtmosphericScatterer.Render();
128 | }
129 |
130 | float tempFloat = mainWindow.AtmosphericScatterer.Time;
131 | if (ImGui.DragFloat("Time", ref tempFloat, 0.005f))
132 | {
133 | hadInput = true;
134 | mainWindow.AtmosphericScatterer.Time = tempFloat;
135 | mainWindow.AtmosphericScatterer.Render();
136 | }
137 |
138 | tempFloat = mainWindow.AtmosphericScatterer.LightIntensity;
139 | if (ImGui.DragFloat("Intensity", ref tempFloat, 0.2f))
140 | {
141 | hadInput = true;
142 | mainWindow.AtmosphericScatterer.LightIntensity = tempFloat;
143 | mainWindow.AtmosphericScatterer.Render();
144 | }
145 | }
146 |
147 | if (hadInput)
148 | frameChanged = hadInput;
149 | }
150 |
151 | ImGui.End();
152 | }
153 |
154 | if (pickedObject != null)
155 | {
156 | ImGui.Begin("GameObjectProperties", ImGuiWindowFlags.AlwaysAutoResize);
157 | {
158 | bool hasInput = false;
159 | System.Numerics.Vector3 nVector3;
160 |
161 | ImGui.Text($"Distance {Vector3.Distance(pickedObject.Position, mainWindow.Camera.Position)}");
162 |
163 | nVector3 = Vector3ToNVector3(pickedObject.Position);
164 | if (ImGui.DragFloat3("Position", ref nVector3))
165 | {
166 | pickedObject.Position = NVector3ToVector3(nVector3);
167 | hasInput = true;
168 | }
169 |
170 | nVector3 = Vector3ToNVector3(pickedObject.Material.Albedo);
171 | if (ImGui.InputFloat3("Albedo", ref nVector3))
172 | {
173 | pickedObject.Material.Albedo = NVector3ToVector3(nVector3);
174 | hasInput = true;
175 | }
176 |
177 | nVector3 = Vector3ToNVector3(pickedObject.Material.Emissiv);
178 | if (ImGui.InputFloat3("Emissiv", ref nVector3))
179 | {
180 | pickedObject.Material.Emissiv = NVector3ToVector3(nVector3);
181 | hasInput = true;
182 | }
183 |
184 | nVector3 = Vector3ToNVector3(pickedObject.Material.AbsorbanceColor);
185 | if (ImGui.InputFloat3("AbsorbanceColor", ref nVector3))
186 | {
187 | pickedObject.Material.AbsorbanceColor = NVector3ToVector3(nVector3);
188 | hasInput = true;
189 | }
190 |
191 | if (ImGui.SliderFloat("SpecularChance", ref pickedObject.Material.SpecularChance, 0, 1))
192 | {
193 | pickedObject.Material.SpecularChance = Math.Clamp(pickedObject.Material.SpecularChance, 0, 1.0f - pickedObject.Material.RefractionChance);
194 | hasInput = true;
195 | }
196 |
197 | if (ImGui.SliderFloat("SpecularRoughness", ref pickedObject.Material.SpecularRoughness, 0, 1))
198 | hasInput = true;
199 |
200 | if (ImGui.SliderFloat("IndexOfRefraction", ref pickedObject.Material.IOR, 1, 5))
201 | hasInput = true;
202 |
203 | if (ImGui.SliderFloat("RefractionChance", ref pickedObject.Material.RefractionChance, 0, 1))
204 | {
205 | pickedObject.Material.RefractionChance = Math.Clamp(pickedObject.Material.RefractionChance, 0, 1.0f - pickedObject.Material.SpecularChance);
206 | hasInput = true;
207 | }
208 |
209 | if (ImGui.SliderFloat("RefractionRoughnes", ref pickedObject.Material.RefractionRoughnes, 0, 1))
210 | hasInput = true;
211 |
212 | if (hasInput)
213 | {
214 | pickedObject.Upload(mainWindow.GameObjectsUBO);
215 | frameChanged = true;
216 | }
217 | ImGui.End();
218 | }
219 | }
220 | ImGuiController.Render();
221 | }
222 |
223 | public static void Update(MainWindow mainWindow)
224 | {
225 | if (mainWindow.CursorVisible && !ImGui.GetIO().WantCaptureMouse)
226 | {
227 | if (MouseManager.IsButtonTouched(MouseButton.Left))
228 | {
229 | System.Drawing.Point windowSpaceCoords = mainWindow.PointToClient(new System.Drawing.Point(MouseManager.WindowPositionX, MouseManager.WindowPositionY)); windowSpaceCoords.Y = mainWindow.Height - windowSpaceCoords.Y; // [0, Width][0, Height]
230 | Vector2 normalizedDeviceCoords = Vector2.Divide(new Vector2(windowSpaceCoords.X, windowSpaceCoords.Y), new Vector2(mainWindow.Width, mainWindow.Height)) * 2.0f - new Vector2(1.0f); // [-1.0, 1.0][-1.0, 1.0]
231 | Ray rayWorld = Ray.GetWorldSpaceRay(mainWindow.inverseProjection, mainWindow.Camera.View.Inverted(), mainWindow.Camera.Position, normalizedDeviceCoords);
232 |
233 | mainWindow.RayTrace(rayWorld, out pickedObject, out _, out _);
234 |
235 | /// Comment out to test object deletion (Spheres in this case)
236 | //if (pickedObject is Sphere sphere)
237 | //{
238 | // /// Procedure to properly delete objects
239 |
240 | // /// Delete from GPU
241 | // int start = pickedObject.BufferOffset + Sphere.GPU_INSTANCE_SIZE;
242 | // int bufferSpheresEnd = Sphere.GPU_INSTANCE_SIZE * mainWindow.PathTracer.NumSpheres - start;
243 |
244 | // /// Shift following Spheres backwards to override the picked one (just using the last sphere to override the picked sphere should work as well !?)
245 | // mainWindow.GameObjectsUBO.GetSubData(start, bufferSpheresEnd, out IntPtr followingSphereData);
246 | // mainWindow.GameObjectsUBO.SubData(pickedObject.BufferOffset, bufferSpheresEnd, followingSphereData); // override selected sphere
247 | // System.Runtime.InteropServices.Marshal.FreeHGlobal(followingSphereData);
248 |
249 | // /// Delete from CPU
250 | // mainWindow.GameObjects.Remove(sphere);
251 | // mainWindow.PathTracer.NumSpheres--;
252 |
253 | // for (int i = 0; i < mainWindow.GameObjects.Count; i++)
254 | // if (mainWindow.GameObjects[i] is Sphere temp && temp is not null && temp.Instance > sphere.Instance)
255 | // temp.Instance--;
256 |
257 | // mainWindow.PathTracer.ThisRenderNumFrame = 0;
258 | // pickedObject = null;
259 | //}
260 | }
261 | }
262 | }
263 |
264 | private static Vector3 NVector3ToVector3(System.Numerics.Vector3 v) => new Vector3(v.X, v.Y, v.Z);
265 | private static System.Numerics.Vector3 Vector3ToNVector3(Vector3 v) => new System.Numerics.Vector3(v.X, v.Y, v.Z);
266 |
267 | public static void SetSize(int width, int height)
268 | {
269 | ImGuiController.WindowResized(width, height);
270 | }
271 | }
272 | }
273 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/Render/Objects/Texture.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SixLabors.ImageSharp;
3 | using SixLabors.ImageSharp.PixelFormats;
4 | using SixLabors.ImageSharp.Processing;
5 | using OpenTK;
6 | using OpenTK.Graphics.OpenGL4;
7 | using PixelFormat = OpenTK.Graphics.OpenGL4.PixelFormat;
8 |
9 | namespace OpenTK_PathTracer.Render.Objects
10 | {
11 | class Texture : IDisposable
12 | {
13 | public enum PixelTypeSize
14 | {
15 | TextureRedSize = 32860,
16 | TextureGreenSize = 32861,
17 | TextureBlueSize = 32862,
18 | TextureAlphaSize = 32863,
19 | }
20 |
21 | public enum TextureDimension
22 | {
23 | Undefined = 0,
24 | One = 1,
25 | Two = 2,
26 | Three = 3,
27 | }
28 |
29 | public readonly int ID;
30 | public readonly TextureTarget Target;
31 | public readonly TextureDimension Dimension;
32 | public int Width { get; private set; }
33 | public int Height { get; private set; }
34 | public int Depth { get; private set; }
35 | public PixelInternalFormat PixelInternalFormat { get; private set; }
36 |
37 | public Texture(TextureTarget3d textureTarget3D)
38 | {
39 | Target = (TextureTarget)textureTarget3D;
40 | Dimension = TextureDimension.Three;
41 |
42 | GL.CreateTextures(Target, 1, out ID);
43 | }
44 |
45 | public Texture(TextureTarget2d textureTarget2D)
46 | {
47 | Target = (TextureTarget)textureTarget2D;
48 | Dimension = TextureDimension.Two;
49 |
50 | GL.CreateTextures(Target, 1, out ID);
51 | }
52 |
53 | public Texture(TextureTarget1d textureTarget1D)
54 | {
55 | Target = (TextureTarget)textureTarget1D;
56 | Dimension = TextureDimension.One;
57 |
58 | GL.CreateTextures(Target, 1, out ID);
59 | }
60 |
61 | public Texture(TextureBufferTarget textureBufferTarget, BufferObject bufferObject, SizedInternalFormat sizedInternalFormat = SizedInternalFormat.Rgba32f)
62 | {
63 | Target = (TextureTarget)textureBufferTarget;
64 | Dimension = TextureDimension.Undefined;
65 |
66 | GL.CreateTextures(Target, 1, out ID);
67 | GL.TextureBuffer(ID, sizedInternalFormat, bufferObject.ID);
68 | GL.TextureBufferRange(ID, sizedInternalFormat, bufferObject.ID, IntPtr.Zero, bufferObject.Size);
69 | }
70 |
71 | public void SetFilter(TextureMinFilter minFilter, TextureMagFilter magFilter)
72 | {
73 | /// Explanation for Mipmap filters from https://learnopengl.com/Getting-started/Textures:
74 | /// GL_NEAREST_MIPMAP_NEAREST: takes the nearest mipmap to match the pixel size and uses nearest neighbor interpolation for texture sampling.
75 | /// GL_LINEAR_MIPMAP_NEAREST: takes the nearest mipmap level and samples that level using linear interpolation.
76 | /// GL_NEAREST_MIPMAP_LINEAR: linearly interpolates between the two mipmaps that most closely match the size of a pixel and samples the interpolated level via nearest neighbor interpolation.
77 | /// GL_LINEAR_MIPMAP_LINEAR: linearly interpolates between the two closest mipmaps and samples the interpolated level via linear interpolation.
78 |
79 | GL.TextureParameter(ID, TextureParameterName.TextureMinFilter, (int)minFilter);
80 | GL.TextureParameter(ID, TextureParameterName.TextureMagFilter, (int)magFilter);
81 | }
82 |
83 | public void SetWrapMode(TextureWrapMode wrapS, TextureWrapMode wrapT)
84 | {
85 | GL.TextureParameter(ID, TextureParameterName.TextureWrapS, (int)wrapS);
86 | GL.TextureParameter(ID, TextureParameterName.TextureWrapT, (int)wrapT);
87 | }
88 |
89 | public void SetWrapMode(TextureWrapMode wrapS, TextureWrapMode wrapT, TextureWrapMode wrapR)
90 | {
91 | GL.TextureParameter(ID, TextureParameterName.TextureWrapS, (int)wrapS);
92 | GL.TextureParameter(ID, TextureParameterName.TextureWrapT, (int)wrapT);
93 | GL.TextureParameter(ID, TextureParameterName.TextureWrapR, (int)wrapR);
94 | }
95 |
96 | public void Bind()
97 | {
98 | GL.BindTexture(Target, ID);
99 | }
100 |
101 | public void AttachImage(int unit, int level, bool layered, int layer, TextureAccess textureAccess, SizedInternalFormat sizedInternalFormat)
102 | {
103 | GL.BindImageTexture(unit, ID, level, layered, layer, textureAccess, sizedInternalFormat);
104 | }
105 | public void AttachSampler(int unit)
106 | {
107 | GL.BindTextureUnit(unit, ID);
108 | }
109 |
110 | public void SubTexture3D(int width, int heigth, int depth, PixelFormat pixelFormat, PixelType pixelType, T[] pixels, int level = 0, int xOffset = 0, int yOffset = 0, int zOffset = 0) where T : struct
111 | {
112 | GL.TextureSubImage3D(ID, level, xOffset, yOffset, zOffset, width, heigth, depth, pixelFormat, pixelType, pixels);
113 | }
114 | public void SubTexture3D(int width, int heigth, int depth, PixelFormat pixelFormat, PixelType pixelType, IntPtr pixels, int level = 0, int xOffset = 0, int yOffset = 0, int zOffset = 0)
115 | {
116 | GL.TextureSubImage3D(ID, level, xOffset, yOffset, zOffset, width, heigth, depth, pixelFormat, pixelType, pixels);
117 | }
118 | public void SubTexture2D(int width, int heigth, PixelFormat pixelFormat, PixelType pixelType, T[] pixels, int level = 0, int xOffset = 0, int yOffset = 0) where T : struct
119 | {
120 | GL.TextureSubImage2D(ID, level, xOffset, yOffset, width, heigth, pixelFormat, pixelType, pixels);
121 | }
122 | public void SubTexture2D(int width, int heigth, PixelFormat pixelFormat, PixelType pixelType, IntPtr pixels, int level = 0, int xOffset = 0, int yOffset = 0)
123 | {
124 | GL.TextureSubImage2D(ID, level, xOffset, yOffset, width, heigth, pixelFormat, pixelType, pixels);
125 | }
126 | public void SubTexture1D(int width, PixelFormat pixelFormat, PixelType pixelType, T[] pixels, int level = 0, int xOffset = 0) where T : struct
127 | {
128 | GL.TextureSubImage1D(ID, level, xOffset, width, pixelFormat, pixelType, pixels);
129 | }
130 | public void SubTexture1D(int width, PixelFormat pixelFormat, PixelType pixelType, IntPtr pixels, int level = 0, int xOffset = 0)
131 | {
132 | GL.TextureSubImage1D(ID, level, xOffset, width, pixelFormat, pixelType, pixels);
133 | }
134 |
135 |
136 | ///
137 | /// To properly generate mipmaps must be set to one of the mipmap options
138 | /// and if immutable storage is used the level parameter should match the number of desired mipmap levels to generate (default: 1).
139 | ///
140 | public void GenerateMipmap()
141 | {
142 | GL.GenerateTextureMipmap(ID);
143 | }
144 |
145 | ///
146 | /// GL_ARB_seamless_cubemap_per_texture must be available
147 | ///
148 | ///
149 | public void SetSeamlessCubeMapPerTexture(bool param)
150 | {
151 | if (Target == TextureTarget.TextureCubeMap)
152 | GL.TextureParameter(ID, (TextureParameterName)All.TextureCubeMapSeamless, param ? 1 : 0);
153 | }
154 |
155 | public void SetBorderColor(Vector4 color)
156 | {
157 | unsafe
158 | {
159 | float* colors = stackalloc[] { color.X, color.Y, color.Z, color.W };
160 | GL.TextureParameter(ID, TextureParameterName.TextureBorderColor, colors);
161 | }
162 | }
163 |
164 | public void SetMipmapLodBias(float bias)
165 | {
166 | GL.TextureParameter(ID, TextureParameterName.TextureLodBias, bias);
167 | }
168 |
169 | public void MutableAllocate(int width, int height, int depth, PixelInternalFormat pixelInternalFormat)
170 | {
171 | Bind();
172 | switch (Dimension)
173 | {
174 | case TextureDimension.One:
175 | GL.TexImage1D(Target, 0, pixelInternalFormat, width, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero);
176 | Width = width;
177 | break;
178 |
179 | case TextureDimension.Two:
180 | if (Target == TextureTarget.TextureCubeMap)
181 | for (int i = 0; i < 6; i++)
182 | GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, pixelInternalFormat, width, height, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero);
183 | else
184 | GL.TexImage2D(Target, 0, pixelInternalFormat, width, height, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero);
185 | Width = width; Height = height;
186 | break;
187 |
188 | case TextureDimension.Three:
189 | GL.TexImage3D(Target, 0, pixelInternalFormat, width, height, depth, 0, PixelFormat.Rgba, PixelType.Float, IntPtr.Zero);
190 | Width = width; Height = height; Depth = depth;
191 | break;
192 |
193 | default:
194 | return;
195 | }
196 | PixelInternalFormat = pixelInternalFormat;
197 | }
198 |
199 | public void MutableAllocate(int width, int height, int depth, PixelInternalFormat pixelInternalFormat, IntPtr intPtr, PixelFormat pixelFormat, PixelType pixelType)
200 | {
201 | Bind();
202 | switch (Dimension)
203 | {
204 | case TextureDimension.One:
205 | GL.TexImage1D(Target, 0, pixelInternalFormat, width, 0, pixelFormat, pixelType, intPtr);
206 | Width = width;
207 | break;
208 |
209 | case TextureDimension.Two:
210 | if (Target == TextureTarget.TextureCubeMap)
211 | for (int i = 0; i < 6; i++)
212 | GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, pixelInternalFormat, width, height, 0, pixelFormat, pixelType, intPtr);
213 | else
214 | GL.TexImage2D(Target, 0, pixelInternalFormat, width, height, 0, pixelFormat, pixelType, intPtr);
215 | Width = width; Height = height;
216 | break;
217 |
218 | case TextureDimension.Three:
219 | GL.TexImage3D(Target, 0, pixelInternalFormat, width, height, depth, 0, pixelFormat, pixelType, intPtr);
220 | Width = width; Height = height; Depth = depth;
221 | break;
222 |
223 | default:
224 | return;
225 | }
226 | PixelInternalFormat = pixelInternalFormat;
227 | }
228 |
229 | public void ImmutableAllocate(int width, int height, int depth, SizedInternalFormat sizedInternalFormat, int levels = 1)
230 | {
231 | switch (Dimension)
232 | {
233 | case TextureDimension.One:
234 | GL.TextureStorage1D(ID, levels, sizedInternalFormat, width);
235 | Width = width;
236 | break;
237 |
238 | case TextureDimension.Two:
239 | GL.TextureStorage2D(ID, levels, sizedInternalFormat, width, height);
240 | Width = width; Height = height;
241 | break;
242 |
243 | case TextureDimension.Three:
244 | GL.TextureStorage3D(ID, levels, sizedInternalFormat, width, height, depth);
245 | Width = width; Height = height; Depth = depth;
246 | break;
247 |
248 | default:
249 | return;
250 | }
251 | PixelInternalFormat = (PixelInternalFormat)sizedInternalFormat;
252 | }
253 |
254 | ///
255 | /// GL_ARB_bindless_texture must be available
256 | ///
257 | ///
258 | public long GetTextureBindlessHandle()
259 | {
260 | long textureHandle = GL.Arb.GetTextureHandle(ID);
261 | GL.Arb.MakeTextureHandleResident(textureHandle);
262 | return textureHandle;
263 | }
264 |
265 | ///
266 | /// GL_ARB_bindless_texture must be available
267 | ///
268 | ///
269 | public static bool UnmakeTextureBindless(long textureHandle)
270 | {
271 | if (GL.Arb.IsTextureHandleResident(textureHandle))
272 | {
273 | GL.Arb.MakeTextureHandleNonResident(textureHandle);
274 | return true;
275 | }
276 | return false;
277 | }
278 |
279 | ///
280 | /// GL_ARB_bindless_texture must be available
281 | ///
282 | ///
283 | public long GetImageBindlessHandle(int level, bool layered, int layer, PixelFormat pixelFormat, TextureAccess textureAccess)
284 | {
285 | long imageHandle = GL.Arb.GetImageHandle(ID, level, layered, layer, pixelFormat);
286 | GL.Arb.MakeImageHandleResident(imageHandle, (All)textureAccess);
287 | return imageHandle;
288 | }
289 |
290 | ///
291 | /// GL_ARB_bindless_texture must be available
292 | ///
293 | ///
294 | public static bool UnmakeImageBindless(long imageHandle)
295 | {
296 | if (GL.Arb.IsImageHandleResident(imageHandle))
297 | {
298 | GL.Arb.MakeImageHandleNonResident(imageHandle);
299 | return true;
300 | }
301 | return false;
302 | }
303 |
304 | public unsafe Image GetTextureContent(int mipmapLevel = 0)
305 | {
306 | GetSizeMipmap(out int width, out int height, mipmapLevel);
307 |
308 | Image image = new Image(width, height);
309 | fixed (void* ptr = image.GetPixelRowSpan(0))
310 | {
311 | GL.GetTextureImage(ID, mipmapLevel, PixelFormat.Rgba, PixelType.UnsignedByte, GetPixelSize(mipmapLevel) * width * height, (IntPtr)ptr);
312 | }
313 | GL.Finish();
314 |
315 | image.Mutate(p => p.Flip(FlipMode.Vertical));
316 |
317 | return image;
318 | }
319 |
320 | public void GetSizeMipmap(out int width, out int height, int mipmapLevel = 0)
321 | {
322 | GL.GetTextureLevelParameter(ID, mipmapLevel, GetTextureParameter.TextureWidth, out width);
323 | GL.GetTextureLevelParameter(ID, mipmapLevel, GetTextureParameter.TextureHeight, out height);
324 | }
325 |
326 | public int GetPixelTypeComponentSize(PixelTypeSize pixelTypeSize, int mipmapLevel = 0)
327 | {
328 | GL.GetTextureLevelParameter(ID, mipmapLevel, (GetTextureParameter)pixelTypeSize, out int bitSize);
329 | return bitSize / 8;
330 | }
331 |
332 | public int GetPixelSize(int mipmapLevel = 0)
333 | {
334 | int r = GetPixelTypeComponentSize(PixelTypeSize.TextureRedSize, mipmapLevel);
335 | int g = GetPixelTypeComponentSize(PixelTypeSize.TextureGreenSize, mipmapLevel);
336 | int b = GetPixelTypeComponentSize(PixelTypeSize.TextureBlueSize, mipmapLevel);
337 | int a = GetPixelTypeComponentSize(PixelTypeSize.TextureAlphaSize, mipmapLevel);
338 |
339 | return r + g + b + a;
340 | }
341 |
342 | public void Dispose()
343 | {
344 | GL.DeleteTexture(ID);
345 | }
346 | }
347 | }
348 |
--------------------------------------------------------------------------------
/OpenTK-PathTracer/src/MainWindow.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Diagnostics;
4 | using System.Collections.Generic;
5 | using OpenTK;
6 | using OpenTK.Input;
7 | using OpenTK.Graphics;
8 | using OpenTK.Graphics.OpenGL4;
9 | using OpenTK_PathTracer.Render;
10 | using OpenTK_PathTracer.GameObjects;
11 | using OpenTK_PathTracer.Render.Objects;
12 |
13 | namespace OpenTK_PathTracer
14 | {
15 | class MainWindow : GameWindow
16 | {
17 | public const int MAX_GAMEOBJECTS_SPHERES = 256, MAX_GAMEOBJECTS_CUBOIDS = 64;
18 | public const float EPSILON = 0.005f, FOV = 103;
19 |
20 | public MainWindow()
21 | #if DEBUG
22 | : base(832, 832, new GraphicsMode(0, 0, 0, 0), string.Empty, GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Debug)
23 | #else
24 | : base(832, 832, new GraphicsMode(0, 0, 0, 0))
25 | #endif
26 | {
27 |
28 | }
29 |
30 |
31 | public Matrix4 inverseProjection;
32 | private Vector2 nearFarPlane = new Vector2(EPSILON, 1000f);
33 | public int FPS, UPS;
34 | private int fps, ups;
35 |
36 | public readonly Camera Camera = new Camera(new Vector3(-17.14f, 3.53f, -8.62f), new Vector3(0, 1, 0), -32.2f, 0.8f); // new Vector3(-9, 12, 4) || new Vector3(-11, 9.3f, -9.7f)
37 |
38 |
39 | public bool IsRenderInBackground = true;
40 | protected override void OnRenderFrame(FrameEventArgs e)
41 | {
42 | if (Focused || IsRenderInBackground)
43 | {
44 | //if (Gui.IsEnvironmentAtmosphere)
45 | //{
46 | // AtmosphericScatterer.Render();
47 | //}
48 |
49 | PathTracer.Render();
50 |
51 | PostProcesser.Render(PathTracer.Result);
52 | GL.Viewport(0, 0, Width, Height);
53 | Framebuffer.Bind(0);
54 | PostProcesser.Result.AttachSampler(0);
55 | finalProgram.Use();
56 | GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
57 |
58 | if (Focused)
59 | {
60 | Gui.Render(this, (float)e.Time, out bool frameChanged);
61 | if (frameChanged)
62 | PathTracer.ResetRenderer();
63 | }
64 | SwapBuffers();
65 | fps++;
66 | }
67 |
68 | base.OnRenderFrame(e);
69 | }
70 |
71 | private readonly Stopwatch fpsTimer = Stopwatch.StartNew();
72 | protected override void OnUpdateFrame(FrameEventArgs e)
73 | {
74 | if (fpsTimer.ElapsedMilliseconds >= 1000)
75 | {
76 | Title = $"FPS: {fps}; RayDepth: {PathTracer.RayDepth}; UPS: {ups} Position {Camera.Position}";
77 | FPS = fps;
78 | UPS = ups;
79 | fps = 0;
80 | ups = 0;
81 | fpsTimer.Restart();
82 | }
83 |
84 |
85 | if (Focused)
86 | {
87 | KeyboardManager.Update();
88 | MouseManager.Update();
89 |
90 | if (ImGuiNET.ImGui.GetIO().WantCaptureMouse && !CursorVisible)
91 | {
92 | System.Drawing.Point point = PointToScreen(new System.Drawing.Point(Width / 2, Height / 2));
93 | Mouse.SetPosition(point.X, point.Y);
94 | }
95 |
96 | Gui.Update(this);
97 |
98 | if (KeyboardManager.IsKeyDown(Key.Escape))
99 | Close();
100 |
101 | if (KeyboardManager.IsKeyTouched(Key.V) && !ImGuiNET.ImGui.GetIO().WantCaptureKeyboard)
102 | VSync = VSync == VSyncMode.Off ? VSyncMode.On : VSyncMode.Off;
103 |
104 | if (KeyboardManager.IsKeyTouched(Key.F11))
105 | WindowState = WindowState == WindowState.Normal ? WindowState.Fullscreen : WindowState.Normal;
106 |
107 | if (KeyboardManager.IsKeyTouched(Key.E) && !ImGuiNET.ImGui.GetIO().WantCaptureKeyboard)
108 | {
109 | CursorVisible = !CursorVisible;
110 | CursorGrabbed = !CursorGrabbed;
111 |
112 | if (!CursorVisible)
113 | {
114 | MouseManager.Update();
115 | Camera.Velocity = Vector3.Zero;
116 | }
117 | }
118 |
119 | if (KeyboardManager.IsKeyTouched(Key.R) && !ImGuiNET.ImGui.GetIO().WantCaptureKeyboard)
120 | {
121 | LoadScene();
122 | PathTracer.ResetRenderer();
123 | }
124 |
125 | if (!CursorVisible)
126 | {
127 | Camera.ProcessInputs((float)e.Time, out bool frameChanged);
128 | if (frameChanged)
129 | PathTracer.ResetRenderer();
130 | }
131 | BasicDataUBO.SubData(Vector4.SizeInBytes * 4, Vector4.SizeInBytes * 4, Camera.View.Inverted());
132 | BasicDataUBO.SubData(Vector4.SizeInBytes * 8, Vector4.SizeInBytes, Camera.Position);
133 | }
134 |
135 | ups++;
136 | base.OnUpdateFrame(e);
137 | }
138 |
139 | public readonly List GameObjects = new List();
140 | private ShaderProgram finalProgram;
141 | public BufferObject BasicDataUBO, GameObjectsUBO;
142 | public PathTracer PathTracer;
143 | public ScreenEffect PostProcesser;
144 | public AtmosphericScatterer AtmosphericScatterer;
145 | public Texture SkyBox;
146 | protected override void OnLoad(EventArgs e)
147 | {
148 | Console.WriteLine($"OpenGL: {Helper.APIVersion}");
149 | Console.WriteLine($"GLSL: {GL.GetString(StringName.ShadingLanguageVersion)}");
150 | Console.WriteLine($"GPU: {GL.GetString(StringName.Renderer)}");
151 |
152 | if (!Helper.IsCoreExtensionAvailable("GL_ARB_direct_state_access", 4.5))
153 | throw new NotSupportedException("Your system does not support GL_ARB_direct_state_access");
154 |
155 | if (!Helper.IsCoreExtensionAvailable("GL_ARB_buffer_storage", 4.4))
156 | throw new NotSupportedException("Your system does not support GL_ARB_buffer_storage");
157 |
158 | if (!Helper.IsCoreExtensionAvailable("GL_ARB_compute_shader", 4.3))
159 | throw new NotSupportedException("Your system does not support GL_ARB_compute_shader");
160 |
161 | if (!Helper.IsCoreExtensionAvailable("GL_ARB_texture_storage", 4.2))
162 | throw new NotSupportedException("Your system does not support GL_ARB_texture_storage");
163 |
164 | GL.DepthMask(false);
165 | GL.Disable(EnableCap.DepthTest);
166 | GL.Disable(EnableCap.CullFace);
167 | GL.Disable(EnableCap.Multisample);
168 | GL.Enable(EnableCap.TextureCubeMapSeamless);
169 |
170 | VSync = VSyncMode.Off;
171 | CursorVisible = false;
172 | CursorGrabbed = true;
173 |
174 | AtmosphericScatterer = new AtmosphericScatterer(256);
175 | AtmosphericScatterer.Render();
176 |
177 | SkyBox = new Texture(TextureTarget2d.TextureCubeMap);
178 | SkyBox.SetFilter(TextureMinFilter.Nearest, TextureMagFilter.Linear);
179 | Helper.ParallelLoadCubemapImages(SkyBox, new string[]
180 | {
181 | "res/Textures/EnvironmentMap/posx.png",
182 | "res/Textures/EnvironmentMap/negx.png",
183 | "res/Textures/EnvironmentMap/posy.png",
184 | "res/Textures/EnvironmentMap/negy.png",
185 | "res/Textures/EnvironmentMap/posz.png",
186 | "res/Textures/EnvironmentMap/negz.png"
187 | }, (SizedInternalFormat)PixelInternalFormat.Srgb8Alpha8);
188 |
189 | PathTracer = new PathTracer(AtmosphericScatterer.Result, Width, Height, 13, 1, 20f, 0.14f);
190 | PostProcesser = new ScreenEffect(new Shader(ShaderType.FragmentShader, File.ReadAllText("res/shaders/PostProcessing/fragment.glsl")), Width, Height);
191 | finalProgram = new ShaderProgram(
192 | new Shader(ShaderType.VertexShader, File.ReadAllText("res/shaders/screenQuad.glsl")),
193 | new Shader(ShaderType.FragmentShader, File.ReadAllText("res/shaders/final.glsl")));
194 |
195 | BasicDataUBO = new BufferObject();
196 | BasicDataUBO.ImmutableAllocate(Vector4.SizeInBytes * 4 * 2 + Vector4.SizeInBytes, IntPtr.Zero, BufferStorageFlags.DynamicStorageBit);
197 | BasicDataUBO.BindRange(BufferRangeTarget.UniformBuffer, 0, 0, BasicDataUBO.Size);
198 |
199 | GameObjectsUBO = new BufferObject();
200 | GameObjectsUBO.ImmutableAllocate(Sphere.GPU_INSTANCE_SIZE * MAX_GAMEOBJECTS_SPHERES + Cuboid.GPU_INSTANCE_SIZE * MAX_GAMEOBJECTS_CUBOIDS, IntPtr.Zero, BufferStorageFlags.DynamicStorageBit);
201 | GameObjectsUBO.BindRange(BufferRangeTarget.UniformBuffer, 1, 0, GameObjectsUBO.Size);
202 |
203 | LoadScene();
204 |
205 | base.OnLoad(e);
206 | }
207 |
208 | public void LoadScene()
209 | {
210 | float width = 40.0f, height = 25.0f, depth = 25.0f;
211 | PathTracer.NumSpheres = 0;
212 | PathTracer.NumCuboids = 0;
213 |
214 | #region SetupSpheres
215 | int balls = 6;
216 | float radius = 1.3f;
217 | Vector3 dimensions = new Vector3(width * 0.6f, height, depth);
218 | for (float x = 0; x < balls; x++)
219 | for (float y = 0; y < balls; y++)
220 | GameObjects.Add(new Sphere(new Vector3(dimensions.X / balls * x * 1.1f - dimensions.X / 2, (dimensions.Y / balls) * y - dimensions.Y / 2 + radius, -5), radius, PathTracer.NumSpheres++, new Material(albedo: new Vector3(0.59f, 0.59f, 0.99f), emissiv: new Vector3(0), refractionColor: Vector3.Zero, specularChance: x / (balls - 1), specularRoughness: y / (balls - 1), indexOfRefraction: 1f, refractionChance: 0.0f, refractionRoughnes: 0.1f)));
221 |
222 | Vector3 delta = dimensions / balls;
223 | for (float x = 0; x < balls; x++)
224 | {
225 | Material material = Material.Zero;
226 | material.Albedo = new Vector3(0.9f, 0.25f, 0.25f);
227 | material.SpecularChance = 0.02f;
228 | material.IOR = 1.05f;
229 | material.RefractionChance = 0.98f;
230 | material.AbsorbanceColor = new Vector3(1, 2, 3) * (x / balls);
231 | Vector3 position = new Vector3(-dimensions.X / 2 + radius + delta.X * x, 3f, -20f);
232 | GameObjects.Add(new Sphere(position, radius, PathTracer.NumSpheres++, material));
233 |
234 |
235 | Material material1 = Material.Zero;
236 | material1.SpecularChance = 0.02f;
237 | material1.SpecularRoughness = (x / balls);
238 | material1.IOR = 1.1f;
239 | material1.RefractionChance = 0.98f;
240 | material1.RefractionRoughnes = x / balls;
241 | material1.AbsorbanceColor = Vector3.Zero;
242 | position = new Vector3(-dimensions.X / 2 + radius + delta.X * x, -6f, -20f);
243 | GameObjects.Add(new Sphere(position, radius, PathTracer.NumSpheres++, material1));
244 | }
245 | #endregion
246 |
247 | #region SetupCuboids
248 |
249 | Cuboid down = new Cuboid(new Vector3(0.0f, -height / 2.0f, -10.0f), new Vector3(width, EPSILON, depth), PathTracer.NumCuboids++, new Material(albedo: new Vector3(0.2f, 0.04f, 0.04f), emissiv: new Vector3(0.0f), refractionColor: Vector3.Zero, specularChance: 0.0f, specularRoughness: 0.051f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
250 |
251 | //Cuboid up = new Cuboid(new Vector3(down.Position.X, down.Position.Y + height, down.Position.Z - down.Dimensions.Z / 4f), new Vector3(down.Dimensions.X, EPSILON, down.Dimensions.Z / 2), PathTracer.NumCuboids++, new Material(albedo: new Vector3(0.6f), emissiv: new Vector3(0.0f), refractionColor: Vector3.Zero, specularChance: 0.023f, specularRoughness: 0.051f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
252 | Cuboid upLight = new Cuboid(new Vector3(0.0f, 18.495f - EPSILON, -4.0f), new Vector3(down.Dimensions.X * 0.3f, EPSILON, down.Dimensions.Z * 0.3f), PathTracer.NumCuboids++, new Material(albedo: new Vector3(0.04f), emissiv: new Vector3(0.917f, 0.945f, 0.513f) * 5f, refractionColor: Vector3.Zero, specularChance: 0.0f, specularRoughness: 0.0f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
253 |
254 | Cuboid back = new Cuboid(new Vector3(down.Position.X, down.Position.Y + height / 2, down.Position.Z + depth / 2 - 5f), new Vector3(width, height, EPSILON), PathTracer.NumCuboids++, new Material(albedo: new Vector3(0.37109375f, 0.67578125f, 0.3359375f), emissiv: new Vector3(0.0f), refractionColor: Vector3.Zero, specularChance: 0.0f, specularRoughness: 0.0f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
255 | Cuboid front = new Cuboid(new Vector3(down.Position.X, down.Position.Y + height / 2 + EPSILON, down.Position.Z - depth / 2), new Vector3(width, height - EPSILON * 2, 0.3f), PathTracer.NumCuboids++, new Material(albedo: new Vector3(1f), emissiv: Vector3.Zero, refractionColor: new Vector3(0.01f), specularChance: 0.04f, specularRoughness: 0.0f, indexOfRefraction: 1f, refractionChance: 0.954f, refractionRoughnes: 0.0f));
256 |
257 | Cuboid right = new Cuboid(new Vector3(down.Position.X + width / 2, down.Position.Y + height / 2.0f, down.Position.Z), new Vector3(EPSILON, height, depth), PathTracer.NumCuboids++, new Material(albedo: new Vector3(0.9453125f, 0.75390625f, 0.3046875f), emissiv: new Vector3(0.0f), refractionColor: Vector3.Zero, specularChance: 1.0f, specularRoughness: 0.19f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
258 | Cuboid left = new Cuboid(new Vector3(down.Position.X - width / 2, down.Position.Y + height / 2.0f, down.Position.Z), new Vector3(EPSILON, height, depth), PathTracer.NumCuboids++, new Material(albedo: new Vector3(0.074219f, 0.25f, 0.453125f), emissiv: new Vector3(0.0f), refractionColor: Vector3.Zero, specularChance: 0.0f, specularRoughness: 0.0f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
259 |
260 | Cuboid middle = new Cuboid(new Vector3(-15.0f, -10.5f + EPSILON, -15.0f), new Vector3(3.0f, 6.0f, 3.0f), PathTracer.NumCuboids++, new Material(albedo: new Vector3(1.0f), emissiv: new Vector3(0.0f), refractionColor: Vector3.Zero, specularChance: 0.0f, specularRoughness: 0.0f, indexOfRefraction: 1.0f, refractionChance: 0.0f, refractionRoughnes: 0.0f));
261 |
262 | GameObjects.AddRange(new Cuboid[] { down, upLight, back, front, right, left, middle });
263 | #endregion
264 |
265 | for (int i = 0; i < GameObjects.Count; i++)
266 | GameObjects[i].Upload(GameObjectsUBO);
267 | }
268 |
269 | private int lastWidth = -1, lastHeight = -1;
270 | protected override void OnResize(EventArgs e)
271 | {
272 | if ((lastWidth != Width || lastHeight != Height) && Width != 0 && Height != 0) // dont resize when minimizing and maximizing
273 | {
274 | PathTracer.SetSize(Width, Height);
275 | PostProcesser.SetSize(Width, Height);
276 | Gui.SetSize(Width, Height);
277 |
278 | inverseProjection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(FOV), Width / (float)Height, nearFarPlane.X, nearFarPlane.Y).Inverted();
279 | BasicDataUBO.SubData(0, Vector4.SizeInBytes * 4, inverseProjection);
280 | lastWidth = Width; lastHeight = Height;
281 | }
282 | base.OnResize(e);
283 | }
284 |
285 | protected override void OnKeyPress(KeyPressEventArgs e)
286 | {
287 | Gui.ImGuiController.PressChar(e.KeyChar);
288 | }
289 |
290 | protected override void OnFocusedChanged(EventArgs e)
291 | {
292 | if (Focused)
293 | MouseManager.Update();
294 | }
295 |
296 | protected override void OnClosed(EventArgs e)
297 | {
298 | ImGuiNET.ImGui.SaveIniSettingsToDisk("res/imgui.ini");
299 | base.OnClosed(e);
300 | }
301 |
302 | public bool RayTrace(Ray ray, out BaseGameObject gameObject, out float t1, out float t2)
303 | {
304 | t1 = t2 = 0;
305 | gameObject = null;
306 | float tMin = float.MaxValue;
307 | for (int i = 0; i < GameObjects.Count; i++)
308 | {
309 | if (GameObjects[i].IntersectsRay(ray, out float tempT1, out float tempT2) && tempT2 > 0 && tempT1 < tMin)
310 | {
311 | t1 = tempT1; t2 = tempT2;
312 | tMin = GetSmallestPositive(t1, t2);
313 | gameObject = GameObjects[i];
314 | }
315 | }
316 |
317 | return tMin != float.MaxValue;
318 | }
319 | public static float GetSmallestPositive(float t1, float t2)
320 | {
321 | return t1 < 0 ? t2 : t1;
322 | }
323 |
324 | public void SetGameObjectsRandomMaterial(int maxNum) where T : BaseGameObject
325 | {
326 | int changed = 0;
327 | for (int i = 0; i < GameObjects.Count && changed < maxNum; i++)
328 | {
329 | if (GameObjects[i] is T)
330 | {
331 | GameObjects[i].Material = Material.GetRndMaterial();
332 | GameObjects[i].Upload(GameObjectsUBO);
333 | changed++;
334 | }
335 | }
336 | }
337 | }
338 | }
--------------------------------------------------------------------------------