import './JS' ; declare var os ; declare var navigator ; navigator = < Navigator > { } ; import WindowEx from './window' ; import Canvas from './canvas' ; declare var _gl ; declare var window ; window = new WindowEx ( ) ; class Runner { private canvas : any ; private engine : any ; private programInfo : any ; private buffers : any ; private c : number ; constructor ( ) { this . canvas = new Canvas ( ) ; this . c = 0.5 ; } create ( ) { const out = new Float32Array ( 16 ) ; out [ 0 ] = 1 ; out [ 1 ] = 0 ; out [ 2 ] = 0 ; out [ 3 ] = 0 ; out [ 4 ] = 0 ; out [ 5 ] = 1 ; out [ 6 ] = 0 ; out [ 7 ] = 0 ; out [ 8 ] = 0 ; out [ 9 ] = 0 ; out [ 10 ] = 1 ; out [ 11 ] = 0 ; out [ 12 ] = 0 ; out [ 13 ] = 0 ; out [ 14 ] = 0 ; out [ 15 ] = 1 ; return out ; } perspective ( out , fovy , aspect , near , far ) { const f = 1.0 / Math . tan ( fovy / 2 ) ; const nf = 1 / ( near - far ) ; out [ 0 ] = f / aspect ; out [ 1 ] = 0 ; out [ 2 ] = 0 ; out [ 3 ] = 0 ; out [ 4 ] = 0 ; out [ 5 ] = f ; out [ 6 ] = 0 ; out [ 7 ] = 0 ; out [ 8 ] = 0 ; out [ 9 ] = 0 ; out [ 10 ] = ( far + near ) * nf ; out [ 11 ] = - 1 ; out [ 12 ] = 0 ; out [ 13 ] = 0 ; out [ 14 ] = 2 * far * near * nf ; out [ 15 ] = 0 ; return out ; } translate ( out , a , v ) { const x = v [ 0 ] , y = v [ 1 ] , z = v [ 2 ] ; let a00 = void 0 , a01 = void 0 , a02 = void 0 , a03 = void 0 ; let a10 = void 0 , a11 = void 0 , a12 = void 0 , a13 = void 0 ; let a20 = void 0 , a21 = void 0 , a22 = void 0 , a23 = void 0 ; if ( a === out ) { out [ 12 ] = a [ 0 ] * x + a [ 4 ] * y + a [ 8 ] * z + a [ 12 ] ; out [ 13 ] = a [ 1 ] * x + a [ 5 ] * y + a [ 9 ] * z + a [ 13 ] ; out [ 14 ] = a [ 2 ] * x + a [ 6 ] * y + a [ 10 ] * z + a [ 14 ] ; out [ 15 ] = a [ 3 ] * x + a [ 7 ] * y + a [ 11 ] * z + a [ 15 ] ; } else { a00 = a [ 0 ] ; a01 = a [ 1 ] ; a02 = a [ 2 ] ; a03 = a [ 3 ] ; a10 = a [ 4 ] ; a11 = a [ 5 ] ; a12 = a [ 6 ] ; a13 = a [ 7 ] ; a20 = a [ 8 ] ; a21 = a [ 9 ] ; a22 = a [ 10 ] ; a23 = a [ 11 ] ; out [ 0 ] = a00 ; out [ 1 ] = a01 ; out [ 2 ] = a02 ; out [ 3 ] = a03 ; out [ 4 ] = a10 ; out [ 5 ] = a11 ; out [ 6 ] = a12 ; out [ 7 ] = a13 ; out [ 8 ] = a20 ; out [ 9 ] = a21 ; out [ 10 ] = a22 ; out [ 11 ] = a23 ; out [ 12 ] = a00 * x + a10 * y + a20 * z + a [ 12 ] ; out [ 13 ] = a01 * x + a11 * y + a21 * z + a [ 13 ] ; out [ 14 ] = a02 * x + a12 * y + a22 * z + a [ 14 ] ; out [ 15 ] = a03 * x + a13 * y + a23 * z + a [ 15 ] ; } return out ; } main ( ) { const vsSource = `
            attribute vec4 aVertexPosition;
            uniform mat4 uModelViewMatrix;
            uniform mat4 uProjectionMatrix;
            void main() {
                gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
            }
            ` ; const fsSource = `
            void main() {
                gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
            }
            ` ; const shaderProgram = this . initShaderProgram ( vsSource , fsSource ) ; this . programInfo = { program : shaderProgram , attribLocations : { vertexPosition : _gl . getAttribLocation ( shaderProgram , 'aVertexPosition' ) , } , uniformLocations : { projectionMatrix : _gl . getUniformLocation ( shaderProgram , 'uProjectionMatrix' ) , modelViewMatrix : _gl . getUniformLocation ( shaderProgram , 'uModelViewMatrix' ) , } , } ; this . buffers = this . initBuffers ( ) ; window . setTimeout ( ( ) => { this . drawScene ( ) ; } , 16 ) ; } initBuffers ( ) { const positionBuffer = _gl . createBuffer ( ) ; _gl . bindBuffer ( _gl . ARRAY_BUFFER , positionBuffer ) ; const positions = [ 1.0 , 1.0 , - 1.0 , 1.0 , 1.0 , - 1.0 , - 1.0 , - 1.0 , ] ; _gl . bufferData ( _gl . ARRAY_BUFFER , ( < any > ( new Float32Array ( positions ) ) ) . buffer . bufferNativeInstance , _gl . STATIC_DRAW ) ; return { position : positionBuffer , } ; } drawScene ( ) { const programInfo = this . programInfo ; const buffers = this . buffers ; if ( this . c > 1.0 ) { this . c = 0.0 ; } else { this . c += 0.1 ; } _gl . clearColor ( this . c , 0.0 , 0.0 , 1.0 ) ; _gl . clearDepth ( 1.0 ) ; _gl . enable ( _gl . DEPTH_TEST ) ; _gl . depthFunc ( _gl . LEQUAL ) ; _gl . clear ( _gl . COLOR_BUFFER_BIT | _gl . DEPTH_BUFFER_BIT ) ; const fieldOfView = 45 * Math . PI / 180 ; const rect = this . canvas . getBoundingClientRect ( ) ; const aspect = rect . width / rect . height ; const zNear = 0.1 ; const zFar = 100.0 ; const projectionMatrix = this . create ( ) ; this . perspective ( projectionMatrix , fieldOfView , aspect , zNear , zFar ) ; const modelViewMatrix = this . create ( ) ; this . translate ( modelViewMatrix , modelViewMatrix , [ - 0.0 , 0.0 , - 6.0 ] ) ; { const numComponents = 2 ; const type = _gl . FLOAT ; const normalize = false ; const stride = 0 ; const offset = 0 ; _gl . bindBuffer ( _gl . ARRAY_BUFFER , buffers . position ) ; _gl . vertexAttribPointer ( programInfo . attribLocations . vertexPosition , numComponents , type , normalize , stride , offset ) ; _gl . enableVertexAttribArray ( programInfo . attribLocations . vertexPosition ) ; } _gl . useProgram ( programInfo . program ) ; _gl . uniformMatrix4fv ( programInfo . uniformLocations . projectionMatrix , false , ( < any > projectionMatrix ) . buffer . bufferNativeInstance ) ; _gl . uniformMatrix4fv ( programInfo . uniformLocations . modelViewMatrix , false , ( < any > modelViewMatrix ) . buffer . bufferNativeInstance ) ; { const offset = 0 ; const vertexCount = 4 ; _gl . drawArrays ( _gl . TRIANGLE_STRIP , offset , vertexCount ) ; } window . setTimeout ( ( ) => { this . drawScene ( ) ; } , 16 ) ; } initShaderProgram ( vsSource , fsSource ) { const vertexShader = this . loadShader ( _gl . VERTEX_SHADER , vsSource ) ; const fragmentShader = this . loadShader ( _gl . FRAGMENT_SHADER , fsSource ) ; const shaderProgram = _gl . createProgram ( ) ; _gl . attachShader ( shaderProgram , vertexShader ) ; _gl . attachShader ( shaderProgram , fragmentShader ) ; _gl . linkProgram ( shaderProgram ) ; if ( ! _gl . getProgramParameter ( shaderProgram , _gl . LINK_STATUS ) ) { console . log ( 'Unable to initialize the shader program: ' + _gl . getProgramInfoLog ( shaderProgram ) ) ; return null ; } return shaderProgram ; } loadShader ( type , source ) { const shader = _gl . createShader ( type ) ; _gl . shaderSource ( shader , source ) ; _gl . compileShader ( shader ) ; if ( ! _gl . getShaderParameter ( shader , _gl . COMPILE_STATUS ) ) { console . log ( 'An error occurred compiling the shaders: ' + _gl . getShaderInfoLog ( shader ) ) ; _gl . deleteShader ( shader ) ; return null ; } return shader ; } } new Runner ( ) . main ( ) ; window . loop ( ) ;