Skip to content

Commit 2416374

Browse files
committed
Implement full texture load, timestamp
And other bugfixes. It looks sweet now. Gonna stuff the 94MB of assets into gh-pages. sorry, github..
1 parent ecf268c commit 2416374

File tree

6 files changed

+194
-63
lines changed

6 files changed

+194
-63
lines changed

octave/pack_ocean.m

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
% pack all the data downloaded into PNG files.
22
pkg load image
3-
fst = pack16(datenum(2011, 01, 01));
4-
% XXX octave flips the alpha channel when saving images (and reading them), so
5-
% unflip it before write
6-
fst(:, :, 4) = 255 - fst(:, :, 4);
7-
imwrite(fst, 'packed16.png');
3+
4+
last = datenum(2012, 12, 01);
5+
6+
current = datenum(1993, 01, 02);
7+
n = 0;
8+
% there are 240 timeslices for an even 15 images,
9+
% uncompressed, they're 8mb each for a total of 1920 mb
10+
while current < last
11+
printf('packing image %02i\n', n);
12+
data = pack16(current);
13+
14+
% XXX octave flips the alpha channel when saving images (and reading them), so
15+
% unflip it before write
16+
data(:, :, 4) = 255 - data(:, :, 4);
17+
18+
imwrite(data, sprintf('packed-%02i.png', n));
19+
20+
n = n + 1;
21+
current = addtodate(current, 16, 'month');
22+
end

webgl/geoid.co

+70-22
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ max-val.addEventListener \input !->
134134
if symmetric.checked
135135
min-val.value = 1 - parseFloat @value
136136

137-
addWheelListener document.body, !->
137+
addWheelListener canvas, !->
138138
fov := clamp fov + it.deltaY / Math.abs(it.deltaY), 1, 100
139139

140140
ctx = document.createElement \canvas .getContext \2d
@@ -295,10 +295,12 @@ $ \advection-steps .addEventListener \change !->
295295
mod = (num, base) -> ((num % base) + base) % base
296296

297297
!function load-ocean-current-common program
298+
slices-available = ocean-field.length
299+
298300
time = parseFloat $(\time)value
299301
cur = Math.floor time / 2
300-
last = mod cur - 1, 8
301-
next = mod cur + 1, 8
302+
last = mod cur - 1, slices-available
303+
next = mod cur + 1, slices-available
302304

303305
load-texture program, ocean-field[cur], \curOcean 0
304306
load-texture program, ocean-field[last], \prevOcean 1
@@ -327,34 +329,37 @@ initial = true
327329

328330
# time state
329331
playing = true
330-
$ \play
332+
$ \play
331333
&disabled = true
332334
&addEventListener \click !->
333335
playing := true
334336
@disabled = true
335337
$ \pause .disabled = false
336-
$ \pause
338+
$ \pause
337339
&disabled = false
338340
&addEventListener \click !->
339341
playing := false
340342
@disabled = true
341343
$ \play .disabled = false
342344

343345
export draw = !->
346+
slices-available = ocean-field.length * 2
344347
t = parseFloat $(\time)value
345348
if playing
346349
speed = parseFloat $(\speed)value
347-
new-t = mod t + speed, 16
350+
new-t = mod t + speed, slices-available
348351
$ \time .value = new-t
349352
t = new-t
350-
353+
354+
update-timestamp!
355+
351356
##################################################################
352357
# first: random noise injection and initial advection
353358
load-plain-quad-program p.noise-transport, noise-transport.framebuffer
354359

355360
#load-ocean-current-common p.noise-transport
356361
load-isWater p.noise-transport
357-
362+
358363
# use random offset of noise to vary over time cheaply
359364
# TODO needs to be time correlated
360365
uniform gl, p.noise-transport, \randomOffset \2f Math.random!, Math.random!
@@ -393,7 +398,7 @@ export draw = !->
393398

394399
gl.getUniformLocation p.advection, \h
395400
gl.uniform1f &, parseFloat($ \advection-h .value)
396-
401+
397402
# use random offset of noise to vary over time cheaply
398403
# TODO time correlation?
399404
uniform gl, p.advection, \randomOffset \2f Math.random!, Math.random!
@@ -515,45 +520,88 @@ function set-texture texture, data
515520
ocean-field = []
516521
land-textures = []
517522

