summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Stensgård <mastensg@mastensg.net>2016-11-02 04:22:43 +0100
committerMartin Stensgård <mastensg@mastensg.net>2016-11-02 04:22:43 +0100
commit63a2b7998c61ce7614446045aa0d0c00cb0b4d1f (patch)
treec366b4bdf917686d35e8e394a7513b1fdff7af94
parentdc68c16069474e4ceb988f6098742ee4c899e1bb (diff)
add pixmap-saving version
-rw-r--r--Makefile.am6
-rw-r--r--main_ppm.c51
2 files changed, 56 insertions, 1 deletions
diff --git a/Makefile.am b/Makefile.am
index 898f522..d978213 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,6 @@
AM_CFLAGS = -Wall -pedantic -std=c99 -flto
-bin_PROGRAMS = ray ray-headless
+bin_PROGRAMS = ray ray-headless ray-ppm
ray_SOURCES = main_glut.c ray.c 3dmath.c 3dmath.h
@@ -11,6 +11,10 @@ ray_headless_SOURCES = main_headless.c ray.c 3dmath.c 3dmath.h
ray_headless_LDADD = -lm
ray_headless_LDFLAGS = -pthread
+ray_ppm_SOURCES = main_ppm.c ray.c 3dmath.c 3dmath.h
+ray_ppm_LDADD = -lm
+ray_ppm_LDFLAGS = -pthread
+
TESTS = ./test-profile-arcs.sh
.PHONY: test
diff --git a/main_ppm.c b/main_ppm.c
new file mode 100644
index 0000000..27febb6
--- /dev/null
+++ b/main_ppm.c
@@ -0,0 +1,51 @@
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "ray.h"
+
+static const size_t kFramesToRender = 126;
+static const size_t kWidth = 512;
+static const size_t kHeight = 512;
+
+void save_ppm(const unsigned char *src, size_t w, size_t h, const char *fmt,
+ ...) {
+ const size_t kPathMax = 1024;
+ char path[kPathMax];
+ va_list ap;
+ size_t y, x;
+ FILE *f;
+
+ va_start(ap, fmt);
+ vsnprintf(path, kPathMax, fmt, ap);
+ va_end(ap);
+
+ f = fopen(path, "w");
+
+ fprintf(f, "P3\n%zu %zu\n%u\n", w, h, 255);
+
+ for (y = 0; y < h; ++y) {
+ for (x = 0; x < w; ++x) {
+ // R G B
+ fprintf(f, "%4u%4u%4u", src[4 * w * y + 4 * x + 0],
+ src[4 * w * y + 4 * x + 1], src[4 * w * y + 4 * x + 2]);
+ }
+ fprintf(f, "\n");
+ }
+
+ fclose(f);
+}
+
+int main() {
+ fprintf(stderr, "Rendering %zu frames\n", kFramesToRender);
+
+ unsigned char *buffer = calloc(4, kWidth * kHeight);
+
+ for (size_t i = 0; i < kFramesToRender; ++i) {
+ trace_scene(i * 0.1f, kWidth, kHeight, buffer, 1);
+ save_ppm(buffer, kWidth, kHeight, "out_%06u.ppm", i);
+ fprintf(stderr, " %zu\n", i);
+ }
+
+ return EXIT_SUCCESS;
+}