Skip to content

Commit

Permalink
Merge pull request #2 from Gluschenko/maintenance-1
Browse files Browse the repository at this point in the history
Maintenance 1
  • Loading branch information
gluschenko authored Jul 11, 2021
2 parents eb11a27 + 23977eb commit 0c2e2b8
Show file tree
Hide file tree
Showing 35 changed files with 779 additions and 282 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: .NET Core Desktop

on:
push:
branches: [ master ]
branches: [ release ]
pull_request:
branches: [ master ]

Expand All @@ -28,6 +28,7 @@ jobs:
run: dotnet publish -c RELEASE -o out

- uses: actions/upload-artifact@v2
if: github.ref == 'refs/heads/release'
with:
name: builds
path: ${{env.ROOT}}/**/out
Expand All @@ -37,7 +38,7 @@ jobs:

runs-on: ubuntu-latest
needs: build
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/release'
env:
ROOT: ./src
NUGET_AUTH_TOKEN: ${{secrets.token}}
Expand Down
Binary file added .media/PTX_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_10.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_11.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_12.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_13.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_8.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added .media/PTX_9.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 35 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,64 @@
# RenderBox
# Render Box | [![.NET Core Desktop](https://github.com/Gluschenko/render-box/actions/workflows/build.yml/badge.svg)](https://github.com/Gluschenko/render-box/actions/workflows/build.yml)

[![.NET Core Desktop](https://github.com/Gluschenko/render-box/actions/workflows/build.yml/badge.svg)](https://github.com/Gluschenko/render-box/actions/workflows/build.yml)
![](src/RenderBox/Resources/RenderBoxLogo.png)

## Introduction

Main aim of this project is implementation of path tracing algorithm in C# (without GPU optimization at first time).
### Introduction

The main goal of this project is to implement a path tracing
algorithm in C# (without GPU optimization at first time).

There are also several different renders, such as the
Mandelbrot Set or Perlin Noise.

Sources:

* https://en.wikipedia.org/wiki/Path_tracing (EN)

* https://ru.wikipedia.org/wiki/Трассировка_пути (RU)

## Requirements
### Requirements

* Visual Studio 2019
* .NET Core SDK 5.0

## Renderers

## Draft (pseudo-code)
### PathRenderer

Color TracePath(Ray ray, count depth) {
if (depth >= MaxDepth) {
return Black; // Bounced enough times.
}
**Key features:**
* Point lingting
* Soft shadows
* Ambient occlusion
* Transparency & reflection
* Camera movement

ray.FindNearestObject();
if (ray.hitSomething == false) {
return Black; // Nothing was hit.
}
![](.media/PTX_7.jpg)
![](.media/PTX_9.jpg)
![](.media/PTX_10.jpg)
![](.media/PTX_11.jpg)
![](.media/PTX_13.jpg)

Material material = ray.thingHit->material;
Color emittance = material.emittance;

// Pick a random direction from here and keep going.
Ray newRay;
newRay.origin = ray.pointWhereObjWasHit;
### MandelbrotRenderer

// This is NOT a cosine-weighted distribution!
newRay.direction = RandomUnitVectorInHemisphereOf(ray.normalWhereObjWasHit);
**Key features:**
* Zoom in / zoon out
* Color filters

// Probability of the newRay
const float p = 1/(2*M_PI);
![](.media/14.png)
![](.media/10.jpg)
![](.media/13.png)

// Compute the BRDF for this ray (assuming Lambertian reflection)
float cos_theta = DotProduct(newRay.direction, ray.normalWhereObjWasHit);
Color BRDF = material.reflectance / M_PI ;
### PerlinRenderer

// Recursively trace reflected light sources.
Color incoming = TracePath(newRay, depth + 1);
![](.media/11.jpg)

// Apply the Rendering Equation here.
return emittance + (BRDF * incoming * cos_theta / p);
}
### RandomRenderer

void Render(Image finalImage, count numSamples) {
foreach (pixel in finalImage) {
foreach (i in numSamples) {
Ray r = camera.generateRay(pixel);
pixel.color += TracePath(r, 0);
}
pixel.color /= numSamples; // Average samples.
}
}

## Work in progress

![](.media/1.jpg)
![](.media/2.jpg)
![](.media/3.jpg)
![](.media/4.jpg)
![](.media/5.jpg)
![](.media/6.jpg)
![](.media/7.jpg)
![](.media/8.jpg)
![](.media/9.jpg)
![](.media/10.jpg)
![](.media/11.jpg)
![](.media/12.jpg)
![](.media/13.png)
![](.media/14.png)

## Aim
## Goal of project

![](.media/aim_1.png)
![](.media/aim_2.png)
15 changes: 15 additions & 0 deletions src/RenderBox.Shared/Core/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public struct Color
public static Color Green => new Color(0, 1, 0);
public static Color Cyan => new Color(0, 1, 1);
public static Color Blue => new Color(0, 0, 1);
public static Color Gray => new Color(0.5, 0.5, 0.5);

//

Expand All @@ -28,6 +29,15 @@ public Color(float r, float g, float b, float a = 1)
A = a;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Color(double r, double g, double b, double a = 1)
{
R = (float)r;
G = (float)g;
B = (float)b;
A = (float)a;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clamp()
{
Expand Down Expand Up @@ -77,5 +87,10 @@ public override bool Equals(object obj)
}
return false;
}

public static Color Lerp(Color a, Color b, float r)
{
return a + (b - a) * r;
}
}
}
35 changes: 22 additions & 13 deletions src/RenderBox.Shared/Core/Rand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,42 @@ namespace RenderBox.Core
{
public static class Rand
{
private static Random random = new Random(RandomizeSeed());
private static Random _random = new Random(RandomizeSeed());

public static void InitState(int seed)
{
random = null;
random = new Random(seed);
_random = null;
_random = new Random(seed);
}

public static int Int(int min, int max) => random.Next(min, max);
public static int Int(int min, int max) => _random.Next(min, max);
public static int Int() => Int(int.MinValue, int.MaxValue);

public static double Double(double min, double max) => MathHelpres.Lerp(min, max, Double());
public static double Double() => random.NextDouble();
public static double Double() => (double)Next() / int.MaxValue; //random.NextDouble();

public static float Float() => random.NextFloat();
public static float Float() => (float)Next() / int.MaxValue; //random.NextFloat();

public static long Long(long min, long max) => MathHelpres.Lerp(min, max, Double());
public static long Long() => Long(long.MinValue, long.MaxValue);

/// <summary>
/// Генерация начального состояния (seed) со значительно сниженной предсказуемостью
/// </summary>
public static int RandomizeSeed() => Guid.NewGuid().GetHashCode();
/// <summary>
/// Сброс состояния
/// </summary>
public static void Reset() => random = new Random(RandomizeSeed());
public static void Reset() => _random = new Random(RandomizeSeed());

private static int
x = RandomizeSeed(),
y = RandomizeSeed(),
z = RandomizeSeed(),
w = RandomizeSeed();

private static int Next()
{
int t = x ^ (x << 11);
x = y;
y = z;
z = w;
return (w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)));
}
}