518-
# 1 ocean texture (soon more)
523+
# 15 ocean textures (each with 16 timeslices) (load 2 first, since they take
524+
# up 240 MB memory total)
519525
# 1 land mask
520526
# 12 blue marble months
521527
# 1 dark marble
522-
textures-to-load = total-textures = 1 + 1 + 12 + 1
528+
textures-to-load = total-textures = 2 + 1 + 12 + 1
523529
maybe-draw = !->
524-
$ \load-progress .value = 1 - total-textures / textures-to-load
530+
$ \load-progress .value = 1 - textures-to-load / total-textures
525531

526532
if textures-to-load is 0
527533
$ \loading .hidden = true
528534
$ \load-progress .value = 0
535+
536+
$ \time .setAttribute \max ocean-field.length * 2 - 0.01
537+
538+
$ \time .value = 0
529539
draw!
530540

531-
load-image \packed16.png !->
532-
c = document.createElement(\canvas) <<< @{width, height}
541+
pad = -> if it >= 10 then it else "0#{it}"
542+
543+
function split-packed img, idx
544+
c = document.createElement(\canvas) <<< img{width, height}
533545
ctx = c.getContext \2d
534-
ctx.drawImage this, 0 0
546+
ctx.drawImage img, 0 0
535547
# unpack sections
548+
start = idx * 8
549+
n = 0
536550
for y of [0 512 1024 1536]
537551
for x of [0 1024]
538-
ocean-field.push do
552+
ocean-field[start + n] =
539553
set-texture gl.createTexture!, ctx.getImageData x, y, 1024 512
554+
++n
540555

541556
--textures-to-load
542557
maybe-draw!
543558

559+
for i til 2
560+
let
561+
load-image "packed-#{pad i}.png" !-> split-packed this, i
562+
563+
# load the rest of textures
564+
$ \load-more .addEventListener \click !->
565+
566+
total-textures := 13
567+
textures-to-load := 13
568+
$ \loading .hidden = false
569+
$ \load-progress .value = 0
570+
571+
for i from 2 til 15
572+
let
573+
load-image "packed-#{pad i}.png" !-> split-packed this, i
574+
575+
$ \time .addEventListener \input update-timestamp
576+
577+
function update-timestamp
578+
# update text timestamp
579+
months = parseFloat $(\time)value
580+
years = Math.floor months / 12
581+
y-months = Math.floor months - years * 12
582+
583+
# a diff date
584+
date = new Date
585+
&setFullYear do
586+
1991 + years
587+
y-months
588+
1
589+
590+
$ \timestamp .textContent =
591+
"#{date.getFullYear!}-#{pad date.getMonth! + 1}"
592+
544593
land-mask = gl.createTexture!
545594
load-image \land-mask.png !->
546595
set-texture land-mask, this
547596
--textures-to-load
548597
maybe-draw!
549598

550-
pad = -> if it >= 10 then it else "0#{it}"
551-
552599
for i from 1 to 12
553-
load-image "blue-marble-#{pad i}.jpg" !->
554-
land-textures.push set-texture gl.createTexture!, this
555-
--textures-to-load
556-
maybe-draw!
600+
let
601+
load-image "blue-marble-#{pad i}.jpg" !->
602+
land-textures[i - 1] = set-texture gl.createTexture!, this
603+
--textures-to-load
604+
maybe-draw!
557605

558606
night-texture = gl.createTexture!
559607
load-image \black-marble.jpg !->

webgl/geoid.js

