-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight.js
408 lines (338 loc) · 13.1 KB
/
flight.js
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
var gl;
var canvas;
var shaderProgram;
var vertexPositionBuffer;
// Create a place to store terrain geometry
var tVertexPositionBuffer;
//Create a place to store normals for shading
var tVertexNormalBuffer;
// Create a place to store the terrain triangles
var tIndexTriBuffer;
//Create a place to store the traingle edges
var tIndexEdgeBuffer;
// View parameters
var eyePt = vec3.fromValues(0.0,0.0,0.0);
var viewDir = vec3.fromValues(0.0,0.0,-1.0);
var up = vec3.fromValues(0.0,1.0,0.0);
// Identity vectors for direction and up vector
var baseDir = vec3.fromValues(0.0,0.0,-1.0);
var baseUp = vec3.fromValues(0.0,1.0,0.0);
var viewPt = vec3.fromValues(0.0,0.0,0.0);
// Create the normal
var nMatrix = mat3.create();
// Create ModelView matrix
var mvMatrix = mat4.create();
//Create Projection matrix
var pMatrix = mat4.create();
var mvMatrixStack = [];
var globe = quat.create();
// To roll left and right
function rollerLR(degree) {
var temp = quat.create();
temp = quat.setAxisAngle(temp, viewDir, degToRad(degree));
quat.normalize(temp, temp);
quat.multiply(globe, globe, temp);
quat.normalize(globe,globe);
}
// To turn left right using j and k
function turn(degree) {
var temp = quat.create();
temp = quat.setAxisAngle(temp, up, degToRad(degree));
quat.normalize(temp, temp);
quat.multiply(globe, globe, temp);
quat.normalize(globe,globe);
}
// To roll up or down
function rollerUD(degree) {
var temp = quat.create();
var temp2 = vec3.create();
vec3.cross(temp2, viewDir, up);
temp = quat.setAxisAngle(temp, temp2, degToRad(degree));
quat.normalize(temp, temp);
quat.multiply(globe, globe, temp);
quat.normalize(globe,globe);
}
//-------------------------------------------------------------------------
function setupTerrainBuffers() {
var vTerrain=[];
var fTerrain=[];
var nTerrain=[];
var eTerrain=[];
var gridN=20;
var numT = terrainFromIteration(gridN, -1,1,-1,1, vTerrain, fTerrain, nTerrain);
console.log("Generated ", numT, " triangles");
tVertexPositionBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tVertexPositionBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vTerrain), gl.STATIC_DRAW);
tVertexPositionBuffer.itemSize = 3;
tVertexPositionBuffer.numItems = (gridN+1)*(gridN+1);
// Specify normals to be able to do lighting calculations
tVertexNormalBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, tVertexNormalBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(nTerrain),
gl.STATIC_DRAW);
tVertexNormalBuffer.itemSize = 3;
tVertexNormalBuffer.numItems = (gridN+1)*(gridN+1);
// Specify faces of the terrain
tIndexTriBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tIndexTriBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(fTerrain),
gl.STATIC_DRAW);
tIndexTriBuffer.itemSize = 1;
tIndexTriBuffer.numItems = numT*3;
//Setup Edges
generateLinesFromIndexedTriangles(fTerrain,eTerrain);
tIndexEdgeBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tIndexEdgeBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(eTerrain),
gl.STATIC_DRAW);
tIndexEdgeBuffer.itemSize = 1;
tIndexEdgeBuffer.numItems = eTerrain.length;
}
//-------------------------------------------------------------------------
function drawTerrain(){
gl.polygonOffset(0,0);
gl.bindBuffer(gl.ARRAY_BUFFER, tVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, tVertexPositionBuffer.itemSize,
gl.FLOAT, false, 0, 0);
// Bind normal buffer
gl.bindBuffer(gl.ARRAY_BUFFER, tVertexNormalBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute,
tVertexNormalBuffer.itemSize,
gl.FLOAT, false, 0, 0);
//Draw
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tIndexTriBuffer);
gl.drawElements(gl.TRIANGLES, tIndexTriBuffer.numItems, gl.UNSIGNED_SHORT,0);
}
//-------------------------------------------------------------------------
function drawTerrainEdges(){
gl.polygonOffset(1,1);
gl.bindBuffer(gl.ARRAY_BUFFER, tVertexPositionBuffer);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, tVertexPositionBuffer.itemSize,
gl.FLOAT, false, 0, 0);
// Bind normal buffer
gl.bindBuffer(gl.ARRAY_BUFFER, tVertexNormalBuffer);
gl.vertexAttribPointer(shaderProgram.vertexNormalAttribute,
tVertexNormalBuffer.itemSize,
gl.FLOAT, false, 0, 0);
//Draw
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, tIndexEdgeBuffer);
gl.drawElements(gl.LINES, tIndexEdgeBuffer.numItems, gl.UNSIGNED_SHORT,0);
}
//-------------------------------------------------------------------------
function uploadModelViewMatrixToShader() {
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
}
//-------------------------------------------------------------------------
function uploadProjectionMatrixToShader() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform,
false, pMatrix);
}
//-------------------------------------------------------------------------
function uploadNormalMatrixToShader() {
mat3.fromMat4(nMatrix,mvMatrix);
mat3.transpose(nMatrix,nMatrix);
mat3.invert(nMatrix,nMatrix);
gl.uniformMatrix3fv(shaderProgram.nMatrixUniform, false, nMatrix);
}
//----------------------------------------------------------------------------------
function mvPushMatrix() {
var copy = mat4.clone(mvMatrix);
mvMatrixStack.push(copy);
}
//----------------------------------------------------------------------------------
function mvPopMatrix() {
if (mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
mvMatrix = mvMatrixStack.pop();
}
//----------------------------------------------------------------------------------
function setMatrixUniforms() {
uploadModelViewMatrixToShader();
uploadNormalMatrixToShader();
uploadProjectionMatrixToShader();
}
//----------------------------------------------------------------------------------
function degToRad(degrees) {
return degrees * Math.PI / 180;
}
//----------------------------------------------------------------------------------
function createGLContext(canvas) {
var names = ["webgl", "experimental-webgl"];
var context = null;
for (var i=0; i < names.length; i++) {
try {
context = canvas.getContext(names[i]);
} catch(e) {}
if (context) {
break;
}
}
if (context) {
context.viewportWidth = canvas.width;
context.viewportHeight = canvas.height;
} else {
alert("Failed to create WebGL context!");
}
return context;
}
//----------------------------------------------------------------------------------
function loadShaderFromDOM(id) {
var shaderScript = document.getElementById(id);
// If we don't find an element with the specified id
// we do an early exit w
if (!shaderScript) {
return null;
}
// Loop through the children for the found DOM element and
// build up the shader source code as a string
var shaderSource = "";
var currentChild = shaderScript.firstChild;
while (currentChild) {
if (currentChild.nodeType == 3) { // 3 corresponds to TEXT_NODE
shaderSource += currentChild.textContent;
}
currentChild = currentChild.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, shaderSource);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
//----------------------------------------------------------------------------------
function setupShaders() {
vertexShader = loadShaderFromDOM("shader-vs");
fragmentShader = loadShaderFromDOM("shader-fs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Failed to setup shaders");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.nMatrixUniform = gl.getUniformLocation(shaderProgram, "uNMatrix");
shaderProgram.uniformLightPositionLoc = gl.getUniformLocation(shaderProgram, "uLightPosition");
shaderProgram.uniformAmbientLightColorLoc = gl.getUniformLocation(shaderProgram, "uAmbientLightColor");
shaderProgram.uniformDiffuseLightColorLoc = gl.getUniformLocation(shaderProgram, "uDiffuseLightColor");
shaderProgram.uniformSpecularLightColorLoc = gl.getUniformLocation(shaderProgram, "uSpecularLightColor");
}
//-------------------------------------------------------------------------
function uploadLightsToShader(loc,a,d,s) {
gl.uniform3fv(shaderProgram.uniformLightPositionLoc, loc);
gl.uniform3fv(shaderProgram.uniformAmbientLightColorLoc, a);
gl.uniform3fv(shaderProgram.uniformDiffuseLightColorLoc, d);
gl.uniform3fv(shaderProgram.uniformSpecularLightColorLoc, s);
}
//----------------------------------------------------------------------------------
function setupBuffers() {
setupTerrainBuffers();
}
//----------------------------------------------------------------------------------
function draw() {
var transformVec = vec3.create();
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
// We'll use perspective
mat4.perspective(pMatrix,degToRad(45), gl.viewportWidth / gl.viewportHeight, 0.1, 200.0);
// Transform from Quat to viewDIR
vec3.transformQuat(viewDir, baseDir, globe);
vec3.transformQuat(up, baseUp, globe);
// We want to look down -z, so create a lookat point in that direction
vec3.add(viewPt, eyePt, viewDir);
// Then generate the lookat matrix and initialize the MV matrix to that view
mat4.lookAt(mvMatrix,eyePt,viewPt,up);
//Draw Terrain
mvPushMatrix();
vec3.set(transformVec,0.0,-0.25,-3.0);
mat4.translate(mvMatrix, mvMatrix,transformVec);
mat4.rotateX(mvMatrix, mvMatrix, degToRad(-75));
mat4.rotateZ(mvMatrix, mvMatrix, degToRad(25));
setMatrixUniforms();
if ((document.getElementById("polygon").checked) || (document.getElementById("wirepoly").checked))
{
uploadLightsToShader([0,1,1],[0.5,0.5,0.5],[1.0,0.5,0.0],[0.0,0.0,0.0]);
drawTerrain();
}
if(document.getElementById("wirepoly").checked){
uploadLightsToShader([0,1,1],[0.0,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]);
drawTerrainEdges();
}
if(document.getElementById("wireframe").checked){
uploadLightsToShader([0,1,1],[1.0,1.0,1.0],[0.0,0.0,0.0],[0.0,0.0,0.0]);
drawTerrainEdges();
}
mvPopMatrix();
}
//----------------------------------------------------------------------------------
function animate(key) {
// To automatically keep moving
var moveDir = vec3.create();
vec3.scale(moveDir, viewDir, 0.001);
vec3.add(eyePt, eyePt, moveDir);
}
//----------------------------------------------------------------------------------
function startup() {
canvas = document.getElementById("myGLCanvas");
gl = createGLContext(canvas);
setupShaders();
setupBuffers();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
//Keydown for add Event Listenter
document.addEventListener('keydown', function(event) {
onKeyDown(event);
});
tick();
}
// To keydown with event keyCOde
function onKeyDown(event) {
if (event.keyCode == 37) {
// Left
rollerLR(-1);
}
else if (event.keyCode == 39) {
// Up
rollerLR(1);
}
// Right
else if (event.keyCode == 38) {
rollerUD(1);
}
//Down
else if (event.keyCode == 40) {
rollerUD(-1);
}
// J
else if (event.keyCode == 74) {
turn(1);
}
// K
else if (event.keyCode == 75) {
turn(-1);
}
}
//----------------------------------------------------------------------------------
function tick() {
requestAnimFrame(tick);
draw();
animate();
}