
- WebGL 示例
- WebGL - 繪製點
- WebGL - 繪製三角形
- WebGL - 繪製模式
- WebGL - 繪製四邊形
- WebGL - 顏色
- WebGL - 平移
- WebGL - 縮放
- WebGL - 旋轉
- WebGL - 立方體旋轉
- WebGL - 互動式立方體
- WebGL 有用資源
- WebGL - 快速指南
- WebGL - 有用資源
- WebGL - 討論
WebGL - 繪製四邊形
在上一章中,我們討論了 WebGL 提供的不同繪製模式。我們還可以使用索引來使用這些模式之一繪製圖元。要在 WebGL 中繪製模型,我們必須選擇其中一個圖元並繪製所需的網格(即,使用一個或多個圖元形成的模型)。
在本章中,我們將透過一個示例演示如何使用 WebGL 繪製四邊形。
繪製四邊形的步驟
建立 WebGL 應用程式以繪製四邊形需要以下步驟。
步驟 1 - 準備畫布並獲取 WebGL 渲染上下文
在此步驟中,我們使用 **getContext()** 獲取 WebGL 渲染上下文物件。
步驟 2 - 定義幾何體並將其儲存在緩衝區物件中
可以使用兩個三角形繪製正方形。在此示例中,我們提供了兩個三角形(具有一個公共邊)的頂點和索引。
var vertices = [ -0.5,0.5,0.0, -0.5,-0.5,0.0, 0.5,-0.5,0.0, 0.5,0.5,0.0 ]; indices = [3,2,1,3,1,0];
步驟 3 - 建立和編譯著色器程式
在此步驟中,您需要編寫頂點著色器和片段著色器程式,編譯它們,並透過連結這兩個程式來建立一個組合程式。
**頂點著色器** - 在程式的頂點著色器中,我們定義向量屬性來儲存 3D 座標並將其分配給 **gl_position**。
var vertCode = 'attribute vec3 coordinates;' + 'void main(void) {' + ' gl_Position = vec4(coordinates, 1.0);' + '}';
**片段著色器** - 在片段著色器中,我們只需將片段顏色分配給 **gl_FragColor** 變數。
var fragCode = 'void main(void) {' +' gl_FragColor = vec4(0.5, 0.3, 0.0, 7.5);' +'}';
步驟 4 - 將著色器程式關聯到緩衝區物件
在此步驟中,我們將緩衝區物件與著色器程式關聯。
步驟 5 - 繪製所需的物件
由於我們正在繪製兩個三角形來形成一個四邊形,使用索引,我們將使用 **drawElements()** 方法。對於此方法,我們必須傳遞索引的數量。**indices.length** 的值給出索引的數量。
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT,0);
示例 – 繪製四邊形
以下程式顯示瞭如何建立 WebGL 應用程式以繪製四邊形。
<!doctype html> <html> <body> <canvas width = "570" height = "570" 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, 0.5,0.5,0.0 ]; indices = [3,2,1,3,1,0]; // Create an empty buffer object to store vertex buffer var vertex_buffer = gl.createBuffer(); // Bind appropriate array buffer to it gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer); // Pass the vertex data to the buffer gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); // Unbind the buffer gl.bindBuffer(gl.ARRAY_BUFFER, null); // Create an empty buffer object to store Index buffer var Index_Buffer = gl.createBuffer(); // Bind appropriate array buffer to it gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer); // Pass the vertex data to the buffer gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW); // Unbind the buffer gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); /*====================== Shaders =======================*/ // Vertex shader source code var vertCode = 'attribute vec3 coordinates;' + 'void main(void) {' + ' gl_Position = vec4(coordinates, 1.0);' + '}'; // Create a vertex shader object var vertShader = gl.createShader(gl.VERTEX_SHADER); // Attach vertex shader source code gl.shaderSource(vertShader, vertCode); // Compile the vertex shader gl.compileShader(vertShader); // Fragment shader source code var fragCode = 'void main(void) {' + ' gl_FragColor = vec4(0.0, 0.0, 0.0, 0.1);' + '}'; // Create fragment shader object var fragShader = gl.createShader(gl.FRAGMENT_SHADER); // Attach fragment shader source code gl.shaderSource(fragShader, fragCode); // Compile the fragmentt shader gl.compileShader(fragShader); // Create a shader program object to // store the combined shader program var shaderProgram = gl.createProgram(); // Attach a vertex shader gl.attachShader(shaderProgram, vertShader); // Attach a fragment shader gl.attachShader(shaderProgram, fragShader); // Link both the programs gl.linkProgram(shaderProgram); // Use the combined shader program object gl.useProgram(shaderProgram); /* ======= Associating shaders to buffer objects =======*/ // Bind vertex buffer object gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer); // Bind index buffer object gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer); // Get the attribute location var coord = gl.getAttribLocation(shaderProgram, "coordinates"); // Point an attribute to the currently bound VBO gl.vertexAttribPointer(coord, 3, gl.FLOAT, false, 0, 0); // Enable the attribute gl.enableVertexAttribArray(coord); /*============= Drawing the Quad ================*/ // Clear the canvas gl.clearColor(0.5, 0.5, 0.5, 0.9); // Enable the depth test gl.enable(gl.DEPTH_TEST); // Clear the color buffer bit gl.clear(gl.COLOR_BUFFER_BIT); // Set the view port gl.viewport(0,0,canvas.width,canvas.height); // Draw the triangle gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT,0); </script> </body> </html>
如果執行此示例,將產生以下輸出:
廣告