commit 95a6c58dc965c7bbce03bf9d3e2a0100bce816f1
parent 2e12698eb0ed56f03b462a0a364079c507304b78
Author: glenda <glenda@device>
Date: Mon, 24 Oct 2022 21:07:09 +0000
add src/cubes
Diffstat:
4 files changed, 245 insertions(+), 0 deletions(-)
diff --git a/src/cubes/bench.c b/src/cubes/bench.c
@@ -0,0 +1,49 @@
+#include <u.h>
+#include <libc.h>
+
+#define BSize (1920 * 1080)
+
+u64int *buf;
+
+vlong
+bench1(void (func)(void))
+{
+ static vlong start, end;
+ start = nsec();
+ func();
+ end = nsec();
+ return end - start;
+}
+
+void
+init(void)
+{
+ buf = mallocz(sizeof(u64int) * BSize, 1);
+}
+
+void
+fill(void)
+{
+ static i;
+ for (i = 0; i < BSize; i++) {
+ static x, y, z;
+ x = i & 0xff;
+ y = i >> 8 & 0xff;
+ z = i >> 16 & 0xff;
+ buf[i] = x + y + z;
+ }
+}
+
+void
+main(void)
+{
+ static vlong sum;
+ static int i;
+ sum = bench1(init);
+ print("mallocz %lld ns\n", sum);
+ sum = bench1(fill);
+ print("fill1 %lld ns\n", sum);
+ for (sum = 0, i = 0; i < 100; i++) sum += bench1(fill);
+ print("fill100 %lld, %lld ns\n", sum, sum / 100);
+
+}
diff --git a/src/cubes/cube.c b/src/cubes/cube.c
@@ -0,0 +1,122 @@
+/*
+ * cube.c: a cube, rotating
+ */
+
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <geometry.h>
+#include <thread.h>
+#include <mouse.h>
+#include <cursor.h>
+#include <keyboard.h>
+
+Image *red;
+Space *s[4];
+double theta;
+
+
+
+Point3 cube[8] = {
+ {-1, -1, -1, 1}, {-1, -1, 1, 1},
+ {-1, 1, -1, 1}, {-1, 1, 1, 1},
+ { 1, -1, -1, 1}, { 1, -1, 1, 1},
+ { 1, 1, -1, 1}, { 1, 1, 1, 1},
+};
+
+int lines[12][2] = {
+ {0, 1}, {1, 3}, {3, 2}, // 0 1 3 2
+ {1, 5}, {5, 7}, {7, 3}, // 1 5 7 3
+ {5, 4}, {4, 6}, {6, 7}, // 5 4 6 7
+ {4, 0}, {0, 2}, {2, 6}, // 4 0 2 6
+};
+
+void
+mkmat(void)
+{
+ s[0] = pushmat(nil);
+ s[1] = pushmat(s[0]);
+ s[2] = pushmat(s[1]);
+ s[3] = pushmat(s[2]);
+ rot(s[0], theta, 2);
+ look(s[1],
+ (Point3){5, 4, 3, 1},
+ (Point3){0, 0, 0, 1},
+ (Point3){0,0,1,1});
+ persp(s[2], 30, 10, 25);
+ viewport(s[3], screen->r, 1);
+}
+
+Point3
+tf(Point3 in, Space *s)
+{
+ if (s == nil) return in;
+ return xformpointd(tf(in, s->next), nil, s);
+}
+
+void
+drawscene(void)
+{
+ int i;
+ Point3 s3, e3;
+ for (i = 0; i < 12; i++) {
+ s3 = tf(cube[lines[i][0]], s[3]);
+ e3 = tf(cube[lines[i][1]], s[3]);
+ line(screen, Pt(s3.x, s3.y), Pt(e3.x, e3.y), 0, 0, 1, red, ZP);
+ }
+}
+
+void
+threadtimer(void *)
+{
+ for(;;) {
+ sleep(1000 / 60);
+ theta += 2.5;
+ if (theta >= 90) theta -= 90;
+ ident(s[0]->t);
+ rot(s[0], theta, 2);
+ draw(screen, screen->r, display->black, nil, ZP);
+ drawscene();
+ flushimage(display, 1);
+ }
+}
+
+void
+threadmain(int, char **)
+{
+ Mouse mv;
+ Rune kv;
+ int rv[2];
+
+ initdraw(nil, nil, "cubes");
+ Mousectl *mc = initmouse(nil, screen);
+ Keyboardctl *kc = initkeyboard(nil);
+
+ red = allocimage(display, Rect(0,0,1,1), CMAP8, 1, DRed);
+
+ mkmat();
+
+ proccreate(threadtimer, nil, 64 * 1024);
+
+ Alt alts[5] = {
+ {kc->c, &kv, CHANRCV},
+ {mc->c, &mv, CHANRCV},
+ {mc->resizec, rv, CHANRCV},
+ {0, 0, CHANEND},
+ };
+ for (;;) {
+ switch(alt(alts)){
+ case 0:
+ if (kv == 0x7f) threadexitsall(nil);
+ break;
+ case 1:
+ break;
+ case 2:
+ if(getwindow(display, Refnone) < 0)
+ sysfatal("resize failed: %r");
+ ident(s[3]->t);
+ viewport(s[3], screen->r, 1);
+ break;
+ }
+ }
+}
diff --git a/src/cubes/cubes.c b/src/cubes/cubes.c
@@ -0,0 +1,68 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <geometry.h>
+
+#define OTSize (64 * 64 * 64)
+#define CSSize (640 * 480)
+
+typedef struct Cube Cube;
+struct Cube {
+ Point3 min, Point3 max,
+};
+
+struct Vect {
+ Point3 pos, Point3 dir,
+};
+
+u32int otree[OTSize];
+u32int imgbuf[CSSize];
+
+Cube cube;
+Vect view;
+
+void
+otinit(void)
+{
+ int i, x,y,z;
+ double X,Y,Z;
+ for (i = 0; i < OTSize; i++)
+ for (y = 0; y < 64; y++)
+ for (z = 0; z < 64; z++) {
+ x = i % 64;
+ y = (i / 64) % 64;
+ z = (i / 64 / 64) % 64;
+ X = -1 + (double)x/32;
+ Y = -1 + (double)y/32;
+ Z = -1 + (double)z/32;
+ if ( (X*X*X + Y*Y*Y + Z*Z*Z) < 1) {
+ otree[i] = 0xffffffff;
+ } else {
+ otree[i] = 0;
+ }
+ }
+}
+
+void
+initscene(void)
+{
+ otinit();
+ cube = (Cube){
+ (Point3){-1, -1, -1, 1},
+ (Point3){ 1, 1, 1, 1},
+
+ };
+}
+
+void
+main(void)
+{
+ otinit();
+ render();
+ initdraw(nil, 0, "cubes");
+ Image *i = allocimage(display, Rect(0, 0, 640, 480), RGBA32, 0, DRed);
+ loadimage(i, i->r, (uchar *)imgbuf, CSSize * 4);
+ draw(screen, screen->r, i, nil, ZP);
+ flushimage(display, 1);
+ for(;;);
+}
diff --git a/src/cubes/mkfile b/src/cubes/mkfile
@@ -0,0 +1,6 @@
+</$objtype/mkfile
+
+TARG=cubes
+OFILES=cubes.$O
+
+</sys/src/cmd/mkone