5 static void cleanup(void);
7 static void display(void *cls);
8 static void reshape(int x, int y, void *cls);
9 static void keyb(int key, int pressed, void *cls);
10 static void mbutton(int bn, int pressed, int x, int y, void *cls);
11 static void mmove(int x, int y, void *cls);
16 static const float vdata[] = {
17 -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1,
18 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1,
19 1, -1, -1, -1, -1, -1, -1, 1, -1, 1, 1, -1,
20 -1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
21 -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1,
22 -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1
24 static const float vcolors[] = {
25 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
26 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
27 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
28 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1,
29 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
30 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1
33 static const unsigned int idata[] = {
37 12, 13, 14, 12, 14, 15,
38 16, 17, 18, 16, 18, 19,
39 20, 21, 22, 20, 22, 23
44 static nex_buffer *vbuf, *ibuf, *cbuf;
45 static nex_geometry *geom;
46 static nex_sdrprog *sdrprog;
48 static float cam_theta, cam_phi = 0.5, cam_dist = 8;
49 static float view_matrix[16], proj_matrix[16];
53 nex_gfxapi_opengl(3, 3, NEX_OPENGL_DEBUG);
54 if(nex_initgfx(1280, 800, 0) == -1) {
58 nex_cbdisplay(display, 0);
59 nex_cbreshape(reshape, 0);
61 nex_cbmousebn(mbutton, 0);
62 nex_cbmousemove(mmove, 0);
68 while(nex_evloop_wait() && !quit);
78 nex_clearcolor(0.1, 0.12, 0.2);
80 vbuf = nex_alloc_buffer(sizeof vdata, vdata);
81 cbuf = nex_alloc_buffer(sizeof vcolors, vcolors);
82 ibuf = nex_alloc_buffer(sizeof idata, idata);
83 geom = nex_alloc_geometry();
84 nex_geom_vbuffer(geom, 0, vbuf, 3 * sizeof(float));
85 nex_geom_vbuffer(geom, 1, cbuf, 3 * sizeof(float));
86 nex_geom_vattr(geom, ATTR_POS, NEX_VEC3, 0, 0);
87 nex_geom_vattr(geom, ATTR_COL, NEX_COL3, 1, 0);
88 nex_geom_ibuffer(geom, ibuf);
90 if(!(sdrprog = nex_load_sdrprog("test.v.spv", "test.p.spv"))) {
94 nex_enable(NEX_DEPTH_TEST);
95 nex_enable(NEX_CULL_FACE);
99 static void cleanup(void)
101 nex_free_buffer(vbuf);
102 nex_free_buffer(cbuf);
103 nex_free_buffer(ibuf);
104 nex_free_geometry(geom);
105 nex_free_sdrprog(sdrprog);
108 static void display(void *cls)
110 float mvp_matrix[16];
114 cgm_midentity(view_matrix);
115 cgm_mpretranslate(view_matrix, 0, 0, -cam_dist);
116 cgm_mprerotate(view_matrix, cam_phi, 1, 0, 0);
117 cgm_mprerotate(view_matrix, cam_theta, 0, 1, 0);
119 cgm_mcopy(mvp_matrix, view_matrix);
120 cgm_mmul(mvp_matrix, proj_matrix);
121 nex_uniform_mat4(sdrprog, 0, mvp_matrix);
123 nex_bind_sdrprog(sdrprog);
124 nex_draw_geometry(geom, NEX_TRIANGLES, 0);
129 static void reshape(int x, int y, void *cls)
131 nex_viewport(0, 0, x, y);
133 cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), (float)x / (float)y, 0.5, 500.0);
136 static void keyb(int key, int pressed, void *cls)
147 static int prev_x, prev_y;
148 static int bnstate[8];
150 static void mbutton(int bn, int pressed, int x, int y, void *cls)
152 if(bn < 8) bnstate[bn] = pressed;
167 static void mmove(int x, int y, void *cls)
174 if((dx | dy) == 0) return;
177 cam_theta += cgm_deg_to_rad(dx * 0.5f);
178 cam_phi += cgm_deg_to_rad(dy * 0.5f);
180 if(cam_phi < -CGM_HPI) cam_phi = -CGM_HPI;
181 if(cam_phi > CGM_HPI) cam_phi = CGM_HPI;
186 cam_dist += dy * 0.1f;
188 if(cam_dist < 0) cam_dist = 0;