-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
150 lines (118 loc) · 3.88 KB
/
index.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
<!doctype html>
<html>
<head>
<title>لعبة الزوايا</title>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
</haed>
<body>
<p> <h3>The WASD Game ! </h3></p>
<p><h4> Game Instructions : </h4>
<ol>
<li>Use the arrows key to move the Object .</li>
<li>W to go Up .</li>
<li>A to go Left .</li>
<li>S to go Down .</li>
<li>D to go Right .</li>
</ol>
</p>
<canvas id="renderCanvas" width="500" height="400"></canvas>
<script src="webgl-utils.js"></script>
<script src="gl-matrix.js"></script>
<script>
var canvas = document.getElementById("renderCanvas");
var gl = canvas.getContext("webgl2");
if (!gl) {
alert("webgl2 is not supported");
}
document.addEventListener("keydown",keyboard);
canvas.addEventListener("click",mousaClick);
var vertexShaderSource =
`#version 300 es
in vec2 a_Position;
uniform mat4 u_ModelMatrix;
void main()
{
gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0);
}`;
var fragmentShaderSource =
`#version 300 es
precision mediump float;
uniform vec3 u_Color;
out vec4 fragColor;
void main()
{
fragColor = vec4(u_Color, 1.0);
}`;
var gl = document.getElementById("renderCanvas").getContext("webgl2");
var vShader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vShader, vertexShaderSource);
gl.compileShader(vShader);
var fShader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fShader, fragmentShaderSource);
gl.compileShader(fShader);
var program = gl.createProgram();
gl.attachShader(program, vShader);
gl.attachShader(program, fShader);
gl.linkProgram(program);
gl.useProgram(program);
// to change the look of your object .
// size of the object
var vertices = new Float32Array([
-0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5
]);
var vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var a_Position = gl.getAttribLocation(program, "a_Position");
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(a_Position);
// color of the object
var u_Color = gl.getUniformLocation(program, "u_Color");
gl.uniform3f(u_Color, 0, 0.52, 1);
var modelMatrix = mat4.create();
var pos = { x: 0.1, y: 0.1 };
var step = 0.2;
//////////////function related to the Requirement of the course :
/// حرف (ث) للأتجاه للأعلى
///حرف (ش) للأتجاه لليسار
///حرف (س) للأتجاه للأسفل
/// حرف (ي) للأتجاه لليمين
// WASD function and Arrow
window.addEventListener('keydown', function (event)
{
if (event.keyCode === 87 || event.keyCode === 38) // W, Up
pos.y += step;
else if (event.keyCode === 65 || event.keyCode === 37) // A, Left
pos.x -= step;
else if (event.keyCode === 83 || event.keyCode === 40) // S, Down
pos.y -= step;
else if (event.keyCode === 68 || event.keyCode === 39) // D, Right
pos.x += step;
draw(pos);
});
draw(pos);
function draw(pos)
{
mat4.identity(modelMatrix);
mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(pos.x, pos.y, 0.0));
mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.1, 0.1, 0.1));
var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix");
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix);
// background-color
gl.clearColor(0.999, 0.70, 0.53, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);
}
function keyboard(event){
let code = event.which || event.keyCode; // Use either which or keyCode, depending on browser support
//alert(code);
}
function mousaClick(event){
//alert(event.offsetX + ' ' + event.offsetY); // Returns the coordinates of the mouse pointer relative to the position of the edge of the target element
}
//Call drawScene draw the first frame
drawScene();
</script>
</body>
</html>