foo
[nexus3d] / test.c
1 #include <stdio.h>
2 #include "nexus3d.h"
3
4 static int init(void);
5 static void cleanup(void);
6
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);
12
13 #define ATTR_POS        0
14 #define ATTR_COL        1
15
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
23 };
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
31 };
32
33 static const unsigned int idata[] = {
34         0, 1, 2, 0, 2, 3,
35         4, 5, 6, 4, 6, 7,
36         8, 9, 10, 8, 10, 11,
37         12, 13, 14, 12, 14, 15,
38         16, 17, 18, 16, 18, 19,
39         20, 21, 22, 20, 22, 23
40 };
41
42
43 static int quit;
44 static nex_buffer *vbuf, *ibuf, *cbuf;
45 static nex_geometry *geom;
46 static nex_sdrprog *sdrprog;
47
48 static float cam_theta, cam_phi = 0.5, cam_dist = 8;
49 static float view_matrix[16], proj_matrix[16];
50
51 int main(void)
52 {
53         nex_gfxapi_opengl(3, 3, NEX_OPENGL_DEBUG);
54         if(nex_initgfx(1280, 800, 0) == -1) {
55                 return 1;
56         }
57
58         nex_cbdisplay(display, 0);
59         nex_cbreshape(reshape, 0);
60         nex_cbkey(keyb, 0);
61         nex_cbmousebn(mbutton, 0);
62         nex_cbmousemove(mmove, 0);
63
64         if(init() == -1) {
65                 goto end;
66         }
67
68         while(nex_evloop_wait() && !quit);
69
70 end:
71         cleanup();
72         nex_closegfx();
73         return 0;
74 }
75
76 static int init(void)
77 {
78         nex_clearcolor(0.1, 0.12, 0.2);
79
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);
89
90         if(!(sdrprog = nex_load_sdrprog("test.v.spv", "test.p.spv"))) {
91                 return -1;
92         }
93
94         nex_enable(NEX_DEPTH_TEST);
95         nex_enable(NEX_CULL_FACE);
96         return 0;
97 }
98
99 static void cleanup(void)
100 {
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);
106 }
107
108 static void display(void *cls)
109 {
110         float mvp_matrix[16];
111
112         nex_clear();
113
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);
118
119         cgm_mcopy(mvp_matrix, view_matrix);
120         cgm_mmul(mvp_matrix, proj_matrix);
121         nex_uniform_mat4(sdrprog, 0, mvp_matrix);
122
123         nex_bind_sdrprog(sdrprog);
124         nex_draw_geometry(geom, NEX_TRIANGLES, 0);
125
126         nex_swap_buffers();
127 }
128
129 static void reshape(int x, int y, void *cls)
130 {
131         nex_viewport(0, 0, x, y);
132
133         cgm_mperspective(proj_matrix, cgm_deg_to_rad(50), (float)x / (float)y, 0.5, 500.0);
134 }
135
136 static void keyb(int key, int pressed, void *cls)
137 {
138         if(!pressed) return;
139
140         switch(key) {
141         case 27:
142                 quit = 1;
143                 break;
144         }
145 }
146
147 static int prev_x, prev_y;
148 static int bnstate[8];
149
150 static void mbutton(int bn, int pressed, int x, int y, void *cls)
151 {
152         if(bn < 8) bnstate[bn] = pressed;
153         prev_x = x;
154         prev_y = y;
155
156         if(pressed) {
157                 if(bn == 3) {
158                         cam_dist -= 0.5f;
159                         nex_redisplay();
160                 } else if(bn == 4) {
161                         cam_dist += 0.5f;
162                         nex_redisplay();
163                 }
164         }
165 }
166
167 static void mmove(int x, int y, void *cls)
168 {
169         int dx = x - prev_x;
170         int dy = y - prev_y;
171         prev_x = x;
172         prev_y = y;
173
174         if((dx | dy) == 0) return;
175
176         if(bnstate[0]) {
177                 cam_theta += cgm_deg_to_rad(dx * 0.5f);
178                 cam_phi += cgm_deg_to_rad(dy * 0.5f);
179
180                 if(cam_phi < -CGM_HPI) cam_phi = -CGM_HPI;
181                 if(cam_phi > CGM_HPI) cam_phi = CGM_HPI;
182
183                 nex_redisplay();
184         }
185         if(bnstate[2]) {
186                 cam_dist += dy * 0.1f;
187
188                 if(cam_dist < 0) cam_dist = 0;
189
190                 nex_redisplay();
191         }
192 }