Skip to content
Snippets Groups Projects
main.js 2.25 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
    
    var mouseCoordLocation;
    var mouseCoordinates =  [null, null];
    var mouseEnableLocation;
    
    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 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
        // setup a GLSL programs
        GPU.createProgram("advect", "2d-vertex-shader", "advectShader");
        GPU.createProgram("render", "2d-vertex-shader", "2d-render-shader");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        resetWindow();
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.initFrameBufferForTexture("velocities");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        render();
    }
    
    function render(){
    
        if (!paused) {
    
    
    
    
    amandaghassaei's avatar
    amandaghassaei committed
            // if (mouseEnable){
            //     gl.uniform1f(mouseEnableLocation, 1);
            //     gl.uniform2f(mouseCoordLocation, mouseCoordinates[0], mouseCoordinates[1]);
            // } else gl.uniform1f(mouseEnableLocation, 0);
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
            GPU.step("render", []);
    
    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(){
        canvas.width = canvas.clientWidth;
        canvas.height = canvas.clientHeight;
        width = canvas.clientWidth;
        height = canvas.clientHeight;
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setSize(width, height);
    
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        GPU.setUniformForProgram("advect" ,"u_textureSize", [width, height], "2f");
    
    amandaghassaei's avatar
    amandaghassaei committed
    
    
    
    amandaghassaei's avatar
    amandaghassaei committed
        var velocities = new Float32Array(width*height*4);
        GPU.initTextureFromData("velocities", width, height, "FLOAT", velocities, true);
    
    amandaghassaei's avatar
    amandaghassaei committed
    
        paused = false;
    }
    
    function onMouseMove(e){
        mouseCoordinates = [e.clientX, height-e.clientY];
    }
    
    function onMouseDown(e){
        // gl.useProgram(stepProgram);
    
    amandaghassaei's avatar
    amandaghassaei committed
        mouseEnable = true;
    
    amandaghassaei's avatar
    amandaghassaei committed
        mouseCoordinates = [e.clientX, height-e.clientY];
    }
    
    function onMouseUp(){
    
    amandaghassaei's avatar
    amandaghassaei committed
        mouseEnable = false;
    
    amandaghassaei's avatar
    amandaghassaei committed
    }