From 21358b25fcb9959a95370f330eeadcf75b4ca0be Mon Sep 17 00:00:00 2001 From: amandaghassaei <amandaghassaei@gmail.com> Date: Sun, 2 Apr 2017 16:43:59 -0400 Subject: [PATCH] debugging --- index.html | 26 ++++++++++++++------------ main.js | 35 +++++++++++++++++++++++++++-------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/index.html b/index.html index 97fc66f..7dbadeb 100644 --- a/index.html +++ b/index.html @@ -20,35 +20,37 @@ <script id="2d-render-shader" type="x-shader/x-fragment"> precision mediump float; - //uniform sampler2D u_velocities; - //uniform vec2 u_textureSize; + uniform sampler2D u_velocity; + uniform vec2 u_textureSize; void main() { - gl_FragColor = vec4(1, 0, 1, 1); + vec2 fragCoord = gl_FragCoord.xy; + vec2 currentVelocity = texture2D(u_velocity, fragCoord/u_textureSize).xy; + gl_FragColor = vec4(currentVelocity.x/4.0, 0, 1, 1); } </script> <script id="advectShader" type="x-shader/x-fragment"> precision mediump float; - //uniform sampler2D u_velocities; - //uniform sampler2D u_material; + uniform sampler2D u_velocity; + uniform sampler2D u_material; - //uniform vec2 u_textureSize; + uniform vec2 u_textureSize; - //uniform float u_dt; + uniform float u_dt; void main() { - //vec2 fragCoord = gl_FragCoord.xy; + vec2 fragCoord = gl_FragCoord.xy; - //vec2 currentVelocity = texture2D(u_velocities, fragCoord/u_textureSize).xy; + vec2 currentVelocity = texture2D(u_velocity, fragCoord/u_textureSize).xy; //implicitly solve advection - //vec2 pos = fragCoord - u_dt * currentVelocity; + vec2 pos = fragCoord - u_dt * currentVelocity; - // bilinear interp between nearest cells - //gl_FragColor = texture2D(u_material, pos/u_textureSize); + // todo bilinear interp between nearest cells + gl_FragColor = texture2D(u_material, pos/u_textureSize); gl_FragColor = vec4(1,1,1,1); } </script> diff --git a/main.js b/main.js index dabcad1..5d6f13a 100755 --- a/main.js +++ b/main.js @@ -31,13 +31,23 @@ function initGL() { GPU = initGPUMath(); + canvas.width = canvas.clientWidth; + canvas.height = canvas.clientHeight; + width = canvas.clientWidth; + height = canvas.clientHeight; + // setup a GLSL programs GPU.createProgram("advect", "2d-vertex-shader", "advectShader"); + GPU.setUniformForProgram("advect" ,"u_textureSize", [width, height], "2f"); + GPU.setUniformForProgram("advect", "u_velocity", 0, "1i"); + GPU.setUniformForProgram("advect", "u_material", 1, "1i"); + GPU.createProgram("render", "2d-vertex-shader", "2d-render-shader"); + GPU.setUniformForProgram("render" ,"u_textureSize", [width, height], "2f"); + GPU.setUniformForProgram("render", "u_velocity", 0, "1i"); resetWindow(); - GPU.initFrameBufferForTexture("velocities"); render(); } @@ -53,7 +63,9 @@ function render(){ // gl.uniform2f(mouseCoordLocation, mouseCoordinates[0], mouseCoordinates[1]); // } else gl.uniform1f(mouseEnableLocation, 0); - GPU.step("render", []); + GPU.step("advect", ["velocity", "material"], "advectedMaterial"); + + GPU.step("render", ["velocity"]); } else resetWindow(); @@ -79,12 +91,19 @@ function resetWindow(){ GPU.setSize(width, height); - - GPU.setUniformForProgram("advect" ,"u_textureSize", [width, height], "2f"); - - - var velocities = new Float32Array(width*height*4); - GPU.initTextureFromData("velocities", width, height, "FLOAT", velocities, true); + 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/100; + } + } + GPU.initTextureFromData("velocity", width, height, "FLOAT", velocity, true); + var material = new Float32Array(width*height*4); + GPU.initTextureFromData("material", width, height, "FLOAT", material, true); + GPU.initFrameBufferForTexture("material"); + GPU.initTextureFromData("advectedMaterial", width, height, "FLOAT", new Float32Array(width*height*4), true); + GPU.initFrameBufferForTexture("advectedMaterial"); paused = false; } -- GitLab