evolving the basic gfx abstractions
[nexus3d] / test.c
diff --git a/test.c b/test.c
index e38d66f..e3aa026 100644 (file)
--- a/test.c
+++ b/test.c
@@ -1,6 +1,8 @@
 #include <stdio.h>
 #include "nexus3d.h"
 
+static int init(void);
+static void cleanup(void);
 
 static void display(void *cls);
 static void reshape(int x, int y, void *cls);
@@ -8,8 +10,32 @@ static void keyb(int key, int pressed, void *cls);
 static void mbutton(int bn, int pressed, int x, int y, void *cls);
 static void mmove(int x, int y, void *cls);
 
+#define ATTR_POS       0
+#define ATTR_COL       1
+
+static const float vdata[] = {
+       -0.25f, -0.25f, 0.25f, 0.25f, -0.25f, 0.25f, 0.25f, -0.25f, -0.25f, -0.25f, -0.25f, -0.25f,
+       -0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, 0.25f, -0.25f, -0.25f, 0.25f, -0.25f
+};
+static const float vcolors[] = {
+       1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0,
+       0, 1, 1, 1, 0, 1, 1, 0.5, 0, 0.5, 0.5, 0.5
+};
+
+static const unsigned int idata[] = {
+       0, 1, 5,        0, 5, 4,
+       1, 2, 6,        1, 6, 5,
+       2, 3, 7,        2, 7, 6,
+       3, 0, 4,        3, 4, 7,
+       4, 5, 6,        4, 6, 7,
+       1, 0, 3,        1, 3, 2
+};
+
 
 static int quit;
+static nex_buffer *vbuf, *ibuf, *cbuf;
+static nex_geometry *geom;
+static nex_sdrprog *sdrprog;
 
 int main(void)
 {
@@ -24,17 +50,54 @@ int main(void)
        nex_cbmousebn(mbutton, 0);
        nex_cbmousemove(mmove, 0);
 
+       if(init() == -1) {
+               goto end;
+       }
+
        while(nex_evloop_wait() && !quit);
 
+end:
+       cleanup();
        nex_closegfx();
        return 0;
 }
 
+static int init(void)
+{
+       nex_clearcolor(0.1, 0.12, 0.2);
+
+       vbuf = nex_alloc_buffer(sizeof vdata, vdata);
+       cbuf = nex_alloc_buffer(sizeof vcolors, vcolors);
+       ibuf = nex_alloc_buffer(sizeof idata, idata);
+       geom = nex_alloc_geometry();
+       nex_geom_vbuffer(geom, 0, vbuf, 3 * sizeof(float));
+       nex_geom_vbuffer(geom, 1, cbuf, 3 * sizeof(float));
+       nex_geom_vattr(geom, ATTR_POS, NEX_VEC3, 0, 0);
+       nex_geom_vattr(geom, ATTR_COL, NEX_COL3, 1, 0);
+       nex_geom_ibuffer(geom, ibuf);
+
+       if(!(sdrprog = nex_load_sdrprog("test.v.spv", "test.p.spv"))) {
+               return -1;
+       }
+       return 0;
+}
+
+static void cleanup(void)
+{
+       nex_free_buffer(vbuf);
+       nex_free_buffer(cbuf);
+       nex_free_buffer(ibuf);
+       nex_free_geometry(geom);
+       nex_free_sdrprog(sdrprog);
+}
 
 static void display(void *cls)
 {
        nex_clear();
 
+       nex_bind_sdrprog(sdrprog);
+       nex_draw_geometry(geom, NEX_TRIANGLES, 0);
+
        nex_swap_buffers();
 }