Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmen G. Zijp committed Jan 8, 2018
0 parents commit b61d92e
Show file tree
Hide file tree
Showing 36 changed files with 5,724 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# mapmind
174 changes: 174 additions & 0 deletions automaton.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// colormap fragment shader

// Watch out: Some horrible ugly hacks were used because stupid webgl cannot handle variable array indices!
// See https://stackoverflow.com/questions/30585265/what-can-i-use-as-an-array-index-in-glsl-in-webgl

precision mediump float;

// our textures
uniform sampler2D waterlevelMap;
uniform sampler2D elevationMap;

// declare the texture coordinate that is passed in from the fragment shader
varying vec2 v_texCoord;

// declare additional variables we pass from the main program into this shader
uniform float zmin;
uniform float zmax;
uniform float dw; // water increment
uniform float du; // the width of the cells
uniform float dv; // the height of the cells

uniform int rain;
uniform float lastX;
uniform float lastY;
uniform float currX;
uniform float currY;
uniform float wellX;
uniform float wellY;

uniform int nSources;
uniform vec2 sourceCoords[16];
uniform int nSinks;
uniform vec2 sinkCoords[16];

uniform int pass;
uniform int rseed;

vec2 coord(int dir) {
if (dir==0) return vec2(v_texCoord.x, v_texCoord.y );
if (dir==1 || dir==-2) return vec2(v_texCoord.x - du, v_texCoord.y - dv);
if (dir==2 || dir==-1) return vec2(v_texCoord.x + du, v_texCoord.y + dv);
if (dir==3 || dir==-4) return vec2(v_texCoord.x - du, v_texCoord.y );
if (dir==4 || dir==-3) return vec2(v_texCoord.x + du, v_texCoord.y );
if (dir==5 || dir==-6) return vec2(v_texCoord.x - du, v_texCoord.y + dv);
if (dir==6 || dir==-5) return vec2(v_texCoord.x + du, v_texCoord.y - dv);
if (dir==7 || dir==-8) return vec2(v_texCoord.x, v_texCoord.y + dv);
if (dir==8 || dir==-7) return vec2(v_texCoord.x, v_texCoord.y - dv);
}

int elevation(int dir) {
return int((zmax-zmin)*texture2D(elevationMap, coord(dir)).r/dw);
}

int waterlevel(int dir) {
return int(255.0*texture2D(waterlevelMap, coord(dir)).r);
}

bool getbit(int a, int n) {
// Below is an *UGLY* WebGL rewrite for
// for (int i=7;i>n;i--) if(a-int(exp2(float(i)))>=0) a = a - int(exp2(float(i)));
for (int i=7;i>=0;i--) if (i>n && a-int(exp2(float(i)))>=0) a = a - int(exp2(float(i)));
if (a-int(exp2(float(n)))>=0) return true;
else return false;
}

bool flowdir(int pos, int dir) {
int flowdirs = int(255.0*texture2D(waterlevelMap, coord(pos)).g);
return getbit(flowdirs, dir);
}

int rand(vec2 co, int max) {
float a = 12.9898;
float b = 78.233;
float c = 43758.5453;
float dt = dot(co.xy ,vec2(a,b));
float sn = mod(dt, 3.14);
int rnd = int(float(max)*fract(abs(sin(sn) * c)));
if (rnd<0) rnd = 0;
if (rnd>=max) rnd = max-1;
return rnd;
}

void main() {
if (pass == 0) {
int flowdirs = 0;
// only continue if water is available at gridpoint
if (waterlevel(0) > 0) {
// calculate slope (drop/verval) in 8 wind directions
int dh[9];
dh[0] = 0;
for (int i=1;i<9;i++) dh[i] = (elevation(0) + waterlevel(0) - elevation(i) - waterlevel(i));

// find out which winddirection has highest drop
int m = 0;
for (int i=1;i<9;i++) {
// Below is an *UGLY* WebGL rewrite for
// if (dh[i] > dh[m]) m = i;
for (int k = 0; k < 9; ++k) if (m == k) {
if (dh[i] > dh[k]) m = i;
}
}

// only continue if gridpoint shows drop in one or more directions
if (m>0) {
// set flags for direction(s) with highest drop
int n = 0;
for (int i=1;i<9;i++) {
// Below is an *UGLY* WebGL rewrite for
// if (dh[i] == dh[m]) {
// flowdirs = flowdirs + int(exp2(i-1));
// n++;
// }
for (int k = 0; k < 9; ++k) if (m == k) {
if (dh[i] == dh[k]) {
flowdirs = flowdirs + int(exp2(float(i-1)));
n++;
}
}
}

// check for available excess water at gridpoint
int available = waterlevel(0);

// Below is an *UGLY* WebGL rewrite for
// if (dh[m] < available) available = dh[m];
for (int k = 0; k < 9; ++k) if (m == k) {
if (dh[k] < available) available = dh[k];
}

// knock down bits from flowdirs until the number matches available units, start at random
int dir = rand(coord(rseed), 8);

// Below is an *UGLY* WebGL rewrite for
// while(n>available) {
for (int d=0; d<8; d++) {
if((n>available) && getbit(flowdirs, dir)) {
flowdirs = flowdirs - int(exp2(float(dir)));
n--;
}
dir++;
if (dir == 8) dir = 0;
}
}
}
gl_FragColor = vec4(texture2D(waterlevelMap, v_texCoord).r, float(flowdirs)/255.0, 0.0, 0.0);
}
else {
int level = int(255.0*texture2D(waterlevelMap, v_texCoord).r);

// account flow from resp to gridpoint
if (level>0) for (int i=1;i<9;i++) if (flowdir(0, i-1)) level--;
for (int i=1;i<9;i++) if (flowdir(-i,i-1)) level++;

// apply raining...
if ((rain==1) && (rand(coord(rseed), 8)==0)) level++;

// apply sources and sinks
vec2 scale = vec2(1, du/dv);
for (int i=0; i<16; i++) {
if (i<nSources) {
if (distance(scale*v_texCoord, scale*sourceCoords[i]) < 3.0*du) level+= 4;
}
}
for (int i=0; i<16; i++) {
if (i<nSinks) {
if (distance(scale*v_texCoord, scale*sinkCoords[i]) < 3.0*du) level = 0;
}
}

// drain at border of frame
if ((v_texCoord.x < du) || (v_texCoord.x >= 1.0-du) || (v_texCoord.y < dv) || (v_texCoord.y >= 1.0-dv)) gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
else gl_FragColor = vec4(float(level)/255.0, 0.0, 0.0, 0.0);
}
}
23 changes: 23 additions & 0 deletions automaton.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// colormap vertex shader
precision mediump float;

