commit f64e0f22830ca6712d361e889d61275bf417ba95
parent fea1ed5534469940e59fceb50af2af0c18dc9bdd
Author: Renev Pavel <an2qzavok@gmail.com>
Date: Fri, 18 Nov 2022 00:39:30 +0000
cubes: some tinkerin'
Diffstat:
M | src/cubes/cubes.c | | | 74 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
1 file changed, 67 insertions(+), 7 deletions(-)
diff --git a/src/cubes/cubes.c b/src/cubes/cubes.c
@@ -66,6 +66,28 @@ raynorm(Raydef r)
*/
Raydef
+intersectX(Raydef ray, double X)
+{
+ Raydef ref;
+ double t;
+ if (ray.D[0] >= X) {
+ Raydef ret = {
+ {Inf(ray.D[0]), Inf(ray.D[1]), Inf(ray.D[2])},
+ {ray.D[0], ray.D[1], ray.D[2]},
+ };
+ return ret;
+ }
+ t = (X-ray.O[0]) / ray.D[0];
+ ref.O[0] = X;
+ ref.O[1] = ray.O[1] + ray.D[1] * t;
+ ref.O[2] = ray.O[2] + ray.D[2] * t;
+ ref.D[0] = -ray.D[0];
+ ref.D[1] = ray.D[1];
+ ref.D[2] = ray.D[2];
+ return ref;
+}
+
+Raydef
intersectY(Raydef ray, double Y)
{
Raydef ref;
@@ -87,10 +109,32 @@ intersectY(Raydef ray, double Y)
return ref;
}
+Raydef
+intersectZ(Raydef ray, double Z)
+{
+ Raydef ref;
+ double t;
+ if (ray.D[2] >= Z) {
+ Raydef ret = {
+ {Inf(ray.D[0]), Inf(ray.D[1]), Inf(ray.D[2])},
+ {ray.D[0], ray.D[1], ray.D[2]},
+ };
+ return ret;
+ }
+ t = (Z-ray.O[0]) / ray.D[0];
+ ref.O[0] = ray.O[0] + ray.D[0] * t;
+ ref.O[1] = ray.O[1] + ray.D[1] * t;
+ ref.O[2] = Z;
+ ref.D[0] = ray.D[0];
+ ref.D[1] = ray.D[1];
+ ref.D[2] = -ray.D[2];
+ return ref;
+}
+
void
vrender(void)
{
- int i, x, y;
+ int i, j, x, y;
double X, Y;
for (i = 0, y = 0; y < CSHeight; y++) {
Y = (double)(y) / CSHeight;
@@ -98,14 +142,30 @@ vrender(void)
X = (double)(x) / CSWidth;
// TODO: generate camera ray properly
Raydef r = {
- .O {0, 0, 0},
+ .O {10, 10, 10},
.D {-1 + X * 2, 1 - Y * 2, 2}
};
- r = intersectY(r, 0);
-
- if (isInf(r.O[0], 0) != 0) imgbuf[i] = 0x004f7fff;
+ Raydef ref[6];
+ ref[0] = intersectX(r, 0);
+ ref[1] = intersectY(r, 0);
+ ref[2] = intersectZ(r, 0);
+
+ ref[3] = intersectX(r, 0);
+ ref[4] = intersectY(r, 0);
+ ref[5] = intersectZ(r, 0);
+
+ Raydef one; // = infray
+ for (j = 0; j < 6; j++) {
+ // check boundary;
+ if ((ref[j].O[0] < 0) || (ref[j].O[1] < 0) || (ref[j].O[2] < 0))
+ continue;
+ // todo check distance;
+ one = ref[j];
+ }
+ if (isInf(ref[0].O[0], 0) != 0) imgbuf[i] = 0x004f7fff;
else {
- double d = sqrt((r.D[1] * r.D[1]) / (r.D[0] * r.D[0] + r.D[2] * r.D[2]));
+ double d = sqrt((ref[0].D[1] * ref[0].D[1])
+ / (ref[0].D[0] * ref[0].D[0] + ref[0].D[2] * ref[0].D[2]));
int br = 0xff * d;
imgbuf[i] = (br << 16) + (br << 8) + (br);
}
@@ -126,7 +186,7 @@ void
eresized(int new)
{
if(new && getwindow(display, Refnone) < 0)
- fprint(2,"can't reattach to window");
+ sysfatal("can't reattach to window");
redraw(screen);
}