Skip to content
Snippets Groups Projects
main.js 6.48 KiB
Newer Older
  • Learn to ignore specific revisions
  • amandaghassaei's avatar
    amandaghassaei committed
    //used a lot of ideas from https://bl.ocks.org/robinhouston/ed597847175cf692ecce to clean this code up
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    var width, height;
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    var lastMouseCoordinates =  [0,0];
    var mouseCoordinates =  [0,0];
    
    amandaghassaei's avatar
    amandaghassaei committed
    var mouseEnable = false;
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    var paused = false;//while window is resizing
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    var dt = 1;
    var dx = 1;
    var nu = 1;//viscosity
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    var GPU;
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    window.onload = initGL;
    
    function initGL() {
    
        $("#about").click(function(e){
            e.preventDefault();
            $("#aboutModal").modal('show');
        });
    
        canvas = document.getElementById("glcanvas");
    
        canvas.onmousemove = onMouseMove;
        canvas.onmousedown = onMouseDown;
    
    amandaghassaei's avatar
    amandaghassaei committed
        canvas.onmouseup = onMouseUp;
    
    amandaghassaei's avatar
    amandaghassaei committed
        canvas.onmouseout = onMouseUp;
    
        window.onresize = onResize;
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU = initGPUMath();
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        canvas.width = canvas.clientWidth;
        canvas.height = canvas.clientHeight;
        width = canvas.clientWidth;
        height = canvas.clientHeight;
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        // setup a GLSL programs
        GPU.createProgram("advect", "2d-vertex-shader", "advectShader");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setUniformForProgram("advect" ,"u_textureSize", [width, height], "2f");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setUniformForProgram("advect", "u_dt", dt, "1f");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setUniformForProgram("advect", "u_velocity", 0, "1i");
        GPU.setUniformForProgram("advect", "u_material", 1, "1i");
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        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");
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        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");
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.createProgram("jacobi", "2d-vertex-shader", "jacobiShader");
        GPU.setUniformForProgram("jacobi" ,"u_textureSize", [width, height], "2f");
    
    amandaghassaei's avatar
    amandaghassaei committed
        var alpha = dx*dx/(nu*dt);
    
    amandaghassaei's avatar
    amandaghassaei committed
        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");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.createProgram("render", "2d-vertex-shader", "2d-render-shader");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setUniformForProgram("render" ,"u_textureSize", [width, height], "2f");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setUniformForProgram("render", "u_material", 0, "1i");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        resetWindow();
    
    
        render();
    }
    
    function render(){
    
        if (!paused) {
    
    
    amandaghassaei's avatar
    amandaghassaei committed
            // Apply the first 3 operators in Equation 12.
            // u = advect(u);
            // u = diffuse(u);
            // u = addForces(u);
            // // Now apply the projection operator to the result.
            // p = computePressure(u);
            // u = subtractPressureGradient(u, p);
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
            GPU.step("advect", ["velocity", "velocity"], "nextVelocity");//advect velocity
            GPU.swapTextures("velocity", "nextVelocity");
    
    amandaghassaei's avatar
    amandaghassaei committed
            for (var i=0;i<1;i++){
    
    amandaghassaei's avatar
    amandaghassaei committed
                GPU.step("jacobi", ["velocity", "velocity"], "nextVelocity");//diffuse velocity
                GPU.step("jacobi", ["nextVelocity", "nextVelocity"], "velocity");//diffuse velocity
    
    amandaghassaei's avatar
    amandaghassaei committed
            }
    
    amandaghassaei's avatar
    amandaghassaei committed
            GPU.setProgram("force");
            if (mouseEnable){
                GPU.setUniformForProgram("force", "u_mouseEnable", 1.0, "1f");
                GPU.setUniformForProgram("force", "u_mouseCoord", [mouseCoordinates[0], mouseCoordinates[1]], "2f");
                GPU.setUniformForProgram("force", "u_mouseDir", [mouseCoordinates[0]-lastMouseCoordinates[0],
                    mouseCoordinates[1]-lastMouseCoordinates[1]], "2f");
            } else {
                GPU.setUniformForProgram("force", "u_mouseEnable", 0.0, "1f");
            }
            GPU.step("force", ["velocity"], "nextVelocity");
            GPU.swapTextures("velocity", "nextVelocity");
    
    
    amandaghassaei's avatar
    amandaghassaei committed
            //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
    
    amandaghassaei's avatar
    amandaghassaei committed
            // GPU.step("diffuse", ["material"], "nextMaterial");
            GPU.step("advect", ["velocity", "material"], "nextMaterial");
    
    amandaghassaei's avatar
    amandaghassaei committed
            // GPU.step("force", ["material"], "nextMaterial");
    
    amandaghassaei's avatar
    amandaghassaei committed
            GPU.step("render", ["nextMaterial"]);
            GPU.swapTextures("nextMaterial", "material");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        } else resetWindow();
    
        window.requestAnimationFrame(render);
    }
    
    function step(i){
        gl.bindFramebuffer(gl.FRAMEBUFFER, frameBuffers[(i+1)%2]);
        gl.bindTexture(gl.TEXTURE_2D, states[i%2]);
    
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4);//draw to framebuffer
    }
    
    function onResize(){
        paused = true;
    }
    
    function resetWindow(){
    
    amandaghassaei's avatar
    amandaghassaei committed
        // canvas.width = canvas.clientWidth;
        // canvas.height = canvas.clientHeight;
        // width = canvas.clientWidth;
        // height = canvas.clientHeight;
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setSize(width, height);
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        var velocity = new Float32Array(width*height*4);
        GPU.initTextureFromData("velocity", width, height, "FLOAT", velocity, true);
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.initFrameBufferForTexture("velocity");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.initTextureFromData("nextVelocity", width, height, "FLOAT", new Float32Array(width*height*4), true);
        GPU.initFrameBufferForTexture("nextVelocity");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        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");
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        var material = new Float32Array(width*height*4);
    
    amandaghassaei's avatar
    amandaghassaei committed
        for (var i=0;i<height;i++){
            for (var j=0;j<width;j++){
                var index = 4*(i*width+j);
                material[index] = Math.random();
            }
        }
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.initTextureFromData("material", width, height, "FLOAT", material, true);
        GPU.initFrameBufferForTexture("material");
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.initTextureFromData("nextMaterial", width, height, "FLOAT", new Float32Array(width*height*4), true);
        GPU.initFrameBufferForTexture("nextMaterial");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        paused = false;
    }
    
    function onMouseMove(e){
    
    amandaghassaei's avatar
    amandaghassaei committed
        lastMouseCoordinates = mouseCoordinates;
    
    amandaghassaei's avatar
    amandaghassaei committed
        mouseCoordinates = [e.clientX, height-e.clientY];
    }
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    function onMouseDown(){
    
    amandaghassaei's avatar
    amandaghassaei committed
        mouseEnable = true;
    
    amandaghassaei's avatar
    amandaghassaei committed
    }
    
    function onMouseUp(){
    
    amandaghassaei's avatar
    amandaghassaei committed
        mouseEnable = false;
    
    amandaghassaei's avatar
    amandaghassaei committed
    }