要畫這個(gè)正方體每個(gè)面可以分成 2 個(gè)三角形醋安,這樣每個(gè)面需要 6 個(gè)頂點(diǎn),6個(gè)面總共需要 36 個(gè)頂點(diǎn)斩箫。如果用 gl.TRIANGLE_FAN 則每個(gè)面需要 4 個(gè)頂點(diǎn)里伯,6 個(gè)面總共需要 24 個(gè)頂點(diǎn)。這樣傳的頂點(diǎn)太多了崎弃,webgl 用索引(index) 解決這個(gè)問題甘晤,我們只需要傳 8 個(gè)頂點(diǎn)進(jìn)去含潘,然后引用頂點(diǎn)的索引值就可以了。
<script id="vertex-shader" type="glsl">
attribute vec4 a_Position;
attribute vec4 a_Color;
uniform mat4 u_mvpMatrix;
varying vec4 v_Color;
void main() {
gl_Position = u_mvpMatrix * a_Position;
v_Color = a_Color;
}
</script>
<script id="fragment-shader" type="glsl">
precision mediump float;
varying vec4 v_Color;
void main() {
gl_FragColor = v_Color;
}
</script>
8個(gè)頂點(diǎn)及顏色
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
var vertices = new Float32Array([
// Vertex coordinates and color
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // v0 White
-1.0, 1.0, 1.0, 1.0, 0.0, 1.0, // v1 Magenta
-1.0, -1.0, 1.0, 1.0, 0.0, 0.0, // v2 Red
1.0, -1.0, 1.0, 1.0, 1.0, 0.0, // v3 Yellow
1.0, -1.0, -1.0, 0.0, 1.0, 0.0, // v4 Green
1.0, 1.0, -1.0, 0.0, 1.0, 1.0, // v5 Cyan
-1.0, 1.0, -1.0, 0.0, 0.0, 1.0, // v6 Blue
-1.0, -1.0, -1.0, 0.0, 0.0, 0.0 // v7 Black
]);
生成 index (索引)數(shù)組
var indices = new Uint8Array([
0, 1, 2, 0, 2, 3, // front
0, 3, 4, 0, 4, 5, // right
0, 5, 6, 0, 6, 1, // up
1, 6, 7, 1, 7, 2, // left
7, 4, 3, 7, 3, 2, // down
4, 7, 6, 4, 6, 5 // back
]);
索引編號(hào) 0~7安皱,每個(gè)面還是用 6 個(gè)頂點(diǎn)畫 2 個(gè)三角形
索引的數(shù)據(jù)類型定義為了 Uint8调鬓,Uint8 取值范圍是 0~255 最多只能表示 256 個(gè)數(shù),如果索引的個(gè)數(shù)超過 256 就應(yīng)該使用范圍更大的數(shù)據(jù)類型酌伊。
創(chuàng)建 index buffer 并拷貝索引值到 GPU 中
var indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
gl.ELEMENT_ARRAY_BUFFER 表示數(shù)據(jù)是索引
gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0);
gl.drawElements 表示使用索引值來畫
第一個(gè)參數(shù)還是表示要畫的圖形
第二參數(shù)表示要畫的索引的個(gè)數(shù)
第三個(gè)參數(shù)表示索引值的類型腾窝,我們用了 Uint8,所以類型是 gl.UNSIGNED_BYTE
最后一個(gè)參數(shù)是 offset 表示起始偏移
完整代碼
<script id="vertex-shader" type="glsl">
attribute vec4 a_Position;
attribute vec4 a_Color;
uniform mat4 u_mvpMatrix;
varying vec4 v_Color;
void main() {
gl_Position = u_mvpMatrix * a_Position;
v_Color = a_Color;
}
</script>
<script id="fragment-shader" type="glsl">
precision mediump float;
varying vec4 v_Color;
void main() {
gl_FragColor = v_Color;
}
</script>
<script src="lib/cuon-matrix.js"></script>
<script src="lib/myutils.js"></script>
<script>
var VERTEX_SHADER_SOURCE = document.getElementById('vertex-shader').text;
var FRAGMENT_SHADER_SOURCE = document.getElementById('fragment-shader').text;
var canvas = document.getElementById("canvas");
var gl = canvas.getContext('webgl');
if (!initShaders(gl, VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE)) {
alert('Failed to init shaders');
}
var n = initVertexBuffers(gl);
var u_mvpMatrix = gl.getUniformLocation(gl.program, 'u_mvpMatrix');
// <projection matrix> * <view matrix>
var mvpMatrix = new Matrix4();
mvpMatrix.setPerspective(30, 1, 1, 100);
mvpMatrix.lookAt(3, 3, 7, 0, 0, 0, 0, 1, 0);
gl.uniformMatrix4fv(u_mvpMatrix, false, mvpMatrix.elements);
gl.enable(gl.DEPTH_TEST);
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawElements(gl.TRIANGLES, n, gl.UNSIGNED_BYTE, 0);
function initVertexBuffers(gl) {
// Create a cube
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
var vertices = new Float32Array([
// Vertex coordinates and color
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, // v0 White
-1.0, 1.0, 1.0, 1.0, 0.0, 1.0, // v1 Magenta
-1.0, -1.0, 1.0, 1.0, 0.0, 0.0, // v2 Red
1.0, -1.0, 1.0, 1.0, 1.0, 0.0, // v3 Yellow
1.0, -1.0, -1.0, 0.0, 1.0, 0.0, // v4 Green
1.0, 1.0, -1.0, 0.0, 1.0, 1.0, // v5 Cyan
-1.0, 1.0, -1.0, 0.0, 0.0, 1.0, // v6 Blue
-1.0, -1.0, -1.0, 0.0, 0.0, 0.0 // v7 Black
]);
// index
var indices = new Uint8Array([
0, 1, 2, 0, 2, 3, // front
0, 3, 4, 0, 4, 5, // right
0, 5, 6, 0, 6, 1, // up
1, 6, 7, 1, 7, 2, // left
7, 4, 3, 7, 3, 2, // down
4, 7, 6, 4, 6, 5 // back
]);
var vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('Failed to create buffer object');
return -1;
}
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var FSIZE = vertices.BYTES_PER_ELEMENT;
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, 6 * FSIZE, 0);
gl.enableVertexAttribArray(a_Position);
var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 6 * FSIZE, 3 * FSIZE);
gl.enableVertexAttribArray(a_Color);
var indexBuffer = gl.createBuffer();
if (!indexBuffer) {
console.log('Failed to create index buffer');
return -1;
}
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, indices, gl.STATIC_DRAW);
return indices.length;
}
</script>