Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions RayTracer.Core/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,39 @@

for (int x = 0; x < _options.Width; x++)
{
float distance = float.MaxValue;

Check warning on line 99 in RayTracer.Core/Engine.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

The variable 'distance' is assigned but its value is never used

Check warning on line 99 in RayTracer.Core/Engine.cs

View workflow job for this annotation

GitHub Actions / Build and run tests

The variable 'distance' is assigned but its value is never used
Vector3 color = Vector3.Zero;

Vector3 pixelPos = pixelStart + camRight * (x * deltaX);
Vector3 direction = Vector3.Normalize(pixelPos - camPos);
// Supersampling / stratified jittered sampling
int spp = System.Math.Max(1, _options.SamplesPerPixel);
int side = (int)System.MathF.Round(System.MathF.Sqrt(spp));
if (side * side != spp) side = 1; // fallback to 1 if not perfect square

Ray ray = new(camPos, direction);
Raytrace(_scene, _options, ray, ref color, 1, ref distance);
Vector3 accum = Vector3.Zero;

// simple stratified grid within pixel
for (int sy = 0; sy < side; sy++)
{
for (int sx = 0; sx < side; sx++)
{
// jitter within subpixel
float jitterX = (sx + 0.5f) / side;
float jitterY = (sy + 0.5f) / side;

Vector3 pixelPosSS = pixelStart + camRight * ((x + jitterX) * deltaX) - camUp * ((y + jitterY) * deltaY);
Vector3 dirSS = Vector3.Normalize(pixelPosSS - camPos);

Ray ssRay = new(camPos, dirSS);
float ssDistance = float.MaxValue;
Vector3 ssColor = Vector3.Zero;

Raytrace(_scene, _options, ssRay, ref ssColor, 1, ref ssDistance);

accum += ssColor;
}
}

color = accum / (side * side);
color = Vector3.Clamp(color, Vector3.Zero, Vector3.One);

pixelRowSpan[x] = new Rgba32(color.X, color.Y, color.Z);
Expand Down
6 changes: 3 additions & 3 deletions RayTracer.Core/Primitives/Torus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private float SDF(Vector3 p)
public override RayIntersection Intersects(Ray ray, ref float distance)
{
// Ray-marching using SDF. Not perfect but simple and robust for tests/scenes.
const int maxSteps = 100;
const float hitEps = 1e-3f;
const int maxSteps = 300; // increase steps for finer intersections
const float hitEps = 5e-4f; // tighter hit epsilon for crisper surface
const float maxDist = 100f;

float t = 0f;
Expand All @@ -60,7 +60,7 @@ public override RayIntersection Intersects(Ray ray, ref float distance)
private Vector3 EstimateNormal(Vector3 p)
{
// numerical gradient
float eps = 1e-4f;
float eps = 5e-5f;
float dx = SDF(new Vector3(p.X + eps, p.Y, p.Z)) - SDF(new Vector3(p.X - eps, p.Y, p.Z));
float dy = SDF(new Vector3(p.X, p.Y + eps, p.Z)) - SDF(new Vector3(p.X, p.Y - eps, p.Z));
float dz = SDF(new Vector3(p.X, p.Y, p.Z + eps)) - SDF(new Vector3(p.X, p.Y, p.Z - eps));
Expand Down
4 changes: 4 additions & 0 deletions RayTracer.Core/RenderOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ public class RenderOptions
public required bool DisableReflections { get; init; }
public bool DisableDiffuse { get; init; }
public bool DisableSpeculation { get; init; }

// Supersampling: number of samples per pixel (1 = no supersampling).
// Use perfect squares like 1,4,9 (1x1,2x2,3x3 stratified sampling).
public int SamplesPerPixel { get; init; } = 1;
}
7 changes: 6 additions & 1 deletion Raytracer.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
int height = 600;
string outPath = "render.png";
int depth = 2;
int samplesPerPixel = 1;

for (int i = 0; i < argsList.Length; i++)
{
Expand All @@ -31,6 +32,9 @@
case "--depth":
if (i + 1 < argsList.Length) int.TryParse(argsList[++i], out depth);
break;
case "--spp":
if (i + 1 < argsList.Length) int.TryParse(argsList[++i], out samplesPerPixel);
break;
}
}

Expand Down Expand Up @@ -78,7 +82,8 @@
TraceDepth = depth,
CameraPosition = cameraPos,
CameraTarget = cameraTarget,
DisableReflections = false
DisableReflections = false,
SamplesPerPixel = samplesPerPixel
};

var engine = new Engine(scene, options);
Expand Down
Binary file added assets/torus_render_spp4_tuned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading