started writing some 3d pipeline stuff
authorJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 6 Sep 2016 04:03:44 +0000 (07:03 +0300)
committerJohn Tsiombikas <nuclear@mutantstargoat.com>
Tue, 6 Sep 2016 04:03:44 +0000 (07:03 +0300)
src/3dgfx.c [new file with mode: 0644]
src/3dgfx.h [new file with mode: 0644]
src/polyfill.c [new file with mode: 0644]
src/polyfill.h [new file with mode: 0644]
src/polytest.c [new file with mode: 0644]

diff --git a/src/3dgfx.c b/src/3dgfx.c
new file mode 100644 (file)
index 0000000..e7aabc2
--- /dev/null
@@ -0,0 +1,81 @@
+#include <string.h>
+#include "3dgfx.h"
+
+#define STACK_SIZE     8
+typedef float g3d_matrix[16];
+
+struct g3d_state {
+       unsigned int opt;
+
+       g3d_matrix mat[G3D_NUM_MATRICES][STACK_SIZE];
+       int mtop[G3D_NUM_MATRICES];
+
+       int width, height;
+       void *pixels;
+};
+
+static struct g3d_state st;
+
+void g3d_init(void)
+{
+       int i;
+
+       memset(&st, 0, sizeof st);
+
+       for(i=0; i<G3D_NUM_MATRICES; i++) {
+               g3d_set_matrix(i, 0);
+       }
+}
+
+void g3d_framebuffer(int width, int height, void *pixels)
+{
+       st.width = width;
+       st.height = height;
+       st.pixels = pixels;
+}
+
+void g3d_enable(unsigned int opt)
+{
+       st.opt |= opt;
+}
+
+void g3d_disable(unsigned int opt)
+{
+       st.opt &= ~opt;
+}
+
+void g3d_setopt(unsigned int opt, unsigned int mask)
+{
+       st.opt = (st.opt & ~mask) | (opt & mask);
+}
+
+unsigned int g3d_getopt(unsigned int mask)
+{
+       return st.opt & mask;
+}
+
+void g3d_set_matrix(int which, const float *m)
+{
+       int top = st.mtop[which];
+       memcpy(st.mat[which][top], m, 16 * sizeof(float));
+}
+
+#define M(i,j) (((i) << 2) + (j))
+void g3d_mult_matrix(int which, const float *m2)
+{
+       int i, j, top = st.mtop[which];
+       float m1[16];
+       float *dest = st.mat[which][top];
+
+       memcpy(m1, dest, sizeof m1);
+
+       for(i=0; i<4; i++) {
+               for(j=0; j<4; j++) {
+                       *dest++ = m1[M(0,j)] * m2[M(i,0)] +
+                               m1[M(1,j)] * m2[M(i,1)] +
+                               m1[M(2,j)] * m2[M(i,2)] +
+                               m1[M(3,j)] * m2[M(i,3)];
+               }
+       }
+}
+/* TODO continue ... */
diff --git a/src/3dgfx.h b/src/3dgfx.h
new file mode 100644 (file)
index 0000000..a6b3645
--- /dev/null
@@ -0,0 +1,70 @@
+#ifndef THREEDGFX_H_
+#define THREEDGFX_H_
+
+struct g3d_vertex {
+       float x, y, z, w;
+       float nx, ny, nz;
+       float u, v;
+       unsigned char r, g, b;
+};
+
+enum {
+       G3D_POINTS = 1,
+       G3D_LINES = 2,
+       G3D_TRIANGLES = 3,
+       G3D_QUADS = 4
+};
+
+/* g3d_enable/g3d_disable bits */
+enum {
+       G3D_CULL_FACE = 1,
+       G3D_DEPTH_TEST = 2,     /* XXX not implemented */
+       G3D_LIGHTING = 4,
+       G3D_TEXTURE = 8,
+
+       G3D_ALL = 0x7fffffff
+};
+
+/* 2nd arg to g3d_draw: which space are input verts in. skips parts of the pipeline */
+enum {
+       G3D_LOCAL_SPACE, /* this being 0 makes a nice default arg. */
+       G3D_WORLD_SPACE, /* ignore world matrix */
+       G3D_VIEW_SPACE,  /* ignore view matrix */
+       G3D_CLIP_SPACE,  /* ignore projection matrix */
+       G3D_SCREEN_SPACE,/* 2D verts, don't divide by w */
+       G3D_PIXEL_SPACE  /* in pixel units, ignore viewport */
+};
+
+/* matrix stacks */
+enum {
+       G3D_WORLD,
+       G3D_VIEW,
+       G3D_PROJECTION,
+
+       G3D_NUM_MATRICES
+};
+
+void g3d_init(void);
+
+void g3d_framebuffer(int width, int height, void *pixels);
+
+void g3d_enable(unsigned int opt);
+void g3d_disable(unsigned int opt);
+void g3d_setopt(unsigned int opt, unsigned int mask);
+unsigned int g3d_getopt(unsigned int mask);
+
+void g3d_set_matrix(int which, const float *m); /* null ptr for identity */
+void g3d_mult_matrix(int which, const float *m);
+void g3d_push_matrix(int which);
+void g3d_pop_matrix(int which);
+
+void g3d_translate(float x, float y, float z);
+void g3d_rotate(float angle, float x, float y, float z);
+void g3d_scale(float x, float y, float z);
+void g3d_ortho(float left, float right, float bottom, float top, float znear, float zfar);
+void g3d_frustum(float left, float right, float bottom, float top, float znear, float zfar);
+void g3d_perspective(float vfov, float aspect, float znear, float zfar);
+
+void g3d_draw(int prim, int space, const struct g3d_vertex *varr, int num);
+
+#endif /* THREEDGFX_H_ */
diff --git a/src/polyfill.c b/src/polyfill.c
new file mode 100644 (file)
index 0000000..d8facb5
--- /dev/null
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "polyfill.h"
+#include "gfxutil.h"
+#include "demo.h"
+
+void (*fillfunc[])(struct pvertex*, int) = {
+       polyfill_wire,
+       0, 0, 0, 0
+};
+
+void polyfill(int mode, struct pvertex *verts, int nverts)
+{
+#ifndef NDEBUG
+       if(!fillfunc[mode]) {
+               fprintf(stderr, "polyfill mode %d not implemented\n", mode);
+               abort();
+       }
+#endif
+
+       fillfunc[mode](verts, nverts);
+}
+
+void polyfill_wire(struct pvertex *verts, int nverts)
+{
+       int i;
+       struct pvertex *v = verts;
+       unsigned short color = ((v->r << 8) & 0xf800) |
+               ((v->g << 3) & 0x7e0) | ((v->b >> 3) & 0x1f);
+
+       for(i=0; i<nverts; i++) {
+               int x0, y0, x1, y1;
+               x0 = v->x >> 8;
+               y0 = v->y >> 8;
+               ++v;
+               x1 = v->x >> 8;
+               y1 = v->y >> 8;
+               clip_line(&x0, &y0, &x1, &y1, 0, 0, fb_width, fb_height);
+               draw_line(x0, y0, x1, y1, color);
+       }
+}
diff --git a/src/polyfill.h b/src/polyfill.h
new file mode 100644 (file)
index 0000000..57f0dc4
--- /dev/null
@@ -0,0 +1,24 @@
+#ifndef POLYFILL_H_
+#define POLYFILL_H_
+
+#include "inttypes.h"
+
+enum {
+       POLYFILL_WIRE,
+       POLYFILL_FLAT,
+       POLYFILL_GOURAUD,
+       POLYFILL_TEX,
+       POLYFILL_TEX_GOURAUD
+};
+
+/* projected vertices for the rasterizer */
+struct pvertex {
+       int32_t x, y; /* 24.8 fixed point */
+       int32_t u, v; /* 16.16 fixed point */
+       unsigned char r, g, b;
+};
+
+void polyfill(int mode, struct pvertex *verts, int nverts);
+void polyfill_wire(struct pvertex *verts, int nverts);
+
+#endif /* POLYFILL_H_ */
diff --git a/src/polytest.c b/src/polytest.c
new file mode 100644 (file)
index 0000000..fb8d9af
--- /dev/null
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "screen.h"
+
+static int init(void);
+static void destroy(void);
+static void draw(void);
+
+static struct screen scr = {
+       "polytest",
+       init,
+       destroy,
+       0, 0,
+       draw
+};
+
+struct screen *polytest_screen(void)
+{
+       return &scr;
+}
+
+static int init(void)
+{
+       return 0;
+}
+
+static void destroy(void)
+{
+}
+
+static void draw(void)
+{
+}