class Water extends Terrain{ float[][][] flow; float[][] froth; float step = 0.9; float flowDecay = 0.85; float addedVolume,volume; Terrain terrain; final int U=0, L=1; //------------ Water( Terrain t, float pillarHeight ){ super(t); this.terrain = t; wireframe = false; gouraud = false; dynamicTexture = true; // initialize arrays flow = new float[res][res][2]; for(int x=0;x0 ? 1 : -1 ); } //------------ void update(){ volume = 0; float diff; for(int x=0; x 0 ){ // water cell volume += getWaterHeight(x,y); // calculate total volume if( 0 < (diff = getDiff(x,y,x,y-1)) ) flow[x][y ][U] += step*diff/4; if( 0 < (diff = getDiff(x,y,x,y+1)) ) flow[x][y+1][U] -= step*diff/4; if( 0 < (diff = getDiff(x,y,x-1,y)) ) flow[x ][y][L] += step*diff/4; if( 0 < (diff = getDiff(x,y,x+1,y)) ) flow[x+1][y][L] -= step*diff/4; }// endif } } // correct flow for(int x=1; x0 ) flow[x][y][U] = 0; if( flow[x][y][L]>0 ) flow[x][y][L] = 0; if( flow[x][y+1][U]<0 ) flow[x][y+1][U] = 0; if( flow[x+1][y][L]>0 ) flow[x+1][y][L] = 0; } } } if(gouraud) makeNormals(); } //------------ float getHeight(int x, int y){ return ( getWaterHeight(x,y)>0 ? heightMap[x][y] : heightMap[x][y]-4 ); } //------------ float neighbourWaterVolume(int x, int y){ return getWaterHeight(x,y-1) + getWaterHeight(x,y+1) + getWaterHeight(x+1,y) + getWaterHeight(x-1,y); } //------------ void buildVertexColors(){ for(int x=1;x0 ){ float flowSum = abs(flow[x][y][U]) + abs(flow[x][y][L]); //shallow streams should be more frothy float whiteness = (flowSum) * 512 / min(30,1+getWaterHeight(x,y)); vertexColor[x][y] = color( whiteness,whiteness,255 ); }else { vertexColor[x][y] = terrain.vertexColor[x][y]; } } } } }