forked from Oli97/jjko-flight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcockpit.html
603 lines (506 loc) · 18.2 KB
/
cockpit.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<html>
<head>
<title>Cockpit — Studienprojekt</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="glMatrix-0.9.5.min.js"></script>
<script type="text/javascript" src="webgl-utils.js"></script>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vColor = aVertexColor;
}
</script>
<script type="text/javascript">
var gl;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
var mvMatrix = mat4.create();
var mvMatrixStack = [];
var pMatrix = mat4.create();
function mvPushMatrix() {
var copy = mat4.create();
mat4.set(mvMatrix, copy);
mvMatrixStack.push(copy);
}
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
var square2VertexPositionBuffer;
var square2VertexColorBuffer;
var square1VertexPositionBuffer;
var square1VertexColorBuffer;
var fensterlVertexPositionBuffer;
var fensterlVertexColorBuffer;
var fensterrVertexPositionBuffer;
var fensterrVertexColorBuffer;
var xRot = 0;
var xSpeed = 0;
var yRot = 0;
var ySpeed = 0;
var z = -5.0;
var filter = 0;
var currentlyPressedKeys = {};
function handleKeyDown(event) {
currentlyPressedKeys[event.keyCode] = true;
}
function handleKeyUp(event) {
currentlyPressedKeys[event.keyCode] = false;
}
function handleKeys() {
if (currentlyPressedKeys[65]) {
// A
z -= 1;
}
if (currentlyPressedKeys[83]) {
// S
z += 1;
}
if (currentlyPressedKeys[37]) {
// Left cursor key
xSpeed -= 0.01;
}
if (currentlyPressedKeys[39]) {
// Right cursor key
xSpeed += 0.01;
}
if (currentlyPressedKeys[38]) {
// Up cursor key
ySpeed += 0.01;
}
if (currentlyPressedKeys[40]) {
// Down cursor key
ySpeed -= 0.01;
}
}
var cubeVertexPositionBuffer;
var cubeVertexColorBuffer;
function initBuffers() {
cubeVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
vertices = [
// Front face
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
// Back face
-1.0, -1.0, -1.0,
-1.0, 1.0, -1.0,
1.0, 1.0, -1.0,
1.0, -1.0, -1.0,
// Top face
-1.0, 1.0, -1.0,
-1.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, -1.0,
// Bottom face
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, -1.0, 1.0,
-1.0, -1.0, 1.0,
// Right face
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
1.0, 1.0, 1.0,
1.0, -1.0, 1.0,
// Left face
-1.0, -1.0, -1.0,
-1.0, -1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, 1.0, -1.0,
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
cubeVertexPositionBuffer.itemSize = 3;
cubeVertexPositionBuffer.numItems = 24;
cubeVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
var colors = [];
for (var i=0; i < 24; i++) {
colors = colors.concat([1.0, 0.0, 0.0, 0.0]);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
cubeVertexColorBuffer.itemSize = 4;
cubeVertexColorBuffer.numItems = 24;
square1VertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, square1VertexPositionBuffer);
var vertices = [
5.0, 1.5, 0.0,
-5.0, 1.5, 0.0,
5.0, -1.0, 0.0,
-5.0, -1.0, 0.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
square1VertexPositionBuffer.itemSize = 3;
square1VertexPositionBuffer.numItems = 4;
square1VertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, square1VertexColorBuffer);
var colors = [];
for (var i=0; i < 4; i++) {
colors = colors.concat([0.0, 0.0, 0.0, 0.6]);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
square1VertexColorBuffer.itemSize = 4;
square1VertexColorBuffer.numItems = 4;
square2VertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, square2VertexPositionBuffer);
var vertices = [
2.7, 0.15, 0.0,
-2.7, 0.15, 0.0,
3.2, -0.5, 0.0,
-3.2, -0.5, 0.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
square2VertexPositionBuffer.itemSize = 3;
square2VertexPositionBuffer.numItems = 4;
square2VertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, square2VertexColorBuffer);
var colors = [];
for (var i=0; i < 4; i++) {
colors = colors.concat([0.0, 0.0, 0.0, 0.5]);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
square2VertexColorBuffer.itemSize = 4;
square2VertexColorBuffer.numItems = 4;
fensterlVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, fensterlVertexPositionBuffer);
vertices = [
1.0, 0.5, 0.0,
-3.0, 0.5, 0.0,
1.0, -2.7, 0.0,
-3.0, -2.7, 0.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
fensterlVertexPositionBuffer.itemSize = 3;
fensterlVertexPositionBuffer.numItems = 4;
fensterlVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, fensterlVertexColorBuffer);
colors = [];
for (var i=0; i < 4; i++) {
colors = colors.concat([0.0, 0.2, 1.0, 0.5]);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
fensterlVertexColorBuffer.itemSize = 4;
fensterlVertexColorBuffer.numItems = 4;
fensterrVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, fensterrVertexPositionBuffer);
vertices = [
3.0, 0.5, 0.0,
-1.0, 0.5, 0.0,
3.0, -2.7, 0.0,
-1.0, -2.7, 0.0
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
fensterrVertexPositionBuffer.itemSize = 3;
fensterrVertexPositionBuffer.numItems = 4;
fensterrVertexColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, fensterrVertexColorBuffer);
colors = [];
for (var i=0; i < 4; i++) {
colors = colors.concat([0.0, 0.2, 1.0, 0.5]);
}
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
fensterrVertexColorBuffer.itemSize = 4;
fensterrVertexColorBuffer.numItems = 4;
}
var rSquare = 15;
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
mat4.identity(mvMatrix);
mat4.translate(mvMatrix, [0.0, -2.5, -7.0]);
gl.bindBuffer(gl.ARRAY_BUFFER, square1VertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, square1VertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, square1VertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, square1VertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, square1VertexPositionBuffer.numItems);
mat4.translate(mvMatrix, [0.0, 2.0, 0.0]);
gl.bindBuffer(gl.ARRAY_BUFFER, square2VertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, square2VertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, square2VertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, square2VertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, square2VertexPositionBuffer.numItems);
mat4.translate(mvMatrix, [xSpeed, ySpeed, z]);
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, cubeVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, cubeVertexPositionBuffer.numItems);
mat4.translate(mvMatrix, [-xSpeed, -ySpeed, -z]);
mat4.translate(mvMatrix, [-2.7, 0.5, 0.0]);
mat4.translate(mvMatrix, [5.4, 0.0, 0.0]);
mat4.translate(mvMatrix, [-3.8, 2.0, 0.0]);
mvPushMatrix();
mat4.rotate(mvMatrix, degToRad(rSquare), [1, 1, 0]);
gl.bindBuffer(gl.ARRAY_BUFFER, fensterlVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, fensterlVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, fensterlVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, fensterlVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, fensterlVertexPositionBuffer.numItems);
mvPopMatrix();
mat4.translate(mvMatrix, [2.2, -0.0, 0.0]);
mvPushMatrix();
mat4.rotate(mvMatrix, degToRad(rSquare), [1, -1, 0]);
gl.bindBuffer(gl.ARRAY_BUFFER, fensterrVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, fensterrVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, fensterrVertexColorBuffer);
gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, fensterrVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
setMatrixUniforms();
gl.drawArrays(gl.TRIANGLE_STRIP, 0, fensterrVertexPositionBuffer.numItems);
mvPopMatrix();
}
function tick() {
requestAnimFrame(tick);
handleKeys();
drawScene();
}
function webGLStart() {
var canvas = document.getElementById("glcanvas");
initGL(canvas);
initShaders();
initBuffers();
gl.clearColor(0.0, 0.0, 0.0, 0.3);
gl.enable(gl.DEPTH_TEST);
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
tick();
}
</script>
</head>
<body onload="webGLStart();">
<div class="container">
<canvas id="glcanvas" width="1000" height="700"></canvas>
<canvas id="text" width="1000" height="700"></canvas>
</canvas>
</div>
<style>
.container {
position: relative;
}
#text {
position: absolute;
left: 0px;
top: 0px;
z-index: 10;
}
#abc {
position: absolute;
left: 200px;
top: 500px;
z-index: 0;
border: 5px solid grey ;
}
</style>
</body>
<script>
(function() {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
window.requestAnimationFrame = requestAnimationFrame;
})();
var canvas = document.getElementById("text");
var ctx=canvas.getContext("2d");
ctx.clearRect(0,0,1000,700);
keys = [];
var y1=560;
function update(){
var c1="white",c2="white",xs=115,ys=550;
ctx.clearRect(0,0,canvas.width,canvas.height);
if (keys[39]) {
// right arrow
c2="red";
xs=130;
}
if (keys[37]) {
// left arrow
c1="red";
xs=100;
}
if (keys[40]) {
// up arrow
ys=565;
}
if (keys[38]) {
// down arrow
ys=535;
}
ctx.font="22px Arial";
ctx.fillStyle = "black";
ctx.textAlign = "left";
ctx.fillText("Geschwindigkeit: 300 kn ", 170, 430);
ctx.font="22px Arial";
ctx.fillStyle = "black";
ctx.textAlign = "left";
ctx.fillText("Hoehe: 100 ft ", 550, 430);
ctx.beginPath();
ctx.lineWidth="2";
ctx.strokeStyle = "white";
/*Schubkontrolle*/
ctx.strokeRect(460,500,30,120);
ctx.strokeRect(510,500,30,120);
/*Landeklappenhebel+Bremsklappenhebel*/
ctx.strokeRect(440,650,20,40);
ctx.strokeRect(480,650,20,40);
/*Gearlevel+Warnanzeige*/
ctx.strokeRect(820,600,26,70);
ctx.strokeRect(880,610,50,50);
ctx.fillStyle= "black";
/*Sidestick*/
ctx.arc(xs,ys,40,0,2*Math.PI);
/*Pedale*/
ctx.rect(120,630,50,50);
ctx.arc(170,655,25,0,2*Math.PI);
ctx.rect(60,630,50,50);
ctx.arc(60,655,25,0,2*Math.PI);
/*Schubkontrolle*/
ctx.rect(455,y1,40,20);
ctx.rect(505,560,40,20);
/*Landeklappenhebel+Bremsklappenhebel*/
ctx.rect(435,670,30,15);
ctx.arc(490,670,15,0,2*Math.PI);
/*Gearlevel+Warnanzeige*/
ctx.rect(815,620,36,15);
ctx.fill();
drawArrow(130,655,170,655,c2);
drawArrow(100,655,60,655,c1);
requestAnimationFrame(update);
}
var click,mx,my;
document.body.addEventListener("keydown", function(e) {
keys[e.keyCode] = true;
});
document.body.addEventListener("keyup", function(e) {
keys[e.keyCode] = false;
});
document.body.addEventListener("mousemove", function(e) {
if(click){
if(my>510&&my<610){
y1=y1+e.offsetY-my;
}
my=e.offsetY;
}
});
document.body.addEventListener("mousedown", function(e) {
mx=e.clientX;
my=e.offsetY;
if(mx > 455 && mx < 495 && my>y1 && my<(y1+20)){
click=true;
}
});
document.body.addEventListener("mouseup", function(e) {
click=false;
});
window.addEventListener("load",function(){
update();
});
function drawArrow(fromx, fromy, tox, toy,color){
//variables to be used when creating the arrow
var headlen = 1;
var angle = Math.atan2(toy-fromy,tox-fromx);
//starting path of the arrow from the start square to the end square and drawing the stroke
ctx.beginPath();
ctx.moveTo(fromx, fromy);
ctx.lineTo(tox, toy);
ctx.strokeStyle = color;
ctx.lineWidth = 10;
ctx.stroke();
//starting a new path from the head of the arrow to one of the sides of the point
ctx.beginPath();
ctx.moveTo(tox, toy);
ctx.lineTo(tox-headlen*Math.cos(angle-Math.PI/7),toy-headlen*Math.sin(angle-Math.PI/7));
//path from the side point of the arrow, to the other side point
ctx.lineTo(tox-headlen*Math.cos(angle+Math.PI/7),toy-headlen*Math.sin(angle+Math.PI/7));
//path from the side point back to the tip of the arrow, and then again to the opposite side point
ctx.lineTo(tox, toy);
ctx.lineTo(tox-headlen*Math.cos(angle-Math.PI/7),toy-headlen*Math.sin(angle-Math.PI/7));
//draws the paths created above
ctx.strokeStyle = color;
ctx.lineWidth = 10;
ctx.stroke();
ctx.fillStyle = color;
ctx.fill();
}
</script>
</html>