3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "../../include/keycodes.h"
26 #include "../libsdl/print.h"
28 #define GL3_PROTOTYPES 1
31 #define MAX_KEYBOARDS 2 /* maximum number of keyboards to we have */
32 #define MAX_KEYS 64 /* maximum number of keys per keyboard */
33 #define MAX_KEY_LINES 16 /* maximum number of lines on each key */
34 #define TILT_KEYBOARD /* keyboard is tilted up from horzontal x-z-plane to vertical x-y-plane */
37 * The keys are defined by coordinates that are described by the following
50 * The lines are defined from digit to digit to digit.... To start with a
51 * new line (not connected to the previous one, add ' ' (space).
55 static struct key_font {
59 { '0', "bdjtxvpfb qi" },
62 { '3', "fbdjntxvp ln" },
65 { '6', "jdbfpvxtnk" },
67 { '8', "lfbdjntxvpln" },
68 { '9', "jnlfbdjtxvp" },
75 { 'G', "jdbfpvxt yom" },
85 { 'Q', "bdjtxvpfb ys" },
87 { 'S', "jdbflntxvp" },
104 { 's', "" }, /* space */
105 { 'b', "beyvkb gs qi" },
106 { 'e', "dsp pl pv" },
112 * f-keys are 25 percent larger, so 4 f-keys are as wide as 5 regular keys.
113 * This is why f-keys use 5 steps instead of four (regular keys).
116 static const char *keyboard_layout[] = {
117 "1 2 3 4 5 6 7 8 9 0 G",
119 " f1 f2 f0 7 8 9 * ",
123 NULL, /* end of keyboard */
124 "1 2 3 4 5 6 7 8 9 0 b",
125 " Q W E R T Y U I O P ",
126 " A S D F G H J K L e ",
127 " Z X C V B N M , . ",
129 NULL, /* end of keyboard */
130 NULL, /* end of all keyboards */
133 static enum keycode keyboard_codes_1[] = {
134 KEYCODE_1, KEYCODE_2, KEYCODE_3, KEYCODE_4, KEYCODE_5, KEYCODE_6, KEYCODE_7, KEYCODE_8, KEYCODE_9, KEYCODE_0, KEYCODE_g,
135 KEYCODE_HELP, KEYCODE_RETURN,
136 KEYCODE_F1, KEYCODE_F2, KEYCODE_F10, KEYCODE_KP_7, KEYCODE_KP_8, KEYCODE_KP_9, KEYCODE_KP_MULTIPLY,
137 KEYCODE_KP_4, KEYCODE_KP_5, KEYCODE_KP_6, KEYCODE_KP_MINUS,
138 KEYCODE_UP, KEYCODE_KP_1, KEYCODE_KP_2, KEYCODE_KP_3, KEYCODE_KP_PLUS,
139 KEYCODE_LEFT, KEYCODE_DOWN, KEYCODE_RIGHT, KEYCODE_KP_0, KEYCODE_KP_ENTER,
142 static enum keycode keyboard_codes_2[] = {
143 KEYCODE_1, KEYCODE_2, KEYCODE_3, KEYCODE_4, KEYCODE_5, KEYCODE_6, KEYCODE_7, KEYCODE_8, KEYCODE_9, KEYCODE_0, KEYCODE_BACKSPACE,
144 KEYCODE_q, KEYCODE_w, KEYCODE_e, KEYCODE_r, KEYCODE_t, KEYCODE_y, KEYCODE_u, KEYCODE_i, KEYCODE_o, KEYCODE_p,
145 KEYCODE_a, KEYCODE_s, KEYCODE_d, KEYCODE_f, KEYCODE_g, KEYCODE_h, KEYCODE_j, KEYCODE_k, KEYCODE_l, KEYCODE_RETURN,
146 KEYCODE_z, KEYCODE_x, KEYCODE_c, KEYCODE_v, KEYCODE_b, KEYCODE_n, KEYCODE_m, KEYCODE_COMMA, KEYCODE_PERIOD,
150 static enum keycode *keyboard_codes[] = {
151 keyboard_codes_1, keyboard_codes_2,
154 /* definition of keys with coordinates for rendering */
159 double left_top_x, right_top_x;
161 double front_top_z, back_top_z;
162 double left_bottom_x, right_bottom_x;
164 double front_bottom_z, back_bottom_z;
165 double x1[MAX_KEY_LINES], y1[MAX_KEY_LINES], z1[MAX_KEY_LINES];
166 double x2[MAX_KEY_LINES], y2[MAX_KEY_LINES], z2[MAX_KEY_LINES];
170 /* definition for each keyboard */
171 static struct keyboard_def {
172 struct key_def key[MAX_KEYS];
174 } keyboard_def[MAX_KEYBOARDS];
176 static int keyboards = 0;
179 * add key to keyboard_def[].key structure
182 * +-------------------+ 4
183 * | +---------------+ | 3
184 * | | a b c d e | | 2
185 * | | f g h i j | | 1
186 * | | k l m n o | | 0
187 * | | p q r s t | | -1
188 * | | u v w x y | | -2
189 * | +---------------+ | -3
190 * +-------------------+ -4
194 static int add_key(char key, int fkey, double width, struct key_def *key_def)
197 double font_scale = 1.0, font_offset = 0.0;
199 memset(key_def, 0, sizeof(*key_def));
202 key_def->fkey = fkey;
204 /* key case (see layout in the description above) */
205 key_def->left_top_x = -3.0;
206 key_def->right_top_x = 3.0 + (width - 1.0) * 9.0;
208 key_def->front_top_z = 3.0;
209 key_def->back_top_z = -3.0;
210 key_def->left_bottom_x = -4.0;
211 key_def->right_bottom_x = 4.0 + (width - 1.0) * 9.0;
212 key_def->bottom_y = -4.0;
213 key_def->front_bottom_z = 4.0;
214 key_def->back_bottom_z = -4.0;
216 /* lines to be drawn ontop of the key (see layout in the description above) */
218 /* up to 4 letters/digits are displayed on key */
219 for (d = 0; d < 4; d++) {
220 if (!key_def->fkey) {
221 /* render regular key */
231 if (key_def->key == 'h')
238 if (key_def->key == 'h')
240 else if (key_def->key != '0')
247 if (key_def->key == 'h')
249 else if (key_def->key != '0')
256 if (key_def->key == 'h')
262 /* search for key font */
263 for (f = 0; key_font[f].key; f++) {
264 if (key_font[f].key == key)
267 if (!key_font[f].key) {
268 print_error("Failed to find keyboard character '%c' in key_font, please fix\n", key);
273 while (key_font[f].font[i] && key_font[f].font[i + 1]) {
274 /* skip spaces, but this should not happen */
275 if (key_font[f].font[i] == ' ') {
279 /* the second character is space, so we start over with a new line sequence */
280 if (key_font[f].font[i + 1] == ' ') {
285 if (l == MAX_KEY_LINES) {
286 print_error("Failed to add key definition, maximum number of lines exeeded, please fix\n");
289 key_def->x1[l] = (double)(((key_font[f].font[i] - 'a') % 5) - 2) * font_scale + font_offset;
291 key_def->z1[l] = (double)((key_font[f].font[i] - 'a') / 5) - 2;
292 key_def->x2[l] = (double)(((key_font[f].font[i + 1] - 'a') % 5) - 2) * font_scale + font_offset;
294 key_def->z2[l] = (double)((key_font[f].font[i + 1] - 'a') / 5) - 2;
304 static void scale_key(double px, double py, double pz, double scale, struct key_def *key_def)
308 /* scale and translate key case */
309 key_def->left_top_x = key_def->left_top_x * scale + px;
310 key_def->right_top_x = key_def->right_top_x * scale + px;
311 key_def->top_y = key_def->top_y * scale + py;
312 key_def->front_top_z = key_def->front_top_z * scale + pz;
313 key_def->back_top_z = key_def->back_top_z * scale + pz;
314 key_def->left_bottom_x = key_def->left_bottom_x * scale + px;
315 key_def->right_bottom_x = key_def->right_bottom_x * scale + px;
316 key_def->bottom_y = key_def->bottom_y * scale + py;
317 key_def->front_bottom_z = key_def->front_bottom_z * scale + pz;
318 key_def->back_bottom_z = key_def->back_bottom_z * scale + pz;
320 /* scale and translate line ontop of keys */
321 for (l = 0; l < key_def->lines; l++) {
322 key_def->x1[l] = key_def->x1[l] * scale + px;
323 key_def->y1[l] = key_def->y1[l] * scale + py;
324 key_def->z1[l] = key_def->z1[l] * scale + pz;
325 key_def->x2[l] = key_def->x2[l] * scale + px;
326 key_def->y2[l] = key_def->y2[l] * scale + py;
327 key_def->z2[l] = key_def->z2[l] * scale + pz;
331 #define KEY_SIZE_INCH 3.0
333 int init_vr_keyboard(double keyboard_height, double keyboard_distance)
341 /* count all keyboards */
342 while (keyboard_layout[l]) {
344 /* for each row in keyboard */
345 for (y = 0; keyboard_layout[l]; y++, l++) {
346 /* for each key in row */
347 for (x = 0; keyboard_layout[l][x]; x++) {
348 /* skip spaces, because this is no key */
349 if (keyboard_layout[l][x] == ' ')
352 key = keyboard_layout[l][x];
354 /* if we have an F-key, this means size * 1.5 */
355 if (keyboard_layout[l][x] == 'f') {
356 if (keyboard_layout[l][x + 1] == 'h')
357 width = 2.0; /* help key */
359 width = 1.5; /* f-key */
360 key = keyboard_layout[l][x + 1];
363 /* if we have a digit after the key, this means different size */
364 if (keyboard_layout[l][x + 1] > '0')
365 width = (double)(keyboard_layout[l][x + 1] - '0');
366 rc = add_key(key, fkey, width, &keyboard_def[keyboards].key[k]);
370 scale_key(((double)x / 4.0 - 5.0) * KEY_SIZE_INCH, -keyboard_distance, (double)y * KEY_SIZE_INCH - keyboard_height, KEY_SIZE_INCH / 9, &keyboard_def[keyboards].key[k]);
372 scale_key(((double)x / 4.0 - 5.0) * KEY_SIZE_INCH, keyboard_height, (double)y * KEY_SIZE_INCH - keyboard_distance, KEY_SIZE_INCH / 9, &keyboard_def[keyboards].key[k]);
375 /* skip size information, if any */
376 if (keyboard_layout[l][x + 1] > ' ')
380 keyboard_def[keyboards].keys = k;
388 static int highlight_k;
389 static double from_x, from_y, from_z;
390 static double to_x, to_y, to_z;
392 /* calculate pointer and to what key it points to */
393 void handle_vr_keyboard(int keyboard, double pointer_x, double pointer_y, double pointer_z, double pointer_yaw, double pointer_pitch, enum keycode *key)
396 struct key_def *key_def;
398 double keyboard_dist = keyboard_def[keyboard].key[0].y1[0]; /* just get the dist from first line of first key */
400 double keyboard_height = keyboard_def[keyboard].key[0].y1[0]; /* just get the height from first line of first key */
403 /* generate poiting vector */
407 to_x = from_x - sin(pointer_yaw) * cos(pointer_pitch) * 20;
408 to_y = from_y + sin(pointer_pitch) * 20;
409 to_z = from_z - cos(pointer_yaw) * cos(pointer_pitch) * 20;
411 /* if pointer points down, intersect with keyboard */
414 if (from_z - 1.0 > keyboard_dist && to_z < from_z - 1.0) {
415 to_x = (to_x - from_x) / (from_z - to_z) * (from_z - keyboard_dist) + from_x;
416 to_y = (to_y - from_y) / (from_z - to_z) * (from_z - keyboard_dist) + from_y;
417 to_z = keyboard_dist;
419 /* find key that we intersect with */
420 for (k = 0; k < keyboard_def[keyboard].keys; k++) {
421 key_def = &keyboard_def[keyboard].key[k];
422 if (to_x < key_def->left_top_x)
424 if (to_x > key_def->right_top_x)
426 if (-to_y < key_def->back_top_z)
428 if (-to_y > key_def->front_top_z)
431 *key = keyboard_codes[keyboard][k];
436 if (from_y - 1.0 > keyboard_height && to_y < from_y - 1.0) {
437 to_x = (to_x - from_x) / (from_y - to_y) * (from_y - keyboard_height) + from_x;
438 to_z = (to_z - from_z) / (from_y - to_y) * (from_y - keyboard_height) + from_z;
439 to_y = keyboard_height;
441 /* find key that we intersect with */
442 for (k = 0; k < keyboard_def[keyboard].keys; k++) {
443 key_def = &keyboard_def[keyboard].key[k];
444 if (to_x < key_def->left_top_x)
446 if (to_x > key_def->right_top_x)
448 if (to_z < key_def->back_top_z)
450 if (to_z > key_def->front_top_z)
453 *key = keyboard_codes[keyboard][k];
461 #define FLIP_YZ(y, z) -z, y
463 #define FLIP_YZ(y, z) y, z
466 /* render keyboard per eye */
467 void render_vr_keyboard(int keyboard, double camera_x, double camera_y)
470 int rendered[MAX_KEYS];
471 double dists[MAX_KEYS], dist_x, dist_y, dist;
472 struct key_def *key_def;
474 glEnable(GL_CULL_FACE);
476 glCullFace(GL_FRONT);
478 memset(rendered, 0, sizeof(rendered));
480 /* calculate distances to keys */
481 for (k = 0; k < keyboard_def[keyboard].keys; k++) {
482 key_def = &keyboard_def[keyboard].key[k];
484 dist_x = (key_def->left_top_x + key_def->right_top_x) / 2.0 - camera_x;
485 dist_y = (key_def->front_top_z + key_def->back_top_z) / 2.0 + camera_y;
487 dist_x = (key_def->left_top_x + key_def->right_top_x) / 2.0 - camera_x;
488 dist_y = (key_def->front_top_z + key_def->back_top_z) / 2.0;
490 dists[k] = sqrt(dist_x * dist_x + dist_y * dist_y);
495 /* search for most distant key */
496 for (k = 0; k < keyboard_def[keyboard].keys; k++) {
497 /* skip keys that have been already rendered */
500 if (!key_def || dists[k] > dist) {
502 key_def = &keyboard_def[keyboard].key[k];
506 /* no unrendered key, we are done */
509 rendered[distant_k] = 1;
511 /* render case of key */
514 if (distant_k == highlight_k)
515 glColor4f(0.0, 0.7, 0.7, 1.0);
517 glColor4f(0.5, 0.5, 0.5, 1.0);
518 glVertex3f(key_def->left_top_x, FLIP_YZ(key_def->top_y, key_def->back_top_z));
519 glVertex3f(key_def->right_top_x, FLIP_YZ(key_def->top_y, key_def->back_top_z));
520 glVertex3f(key_def->right_top_x, FLIP_YZ(key_def->top_y, key_def->front_top_z));
521 glVertex3f(key_def->left_top_x, FLIP_YZ(key_def->top_y, key_def->front_top_z));
522 /* front plane (in front of player) */
523 glColor4f(0.1, 0.1, 0.1, 1.0);
524 glVertex3f(key_def->left_top_x, FLIP_YZ(key_def->top_y, key_def->front_top_z));
525 glVertex3f(key_def->right_top_x, FLIP_YZ(key_def->top_y, key_def->front_top_z));
526 glVertex3f(key_def->right_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->front_bottom_z));
527 glVertex3f(key_def->left_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->front_bottom_z));
528 /* back plane (away from player) */
529 glColor4f(0.3, 0.3, 0.3, 1.0);
530 glVertex3f(key_def->right_top_x, FLIP_YZ(key_def->top_y, key_def->back_top_z));
531 glVertex3f(key_def->left_top_x, FLIP_YZ(key_def->top_y, key_def->back_top_z));
532 glVertex3f(key_def->left_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->back_bottom_z));
533 glVertex3f(key_def->right_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->back_bottom_z));
535 glColor4f(0.2, 0.2, 0.2, 1.0);
536 glVertex3f(key_def->left_top_x, FLIP_YZ(key_def->top_y, key_def->back_top_z));
537 glVertex3f(key_def->left_top_x, FLIP_YZ(key_def->top_y, key_def->front_top_z));
538 glVertex3f(key_def->left_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->front_bottom_z));
539 glVertex3f(key_def->left_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->back_bottom_z));
541 glVertex3f(key_def->right_top_x, FLIP_YZ(key_def->top_y, key_def->front_top_z));
542 glVertex3f(key_def->right_top_x, FLIP_YZ(key_def->top_y, key_def->back_top_z));
543 glVertex3f(key_def->right_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->back_bottom_z));
544 glVertex3f(key_def->right_bottom_x, FLIP_YZ(key_def->bottom_y, key_def->front_bottom_z));
545 /* no bottom plane, because it is unlikely visible */
548 /* render lines of key */
549 glColor4f(1.0, 1.0, 1.0, 1.0);
550 for (l = 0; l < key_def->lines; l++) {
551 if (key_def->x1[l] == key_def->x2[l] && key_def->z1[l] == key_def->z2[l]) {
554 glVertex3f(key_def->x1[l], FLIP_YZ(key_def->y1[l], key_def->z1[l]));
558 glVertex3f(key_def->x1[l], FLIP_YZ(key_def->y1[l], key_def->z1[l]));
559 glVertex3f(key_def->x2[l], FLIP_YZ(key_def->y2[l], key_def->z2[l]));
565 glDisable(GL_CULL_FACE);
568 glColor4f(1.0, 0.0, 0.0, 1.0);
570 glVertex3f(from_x, from_y, from_z);
571 glVertex3f(to_x, to_y, to_z);