// declare variables for various coordinates (framebuffer, texture) we get from the main program
attribute vec2 a_position;
attribute vec2 a_texCoord;

// declare additional variables we pass from the main program into this shader
uniform vec2 u_resolution;

// declare the texture coordinate that is passed on to the fragment shader
varying vec2 v_texCoord;

void main() {
// convert the rectangle from pixels (0,0 to u_resolution) to clipspace (-1.0 to 1.0)
vec2 clipCoord = 2.0*(a_position/u_resolution) - 1.0;

// set position
gl_Position = vec4(clipCoord, 0, 1);

// pass the texCoord to the fragment shader
v_texCoord = a_texCoord;
}
41 changes: 41 additions & 0 deletions colormap.fsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// colormap fragment shader
precision mediump float;

// our textures
uniform sampler2D elevationMap;
uniform sampler2D waterlevelMap;

// declare the texture coordinate that is passed in from the fragment shader
varying vec2 v_texCoord;

// declare additional variables we pass from the main program into this shader
uniform float level;
uniform float heights[16];
uniform float reds[16];
uniform float greens[16];
uniform float blues[16];

vec4 col(float h) {
h = h/2.0;
float dh, r, g, b;
for (int i=0; i<15; i++) if ((h>=heights[i]) && (h<heights[i+1])) {
dh = float(h-heights[i])/float(heights[i+1]-heights[i]);
r = reds[i] + (reds[i+1] - reds[i]) * dh;
g = greens[i] + (greens[i+1] - greens[i]) * dh;
b = blues[i] + (blues[i+1] - blues[i]) * dh;
break;
}
vec4 c = vec4(r, g, b, 1.0);
return c;
}

void main() {
float elevation = texture2D(elevationMap, v_texCoord).r;
float waterlayer = texture2D(waterlevelMap, v_texCoord).r;

if (elevation==0.0) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
else {
if (waterlayer > 0.0 && waterlayer > level - elevation) gl_FragColor = col(-waterlayer);
else gl_FragColor = col(elevation - level);
}
}
42 changes: 42 additions & 0 deletions colormap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!-- colormap fragment shader -->
<script id="colormap-fragment-shader" type="x-shader/x-fragment">
precision mediump float;

// our texture
uniform sampler2D u_image;

// the texCoords passed in from the vertex shader.
varying vec2 v_texCoord;

uniform float level;
uniform float heights[16];
uniform float reds[16];
uniform float greens[16];
uniform float blues[16];

vec4 col(float h) {
h = h/2.0;
float dh, r, g, b;
for (int i=0; i<15; i++) if ((h>=heights[i]) && (h<heights[i+1])) {
dh = float(h-heights[i])/float(heights[i+1]-heights[i]);
r = reds[i] + (reds[i+1] - reds[i]) * dh;
g = greens[i] + (greens[i+1] - greens[i]) * dh;
b = blues[i] + (blues[i+1] - blues[i]) * dh;
break;
}
vec4 c = vec4(r, g, b, 1.0);
return c;
}

void main() {
// float elevation = texture2D(elevationMap, gl_TexCoord[0].st).a;
float elevation = texture2D(u_image, v_texCoord).a;
float waterlayer = 0.0; //texture2D(waterlevelMap, v_texCoord).a;

if (elevation==0.0) gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
else {
if (waterlayer > 0.0 && waterlayer > level - elevation) gl_FragColor = col(-waterlayer);
else gl_FragColor = col(elevation - level);
}
}
</script>
23 changes: 23 additions & 0 deletions colormap.vsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// colormap vertex shader
precision mediump float;

// declare variables for various coordinates (framebuffer, texture) we get from the main program
attribute vec2 a_position;
attribute vec2 a_texCoord;

// declare additional variables we pass from the main program into this shader
uniform vec2 u_resolution;

// declare the texture coordinate that is passed on to the fragment shader
varying vec2 v_texCoord;

void main() {
// convert the rectangle from pixels (0,0 to u_resolution) to clipspace (-1.0 to 1.0)
vec2 clipCoord = 2.0*(a_position/u_resolution) - 1.0;

// set position
gl_Position = vec4(clipCoord * vec2(1, -1), 0, 1);

// pass the texCoord to the fragment shader
v_texCoord = a_texCoord;
}
Binary file added cua.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added futura.otf
Binary file not shown.
Loading

0 comments on commit b61d92e

Please sign in to comment.