summaryrefslogtreecommitdiff
path: root/pineapple.c
diff options
context:
space:
mode:
authorSam Nystrom <sam@samnystrom.dev>2024-02-14 13:45:19 +0000
committerSam Nystrom <sam@samnystrom.dev>2024-02-14 16:49:05 -0500
commit24af871f1284f2009a6c7ba32dbe66d142820ad0 (patch)
tree9034ff9b6ee26cc10c49362a0a3bf783eba4f7cd /pineapple.c
parent85c8fe423a3e1e2d7a63460c68ed1ec3d8faeb76 (diff)
Add width and height flags
Diffstat (limited to 'pineapple.c')
-rw-r--r--pineapple.c27
1 files changed, 15 insertions, 12 deletions
diff --git a/pineapple.c b/pineapple.c
index 20ee78b..870fd4c 100644
--- a/pineapple.c
+++ b/pineapple.c
@@ -1,6 +1,7 @@
/* ISC License */
#include <endian.h>
+#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <math.h>
@@ -16,7 +17,7 @@
#define die(code, msg) do { fprintf(stderr, "pineapple: %s\n", msg); exit(code); } while (0);
-#define USAGE "pineapple [-v] [-f | -a]"
+#define USAGE "pineapple [-v] [-w width] [-h height] [-f | -a]"
#define dieusage() die(100, "usage: " USAGE)
#define ASCII_CHARS "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
@@ -82,8 +83,8 @@ ray_triangle_intersect(vec3 P, vec3 d, struct triangle tri)
float t = (dot(n, A) - dot(n, P)) / dot(n, d);
vec3 Q = vadd(P, smul(d, t));
if (dot(cross(vsub(B, A), vsub(Q, A)), n) >= 0.0 &&
- dot(cross(vsub(C, B), vsub(Q, B)), n) >= 0.0 &&
- dot(cross(vsub(A, C), vsub(Q, C)), n) >= 0.0)
+ dot(cross(vsub(C, B), vsub(Q, B)), n) >= 0.0 &&
+ dot(cross(vsub(A, C), vsub(Q, C)), n) >= 0.0)
return t;
return NAN;
}
@@ -91,20 +92,25 @@ ray_triangle_intersect(vec3 P, vec3 d, struct triangle tri)
int
main(int argc, char *argv[])
{
- int c;
int verbosity = 0;
+ uint32_t width = 80;
+ uint32_t height = 40;
enum { ASCII, FARBFELD } mode = ASCII;
- while ((c = getopt(argc, argv, "vfa")) > 0) {
+ int c;
+ while ((c = getopt(argc, argv, "afvh:w:")) > 0) {
+ errno = 0;
switch (c) {
- case 'v': verbosity++; break;
- case 'f': mode = FARBFELD; break;
case 'a': mode = ASCII; break;
+ case 'f': mode = FARBFELD; break;
+ case 'v': verbosity = 1; break;
+ case 'h': height = strtoul(optarg, NULL, 10); if (errno) dieusage(); break;
+ case 'w': width = strtoul(optarg, NULL, 10); if (errno) dieusage(); break;
default: dieusage();
}
}
argv += optind; argc -= optind;
if (argc > 0) dieusage();
-
+
const char *stl;
{
int stl_fd = open("pineapple.stl", O_RDONLY);
@@ -117,7 +123,7 @@ main(int argc, char *argv[])
}
stl += 80;
uint32_t len = le32toh(*(uint32_t *)stl);
- stl += sizeof(uint32_t);
+ stl += 4;
const struct triangle *triangles = (const struct triangle *)stl;
vec3 camera_pos = { -70.0, 60.0, 60.0 };
@@ -126,9 +132,6 @@ main(int argc, char *argv[])
float fov = 80.0 * PI / 180.0;
vec3 light = { 0.0, 0.0, -1.0 };
- uint32_t width = 106;
- uint32_t height = 53;
-
if (mode == FARBFELD) {
uint32_t wbe = htobe32(width);
uint32_t hbe = htobe32(height);