+70-27
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
original commented source there. */
33
(function(){
44
"use strict";
5-
var canvas, ref$, width, height, k, ref1$, v, x0$, arr, rotation, e, currentRot, fov, distance, symmetric, x1$, minVal, ref2$, x2$, maxVal, reclamp, ctx, buffers, latBands, lonBands, noiseTex, noiseTransport, orthogonalLic, advection, blend, setupBuffers, numTriangles, p, mod, frame, initial, playing, x3$, x4$, draw, oceanField, landTextures, texturesToLoad, totalTextures, maybeDraw, landMask, pad, i, nightTexture, pointUnder, x5$, out$ = typeof exports != 'undefined' && exports || this;
5+
var canvas, ref$, width, height, k, ref1$, v, x0$, arr, rotation, e, currentRot, fov, distance, symmetric, x1$, minVal, ref2$, x2$, maxVal, reclamp, ctx, buffers, latBands, lonBands, noiseTex, noiseTransport, orthogonalLic, advection, blend, setupBuffers, numTriangles, p, mod, frame, initial, playing, x3$, x4$, draw, oceanField, landTextures, texturesToLoad, totalTextures, maybeDraw, pad, i, landMask, nightTexture, pointUnder, x5$, out$ = typeof exports != 'undefined' && exports || this;
66
canvas = document.getElementById('canvas');
77
ref$ = document.documentElement, canvas.width = ref$.clientWidth, canvas.height = ref$.clientHeight;
88
width = canvas.width, height = canvas.height;
@@ -122,7 +122,7 @@ original commented source there. */
122122
minVal.value = 1 - parseFloat(this.value);
123123
}
124124
});
125-
addWheelListener(document.body, function(it){
125+
addWheelListener(canvas, function(it){
126126
fov = clamp(fov + it.deltaY / Math.abs(it.deltaY), 1, 100);
127127
});
128128
ctx = document.createElement('canvas').getContext('2d');
@@ -249,11 +249,12 @@ original commented source there. */
249249
return (num % base + base) % base;
250250
};
251251
function loadOceanCurrentCommon(program){
252-
var time, cur, last, next, timeVal, x3$;
252+
var slicesAvailable, time, cur, last, next, timeVal, x3$;
253+
slicesAvailable = oceanField.length;
253254
time = parseFloat($('time').value);
254255
cur = Math.floor(time / 2);
255-
last = mod(cur - 1, 8);
256-
next = mod(cur + 1, 8);
256+
last = mod(cur - 1, slicesAvailable);
257+
next = mod(cur + 1, slicesAvailable);
257258
loadTexture(program, oceanField[cur], 'curOcean', 0);
258259
loadTexture(program, oceanField[last], 'prevOcean', 1);
259260
loadTexture(program, oceanField[next], 'nextOcean', 2);
@@ -287,13 +288,15 @@ original commented source there. */
287288
$('play').disabled = false;
288289
});
289290
out$.draw = draw = function(){
290-
var t, speed, newT, x5$, x6$, x7$, x8$, x9$, rot, modelView, useDay, monthNo, nextNo, x10$, x11$, x12$, x13$, x14$, x15$, x16$;
291+
var slicesAvailable, t, speed, newT, x5$, x6$, x7$, x8$, x9$, rot, modelView, useDay, monthNo, nextNo, x10$, x11$, x12$, x13$, x14$, x15$, x16$;
292+
slicesAvailable = oceanField.length * 2;
291293
t = parseFloat($('time').value);
292294
if (playing) {
293295
speed = parseFloat($('speed').value);
294-
newT = mod(t + speed, 16);
296+
newT = mod(t + speed, slicesAvailable);
295297
$('time').value = newT;
296298
t = newT;
299+
updateTimestamp();
297300
}
298301
loadPlainQuadProgram(p.noiseTransport, noiseTransport.framebuffer);
299302
loadIsWater(p.noiseTransport);
@@ -395,45 +398,78 @@ original commented source there. */
395398
}
396399
oceanField = [];
397400
landTextures = [];
398-
texturesToLoad = totalTextures = 1 + 1 + 12 + 1;
401+
texturesToLoad = totalTextures = 2 + 1 + 12 + 1;
399402
maybeDraw = function(){
400-
$('load-progress').value = 1 - totalTextures / texturesToLoad;
403+
$('load-progress').value = 1 - texturesToLoad / totalTextures;
401404
if (texturesToLoad === 0) {
402405
$('loading').hidden = true;
403406
$('load-progress').value = 0;
407+
$('time').setAttribute('max', oceanField.length * 2 - 0.01);
408+
$('time').value = 0;
404409
draw();
405410
}
406411
};
407-
loadImage('packed16.png', function(){
408-
var c, ref$, ctx, i$, ref1$, len$, y, j$, ref2$, len1$, x;
409-
c = (ref$ = document.createElement('canvas'), ref$.width = this.width, ref$.height = this.height, ref$);
412+
pad = function(it){
413+
if (it >= 10) {
414+
return it;
415+
} else {
416+
return "0" + it;
417+
}
418+
};
419+
function splitPacked(img, idx){
420+
var c, ref$, ctx, start, n, i$, ref1$, len$, y, j$, ref2$, len1$, x;
421+
c = (ref$ = document.createElement('canvas'), ref$.width = img.width, ref$.height = img.height, ref$);
410422
ctx = c.getContext('2d');
411-
ctx.drawImage(this, 0, 0);
423+
ctx.drawImage(img, 0, 0);
424+
start = idx * 8;
425+
n = 0;
412426
for (i$ = 0, len$ = (ref1$ = [0, 512, 1024, 1536]).length; i$ < len$; ++i$) {
413427
y = ref1$[i$];
414428
for (j$ = 0, len1$ = (ref2$ = [0, 1024]).length; j$ < len1$; ++j$) {
415429
x = ref2$[j$];
416-
oceanField.push(setTexture(gl.createTexture(), ctx.getImageData(x, y, 1024, 512)));
430+
oceanField[start + n] = setTexture(gl.createTexture(), ctx.getImageData(x, y, 1024, 512));
431+
++n;
417432
}
418433
}
419434
--texturesToLoad;
420-
maybeDraw();
435+
return maybeDraw();
436+
}
437+
for (i = 0; i < 2; ++i) {
438+
(fn$.call(this, i));
439+
}
440+
$('load-more').addEventListener('click', function(){
441+
var i;
442+
totalTextures = 13;
443+
texturesToLoad = 13;
444+
$('loading').hidden = false;
445+
$('load-progress').value = 0;
446+
for (i = 2; i < 15; ++i) {
447+
(fn$.call(this, i));
448+
}
449+
function fn$(i){
450+
loadImage("packed-" + pad(i) + ".png", function(){
451+
splitPacked(this, i);
452+
});
453+
}
421454
});
455+
$('time').addEventListener('input', updateTimestamp);
456+
function updateTimestamp(){
457+
var months, years, yMonths, x5$, date;
458+
months = parseFloat($('time').value);
459+
years = Math.floor(months / 12);
460+
yMonths = Math.floor(months - years * 12);
461+
x5$ = date = new Date;
462+
x5$.setFullYear(1991 + years, yMonths, 1);
463+
return $('timestamp').textContent = date.getFullYear() + "-" + pad(date.getMonth() + 1);
464+
}
422465
landMask = gl.createTexture();
423466
loadImage('land-mask.png', function(){
424467
setTexture(landMask, this);
425468
--texturesToLoad;
426469
maybeDraw();
427470
});
428-
pad = function(it){
429-
if (it >= 10) {
430-
return it;
431-
} else {
432-
return "0" + it;
433-
}
434-
};
435471
for (i = 1; i <= 12; ++i) {
436-
loadImage("blue-marble-" + pad(i) + ".jpg", fn$);
472+
(fn1$.call(this, i));
437473
}
438474
nightTexture = gl.createTexture();
439475
loadImage('black-marble.jpg', function(){
@@ -487,9 +523,16 @@ original commented source there. */
487523
x5$.addEventListener('mouseup', stop);
488524
x5$.addEventListener('mouseleave', stop);
489525
});
490-
function fn$(){
491-
landTextures.push(setTexture(gl.createTexture(), this));
492-
--texturesToLoad;
493-
maybeDraw();
526+
function fn$(i){
527+
loadImage("packed-" + pad(i) + ".png", function(){
528+
splitPacked(this, i);
529+
});
530+
}
531+
function fn1$(i){
532+
loadImage("blue-marble-" + pad(i) + ".jpg", function(){
533+
landTextures[i - 1] = setTexture(gl.createTexture(), this);
534+
--texturesToLoad;
535+
maybeDraw();
536+
});
494537
}
495538
}).call(this);

0 commit comments

Comments
 (0)