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
20 static const float vcolors[] = {
21 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0,
22 0, 1, 1, 1, 0, 1, 1, 0.5, 0, 0.5, 0.5, 0.5
25 static const unsigned int idata[] = {
36 static nex_buffer *vbuf, *ibuf, *cbuf;
37 static nex_geometry *geom;
38 static nex_sdrprog *sdrprog;
40 static float cam_theta, cam_phi = 0.5, cam_dist = 8;
41 static float view_matrix[16], proj_matrix[16];
45 nex_gfxapi_opengl(3, 3, NEX_OPENGL_DEBUG);
46 if(nex_initgfx(1280, 800, 0) == -1) {
50 nex_cbdisplay(display, 0);
51 nex_cbreshape(reshape, 0);
53 nex_cbmousebn(mbutton, 0);
54 nex_cbmousemove(mmove, 0);
60 while(nex_evloop_wait() && !quit);
70 nex_clearcolor(0.1, 0.12, 0.2);
72 vbuf = nex_alloc_buffer(sizeof vdata, vdata);
73 cbuf = nex_alloc_buffer(sizeof vcolors, vcolors);
74 ibuf = nex_alloc_buffer(sizeof idata, idata);
75 geom = nex_alloc_geometry();
76 nex_geom_vbuffer(geom, 0, vbuf, 3 * sizeof(float));
77 nex_geom_vbuffer(geom, 1, cbuf, 3 * sizeof(float));
78 nex_geom_vattr(geom, ATTR_POS, NEX_VEC3, 0, 0);
79 nex_geom_vattr(geom, ATTR_COL, NEX_COL3, 1, 0);
80 nex_geom_ibuffer(geom, ibuf);
82 if(!(sdrprog = nex_load_sdrprog("test.v.spv", "test.p.spv"))) {
86 nex_enable(NEX_DEPTH_TEST);
87 nex_enable(NEX_CULL_FACE);
91 static void cleanup(void)
93 nex_free_buffer(vbuf);
94 nex_free_buffer(cbuf);
95 nex_free_buffer(ibuf);
96 nex_free_geometry(geom);
97 nex_free_sdrprog(sdrprog);
100 static void display(void *cls)
102 float mvp_matrix[16];
106 cgm_midentity(view_matrix);
107 cgm_mpretranslate(view_matrix, 0, 0, -cam_dist);
108 cgm_mprerotate(view_matrix, cam_phi, 1, 0, 0);
109 cgm_mprerotate(view_matrix, cam_theta, 0, 1, 0);
111 cgm_mcopy(mvp_matrix, view_matrix);
112 cgm_mmul(mvp_matrix, proj_matrix);
113 nex_uniform_mat4(sdrprog, 0, mvp_matrix);
115 nex_bind_sdrprog(sdrprog);
116 nex_draw_geometry(geom, NEX_TRIANGLES, 0);
121 static void reshape(int x, int y, void *cls)
123 nex_viewport(0, 0, x, y);
125 cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), (float)x / (float)y, 0.5, 500.0);
128 static void keyb(int key, int pressed, void *cls)
139 static int prev_x, prev_y;
140 static int bnstate[8];
142 static void mbutton(int bn, int pressed, int x, int y, void *cls)
144 if(bn < 8) bnstate[bn] = pressed;
159 static void mmove(int x, int y, void *cls)
166 if((dx | dy) == 0) return;
169 cam_theta += cgm_deg_to_rad(dx * 0.5f);
170 cam_phi += cgm_deg_to_rad(dy * 0.5f);
172 if(cam_phi < -CGM_HPI) cam_phi = -CGM_HPI;
173 if(cam_phi > CGM_HPI) cam_phi = CGM_HPI;
178 cam_dist += dy * 0.1f;
180 if(cam_dist < 0) cam_dist = 0;