public static class RandomExtensions
Expand Down
36 changes: 24 additions & 12 deletions src/RenderBox.Shared/Core/Vector.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;

namespace RenderBox.Core
{
Expand Down Expand Up @@ -52,10 +51,10 @@ public Vector2(float x) : this(x, x) { }

public double Length => Math.Sqrt(x * x + y * y);

public override bool Equals(object obj)
public override bool Equals(object obj)
=> EqualsInternal(obj as Vector2?);

private bool EqualsInternal(Vector2? vector)
private bool EqualsInternal(Vector2? vector)
=> vector.HasValue && vector.Value == this;

public override int GetHashCode() => HashCode.Combine(x, y);
Expand Down Expand Up @@ -122,7 +121,6 @@ private bool EqualsInternal(Vector3? vector)
=> vector.HasValue && vector.Value == this;

public override int GetHashCode() => HashCode.Combine(x, y, z);

}

public struct Vector4
Expand Down Expand Up @@ -168,12 +166,23 @@ public Quaternion(bool fill) : this()
M[3] = 0.0f; M[7] = 0.0f; M[11] = 0.0f; M[15] = 1.0f;
}

public double this[int i] => M[i];
public double this[int i]
{
get
{
return M[i];
}
set
{
M[i] = value;
}
}

public double this[int x, int y] => M[y * 4 + x];

public static Quaternion operator *(Quaternion a, Quaternion b)
public static Quaternion operator *(Quaternion a, Quaternion b)
{
Quaternion c = new Quaternion(false);
var c = new Quaternion(false);

c.M[0] = a.M[0] * b.M[0] + a.M[4] * b.M[1] + a.M[8] * b.M[2] + a.M[12] * b.M[3];
c.M[1] = a.M[1] * b.M[0] + a.M[5] * b.M[1] + a.M[9] * b.M[2] + a.M[13] * b.M[3];
Expand All @@ -200,7 +209,7 @@ public Quaternion(bool fill) : this()

public static Vector2 operator *(Quaternion a, Vector2 b) => (Vector2)(a * new Vector4(b.x, b.y, 0.0f, 1.0f));
public static Vector3 operator *(Quaternion a, Vector3 b) => (Vector3)(a * new Vector4(b.x, b.y, b.z, 1.0f));
public static Vector4 operator *(Quaternion a, Vector4 b)
public static Vector4 operator *(Quaternion a, Vector4 b)
{
Vector4 v;

Expand All @@ -225,7 +234,7 @@ private bool EqualsInternal(Quaternion? vector)
public override int GetHashCode() => HashCode.Combine(M);
}

public static class VectorMath
public static class VectorMath
{
#region Vector2

Expand Down Expand Up @@ -266,7 +275,10 @@ public static Vector3 Cross(Vector3 a, Vector3 b)
return new Vector3(x, y, z);
}

public static Vector3 Normalize(Vector3 a) => a / a.Length;
public static Vector3 Normalize(Vector3 a)
{
return a * (1.0 / a.Length);
}

public static Vector3 Reflect(Vector3 i, Vector3 n)
{
Expand All @@ -278,8 +290,8 @@ public static Vector3 Refract(Vector3 i, Vector3 n, float eta)
var ni = Dot(n, i);
var k = 1.0f - eta * eta * (1.0f - ni * ni);

var result = k >= 0.0f
? eta * i - n * (eta * ni + Math.Sqrt(k))
var result = k >= 0.0f
? eta * i - n * (eta * ni + Math.Sqrt(k))
: new Vector3();

return result;
Expand Down
Loading

0 comments on commit 0c2e2b8

Please sign in to comment.