Skip to content

Commit

Permalink
Merge pull request #55 from tomasz-herman/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tomasz-herman committed Jan 7, 2021
2 parents 4ee6581 + daee217 commit 0994b20
Show file tree
Hide file tree
Showing 90 changed files with 5,038 additions and 2,548 deletions.
41 changes: 37 additions & 4 deletions RayTracer/Source/Cameras/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,52 @@ namespace RayTracing.Cameras
{
public abstract class Camera
{
protected Vector3 Front { get; set; } = -Vector3.UnitZ;
public Vector3 Front { get; protected set; } = -Vector3.UnitZ;
protected Vector3 Up { get; set; } = Vector3.UnitY;
protected Vector3 Right { get; set; } = Vector3.UnitX;
protected float Pitch { get; set; }
protected float Yaw { get; set; } = -MathHelper.PiOver2;
private float _pitch;

public float Pitch
{
get => _pitch;
set
{
_pitch = value;
UpdateVectors();
}
}

private float _yaw = -MathHelper.PiOver2;

public float Yaw
{
get => _yaw;
set
{
_yaw = value;
UpdateVectors();
}
}

private float _aspectRatio = 16f / 9;
protected float _fov = MathHelper.PiOver3;
public Vector3 Position { get; set; }
protected Vector3 Horizontal { get; set; }
protected Vector3 Vertical { get; set; }
protected Vector3 LowerLeft { get; set; }

public float FarPlane { get; set; } = 1000f;

public float Fov
{
get => MathHelper.RadiansToDegrees(_fov);
set
{
var angle = MathHelper.Clamp(value, 1f, 90f);
_fov = MathHelper.DegreesToRadians(angle);
}
}

public float AspectRatio
{
get => _aspectRatio;
Expand Down
31 changes: 13 additions & 18 deletions RayTracer/Source/Cameras/LensCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,14 @@ namespace RayTracing.Cameras
public class LensCamera : Camera
{
private const int SAMPLE_SETS = 8;

private float _fov = MathHelper.PiOver3;

private float _lensRadius;
private float _focusDistance;
private AbstractSampler<Vector2> _lensSampler;
private Func<int, List<Vector2>> _sampling;
private int _samplesCount;

public float Fov
{
get => MathHelper.RadiansToDegrees(_fov);
set
{
var angle = MathHelper.Clamp(value, 1f, 90f);
_fov = MathHelper.DegreesToRadians(angle);
}
}
public bool AutoFocus { get; set; }

public float LensRadius
{
Expand All @@ -46,28 +37,31 @@ public float FocusDistance
UpdateViewport();
}
}

public Func<int, List<Vector2>> Sampling
{
get => _sampling;
set
{
_sampling = value;
_lensSampler = new ThreadSafeSampler<Vector2>(_sampling, _samplesCount, SAMPLE_SETS, Vec2Sampling.ToDisk);
_lensSampler =
new ThreadSafeSampler<Vector2>(_sampling, _samplesCount, SAMPLE_SETS, Vec2Sampling.ToDisk);
}
}

public int SamplesCount
{
get => _samplesCount;
set
{
_samplesCount = value;
_lensSampler = new ThreadSafeSampler<Vector2>(_sampling, _samplesCount, SAMPLE_SETS, Vec2Sampling.ToDisk);
_lensSampler =
new ThreadSafeSampler<Vector2>(_sampling, _samplesCount, SAMPLE_SETS, Vec2Sampling.ToDisk);
}
}

public LensCamera(Vector3 position, float lensRadius, float focusDistance, int samplesCount = 64, Func<int, List<Vector2>> sampling = null)
public LensCamera(Vector3 position, float lensRadius = 0.25f, float focusDistance = 5, int samplesCount = 10000,
Func<int, List<Vector2>> sampling = null)
{
Position = position;
_lensRadius = lensRadius;
Expand Down Expand Up @@ -101,8 +95,9 @@ public override Ray GetRay(float x, float y)
{
Vector2 rd = _lensRadius * _lensSampler.Sample;
Vector3 offset = Right * rd.X + Up * rd.Y;

return new Ray(Position + offset, (LowerLeft + x * Horizontal + y * Vertical - Position - offset).Normalized());

return new Ray(Position + offset,
(LowerLeft + x * Horizontal + y * Vertical - Position - offset).Normalized());
}
}
}
1 change: 0 additions & 1 deletion RayTracer/Source/Cameras/OrthographicCamera.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using OpenTK;
using RayTracing.Maths;

Expand Down
12 changes: 0 additions & 12 deletions RayTracer/Source/Cameras/PerspectiveCamera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ namespace RayTracing.Cameras
{
public class PerspectiveCamera : Camera
{
private float _fov = MathHelper.PiOver3;

public float Fov
{
get => MathHelper.RadiansToDegrees(_fov);
set
{
var angle = MathHelper.Clamp(value, 1f, 90f);
_fov = MathHelper.DegreesToRadians(angle);
}
}

public PerspectiveCamera(Vector3 position)
{
this.Position = position;
Expand Down
2 changes: 1 addition & 1 deletion RayTracer/Source/Lights/AmbientLight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace RayTracing.Lights
{
public struct AmbientLight
public class AmbientLight
{
public Color Color;
}
Expand Down
12 changes: 0 additions & 12 deletions RayTracer/Source/Lights/Light.cs

This file was deleted.

23 changes: 13 additions & 10 deletions RayTracer/Source/Materials/Emissive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ namespace RayTracing.Materials
public class Emissive : IMaterial
{
private const int SAMPLES = 10000;
private ITexture _emit;
private ITexture _albedo;

public Color AverageColor => _averageColor;
public float Amplification { get; set; } = 1;

public ITexture Emit
public ITexture Albedo
{
get => _emit;
get => _albedo;
set
{
_emit = value;
_albedo = value;
UpdateAverageColor(value);
}
}

private Color _averageColor;

public Emissive(ITexture emit)
public Emissive(ITexture emit, float amp = 1)
{
Emit = emit;
Amplification = amp;
Albedo = emit;
}

public Emissive(Color emitColor)
public Emissive(Color emitColor, float amp = 1)
{
Emit = new SolidColor(emitColor);
Amplification = amp;
Albedo = new SolidColor(emitColor);
}

private void UpdateAverageColor(ITexture texture)
Expand All @@ -56,12 +59,12 @@ public bool Scatter(ref Ray ray, ref HitInfo hit, out Color attenuation, out Ray

public Color Emitted(float u, float v)
{
return Emit[u, v];
return Albedo[u, v] * Amplification;
}

public void Use(Shader shader, float part)
{
Emit.Use(shader, 0, part);
Albedo.Use(shader, 0, part);
}
}
}
8 changes: 5 additions & 3 deletions RayTracer/Source/Materials/MasterMaterial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ public Color Emitted(float u, float v)

public void Use(Shader shader, float part)
{
Emissive.Use(shader, Parts.emissive);
Diffuse.Use(shader, Parts.diffuse);
Reflective.Use(shader, Parts.reflective);
var sum = Parts.diffuse + Parts.emissive + Parts.reflective + Parts.refractive;
if (sum == 0) sum = 1;
Emissive.Use(shader, Parts.emissive / sum);
Diffuse.Use(shader, Parts.diffuse / sum);
Reflective.Use(shader, Parts.reflective / sum);
}
}
}
46 changes: 39 additions & 7 deletions RayTracer/Source/Materials/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ public Texture(string path, bool glLoad = true)

public Color this[float u, float v]
{
get => _data[(int) (u * (Width - 1)), (int) (v * (Height - 1))];
get
{
if (u < 0 || u > 1 || v < 0 || v > 1)
{
u -= (float) Math.Floor(u);
v -= (float) Math.Floor(v);
}

return _data[(int) (u * (Width - 1)), (int) (v * (Height - 1))];
}
}

public Color this[int w, int h]
Expand All @@ -60,25 +69,48 @@ public Texture(string path, bool glLoad = true)
set => _data[w, h] = value;
}

public void Bloom(Color color, int w, int h, int strength = 1)
public void CopyFrom(Texture texture)
{
if (this.Width != texture.Width || this.Height != texture.Height) return;
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
this[i, j] = texture[i, j];
}
}
}

public void Bloom(Color color, int w, int h, int brightness, int strength = 1)
{
if (strength == 0 || brightness <= 1) return;

brightness *= strength;

bool In(int x, int y)
{
return x >= 0 && x < Width && y >= 0 && y < Height;
}

for (int i = -strength; i < strength; i++)
for (int j = -strength; j < strength; j++)
for (int i = -brightness; i < brightness; i++)
for (int j = -brightness; j < brightness; j++)
{
if (In(w + i, h + j))
if (In(w + i, h + j) && i != 0 && j != 0)
{
int rs = i * i + j * j;
if (rs <= strength * strength)
_data[w + i, h + j] += color / (10*strength * (rs + 1));
if (rs <= brightness * brightness)
_data[w + i, h + j] += color * strength * strength / (10 * brightness * (rs + 1));
}
}
}

public static Texture operator *(Texture texture, float multiplier)
{
var res = new Texture(texture);
res.Process(color => color * multiplier);
return res;
}

public void Process(Func<Color, Color> function)
{
for (int i = 0; i < Width; i++)
Expand Down
9 changes: 5 additions & 4 deletions RayTracer/Source/Maths/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ public class AABB
{
public Vector3 Min { get; set; }
public Vector3 Max { get; set; }

public AABB(Vector3 min, Vector3 max)
{
if (min.X > max.X || min.Y > max.Y || min.Z > max.Z)
throw new ArgumentException("Minimum vector is greater than maximum vector");
Min = min;
Max = max;
}

public AABB(float xMin, float yMin, float zMin, float xMax, float yMax, float zMax)
{
if (xMin > xMax || yMin > yMax || zMin > zMax)
Expand All @@ -26,7 +26,7 @@ public AABB(float xMin, float yMin, float zMin, float xMax, float yMax, float zM

public bool Test(ref Ray ray, float from = 0, float to = float.PositiveInfinity)
{
for (int a = 0; a < 3; a++)
for (int a = 0; a < 3; a++)
{
float invD = 1.0f / ray.Direction[a];
float t0 = (Min[a] - ray.Origin[a]) * invD;
Expand All @@ -38,12 +38,13 @@ public bool Test(ref Ray ray, float from = 0, float to = float.PositiveInfinity)
if (to <= from)
return false;
}

return true;
}

public static AABB operator +(AABB first, AABB second)
{
return new AABB(Vector3.ComponentMin(first.Min, second.Min),
return new AABB(Vector3.ComponentMin(first.Min, second.Min),
Vector3.ComponentMax(first.Max, second.Max));
}
}
Expand Down
15 changes: 15 additions & 0 deletions RayTracer/Source/Maths/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ public static Color FromAssimpColor4(Color4D color4)
};
}

public static Color FromSystemDrawing(System.Drawing.Color color)
{
return new Color
{
R = color.R/256f,
G = color.G/256f,
B = color.B/256f
};
}

public System.Drawing.Color ToSystemDrawing()
{
return System.Drawing.Color.FromArgb(RComp, GComp, BComp);
}

public static Color operator +(Color first, Color second)
{
first.R += second.R;
Expand Down
Loading

0 comments on commit 0994b20

Please sign in to comment.