commit 606dd285d380baf59ecf643f8171f0f359437721
parent ba7443a2cdd8fe83d60ce3236d3d2d4470210139
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Sun, 28 Mar 2021 19:26:55 +0000
render: a little refactoring
Diffstat:
3 files changed, 31 insertions(+), 29 deletions(-)
diff --git a/mkfile b/mkfile
@@ -2,7 +2,7 @@
TARG=fs node render
OFILES=
-HFILE=octree.h
+HFILE=octree.h render.h
BIN=/$objtype/bin/octree
</sys/src/cmd/mkmany
diff --git a/render.c b/render.c
@@ -1,34 +1,11 @@
#include <u.h>
#include <libc.h>
-#include "octree.h"
+#include "render.h"
#define BSIZE 512
#define SQR(X) (X * X)
-typedef struct Vect Vect;
-typedef struct Ray Ray;
-typedef struct Sphere Sphere;
-
-struct Vect {
- double x;
- double y;
- double z;
-};
-
-struct Ray {
- Vect pos;
- Vect dir;
-};
-
-struct Sphere {
- Vect pos;
- double radius;
-};
-
-Vect Pwhite = {1, 1, 1};
-Vect Pblack = {0, 0, 0};
-
double
vectmod(Vect d)
{
@@ -56,17 +33,18 @@ addvect(Vect d1, Vect d2)
return (Vect){d1.x + d2.x, d1.y + d2.y, d1.z + d2.z};
}
-
Vect
subvect(Vect d1, Vect d2)
{
return addvect(d1, (Vect){-d2.x, -d2.y, -d2.z});
}
-
Vect
todecart(Vect v)
{
+ /* TODO: recheck this code, it's probably wrong.
+ * hypot specifically.
+ */
return (Vect) {
v.z * sin(v.x),
v.z * sin(v.y),
@@ -79,6 +57,7 @@ vectscalar(Vect d1, Vect d2)
{
return d1.x * d2.x + d1.y * d2.y + d1.z * d2.z;
}
+
int
spherehit(Ray *v, Sphere s)
{
@@ -104,7 +83,7 @@ ray(Ray v)
{
Vect vv;
vv = v.pos;
- Sphere sphere = {{0, 0, 10}, 0.95};
+ Sphere sphere = {{0, 0, }, 1};
Vect light = vectnorm((Vect){10, -10, 10});
Vect p = Pblack;
if (spherehit(&v, sphere)) {
@@ -126,7 +105,7 @@ render(int x, int y)
u32int r, g, b;
Ray v;
fov = PI/2;
- v.pos = (Vect) { 0, 0, -1 };
+ v.pos = (Vect) { 0, 0, -3 };
v.dir = todecart((Vect) {
fov*((double)x/BSIZE - 0.5),
fov*((double)y/BSIZE - 0.5),
diff --git a/render.h b/render.h
@@ -0,0 +1,23 @@
+typedef struct Vect Vect;
+typedef struct Ray Ray;
+typedef struct Sphere Sphere;
+
+struct Vect {
+ double x;
+ double y;
+ double z;
+};
+
+struct Ray {
+ Vect pos;
+ Vect dir;
+};
+
+struct Sphere {
+ Vect pos;
+ double radius;
+};
+
+Vect Pwhite = {1, 1, 1};
+Vect Pblack = {0, 0, 0};
+