Commit 3fdabc26 authored by amandaghassaei's avatar amandaghassaei

compute pressure

parent a10c4b6d
......@@ -29,6 +29,30 @@
}
</script>
<script id="divergenceShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_velocity;
uniform vec2 u_textureSize;
uniform float u_halfReciprocalDx;
void main() {
vec2 fragCoord = gl_FragCoord.xy;
//finite difference formulation of divergence
float n = texture2D(u_velocity, (fragCoord+vec2(0.0, 1.0))/u_textureSize).y;
float s = texture2D(u_velocity, (fragCoord+vec2(0.0, -1.0))/u_textureSize).y;
float e = texture2D(u_velocity, (fragCoord+vec2(1.0, 0.0))/u_textureSize).x;
float w = texture2D(u_velocity, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).x;
gl_FragColor = vec4(u_halfReciprocalDx*(e-w + n-s), 0, 0, 0);
}
</script>
<script id="forceShader" type="x-shader/x-fragment">
precision mediump float;
......@@ -59,10 +83,11 @@
}
</script>
<script id="diffuseShader" type="x-shader/x-fragment">
<script id="jacobiShader" type="x-shader/x-fragment">
precision mediump float;
uniform sampler2D u_material;
uniform sampler2D u_b;
uniform sampler2D u_x;
uniform vec2 u_textureSize;
......@@ -73,20 +98,20 @@
vec2 fragCoord = gl_FragCoord.xy;
vec2 currentState = texture2D(u_material, fragCoord/u_textureSize).xy;
vec2 currentState = texture2D(u_b, fragCoord/u_textureSize).xy;
//implicitly solve diffusion via jacobi iteration
vec2 n = texture2D(u_material, (fragCoord+vec2(0.0, 1.0))/u_textureSize).xy;
vec2 s = texture2D(u_material, (fragCoord+vec2(0.0, -1.0))/u_textureSize).xy;
vec2 e = texture2D(u_material, (fragCoord+vec2(1.0, 0.0))/u_textureSize).xy;
vec2 w = texture2D(u_material, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).xy;
vec2 n = texture2D(u_x, (fragCoord+vec2(0.0, 1.0))/u_textureSize).xy;
vec2 s = texture2D(u_x, (fragCoord+vec2(0.0, -1.0))/u_textureSize).xy;
vec2 e = texture2D(u_x, (fragCoord+vec2(1.0, 0.0))/u_textureSize).xy;
vec2 w = texture2D(u_x, (fragCoord+vec2(-1.0, 0.0))/u_textureSize).xy;
vec2 nextState = (n + s + e + w + u_alpha * currentState) * u_reciprocalBeta;
gl_FragColor = vec4(nextState, 0, 0);
}
</script>
</script>
<script id="advectShader" type="x-shader/x-fragment">
precision mediump float;
......
......@@ -46,17 +46,23 @@ function initGL() {
GPU.setUniformForProgram("advect", "u_velocity", 0, "1i");
GPU.setUniformForProgram("advect", "u_material", 1, "1i");
GPU.createProgram("diverge", "2d-vertex-shader", "divergenceShader");
GPU.setUniformForProgram("diverge" ,"u_textureSize", [width, height], "2f");
GPU.setUniformForProgram("diverge", "u_halfReciprocalDx", 1/(2*dx), "1f");
GPU.setUniformForProgram("diverge", "u_velocity", 0, "1i");
GPU.createProgram("force", "2d-vertex-shader", "forceShader");
GPU.setUniformForProgram("force" ,"u_textureSize", [width, height], "2f");
GPU.setUniformForProgram("force", "u_dt", dt, "1f");
GPU.setUniformForProgram("force", "u_velocity", 0, "1i");
GPU.createProgram("diffuse", "2d-vertex-shader", "diffuseShader");
GPU.setUniformForProgram("diffuse" ,"u_textureSize", [width, height], "2f");
GPU.createProgram("jacobi", "2d-vertex-shader", "jacobiShader");
GPU.setUniformForProgram("jacobi" ,"u_textureSize", [width, height], "2f");
var alpha = dx*dx/(nu*dt);
GPU.setUniformForProgram("diffuse", "u_alpha", alpha, "1f");
GPU.setUniformForProgram("diffuse", "u_reciprocalBeta", 1/(4+alpha), "1f");
GPU.setUniformForProgram("diffuse", "u_material", 0, "1i");
GPU.setUniformForProgram("jacobi", "u_alpha", alpha, "1f");
GPU.setUniformForProgram("jacobi", "u_reciprocalBeta", 1/(4+alpha), "1f");
GPU.setUniformForProgram("jacobi", "u_b", 0, "1i");
GPU.setUniformForProgram("jacobi", "u_x", 1, "1i");
GPU.createProgram("render", "2d-vertex-shader", "2d-render-shader");
GPU.setUniformForProgram("render" ,"u_textureSize", [width, height], "2f");
......@@ -83,8 +89,8 @@ function render(){
GPU.step("advect", ["velocity", "velocity"], "nextVelocity");//advect velocity
GPU.swapTextures("velocity", "nextVelocity");
for (var i=0;i<1;i++){
GPU.step("diffuse", ["velocity"], "nextVelocity");//diffuse velocity
GPU.step("diffuse", ["nextVelocity"], "velocity");//diffuse velocity
GPU.step("jacobi", ["velocity", "velocity"], "nextVelocity");//diffuse velocity
GPU.step("jacobi", ["nextVelocity", "nextVelocity"], "velocity");//diffuse velocity
}
GPU.setProgram("force");
if (mouseEnable){
......@@ -98,6 +104,14 @@ function render(){
GPU.step("force", ["velocity"], "nextVelocity");
GPU.swapTextures("velocity", "nextVelocity");
//compute pressure
GPU.step("diverge", ["velocity"], "velocityDivergence");//calc velocity divergence
for (var i=0;i<3;i++){
GPU.step("jacobi", ["velocityDivergence", "pressure"], "nextPressure");//diffuse velocity
GPU.step("jacobi", ["velocityDivergence", "nextPressure"], "pressure");//diffuse velocity
}
//subtract pressure gradient
// GPU.step("diffuse", ["material"], "nextMaterial");
GPU.step("advect", ["velocity", "material"], "nextMaterial");
......@@ -130,17 +144,18 @@ function resetWindow(){
GPU.setSize(width, height);
var velocity = new Float32Array(width*height*4);
for (var i=0;i<height;i++){
for (var j=0;j<width;j++){
var index = 4*(i*width+j);
// velocity[index] = i/1000;
// velocity[index+1] = j/1000;
}
}
GPU.initTextureFromData("velocity", width, height, "FLOAT", velocity, true);
GPU.initFrameBufferForTexture("velocity");
GPU.initTextureFromData("nextVelocity", width, height, "FLOAT", new Float32Array(width*height*4), true);
GPU.initFrameBufferForTexture("nextVelocity");
GPU.initTextureFromData("velocityDivergence", width, height, "FLOAT", new Float32Array(width*height*4), true);
GPU.initFrameBufferForTexture("velocityDivergence");
GPU.initTextureFromData("pressure", width, height, "FLOAT", new Float32Array(width*height*4), true);
GPU.initFrameBufferForTexture("pressure");
GPU.initTextureFromData("nextPressure", width, height, "FLOAT", new Float32Array(width*height*4), true);
GPU.initFrameBufferForTexture("nextPressure");
var material = new Float32Array(width*height*4);
for (var i=0;i<height;i++){
for (var j=0;j<width;j++){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment