This repository was archived by the owner on Jun 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalphabet.html
526 lines (455 loc) · 16.2 KB
/
alphabet.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<!DOCTYPE html>
<html lang="en-GB" dir="ltr">
<head>
<meta charset="UTF-8">
<title>Alphabet Receiver</title>
<link rel="stylesheet" type="text/css" href="/css/receiver.css">
<script type="text/javascript" src="/js/transforms3d.js"></script>
<script type="text/javascript" src="/js/webgl-utils.js"></script>
<script type="text/javascript" src="/js/math2.js"></script>
<script type="text/javascript" src="/js/ws.js"></script>
<script type="text/javascript" src="/js/helpers.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D texture;
varying vec2 textureCoord;
void main(void) {
vec4 finalColor = texture2D(texture, textureCoord);
gl_FragColor = finalColor;
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
uniform mat4 projection;
uniform mat4 modelview;
varying vec2 textureCoord;
void main(void) {
textureCoord = aTextureCoord;
gl_Position = projection * modelview * vec4(aVertexPosition, 1.0);
}
</script>
<script type="text/javascript">
"use strict";
// HTMLCanvasElement
var canvas;
// WebGLRenderingContext
var gl;
// WebGLProgram
var program;
// WebGLBuffer
var vertexBuffer, indexBuffer, textureBuffer;
// WebGLTexture array
var textures;
// Mat4
var rotation, rotationAlphabet, translation;
// Camera
var camera;
// number, holds indices count of a block object with alphabet
var oneFaceIndicesCount;
// POSITION VARIABLES
// numbers, holding first received data after restart for computing relative change
var firstMarkerX = 0, firstMarkerY = 0;
// numbers, holding last sequence number for skipping delayed packets
var lastReceivedRotationSocketSequence = -1, lastReceivedMarkerSocketSequence = -1;
// boolean, if position data sequence should be restarted
var toBeRestarted = true;
// HTMLDivElement
var lost;
// variables for debugging
var debug = true, debugTranslation;
// ROTATION VARIABLES
// alphabet rotation
var rotationChange = 0;
// constants for rotation
const NO_ROTATION = 0, ROTATE_RIGHT = 1, ROTATE_LEFT = 2;
// current state of rotation
var nowRotating = NO_ROTATION;
// variables for stepping rotation
var startRotation = 0, currentRotation = 0, rotationStep = Math.PI * 2 / 4;
// basic translation unit because whole face takes 14 units on width
const BASE_TRANSL_UNIT = 7;
// ALPHABET VARIABLES
// alphabet map
var alphabet;
// char holding current letter
var currentLetter = "";
// current alphabet according to the texture that is currently being displayed
var currentAlphabet;
/**
* Set alphabet map for obtaining focused letters
*/
function initAlphabetMap() {
currentAlphabet = 0;
alphabet = [];
alphabet[0] = 'abcdefghijklmnopq rstuvwxyz←';
alphabet[1] = 'ABCDEFGHIJKLMNOPQ RSTUVWXYZ←';
alphabet[3] = '0123456789áčďéěíň óřšťúůýžü←';
alphabet[2] = '.,?!-+_/\\@&#():;% €$"\'|[]{}←';
//alphabet[0] = '0123456789ÁČĎÉĚÍŇ ÓŘŠŤÚŮÝŽÜ←';
}
/**
* Init scene and buffers
*/
function initBuffers() {
let scene = new Utils.Scene();
let or = new Mat3RotX(Math.PI / 2);
// alphabet face
scene.add(new Utils.Face(7, 4, 0, 0, 0, {strip: false, orientation: or}));
oneFaceIndicesCount = scene.indices.length;// get the count of indices one face has
//border face
scene.add(new Utils.Face(1, 1, 0, -0.0001, -1, {strip: false, orientation: or}));
vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(scene.vertices), gl.STATIC_DRAW);
vertexBuffer.itemSize = 3;
vertexBuffer.numItems = scene.vertices.length;
indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(scene.indices), gl.STATIC_DRAW);
indexBuffer.itemSize = 1;
indexBuffer.numItems = scene.indices.length;
textureBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(scene.textureCoords), gl.STATIC_DRAW);
textureBuffer.itemSize = 2;
textureBuffer.numItems = scene.textureCoords.length;
}
/**
* Main repaint function
*/
function repaint() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(program);
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.vertexAttribPointer(program.vertexPositionAttribute, vertexBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, textureBuffer);
gl.vertexAttribPointer(program.vertexTexCoordAttribute, textureBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
// draw basic small alphabet
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[0]);
gl.uniformMatrix4fv(program.modelview, false, Utils.convert(rotationAlphabet.mul(camera.view)));
gl.drawElements(gl.TRIANGLES, oneFaceIndicesCount, gl.UNSIGNED_SHORT, 0);
// draw basic big alphabet
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[1]);
gl.uniformMatrix4fv(program.modelview, false,
Utils.convert( new Mat4RotZ(-Math.PI / 2).mul(new Mat4Transl(-BASE_TRANSL_UNIT, BASE_TRANSL_UNIT, 0)).mul(rotationAlphabet).mul(camera.view))
);
gl.drawElements(gl.TRIANGLES, oneFaceIndicesCount, gl.UNSIGNED_SHORT, 0);
// draw basic special alphabet
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[2]);
gl.uniformMatrix4fv(program.modelview, false,
Utils.convert( new Mat4RotZ(Math.PI).mul(new Mat4Transl(0, 2*BASE_TRANSL_UNIT, 0)).mul(rotationAlphabet).mul(camera.view))
);
gl.drawElements(gl.TRIANGLES, oneFaceIndicesCount, gl.UNSIGNED_SHORT, 0);
// draw numbers alphabet
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[3]);
gl.uniformMatrix4fv(program.modelview, false,
Utils.convert( new Mat4RotZ(Math.PI / 2).mul(new Mat4Transl(BASE_TRANSL_UNIT, BASE_TRANSL_UNIT, 0)).mul(rotationAlphabet).mul(camera.view))
);
gl.drawElements(gl.TRIANGLES, oneFaceIndicesCount, gl.UNSIGNED_SHORT, 0);
// draw border
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[4]);
gl.uniformMatrix4fv(program.modelview, false, Utils.convert(translation.mul(camera.view)));
// second face from the start
gl.drawElements(gl.TRIANGLES, oneFaceIndicesCount, gl.UNSIGNED_SHORT, oneFaceIndicesCount * 2);
// draw debug border
if (debug) {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[5]);
gl.uniformMatrix4fv(program.modelview, false, Utils.convert(debugTranslation.mul(camera.view)));
// last face
gl.drawElements(gl.TRIANGLES, oneFaceIndicesCount, gl.UNSIGNED_SHORT, oneFaceIndicesCount * 2);
}
}
window.onload = function() {
lost = document.querySelector("#lost");
canvas = document.querySelector("canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight-5;
gl = Utils.initWebGL(canvas);
gl.clearColor(0.1, 0.1, 0.1, 1);
gl.disable(gl.DEPTH_TEST);
gl.enable(gl.BLEND);
gl.blendEquation(gl.FUNC_ADD);
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
gl.enable(gl.CULL_FACE);
gl.frontFace(gl.CCW);
gl.cullFace(gl.BACK);
program = gl.createProgram();
Utils.initShaders(gl, program, "shader-vs", "shader-fs");
gl.linkProgram(program);
gl.useProgram(program);
gl.viewport(0, 0, canvas.width, canvas.height);
program.vertexPositionAttribute = gl.getAttribLocation(program, "aVertexPosition");
gl.enableVertexAttribArray(program.vertexPositionAttribute);
program.vertexTexCoordAttribute = gl.getAttribLocation(program, "aTextureCoord");
gl.enableVertexAttribArray(program.vertexTexCoordAttribute);
program.projection = gl.getUniformLocation(program, "projection");
program.modelview = gl.getUniformLocation(program, "modelview");
program.texture = gl.getUniformLocation(program, "texture");
let projection = Utils.convert(new Mat4PerspRH(45, canvas.height / canvas.width, 0.1, 100.0));
gl.uniformMatrix4fv(program.projection, false, projection);
rotation = new Mat4RotY(-Math.PI/2);
rotationAlphabet = new Mat4Identity();
translation = new Mat4Identity();
debugTranslation = new Mat4Identity();
camera = new Camera();
camera.setAzimuth(Math2.toRadians(0));
camera.setZenith(Math2.toRadians(0));
camera.setPosition(new Vec3D(0, -10, 0));
initBuffers();
initAlphabetMap();
WS.setWebSocket(() => {}, onWsMessage, () => {});
Utils.initRequestAnimationFrame();
// load all textures
let images = ["img/alphabet-basic-small.png", "img/alphabet-basic-big.png", "img/alphabet-special.png", "img/alphabet-numerics-small.png", "img/border.png", "img/border-debug.png"];
Utils.loadTexture(gl, images, (imgs) => {
textures = [];
for (let i = 0; i < imgs.length; i++) {
textures[i] = imgs[i];
gl.bindTexture(gl.TEXTURE_2D, textures[i]);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textures[i].image);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, textures[i]);
}
// start when textures are loaded and set
animate();
});
};
/**
* Main animate function.
* Called using requestAnimFrame.
*/
function animate() {
manageRotation();
repaint();
requestAnimFrame(animate);
}
/**
* Check if any rotation is happening before every repaint call
*/
function manageRotation() {
if (nowRotating === NO_ROTATION) return;
if (nowRotating === ROTATE_RIGHT) {
currentRotation += 0.06;
if (currentRotation > startRotation + rotationStep) {
currentRotation = startRotation + rotationStep;
rotationChange = 0;
nowRotating = NO_ROTATION;
currentAlphabet++;
}
} else if (nowRotating === ROTATE_LEFT) {
currentRotation -= 0.06;
if (currentRotation < startRotation - rotationStep) {
currentRotation = startRotation - rotationStep;
rotationChange = 0;
nowRotating = NO_ROTATION;
currentAlphabet--;
}
}
// -1 means rotation to left going from 0 to right alphabet (special chars)
if (currentAlphabet < 0) currentAlphabet = 3;
// 4 means rotation to right going from 3 to left alphabet (basic small)
if (currentAlphabet > 3) currentAlphabet = 0;
rotationAlphabet = new Mat4Transl(0, -BASE_TRANSL_UNIT, 0).mul(new Mat4RotZ(currentRotation)).mul(new Mat4Transl(0, BASE_TRANSL_UNIT, 0));
}
/**
* Web sockets handler.
* Calls different method according to the type of received data
* @param {Array} data array of sensor data
*/
function onWsMessage(data) {
for (let i = 0; i < data.length; i++) {
if (data[i].type === "rotation") {
doRotation(data[i]);
} else if (data[i].type === "motion") {
doMotion(data[i]);
} else if (data[i].type === "image") {
receivedImage(data[i]);
} else if (data[i].type === "marker") {
doPosition(data[i]);
} else if (data[i].type === "touch") {
addText();
}
}
}
/**
* Rotation handler
* @param {object} data object with rotation data
*/
function doRotation(data) {
// start or restart
if (data.sequence <= 2) {
lastReceivedRotationSocketSequence = -1;
}
// skip all delayed data
if (data.sequence > lastReceivedRotationSocketSequence) {
lastReceivedRotationSocketSequence = data.sequence;
// do not do anything when rotation is being performed
if (nowRotating === NO_ROTATION) {
Helpers.checkRotation(data, function rotationToRight() {
if (rotationChange < 0) rotationChange = 0;
rotationChange++;
}, function rotationToLeft() {
if (rotationChange > 0) rotationChange = 0;
rotationChange--;
}, () => {});
// do rotation only if the right data come 3 times in a row (stored in rotationChange)
if (rotationChange > 3) {
nowRotating = ROTATE_RIGHT;
startRotation = currentRotation;
} else if (rotationChange < -3) {
nowRotating = ROTATE_LEFT;
startRotation = currentRotation;
}
}
} else {
//console.log("skipping rotation data " + data.sequence);
}
}
var blockMotion = false;
function doMotion(data) {
if (Math.abs(data.z) > 2.3) addText();
blockMotion = (Math.abs(data.x) > 1 || Math.abs(data.y) > 1 || Math.abs(data.z) > 1);
}
function receivedImage(data) {
let img = document.querySelector("img");
img.style.display = "none";
img.src = data.dataUrl;
console.log("Received image: ", data.dataUrl.substr(0, 50), "...");
}
/**
* Position handler
* @param {object} data object with position data
*/
function doPosition(data) {
if (blockMotion) {
console.log("blokujeme");
toBeRestarted = true;
}
// finish
if (data.sequence === -1) {
// hide lost message
lost.style.top = "-50px";
// prevent any late from showing
lastReceivedMarkerSocketSequence = Number.MAX_VALUE;
return;
}
// start or restart the sequence so we can skip delayed data
// wait for two data sets to be sure
// first has sequence 1
if (data.sequence <= 2) {
lastReceivedMarkerSocketSequence = -1;
toBeRestarted = true;
}
if (data.sequence > lastReceivedMarkerSocketSequence) {
lastReceivedMarkerSocketSequence = data.sequence;
if (data.count > 0) {
if (!toBeRestarted) {
translate(firstMarkerX - data.x, firstMarkerY - data.y);
if (debug) {
translateDebug(firstMarkerX - data.x, firstMarkerY - data.y, 50);
}
} else {
toBeRestarted = false;
firstMarkerX = data.x;
firstMarkerY = data.y;
}
lost.style.top = "-50px";
} else {
lost.style.top = "0px";
}
} else {
//console.log("skipping marker data " + data.sequence);
}
}
/**
* Apply translation for debug aiming square
* @param {number} x change from the beginning number over x axis
* @param {number} y change from the beginning number over y axis
* @param {number} divisor magic number to make x and y more reasonable (not really magic - one square is 100 units)
*/
function translateDebug(x, y, divisor) {
debugTranslation = new Mat4Transl(x/divisor, 0, -y/divisor);
}
/**
* Apply translation for aiming square
* The difference to the debug square is that this one is always aligned to exactly to a letter
* @param {number} x change from the beginning number over x axis
* @param {number} y change from the beginning number over y axis
*/
function translate(x, y) {
x = editNumber(x, -6, 6);
y = -editNumber(y, -4, 2);
setCurrentLetter(x, y);
translation = new Mat4Transl(x, 0, y);
}
/**
* Change number value to make it exactly aligned to a letter in the grid
* @param {number} num number to be edited
* @param {number} min minimum value for given number
* @param {number} max maximum value for given number
* @return {number} edited number
*/
function editNumber(num, min, max) {
// divide by 100 because one square is 100 units
num /= 100;
// round the number mathematically
num = Math.round(num);
// multiply by two because of the size of face
num *= 2;
// clamp number
num = Math2.clamp(num, min, max);
return num;
}
/**
* Takes edited numbers and finds letter which is being aimed on
* @param {number} x number on x axis
* @param {number} y number on y axis
*/
function setCurrentLetter(x, y) {
// divide by two because it has been multiplied for the size of the face
x /= 2;
y /= -2;
// x is in interval <-3;3>
// y is in interval <-2;1>
// narrow numbers to be in interval starting with 0
x += 3;
y += 2;
// 7 letters in a row
let seq = x + y * 7;
currentLetter = alphabet[currentAlphabet][seq];
}
/**
* Takes current alphabet and print currently aimed letter or character.
* Removes the last one if "←" is aimed on.
*/
function addText() {
if (currentLetter === '←') {
var currentText = document.querySelector("#text").innerHTML;
// check if there is anything to remove
if (currentText.length > 0) {
document.querySelector("#text").innerHTML = currentText.substr(0, currentText.length - 1);
}
} else {
document.querySelector("#text").innerHTML += currentLetter;
}
}
</script>
</head>
<body>
<div id="text"></div>
<img src="">
<canvas></canvas>
<div id="lost">Marker is lost.</div>
</body>
</html>