12 static void recalc_sball_matrix(float *xform, float *inv_xform);
14 static int console_active;
15 static int sball_update_pending;
18 static float rot[4] = {0, 0, 0, 1};
20 int demo_init(int argc, char **argv)
25 if(load_config("demo.cfg") == -1) {
28 if((env = getenv("START_SCR"))) {
31 if(parse_args(argc, argv) == -1) {
35 glEnable(GL_DEPTH_TEST);
36 glEnable(GL_CULL_FACE);
38 sball_matrix[0] = sball_matrix[5] = sball_matrix[10] = sball_matrix[15] = 1.0f;
39 sball_inv_matrix[0] = sball_inv_matrix[5] = sball_inv_matrix[10] = sball_inv_matrix[15] = 1.0f;
41 if(scr_init() == -1) {
45 scr = scr_lookup(opt.start_scr);
50 if(!scr || scr_change(scr, 4000) == -1) {
51 fprintf(stderr, "screen %s not found\n", opt.start_scr ? opt.start_scr : "0");
58 void demo_cleanup(void)
65 if(sball_update_pending) {
66 recalc_sball_matrix(sball_matrix, sball_inv_matrix);
67 sball_update_pending = 0;
74 void demo_reshape(int x, int y)
76 glViewport(0, 0, x, y);
78 glMatrixMode(GL_PROJECTION);
80 gluPerspective(50.0, win_aspect, 0.5, 500.0);
83 static void change_screen(int idx)
85 printf("change screen %d\n", idx);
86 scr_change(scr_screen(idx), 4000);
90 #define CBUF_MASK (CBUF_SIZE - 1)
91 void demo_keyboard(int key, int pressed)
93 static char cbuf[CBUF_SIZE];
95 char inp[CBUF_SIZE + 1], *dptr;
105 console_active = !console_active;
119 wr = (wr + CBUF_SIZE - 1) & CBUF_MASK;
131 rd = (rd + 1) & CBUF_MASK;
135 printf("\ntrying to match: %s\n", inp);
136 nscr = scr_num_screens();
137 for(i=0; i<nscr; i++) {
138 if(strstr(scr_screen(i)->name, inp)) {
150 if(key >= '1' && key <= '9' && key <= '1' + scr_num_screens()) {
151 change_screen(key - '1');
152 } else if(key == '0' && scr_num_screens() >= 10) {
157 if(key < 256 && isprint(key)) {
162 wr = (wr + 1) & CBUF_MASK;
163 if(wr == rd) { /* overflow */
164 rd = (rd + 1) & CBUF_MASK;
176 void demo_mbutton(int bn, int pressed, int x, int y)
180 void demo_mmotion(int x, int y)
185 static void quat_rotate(float *qres, const float *q, float angle, float x, float y, float z);
186 static void quat_mul(float *qres, const float *q1, const float *q2);
187 static void quat_to_mat(float *res, const float *q);
189 void demo_sball_motion(int x, int y, int z)
191 pos[0] += (float)x * 0.01;
192 pos[1] += (float)y * 0.01;
193 pos[2] -= (float)z * 0.01;
194 sball_update_pending = 1;
197 void demo_sball_rotate(int x, int y, int z)
202 float axis_len = sqrt(rx * rx + ry * ry + rz * rz);
204 quat_rotate(rot, rot, axis_len * 0.001, -rx / axis_len,
205 -ry / axis_len, rz / axis_len);
207 sball_update_pending = 1;
210 void demo_sball_button(int bn, int pressed)
216 pos[0] = pos[1] = pos[2] = 0;
217 rot[0] = rot[1] = rot[2] = 0;
226 static void recalc_sball_matrix(float *xform, float *inv_xform)
230 quat_to_mat(xform, rot);
235 inv_xform[0] = xform[0];
236 inv_xform[5] = xform[5];
237 inv_xform[10] = xform[10];
238 inv_xform[15] = 1.0f;
240 inv_xform[1] = xform[4];
241 inv_xform[2] = xform[8];
242 inv_xform[6] = xform[9];
243 inv_xform[4] = xform[1];
244 inv_xform[8] = xform[2];
245 inv_xform[9] = xform[6];
247 inv_xform[3] = inv_xform[7] = inv_xform[11] = 0.0f;
252 inv_xform[12] = tx * inv_xform[0] + ty * inv_xform[4] + tz * inv_xform[8];
253 inv_xform[13] = tx * inv_xform[1] + ty * inv_xform[5] + tz * inv_xform[9];
254 inv_xform[14] = tx * inv_xform[2] + ty * inv_xform[6] + tz * inv_xform[10];
257 static void quat_rotate(float *qres, const float *q, float angle, float x, float y, float z)
260 float half_angle = angle * 0.5f;
261 float sin_half = sin(half_angle);
263 rq[3] = cos(half_angle);
264 rq[0] = x * sin_half;
265 rq[1] = y * sin_half;
266 rq[2] = z * sin_half;
268 quat_mul(qres, q, rq);
271 static void quat_mul(float *qres, const float *q1, const float *q2)
273 float w = q1[3] * q2[3] - (q1[0] * q2[0] + q1[1] * q2[1] + q1[2] * q2[2]);
274 float x = q2[0] * q1[3] + q1[0] * q2[3] + (q1[1] * q2[2] - q1[2] * q2[1]);
275 float y = q2[1] * q1[3] + q1[1] * q2[3] + (q1[2] * q2[0] - q1[0] * q2[2]);
276 float z = q2[2] * q1[3] + q1[2] * q2[3] + (q1[0] * q2[1] - q1[1] * q2[0]);
283 static void quat_to_mat(float *res, const float *q)
285 res[0] = 1.0f - 2.0f * q[1]*q[1] - 2.0f * q[2]*q[2];
286 res[1] = 2.0f * q[0] * q[1] - 2.0f * q[3] * q[2];
287 res[2] = 2.0f * q[2] * q[0] + 2.0f * q[3] * q[1];
289 res[4] = 2.0f * q[0] * q[1] + 2.0f * q[3] * q[2];
290 res[5] = 1.0f - 2.0f * q[0]*q[0] - 2.0f * q[2]*q[2];
291 res[6] = 2.0f * q[1] * q[2] - 2.0f * q[3] * q[0];
293 res[8] = 2.0f * q[2] * q[0] - 2.0f * q[3] * q[1];
294 res[9] = 2.0f * q[1] * q[2] + 2.0f * q[3] * q[0];
295 res[10] = 1.0f - 2.0f * q[0]*q[0] - 2.0f * q[1]*q[1];
297 res[12] = res[13] = res[14] = 0.0f;