<commit_before><!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 <NAME>

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <title>PicoGL.js: Render to Texture</title>
    <script src="utils/gl-matrix.js"></script>
    <script src="../build/picogl.js"></script>
    <script src="utils/utils.js"></script>
    <link rel="stylesheet" href="../site/css/picogl-example.css"> 
</head>
<body>
    <div id="example-title">
        PicoGL.js Example: Render to Texture
        <div>
            <a href="https://github.com/tsherif/picogl.js/blob/master/examples/rtt.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="shader/vs" id="vs">
        #version 300 es
        
        layout(location=0) in vec4 position;
        layout(location=1) in vec3 color;
        
        out vec3 vColor; 
        void main() {
            vColor = color;
            gl_Position = position;
        }
    </script>
    <script type="shader/fs" id="fs">
        #version 300 es
        precision highp float;
        
        in vec3 vColor;
        
        layout(location=0) out vec4 fragColor;
        layout(location=1) out vec4 fragColor1;
        layout(location=2) out vec4 fragColor2;
        layout(location=3) out vec4 fragColor3;
        layout(location=4) out vec4 fragColor4;
        layout(location=5) out vec4 fragColor5;
        void main() {
            fragColor = vec4(vColor, 1.0);
            fragColor1 = vec4(vColor, 1.0);
            fragColor2 = vec4(vColor, 1.0);
            fragColor3 = vec4(vColor, 1.0);
            fragColor4 = vec4(vColor, 1.0);
            fragColor5 = vec4(vColor, 1.0);
        }
    </script>
    <script type="x-shader/vs" id="vertex-draw">
        #version 300 es
        
        layout(std140, column_major) uniform;

        layout(location=0) in vec4 position;
        layout(location=1) in vec2 uv;
        layout(location=2) in vec4 normal;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform mat4 uModel;
        
        out vec3 vModelPosition;
        out vec3 vPosition;
        out vec2 vUV;
        out vec3 vNormal;
        void main() {
            vModelPosition = position.xyz;
            vec4 worldPosition = uModel * position;
            vPosition = worldPosition.xyz;
            vUV = uv;
            vNormal = (uModel * normal).xyz;
            gl_Position = uViewProj * worldPosition;
        }
    </script>
    <script type="x-shader/vf" id="fragment-draw">
        #version 300 es
        precision highp float;

        layout(std140, column_major) uniform;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform samplerCube tex;
        
        in vec3 vModelPosition;
        in vec3 vPosition;
        in vec2 vUV;
        in vec3 vNormal;

        layout(location=0) out vec4 fragColor;
        layout(location=1) out vec4 fragColor1;
        layout(location=2) out vec4 fragColor2;
        layout(location=3) out vec4 fragColor3;
        layout(location=4) out vec4 fragColor4;
        layout(location=5) out vec4 fragColor5;

        void main() {
            vec3 color = texture(tex, vModelPosition).rgb;

            vec3 normal = normalize(vNormal);
            vec3 eyeVec = normalize(uEyePosition.xyz - vPosition);
            vec3 incidentVec = normalize(vPosition - uLightPosition.xyz);
            vec3 lightVec = -incidentVec;
            float diffuse = max(dot(lightVec, normal), 0.0);
            float highlight = pow(max(dot(eyeVec, reflect(incidentVec, normal)), 0.0), 100.0);
            float ambient = 0.1;
            fragColor = vec4(color * (diffuse + highlight + ambient), 1.0);
            fragColor1 = fragColor;
            fragColor2 = fragColor;
            fragColor3 = fragColor;
            fragColor4 = fragColor;
            fragColor5 = fragColor;

        }
    </script>
    <script type="text/javascript">
        utils.addTimerElement();

        var canvas = document.getElementById("gl-canvas");

        if (!utils.testWebGL2()) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas)
        .depthTest();

        var timer = app.createTimer();

        // FRAMEBUFFER
        var colorTarget = app.createCubemap({
            width: 512,
            height: 512
        });
        var depthTarget = app.createTexture2D(512, 512, {
            format: PicoGL.DEPTH_COMPONENT
        });

        var rttBuffer = app.createFramebuffer()
        .colorTarget(0, colorTarget, PicoGL.TEXTURE_CUBE_MAP_NEGATIVE_X)
        .colorTarget(1, colorTarget, PicoGL.TEXTURE_CUBE_MAP_POSITIVE_X)
        .colorTarget(2, colorTarget, PicoGL.TEXTURE_CUBE_MAP_NEGATIVE_Y)
        .colorTarget(3, colorTarget, PicoGL.TEXTURE_CUBE_MAP_POSITIVE_Y)
        .colorTarget(4, colorTarget, PicoGL.TEXTURE_CUBE_MAP_NEGATIVE_Z)
        .colorTarget(5, colorTarget, PicoGL.TEXTURE_CUBE_MAP_POSITIVE_Z)
        .depthTarget(depthTarget);

        // GEOMETRY
        var box = utils.createBox({dimensions: [1.0, 1.0, 1.0]})
        var positions = app.createVertexBuffer(PicoGL.FLOAT, 3, box.positions);
        var uv = app.createVertexBuffer(PicoGL.FLOAT, 2, box.uvs);
        var normals = app.createVertexBuffer(PicoGL.FLOAT, 3, box.normals);

        var boxArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions)
        .vertexAttributeBuffer(1, uv)
        .vertexAttributeBuffer(2, normals);

        // UNIFORMS
        var projMatrix = mat4.create();
        mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 10.0);

        var viewMatrix = mat4.create();
        var eyePosition = vec3.fromValues(1, 1, 1);
        mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var viewProjMatrix = mat4.create();
        mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

        var lightPosition = vec3.fromValues(1, 1, 0.5);

        // UNIFORM BUFFER
        var sceneUniformBuffer = app.createUniformBuffer([
            PicoGL.FLOAT_MAT4,
            PicoGL.FLOAT_VEC4,
            PicoGL.FLOAT_VEC4
        ])
        .set(0, viewProjMatrix)
        .set(1, eyePosition)
        .set(2, lightPosition)
        .update();

        var modelMatrix = mat4.create();
        var rotateXMatrix = mat4.create();
        var rotateYMatrix = mat4.create();

        window.onresize = function() {
            app.resize(window.innerWidth, window.innerHeight);
            rttBuffer.resize();

            mat4.perspective(projMatrix, Math.PI / 2, app.width / app.height, 0.1, 10.0);
            mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

            sceneUniformBuffer.set(0, viewProjMatrix).update();
        };

        var image = new Image();

        image.onload = function() {
            var cubemap = app.createCubemap({
                negX: image,
                posX: image,
                negY: image,
                posY: image,
                negZ: image,
                posZ: image,
                flipY: false
            });

            // PROGRAM
            var vsSource =  document.getElementById("vertex-draw").text.trim();
            var fsSource =  document.getElementById("fragment-draw").text.trim();
            var program = app.createProgram(vsSource, fsSource);

            var drawCall2 = app.createDrawCall(program, boxArray)
            .uniformBlock("SceneUniforms", sceneUniformBuffer);

            var angleX = 0;
            var angleY = 0;

            function draw() {
                if (timer.ready()) {
                    utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
                }

                timer.start();

                angleX += 0.01;
                angleY += 0.02;

                mat4.fromXRotation(rotateXMatrix, angleX);
                mat4.fromYRotation(rotateYMatrix, angleY);
                mat4.multiply(modelMatrix, rotateXMatrix, rotateYMatrix);

                drawCall2.uniform("uModel", modelMatrix);

                // RENDER TO OFFSCREEN TEXTURE
                app
                .drawFramebuffer(rttBuffer)
                .viewport(0, 0, 512, 512)
                .clearColor(0.4, 0.4, 0.4, 1.0).clear();
                drawCall2.texture("tex", cubemap).draw();
                
                // RENDER TO SCREEN
                app.defaultDrawFramebuffer().defaultViewport().clearColor(0.0, 0.0, 0.0, 1.0).clear()
                drawCall2.texture("tex", colorTarget).draw();

                timer.end(); 

                requestAnimationFrame(draw);
            }

            requestAnimationFrame(draw);
            
        }

        image.src = "img/khronos_webgl.png";
    </script>
    <a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <script src="../site/js/iframe.js"></script>
