下面的步驟是必需的,以建立一個WebGL的應用來繪製三角形,然後將其平移到一個新的位置。
在此步驟中,我們使用getContext()得到 WebGL 渲染上下文物件。
var vertices = [ -0.5,0.5,0.0, -0.5,-0.5,0.0, 0.5,-0.5,0.0, ];
var vertCode = 'attribute vec4 coordinates;' + 'uniform vec4 translation;'+ 'void main(void) {' + ' gl_Position = coordinates + translation;' + '}';
var fragCode = 'void main(void) {' +' gl_FragColor = vec4(1, 0.5, 0.0, 1);' +'}';
在這一步,我們關聯著色器程式到緩衝器的物件。
gl.drawArrays(gl.TRIANGLES, 0, 3);
<!doctype html> <html> <body> <canvas width = "300" height = "300" id = "my_Canvas"></canvas> <script> /*=================Creating a canvas=========================*/ var canvas = document.getElementById('my_Canvas'); gl = canvas.getContext('experimental-webgl'); /*===========Defining and storing the geometry==============*/ var vertices = [ -0.5,0.5,0.0, -0.5,-0.5,0.0, 0.5,-0.5,0.0, ]; //Create an empty buffer object and store vertex data var vertex_buffer = gl.createBuffer(); //Create a new buffer gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer); //bind it to the current buffer gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); // Pass the buffer data gl.bindBuffer(gl.ARRAY_BUFFER, null); /*========================Shaders============================*/ //vertex shader source code var vertCode = 'attribute vec4 coordinates;' + 'uniform vec4 translation;'+ 'void main(void) {' + ' gl_Position = coordinates + translation;' + '}'; //Create a vertex shader program object and compile it var vertShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertShader, vertCode); gl.compileShader(vertShader); //fragment shader source code var fragCode = 'void main(void) {' + ' gl_FragColor = vec4(0.0, 0.0, 0.0, 0.1);' + '}'; //Create a fragment shader program object and compile it var fragShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fragShader, fragCode); gl.compileShader(fragShader); //Create and use combiened shader program var shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertShader); gl.attachShader(shaderProgram, fragShader); gl.linkProgram(shaderProgram); gl.useProgram(shaderProgram); /* ===========Associating shaders to buffer objects============*/ gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer); var coordinatesVar = gl.getAttribLocation(shaderProgram, "coordinates"); gl.vertexAttribYiibaier(coordinatesVar, 3, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(coordinatesVar); /* ==========translation======================================*/ var Tx = 0.5, Ty = 0.5, Tz = 0.0; var translation = gl.getUniformLocation(shaderProgram, 'translation'); gl.uniform4f(translation, Tx, Ty, Tz, 0.0); /*=================Drawing the riangle and transforming it========================*/ gl.clearColor(0.5, 0.5, 0.5, 0.9); gl.enable(gl.DEPTH_TEST); gl.clear(gl.COLOR_BUFFER_BIT); gl.viewport(0,0,canvas.width,canvas.height); gl.drawArrays(gl.TRIANGLES, 0, 3); </script> </body> </html>
這將產生以下結果 -