1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| console.log('hello ts')
const V_SHADER_SOURCE: string =` attribute vec3 a_Position; attribute vec3 a_Color; varying vec3 v_Color; void main() { gl_Position = vec4(a_Position.x, a_Position.y, a_Position.z, 1.0); v_Color = a_Color; }`
const F_SHADER_SOURCE: string =` precision mediump float; varying vec3 v_Color; void main() { gl_FragColor = vec4(v_Color.r, v_Color.g, v_Color.b, 1.0);; }`
let gl: WebGLRenderingContext = null let program: WebGLProgram = null let CANVAS_WIDTH: number = 0 let CANVAS_HEIGHT: number = 0 let count = 0
function initBuffer(gl: WebGLRenderingContext) { const vertices = new Float32Array([ -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, ]) const vertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); const a_Position = gl.getAttribLocation(program, 'a_Position'); gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false,0, 0); gl.enableVertexAttribArray(a_Position);
const colors = new Float32Array([ 1.0, 0, 0, 1.0, 0, 0, 1.0, 0, 0, ]) const colorBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.bufferData(gl.ARRAY_BUFFER, colors, gl.STATIC_DRAW); const a_Color = gl.getAttribLocation(program, 'a_Color'); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Color);
count = vertices.length / 3 console.log(count) }
function main() { let canvas: HTMLCanvasElement = document.getElementById("webgl") as HTMLCanvasElement CANVAS_WIDTH = canvas.width CANVAS_HEIGHT = canvas.height gl = getWebGLContext(canvas) program = createProgram(gl) initBuffer(gl) render() }
main()
function render() { gl.clearColor(0, 0, 0, 1) gl.enable(gl.DEPTH_TEST) gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA) gl.viewport(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT) gl.drawArrays(gl.TRIANGLES, 0, count) requestAnimationFrame(render) }
function getWebGLContext(canvas: HTMLCanvasElement) : WebGLRenderingContext { let gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl') return gl }
function createShader(gl: WebGLRenderingContext, type: number, source: string) { const shader = gl.createShader(type) gl.shaderSource(shader, source) gl.compileShader(shader) const compiled = gl.getShaderParameter(shader, gl.COMPILE_STATUS) if(!compiled){ const err = gl.getShaderInfoLog(shader) console.error('Failed to compile shader: ' + err) gl.deleteShader(shader) return null } return shader }
function createProgram(gl: WebGLRenderingContext, vShaderSrc?: string, fShaderSrc?: string) : WebGLProgram { const vertexShader = createShader(gl, gl.VERTEX_SHADER, vShaderSrc || V_SHADER_SOURCE) const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fShaderSrc || F_SHADER_SOURCE) const program = gl.createProgram() gl.attachShader(program, vertexShader) gl.attachShader(program, fragmentShader) gl.linkProgram(program) gl.useProgram(program) return program }
|