diff --git a/index.html b/index.html index 3566b54d04406a85864b4b609b9c3f3d87cb524b..ba3879670acde61e391dc7ea58221b02dcaa4f13 100644 --- a/index.html +++ b/index.html @@ -17,6 +17,28 @@ } </script> + <script id="boundaryShader" type="x-shader/x-fragment"> + precision mediump float; + + uniform sampler2D u_texture; + uniform float u_scale; + uniform vec2 u_textureSize; + + void main() { + vec2 fragCoord = gl_FragCoord.xy; + + vec4 neighborVal; + if (fragCoord.x < 1.0){ + neighborVal = texture2D(u_texture, (fragCoord + vec2(1.0, 0.0))/u_textureSize); + + gl_FragColor = u_scale*neighborVal; + return; + } + + gl_FragColor = texture2D(u_texture, (fragCoord)/u_textureSize); + } + </script> + <script id="2d-render-shader" type="x-shader/x-fragment"> precision mediump float; @@ -204,9 +226,9 @@ <script type="text/javascript" src="dependencies/jquery-3.1.0.min.js"></script> <script type="text/javascript" src="dependencies/flat-ui.min.js"></script> - <script type="text/javascript" src="GLBoilerplate.js"></script> - <script type="text/javascript" src="GPUMath.js"></script> - <script type="text/javascript" src="main.js"></script> + <script type="text/javascript" src="js/GLBoilerplate.js"></script> + <script type="text/javascript" src="js/GPUMath.js"></script> + <script type="text/javascript" src="js/main.js"></script> </head> <body> diff --git a/GlBoilerplate.js b/js/GLBoilerplate.js similarity index 96% rename from GlBoilerplate.js rename to js/GLBoilerplate.js index a8ec137f59ad593a9985bcfc8088838f8ae65a5b..cc2d21b2134c3333854a9d10bcf537ba0af273fa 100644 --- a/GlBoilerplate.js +++ b/js/GLBoilerplate.js @@ -122,7 +122,8 @@ function initBoilerPlate(){ function loadVertexData(gl, program) { gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer()); - gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ -1,-1, 1,-1, -1, 1, 1, 1]), gl.STATIC_DRAW); + var val = 0.9; + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ -val,-val, val,-val, -val, val, val, val, val, val, val, -val, -val, val, -val, -val]), gl.STATIC_DRAW); // look up where the vertex data needs to go. var positionLocation = gl.getAttribLocation(program, "a_position"); diff --git a/GPUMath.js b/js/GPUMath.js similarity index 91% rename from GPUMath.js rename to js/GPUMath.js index 0d045a0ec7b908a6ac23a2b1d4f2f4c39bf55eb7..99c19f8b8fbd5f0bacc108e33984bc839b689aa5 100644 --- a/GPUMath.js +++ b/js/GPUMath.js @@ -110,6 +110,17 @@ function initGPUMath(){ gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);//draw to framebuffer }; + GPUMath.prototype.stepBoundary = function(programName, inputTextures, outputTexture){ + + gl.useProgram(this.programs[programName].program); + gl.bindFramebuffer(gl.FRAMEBUFFER, this.frameBuffers[outputTexture]); + for (var i=0;i<inputTextures.length;i++){ + gl.activeTexture(gl.TEXTURE0 + i); + gl.bindTexture(gl.TEXTURE_2D, this.textures[inputTextures[i]]); + } + gl.drawArrays(gl.LINES, 0, 8);//draw to framebuffer + }; + GPUMath.prototype.swapTextures = function(texture1Name, texture2Name){ var temp = this.textures[texture1Name]; this.textures[texture1Name] = this.textures[texture2Name]; diff --git a/main.js b/js/main.js similarity index 94% rename from main.js rename to js/main.js index e87ba4484999ad384c2832ae6b7975aa7cb4f8e1..559bbe00fca297bb20a1813905f5c7de8b0d7fa8 100755 --- a/main.js +++ b/js/main.js @@ -65,6 +65,9 @@ function initGL() { GPU.createProgram("render", "2d-vertex-shader", "2d-render-shader"); GPU.setUniformForProgram("render", "u_material", 0, "1i"); + GPU.createProgram("boundary", "2d-vertex-shader", "boundaryShader"); + GPU.setUniformForProgram("boundary", "u_texture", 0, "1i"); + resetWindow(); render(); @@ -80,7 +83,10 @@ function render(){ GPU.setUniformForProgram("advect" ,"u_textureSize", [width, height], "2f"); GPU.setUniformForProgram("advect" ,"u_scale", 1, "1f"); GPU.step("advect", ["velocity", "velocity"], "nextVelocity"); - GPU.swapTextures("velocity", "nextVelocity"); + + // GPU.setProgram("boundary"); + // GPU.setUniformForProgram("boundary", "u_scale", -1, "1f"); + // GPU.stepBoundary("boundary", ["nextVelocity"], "velocity"); //diffuse velocity GPU.setProgram("jacobi"); @@ -143,7 +149,7 @@ function resetWindow(){ actualHeight = body.clientHeight; var maxDim = Math.max(actualHeight, actualWidth); - var _scale = maxDim/200; + var _scale = maxDim/150; width = Math.floor(actualWidth/_scale); height = Math.floor(actualHeight/_scale); @@ -168,6 +174,8 @@ function resetWindow(){ GPU.setUniformForProgram("jacobi" ,"u_textureSize", [width, height], "2f"); GPU.setProgram("render"); GPU.setUniformForProgram("render" ,"u_textureSize", [actualWidth, actualHeight], "2f"); + GPU.setProgram("boundary"); + GPU.setUniformForProgram("boundary" ,"u_textureSize", [width, height], "2f"); var velocity = new Float32Array(width*height*4); // for (var i=0;i<height;i++){