textures
[nexus3d] / test.c
diff --git a/test.c b/test.c
index e38d66f..b74f6a5 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,43 @@ 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[] = {
+       -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, -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, -1, -1, 1, -1, 1, -1, -1, 1
+};
+static const float vcolors[] = {
+       1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
+       0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
+       0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
+       1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1,
+       1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
+       0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1
+};
+
+static const unsigned int idata[] = {
+       0, 1, 2, 0, 2, 3,
+       4, 5, 6, 4, 6, 7,
+       8, 9, 10, 8, 10, 11,
+       12, 13, 14, 12, 14, 15,
+       16, 17, 18, 16, 18, 19,
+       20, 21, 22, 20, 22, 23
+};
+
 
 static int quit;
+static nex_buffer *vbuf, *ibuf, *cbuf;
+static nex_geometry *geom;
+static nex_sdrprog *sdrprog;
+
+static float cam_theta, cam_phi = 0.5, cam_dist = 8;
+static float view_matrix[16], proj_matrix[16];
 
 int main(void)
 {
@@ -24,23 +61,76 @@ 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;
+       }
+
+       nex_enable(NEX_DEPTH_TEST);
+       nex_enable(NEX_CULL_FACE);
+       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)
 {
+       float mvp_matrix[16];
+
        nex_clear();
 
+       cgm_midentity(view_matrix);
+       cgm_mpretranslate(view_matrix, 0, 0, -cam_dist);
+       cgm_mprerotate(view_matrix, cam_phi, 1, 0, 0);
+       cgm_mprerotate(view_matrix, cam_theta, 0, 1, 0);
+
+       cgm_mcopy(mvp_matrix, view_matrix);
+       cgm_mmul(mvp_matrix, proj_matrix);
+       nex_uniform_mat4(sdrprog, 0, mvp_matrix);
+
+       nex_bind_sdrprog(sdrprog);
+       nex_draw_geometry(geom, NEX_TRIANGLES, 0);
+
        nex_swap_buffers();
 }
 
 static void reshape(int x, int y, void *cls)
 {
        nex_viewport(0, 0, x, y);
+
+       cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), (float)x / (float)y, 0.5, 500.0);
 }
 
 static void keyb(int key, int pressed, void *cls)
@@ -54,10 +144,49 @@ static void keyb(int key, int pressed, void *cls)
        }
 }
 
+static int prev_x, prev_y;
+static int bnstate[8];
+
 static void mbutton(int bn, int pressed, int x, int y, void *cls)
 {
+       if(bn < 8) bnstate[bn] = pressed;
+       prev_x = x;
+       prev_y = y;
+
+       if(pressed) {
+               if(bn == 3) {
+                       cam_dist -= 0.5f;
+                       nex_redisplay();
+               } else if(bn == 4) {
+                       cam_dist += 0.5f;
+                       nex_redisplay();
+               }
+       }
 }
 
 static void mmove(int x, int y, void *cls)
 {
+       int dx = x - prev_x;
+       int dy = y - prev_y;
+       prev_x = x;
+       prev_y = y;
+
+       if((dx | dy) == 0) return;
+
+       if(bnstate[0]) {
+               cam_theta += cgm_deg_to_rad(dx * 0.5f);
+               cam_phi += cgm_deg_to_rad(dy * 0.5f);
+
+               if(cam_phi < -CGM_HPI) cam_phi = -CGM_HPI;
+               if(cam_phi > CGM_HPI) cam_phi = CGM_HPI;
+
+               nex_redisplay();
+       }
+       if(bnstate[2]) {
+               cam_dist += dy * 0.1f;
+
+               if(cam_dist < 0) cam_dist = 0;
+
+               nex_redisplay();
+       }
 }