-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
478 lines (352 loc) · 13 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
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
<html>
<head>
<title>Spacelike - a WebGL orrery</title>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript" src="mjs.js"></script>
<script type="text/javascript" src="sphere.js"></script>
<script type="text/javascript" src="texture.js"></script>
<script type="text/javascript" src="skysphere.js"></script>
<script type="text/javascript" src="sun.js"></script>
<script type="text/javascript" src="earth.js"></script>
<script type="text/javascript" src="spacecraft.js"></script>
<script id="per-fragment-lighting-fs" type="x-shader/x-fragment">
#ifdef GL_ES
precision highp float;
#endif
varying vec2 vTextureCoord;
varying vec4 vTransformedNormal;
varying vec4 vPosition;
uniform bool uUseColorMap;
uniform sampler2D uColorMapSampler;
uniform vec4 uColor;
uniform bool uUseSpecularMap;
uniform sampler2D uSpecularMapSampler;
uniform float uShininess;
uniform bool uUseLighting;
uniform vec3 uAmbientColor;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingSpecularColor;
uniform vec3 uPointLightingDiffuseColor;
void main(void) {
vec3 lightWeighting;
if (!uUseLighting) {
lightWeighting = vec3(1.0, 1.0, 1.0);
} else {
vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
vec3 normal = normalize(vTransformedNormal.xyz);
float specularLightWeighting = 0.0;
float shininess = 32.0;
if (uUseSpecularMap) {
shininess = texture2D(uSpecularMapSampler, vec2(vTextureCoord.s, 1.0 - vTextureCoord.t)).r * 255.0;
} else {
shininess = uShininess;
}
if (shininess != 0.0 && shininess < 255.0) {
vec3 eyeDirection = normalize(-vPosition.xyz);
vec3 reflectionDirection = reflect(-lightDirection, normal);
specularLightWeighting = pow(max(dot(reflectionDirection, eyeDirection), 0.0), shininess);
}
float diffuseLightWeighting = max(dot(normal, lightDirection), 0.0);
lightWeighting = uAmbientColor
+ uPointLightingSpecularColor * specularLightWeighting
+ uPointLightingDiffuseColor * diffuseLightWeighting;
}
vec4 fragmentColor;
if (uUseColorMap) {
fragmentColor = texture2D(uColorMapSampler, vec2(vTextureCoord.s, 1.0 - vTextureCoord.t));
} else {
fragmentColor = uColor;
}
gl_FragColor = vec4(fragmentColor.rgb * lightWeighting, fragmentColor.a);
}
</script>
<script id="per-fragment-lighting-vs" type="x-shader/x-vertex">
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
uniform mat4 uNMatrix;
varying vec2 vTextureCoord;
varying vec4 vTransformedNormal;
varying vec4 vPosition;
void main(void) {
vPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * vPosition;
vTextureCoord = aTextureCoord;
vTransformedNormal = uNMatrix * vec4(aVertexNormal, 1.0);
}
</script>
<script type="text/javascript">
var gl;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
gl.viewportWidth = canvas.width;
gl.viewportHeight = canvas.height;
gl.mvPushMatrix = mvPushMatrix;
gl.mvPopMatrix = mvPopMatrix;
gl.mvTranslate = mvTranslate;
gl.mvRotate = mvRotate;
gl.mvScale = mvScale;
gl.mvMatrixStack = [];
} 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, "per-fragment-lighting-fs");
var vertexShader = getShader(gl, "per-fragment-lighting-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.vertexNormalAttribute = gl.getAttribLocation(shaderProgram, "aVertexNormal");
gl.enableVertexAttribArray(shaderProgram.vertexNormalAttribute);
shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
shaderProgram.nMatrixUniform = gl.getUniformLocation(shaderProgram, "uNMatrix");
shaderProgram.useColorMapUniform = gl.getUniformLocation(shaderProgram, "uUseColorMap");
shaderProgram.colorMapSamplerUniform = gl.getUniformLocation(shaderProgram, "uColorMapSampler");
shaderProgram.colorUniform = gl.getUniformLocation(shaderProgram, "uColor");
shaderProgram.useSpecularMapUniform = gl.getUniformLocation(shaderProgram, "uUseSpecularMap");
shaderProgram.specularMapSamplerUniform = gl.getUniformLocation(shaderProgram, "uSpecularMapSampler");
shaderProgram.shininessUniform = gl.getUniformLocation(shaderProgram, "uShininess");
shaderProgram.useLightingUniform = gl.getUniformLocation(shaderProgram, "uUseLighting");
shaderProgram.ambientColorUniform = gl.getUniformLocation(shaderProgram, "uAmbientColor");
shaderProgram.pointLightingLocationUniform = gl.getUniformLocation(shaderProgram, "uPointLightingLocation");
shaderProgram.pointLightingSpecularColorUniform = gl.getUniformLocation(shaderProgram, "uPointLightingSpecularColor");
shaderProgram.pointLightingDiffuseColorUniform = gl.getUniformLocation(shaderProgram, "uPointLightingDiffuseColor");
}
function mvPushMatrix() {
gl.mvMatrixStack.push(M4x4.clone(gl.mvMatrix));
}
function mvPopMatrix() {
if (gl.mvMatrixStack.length == 0) {
throw "Invalid popMatrix!";
}
gl.mvMatrix = gl.mvMatrixStack.pop();
return gl.mvMatrix;
}
function loadIdentity() {
gl.mvMatrix = M4x4.clone(M4x4.I);
}
function mvTranslate(v) {
gl.mvMatrix = M4x4.translate(v, gl.mvMatrix);
}
function mvRotate(angle, v) {
gl.mvMatrix = M4x4.rotate(angle * Math.PI / 180, v, gl.mvMatrix);
}
function mvScale(x, y, z) {
gl.mvMatrix = M4x4.scale3(x, y, z, gl.mvMatrix);
}
function perspective(fovy, aspect, znear, zfar) {
gl.pMatrix = M4x4.makePerspective(fovy, aspect, znear, zfar);
}
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, gl.pMatrix);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, gl.mvMatrix);
var normalMatrix = M4x4.inverseOrthonormal(gl.mvMatrix);
M4x4.transpose(normalMatrix, normalMatrix);
gl.uniformMatrix4fv(shaderProgram.nMatrixUniform, false, normalMatrix);
}
var currentlyPressedKeys = Object();
function handleKeyDown(event) {
currentlyPressedKeys[event.keyCode] = true;
}
function handleKeyUp(event) {
currentlyPressedKeys[event.keyCode] = false;
}
function handleKeys() {
if (currentlyPressedKeys[33]) {
// Page Up
if (cameraDistance > 0.17) {
cameraDistance /= 1.05;
}
}
if (currentlyPressedKeys[34]) {
// Page Down
cameraDistance *= 1.05;
}
if (currentlyPressedKeys[37]) {
// Left cursor key
}
if (currentlyPressedKeys[39]) {
// Right cursor key
}
if (currentlyPressedKeys[38]) {
// Up cursor key
}
if (currentlyPressedKeys[40]) {
// Down cursor key
}
}
var mouseDown = false;
var lastMouseX = null;
var lastMouseY = null;
function handleMouseDown(event) {
mouseDown = true;
lastMouseX = event.clientX;
lastMouseY = event.clientY;
}
function handleMouseUp(event) {
mouseDown = false;
}
function handleMouseMove(event) {
if (!mouseDown) {
return;
}
var newX = event.clientX;
var newY = event.clientY;
var deltaX = newX - lastMouseX
var newRotationMatrix = M4x4.makeRotate(deltaX / 100, [0, 1, 0]);
var deltaY = newY - lastMouseY;
newRotationMatrix = M4x4.rotate(deltaY / 100, [1, 0, 0], newRotationMatrix);
cameraRotationMatrix = M4x4.mul(newRotationMatrix, cameraRotationMatrix);
lastMouseX = newX
lastMouseY = newY;
}
var cameraRotationMatrix;
var cameraDistance;
function drawScene() {
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 2000000000.0);
gl.uniform3f(shaderProgram.ambientColorUniform, 0.05, 0.05, 0.05);
loadIdentity();
mvTranslate([0, 0, -cameraDistance]);
gl.mvMatrix = M4x4.mul(gl.mvMatrix, cameraRotationMatrix);
skySphere.draw(shaderProgram, cameraDistance * 100);
for (var i in worldObjects) {
worldObjects[i].draw(shaderProgram, playerAvatar.location);
}
}
// NB our distances are in kilometers so G is 1,000,000 times smaller than
// it would normally be.
var G = 0.00000000000000006674
function doPhysics() {
for (var i in worldObjects) {
var obj1 = worldObjects[i];
obj1.acceleration = V3.$(0, 0, 0);
for (var j in worldObjects) {
var obj2 = worldObjects[j];
if (obj1 != obj2) {
var displacement = V3.sub(obj2.location, obj1.location);
// NB We have to divide by a further 1,000 because although our value for G is scaled to km,
// we still get a value from Newton's equation in terms of m/s, so need to rescale.
var acceleration = V3.scale(V3.normalize(displacement), (G * obj2.mass) / (V3.lengthSquared(displacement) * 1000));
obj1.acceleration = V3.add(obj1.acceleration, acceleration);
}
}
}
}
var lastTime = 0;
function animate() {
var timeNow = new Date().getTime();
if (lastTime != 0) {
var elapsed = timeNow - lastTime;
for (var i in worldObjects) {
worldObjects[i].animate(elapsed);
}
}
lastTime = timeNow;
}
function updateDebugFields() {
document.getElementById("cameraDistance").innerHTML = cameraDistance;
document.getElementById("acceleration").innerHTML = playerAvatar.acceleration[0] + ", " + playerAvatar.acceleration[1] + ", " + playerAvatar.acceleration[2];
document.getElementById("velocity").innerHTML = playerAvatar.velocity[0] + ", " + playerAvatar.velocity[1] + ", " + playerAvatar.velocity[2];
}
function tick() {
handleKeys();
drawScene();
doPhysics();
animate();
updateDebugFields();
}
function initCamera() {
cameraRotationMatrix = M4x4.makeRotate(10 * Math.PI / 180, [1, 0, 0]);
cameraDistance = 0.3;
}
var worldObjects = [];
var skySphere;
var playerAvatar;
var earth;
function initWorld() {
skySphere = new SkySphere(gl);
var sun = new Sun(gl, V3.$(0, 0, 0));
worldObjects.push(sun);
earth = new Earth(gl, V3.add(V3.$(149597887, 0, 0), sun.location));
worldObjects.push(earth);
playerAvatar = new Spacecraft(gl, V3.add(V3.$(0, 0, 18000), earth.location));
worldObjects.push(playerAvatar);
}
function webGLStart() {
var canvas = document.getElementById("canvas-3d");
initGL(canvas);
initCamera();
initShaders();
initWorld();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
canvas.onmousedown = handleMouseDown;
document.onmouseup = handleMouseUp;
document.onmousemove = handleMouseMove;
document.onkeydown = handleKeyDown;
document.onkeyup = handleKeyUp;
setInterval(tick, 15);
}
</script>
</head>
<body onload="webGLStart();">
<canvas id="canvas-3d" style="border: none;" width="1000" height="800"></canvas>
<p>Camera distance: <span id="cameraDistance"></span></p>
<p>Acceleration: <span id="acceleration"></span></p>
<p>Velocity: <span id="velocity"></span></p>
<br/>
Galaxy texture from <a href="http://www.gigagalaxyzoom.org/">Gigagalaxy Zoom</a>, by ESO/S. Brunier.<br/>
Earth texture courtesy of <a href="http://www.esa.int/esaEO/SEMGSY2IU7E_index_0.html">the European Space Agency/Envisat</a>.<br/>
<br/>
</body>
</html>