</body>
</html>
<commit_msg>Update render to cubemap<commit_after><!DOCTYPE html>
<!--
  The MIT License (MIT)

  Copyright (c) 2017 <NAME>

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<html>
<head>
    <title>PicoGL.js: Render to Texture</title>
    <script src="utils/gl-matrix.js"></script>
    <script src="../build/picogl.js"></script>
    <script src="utils/utils.js"></script>
    <link rel="stylesheet" href="../site/css/picogl-example.css"> 
</head>
<body>
    <div id="example-title">
        PicoGL.js Example: Render to Texture
        <div>
            <a href="https://github.com/tsherif/picogl.js/blob/master/examples/rtt.html">Source code</a>
        </div>
    </div>
    <canvas id="gl-canvas"></canvas>
    <script type="x-shader/vs" id="vertex-skybox">
        #version 300 es
        
        layout(std140, column_major) uniform;

        layout(location=0) in vec4 position;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
        };
        
        out vec3 vPosition;
        void main() {
            vec4 worldPosition = vec4(position.xyz * 5.0, 1.0);            
            vPosition = worldPosition.xyz;
            gl_Position = uViewProj * worldPosition;
        }
    </script>
    <script type="x-shader/vf" id="fragment-skybox">
        #version 300 es
        precision highp float;
        
        layout(std140, column_major) uniform;

        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
        };

        uniform samplerCube cubemap;
        
        in vec3 vPosition;
        out vec4 fragColor;
        void main() {
            vec3 viewDir = vPosition - uEyePosition.xyz;
            vec3 color = texture(cubemap, normalize(viewDir)).rgb;
            fragColor = vec4(color, 1.0);
        }
    </script>
    <script type="x-shader/vs" id="vertex-draw">
        #version 300 es
        
        layout(std140, column_major) uniform;

        layout(location=0) in vec4 position;
        layout(location=1) in vec2 uv;
        layout(location=2) in vec4 normal;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform mat4 uModel;
        
        out vec3 vModelPosition;
        out vec3 vPosition;
        out vec2 vUV;
        out vec3 vNormal;
        void main() {
            vModelPosition = position.xyz;
            vec4 worldPosition = uModel * position;
            vPosition = worldPosition.xyz;
            vUV = uv;
            vNormal = (uModel * normal).xyz;
            gl_Position = uViewProj * worldPosition;
        }
    </script>
    <script type="x-shader/vf" id="fragment-draw">
        #version 300 es
        precision highp float;

        layout(std140, column_major) uniform;
        
        uniform SceneUniforms {
            mat4 uViewProj;
            vec4 uEyePosition;
            vec4 uLightPosition;
        };

        uniform samplerCube cubemap;
        
        in vec3 vModelPosition;
        in vec3 vPosition;
        in vec2 vUV;
        in vec3 vNormal;

        layout(location=0) out vec4 fragColor;
        layout(location=1) out vec4 fragColor1;
        layout(location=2) out vec4 fragColor2;
        layout(location=3) out vec4 fragColor3;
        layout(location=4) out vec4 fragColor4;
        layout(location=5) out vec4 fragColor5;

        void main() {
            vec3 normal = normalize(vNormal);
            vec3 viewVec = normalize(vPosition - uEyePosition.xyz);
            fragColor = texture(cubemap, reflect(viewVec, normal)) + vec4(0.1, 0.1, 0.1, 0.0);
            fragColor1 = fragColor;
            fragColor2 = fragColor;
            fragColor3 = fragColor;
            fragColor4 = fragColor;
            fragColor5 = fragColor;

        }
    </script>
    <script type="text/javascript">
        utils.addTimerElement();

        var canvas = document.getElementById("gl-canvas");

        if (!utils.testWebGL2()) {
            console.error("WebGL 2 not available");
            document.body.innerHTML = "This example requires WebGL 2 which is unavailable on this system."
        }

        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        
        var app = PicoGL.createApp(canvas)
        .clearColor(0.0, 0.0, 0.0, 1.0)
        .depthTest();

        var timer = app.createTimer();

        // FRAMEBUFFER
        var colorTarget = app.createCubemap({
            width: 2048,
            height: 2048
        });
        var depthTarget = app.createTexture2D(2048, 2048, {
            format: PicoGL.DEPTH_COMPONENT
        });

        var rttBuffer = app.createFramebuffer()
        .colorTarget(0, colorTarget, PicoGL.TEXTURE_CUBE_MAP_NEGATIVE_X)
        .colorTarget(1, colorTarget, PicoGL.TEXTURE_CUBE_MAP_POSITIVE_X)
        .colorTarget(2, colorTarget, PicoGL.TEXTURE_CUBE_MAP_NEGATIVE_Y)
        .colorTarget(3, colorTarget, PicoGL.TEXTURE_CUBE_MAP_POSITIVE_Y)
        .colorTarget(4, colorTarget, PicoGL.TEXTURE_CUBE_MAP_NEGATIVE_Z)
        .colorTarget(5, colorTarget, PicoGL.TEXTURE_CUBE_MAP_POSITIVE_Z)
        .depthTarget(depthTarget);

        // GEOMETRY
        var box = utils.createBox({dimensions: [1.0, 1.0, 1.0]})
        var positions = app.createVertexBuffer(PicoGL.FLOAT, 3, box.positions);
        var uv = app.createVertexBuffer(PicoGL.FLOAT, 2, box.uvs);
        var normals = app.createVertexBuffer(PicoGL.FLOAT, 3, box.normals);

        var boxArray = app.createVertexArray()
        .vertexAttributeBuffer(0, positions)
        .vertexAttributeBuffer(1, uv)
        .vertexAttributeBuffer(2, normals);

        // UNIFORMS
        var projMatrix = mat4.create();
        mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 10.0);

        var viewMatrix = mat4.create();
        var eyePosition = vec3.fromValues(1, 1, 1);
        mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0, 0), vec3.fromValues(0, 1, 0));

        var viewProjMatrix = mat4.create();
        mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

        var lightPosition = vec3.fromValues(1, 1, 0.5);

        var skyboxViewMatrix = mat4.create();
        skyboxViewMatrix.set(viewMatrix);
        skyboxViewMatrix[12] = 0;
        skyboxViewMatrix[13] = 0;
        skyboxViewMatrix[14] = 0;

        var skyboxViewProjMatrix = mat4.create();
        mat4.multiply(skyboxViewProjMatrix, projMatrix, skyboxViewMatrix);

        var lightPosition = vec3.fromValues(1, 1, 0.5);

        // UNIFORM BUFFER
        var sceneUniformBuffer = app.createUniformBuffer([
            PicoGL.FLOAT_MAT4,
            PicoGL.FLOAT_VEC4,
            PicoGL.FLOAT_VEC4
        ])
        .set(0, viewProjMatrix)
        .set(1, eyePosition)
        .set(2, lightPosition)
        .update();

        var skyboxSceneUniforms = app.createUniformBuffer([
            PicoGL.FLOAT_MAT4,
            PicoGL.FLOAT_VEC4
        ])
        .set(0, skyboxViewProjMatrix)
        .set(1, eyePosition)
        .update();

        var modelMatrix = mat4.create();
        var rotateXMatrix = mat4.create();
        var rotateYMatrix = mat4.create();

        window.onresize = function() {
            app.resize(window.innerWidth, window.innerHeight);
            rttBuffer.resize();

            mat4.perspective(projMatrix, Math.PI / 2, app.width / app.height, 0.1, 10.0);
            mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);

            sceneUniformBuffer.set(0, viewProjMatrix).update();
        };

        var image = new Image();

        image.onload = function() {
            var color = new Uint8Array([255, 0, 0, 255]);
            var cubemap = app.createCubemap({
                negX: color,
                posX: color,
                negY: color,
                posY: color,
                negZ: color,
                posZ: color,
                width: 1,
                height: 1
            });

            // PROGRAM
            var vsSource =  document.getElementById("vertex-draw").text.trim();
            var fsSource =  document.getElementById("fragment-draw").text.trim();
            var program = app.createProgram(vsSource, fsSource);

            var skyboxVsSource =  document.getElementById("vertex-skybox").text.trim();
            var skyboxFsSource =  document.getElementById("fragment-skybox").text.trim();
            var skyboxProgram = app.createProgram(skyboxVsSource, skyboxFsSource);

            var drawCall2 = app.createDrawCall(program, boxArray)
            .uniformBlock("SceneUniforms", sceneUniformBuffer);

            var skyboxDrawcall = app.createDrawCall(skyboxProgram, boxArray)
            .uniformBlock("SceneUniforms", skyboxSceneUniforms)
            .texture("cubemap", colorTarget);

            var angleX = 0;
            var angleY = 0;

            function draw() {
                if (timer.ready()) {
                    utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
                }

                timer.start();

                angleX += 0.01;
                angleY += 0.02;

                mat4.fromXRotation(rotateXMatrix, angleX);
                mat4.fromYRotation(rotateYMatrix, angleY);
                mat4.multiply(modelMatrix, rotateXMatrix, rotateYMatrix);

                drawCall2.uniform("uModel", modelMatrix);

                // RENDER TO OFFSCREEN TEXTURE
                app
                .drawFramebuffer(rttBuffer)
                .viewport(0, 0, 2048, 2048)
                .clear();
                drawCall2.texture("cubemap", cubemap).draw();
                
                // RENDER TO SCREEN
                app.defaultDrawFramebuffer().defaultViewport().clear();
                skyboxDrawcall.draw();
                drawCall2.texture("cubemap", colorTarget).draw();

                timer.end(); 

                requestAnimationFrame(draw);
            }

            requestAnimationFrame(draw);
            
        }

        image.src = "img/khronos_webgl.png";
    </script>
    <a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
    <script src="../site/js/iframe.js"></script>
</body>
</html>

{'fraction_non_alphanumeric': 0.10271795101419424, 'fraction_numerical': 0.035687099807459814, 'mean_word_length': 2.0784286698828396, 'pattern_counts': {'":': 0, '<': 85, '<?xml version=': 0, '>': 85, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5301263', 'n_tokens_mistral': 7175, 'n_tokens_neox': 6545, 'n_words': 1547}