Add antialiasing

This commit is contained in:
Michael Kohl 2021-03-05 13:18:53 +07:00
parent 28ff1da236
commit b36a49acd8
8 changed files with 90110 additions and 22 deletions

View File

@ -5,8 +5,8 @@
"configurations": [
{
"name": "executable",
"targetType": "executable",
"targetName": "owe"
"targetName": "owe",
"targetType": "executable"
},
{
"dependencies": {
@ -17,7 +17,10 @@
}
],
"copyright": "Copyright © 2021, Michael Kohl",
"dependencies": {
"serve-d": "~>0.6.0"
},
"description": "Ray Tracing in One Weekend",
"license": "MIT",
"name": "oneweekend-d"
}
}

View File

@ -1,6 +1,40 @@
{
"fileVersion": 1,
"versions": {
"silly": "1.1.1"
"botan": "1.12.19",
"botan-math": "1.0.3",
"cachetools": "0.3.1",
"dfmt": "0.11.0",
"diet-complete": "0.0.3",
"diet-ng": "1.7.4",
"dscanner": "0.8.0",
"dsymbol": "0.9.2",
"dub": "1.18.0",
"dunit": "1.0.16",
"emsi_containers": "0.8.0-alpha.19",
"eventcore": "0.9.13",
"eventsystem": "1.2.0",
"inifiled": "1.3.1",
"isfreedesktop": "0.1.1",
"libasync": "0.8.6",
"libddoc": "0.7.0",
"libdparse": "0.13.1",
"memutils": "1.0.4",
"mir-linux-kernel": "1.0.1",
"openssl": "1.1.6+1.0.1g",
"painlessjson": "1.4.0",
"painlesstraits": "0.3.0",
"requests": "1.0.15",
"rm-rf": "0.1.0",
"serve-d": "0.6.0",
"silly": "1.1.1",
"standardpaths": "0.8.1",
"stdx-allocator": "2.77.5",
"taggedalgebraic": "0.11.19",
"unit-threaded": "1.0.11",
"vibe-core": "1.13.0",
"vibe-d": "0.9.3",
"workspace-d": "3.5.0",
"xdgpaths": "0.2.5"
}
}

BIN
images/06-antialiasing.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

90003
images/06-antialiasing.ppm Normal file

File diff suppressed because it is too large Load Diff

31
source/camera.d Normal file
View File

@ -0,0 +1,31 @@
import ray;
import vec3;
class Camera
{
this()
{
auto aspectRatio = 16.0 / 9.0;
auto viewportHeight = 2.0;
auto viewportWidth = aspectRatio * viewportHeight;
auto focalLength = 1.0;
_origin = new Point(0, 0, 0);
_horizontal = new Vec3(viewportWidth, 0, 0);
_vertical = new Vec3(0, viewportHeight, 0);
_lowerLeftCorner = _origin - _horizontal / 2 - _vertical / 2 -
new Vec3(0, 0, focalLength);
}
Ray getRay(double u, double v)
{
return new Ray(_origin, _lowerLeftCorner + u * _horizontal +
v * _vertical - _origin);
}
private:
Point _origin;
Point _lowerLeftCorner;
Vec3 _horizontal;
Vec3 _vertical;
}

View File

@ -1,3 +1,4 @@
import std.algorithm.comparison : clamp;
import std.conv : to;
import std.format : format;
import std.stdio : writeln;
@ -10,13 +11,15 @@ import vec3;
alias Color = Vec3;
void writeColor(Color pixelColor)
void writeColor(Color pixelColor, int samplesPerPixel)
{
const int r = to!int(255.999 * pixelColor.x);
const int g = to!int(255.999 * pixelColor.y);
const int b = to!int(255.999 * pixelColor.z);
// Divide the color by the number of samples
auto scale = 1.0 / samplesPerPixel;
auto r = 256 * clamp(pixelColor.x * scale, 0.0, 0.999);
auto g = 256 * clamp(pixelColor.y * scale, 0.0, 0.999);
auto b = 256 * clamp(pixelColor.z * scale, 0.0, 0.999);
writeln(format("%d %d %d", r, g, b));
writeln(format("%d %d %d", r.to!int, g.to!int, b.to!int));
}
Color rayColor(Ray r, Hittable world)

View File

@ -2,10 +2,12 @@ import std.conv : to;
import std.format : format;
import std.stdio;
import camera;
import color;
import hittableList;
import ray;
import sphere;
import util;
import vec3;
version (unittest)
@ -20,6 +22,7 @@ else
const auto aspectRatio = 16.0 / 9.0;
const int imageWidth = 400;
const imageHeight = to!int(imageWidth / aspectRatio);
const int samplesPerPixel = 100;
// World
auto world = new HittableList();
@ -27,14 +30,7 @@ else
world.add(new Sphere(new Point(0, -100.5, -1), 100));
// Camera
auto viewportHeight = 2.0;
auto viewportWidth = aspectRatio * viewportHeight;
auto focalLenght = 1.0;
auto origin = new Point(0, 0, 0);
auto horizontal = new Vec3(viewportWidth, 0, 0);
auto vertical = new Vec3(0, viewportHeight, 0);
auto lowerLeftCorner = origin - horizontal / 2 - vertical / 2 - new Vec3(0, 0, focalLenght);
auto camera = new Camera();
// Render
writeln(format("P3\n%s %s\n255", imageWidth, imageHeight));
@ -43,11 +39,15 @@ else
stderr.write(format("\rScanlines remaining: %d", j));
for (int i = 0; i < imageWidth; ++i)
{
auto u = to!double(i) / (imageWidth - 1);
auto v = to!double(j) / (imageHeight - 1);
auto r = new Ray(origin, lowerLeftCorner + u * horizontal + v * vertical - origin);
Color pixelColor = rayColor(r, world);
writeColor(pixelColor);
auto pixelColor = new Color(0, 0, 0);
for (int s = 0; s < samplesPerPixel; ++s)
{
auto u = (i + randomDouble) / (imageWidth - 1);
auto v = (j + randomDouble) / (imageHeight - 1);
auto r = camera.getRay(u, v);
pixelColor += r.rayColor(world);
}
writeColor(pixelColor, samplesPerPixel);
}
}
stderr.writeln("\nDone.");

View File

@ -1,3 +1,5 @@
import std.random : uniform;
const double infinity = double.infinity;
const double pi = 3.1415926535897932385;
@ -6,3 +8,15 @@ double degreesToRadians(double degrees)
{
return degrees * pi / 180.0;
}
pragma(inline):
double randomDouble()
{
return uniform(0.0, 1.0);
}
pragma(inline):
double randomDouble(double min, double max)
{
return uniform(min, max);
}