Add basic PPM generation

This commit is contained in:
Michael Kohl 2021-02-10 22:34:50 +07:00
commit b8047f4d4e
5 changed files with 65614 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
.dub
docs.json
__dummy.html
docs/
/oneweekend-d
oneweekend-d.so
oneweekend-d.dylib
oneweekend-d.dll
oneweekend-d.a
oneweekend-d.lib
oneweekend-d-test-*
*.exe
*.o
*.obj
*.lst
owe

16
Makefile Normal file
View File

@ -0,0 +1,16 @@
# vim:noexpandtab
.PHONY: build clean run
dmd_opts = -de -w -unittest
prog = owe
main = source/$(prog).d
run:
rdmd $(dmd_opts) $(main)
build:
dmd $(dmd_opts) $(main)
clean:
rm $(prog) $(prog).o

9
dub.json Normal file
View File

@ -0,0 +1,9 @@
{
"authors": [
"Michael Kohl"
],
"copyright": "Copyright © 2021, Michael Kohl",
"description": "Ray Tracing in One Weekend",
"license": "MIT",
"name": "oneweekend-d"
}

65539
images/01-test.ppm Normal file

File diff suppressed because it is too large Load Diff

34
source/owe.d Normal file
View File

@ -0,0 +1,34 @@
import std.conv : to;
import std.format : format;
import std.range : iota;
import std.stdio;
void main()
{
// Image
const int imageWidth = 256;
const imageHeight = 256;
// Render
write(format("P3\n%s %s\n255\n", imageWidth, imageHeight));
for (int j = imageHeight - 1; j >= 0; --j)
{
stderr.write(format("\rScanlines remaining: %d", j));
for (int i = 0; i < imageWidth; ++i)
{
auto r = to!double(i) / (imageWidth - 1);
auto g = to!double(j) / (imageHeight - 1);
auto b = 0.25;
const int ir = to!int(255.999 * r);
const int ig = to!int(255.999 * g);
const int ib = to!int(255.999 * b);
write(format("%d %d %d\n", ir, ig, ib));
}
}
stderr.writeln("\nDone.");
}