1. webgl的隐藏面消除
在将这个的时候,我想需要知道一个前提。
- webgl中使用缓冲区,来绘制图形时,是按照顺序来绘制的。
- webgl中绘制图形时,后绘制的图形会覆盖先绘制的图形。
缓冲区的数据
var verticesColors = new Float32Array([
0.0, 1.0, 0.0, 0.4, 0.4, 1.0, // The front blue one
-0.5, -1.0, 0.0, 0.4, 0.4, 1.0, // 蓝色 z: 0;
0.5, -1.0, 0.0, 1.0, 0.4, 0.4,
// Vertex coordinates and color
0.0, 1.0, -2.0, 1.0, 1.0, 0.4, // The middle yellow one
-0.5, -1.0, -2.0, 1.0, 1.0, 0.4, // 黄色 z: -2;
0.5, -1.0, -2.0, 1.0, 0.4, 0.4,
0.0, 1.0, -4.0, 0.4, 1.0, 0.4, // The back green one
-0.5, -1.0, -4.0, 0.4, 1.0, 0.4, // 绿色 z: -4;
0.5, -1.0, -4.0, 1.0, 0.4, 0.4,
]);
效果
- 首先它是先绘制了, 蓝色的,中间是黄色的,最后是绿色的。
- 后绘制的会覆盖先绘制的,所以我们会看到
绿色(最近),
黄色(中间),
蓝色(最远)。
但是你要知道
蓝色的三角形的坐标 (x,y, 0);
黄色的三角形的坐标(x,y, -2);
绿色的三角形的坐标(x,y, -4);
就是按道理来说应该是
蓝色 (最近)
黄色 (中间)
绿色 (最远)
但恰恰的却不是这样的,因为后绘制的图形,会覆盖先绘制的。
那么我只有修改 缓冲区数据中的顺序,就可以改变这个问题。
var verticesColors = new Float32Array([
0.0, 1.0, -4.0, 0.4, 1.0, 0.4, // The back green one
-0.5, -1.0, -4.0, 0.4, 1.0, 0.4, // 绿色 z: -4;
0.5, -1.0, -4.0, 1.0, 0.4, 0.4,
0.0, 1.0, -2.0, 1.0, 1.0, 0.4, // The middle yellow one
-0.5, -1.0, -2.0, 1.0, 1.0, 0.4, // 黄色 z: -2;
0.5, -1.0, -2.0, 1.0, 0.4, 0.4,
0.0, 1.0, 0.0, 0.4, 0.4, 1.0, // The front blue one
-0.5, -1.0, 0.0, 0.4, 0.4, 1.0, // 蓝色 z: 0;
0.5, -1.0, 0.0, 1.0, 0.4, 0.4,
// Vertex coordinates and color
]);
效果图
- 缓冲区的数据,是按照顺序进行绘制的
- 后绘制的图形,会吧先绘制的图形进行覆盖。
- webgl中在被覆盖,挡住的部分,还是被绘制出来了。
2.所以我们就要开启,隐藏面消除了。
但是开启这个有一个前提
正确设置可视区空间,就是 "透视投影矩阵","正射投影矩阵"这两个一定要选择一个。
- 开启了隐藏面消除,我们就不需要再关注缓冲区中 数据的顺序。
- 这个功能会帮助我们消除那些被遮挡的表面 (隐藏面)
- 因为远处的物体,会自动被近处的物体挡住,不会被绘制出来。
2.步骤
开启隐藏面消除功能
gl.enable(gl.DEPTH_TEST);
在绘制之前,清除深度缓冲区
gl.clear(gl.DEPTH_BUFFER_BIT);
实例代码
// DepthBuffer.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'attribute vec4 a_Color;\n' +
'uniform mat4 u_mvpMatrix;\n' +
'varying vec4 v_Color;\n' +
'void main() {\n' +
' gl_Position = u_mvpMatrix * a_Position;\n' +
' v_Color = a_Color;\n' +
'}\n';
// Fragment shader program
var FSHADER_SOURCE =
'#ifdef GL_ES\n' +
'precision mediump float;\n' +
'#endif\n' +
'varying vec4 v_Color;\n' +
'void main() {\n' +
' gl_FragColor = v_Color;\n' +
'}\n';
function main() {
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
// Set the vertex coordinates and color (the blue triangle is in the front)
var n = initVertexBuffers(gl);
if (n < 0) {
console.log('Failed to set the vertex information');
return;
}
// Specify the color for clearing <canvas>
gl.clearColor(0.0, 0.0, 0.0, 1.0);
// Enable depth test
gl.enable(gl.DEPTH_TEST);
// Get the storage location of u_mvpMatrix
var u_mvpMatrix = gl.getUniformLocation(gl.program, 'u_mvpMatrix');
if (!u_mvpMatrix) {
console.log('Failed to get the storage location of u_mvpMatrix');
return;
}
var modelMatrix = new Matrix4(); // Model matrix
var viewMatrix = new Matrix4(); // View matrix
var projMatrix = new Matrix4(); // Projection matrix
var mvpMatrix = new Matrix4(); // Model view projection matrix
// Calculate the view matrix and the projection matrix
modelMatrix.setTranslate(0.75, 0, 0);
viewMatrix.setLookAt(0, 0, 5, 0, 0, -100, 0, 1, 0);
projMatrix.setPerspective(30, canvas.width/canvas.height, 1, 100);
// Calculate the model view projection matrix
mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
// Pass the model view projection matrix
gl.uniformMatrix4fv(u_mvpMatrix, false, mvpMatrix.elements);
// Clear color and depth buffer
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.drawArrays(gl.TRIANGLES, 0, n); // Draw the triangles
// Prepare the model matrix for another pair of triangles
modelMatrix.setTranslate(-0.75, 0, 0);
// Calculate the model view projection matrix
mvpMatrix.set(projMatrix).multiply(viewMatrix).multiply(modelMatrix);
// Pass the model view projection matrix to u_MvpMatrix
gl.uniformMatrix4fv(u_mvpMatrix, false, mvpMatrix.elements);
gl.drawArrays(gl.TRIANGLES, 0, n); // Draw the triangles
}
function initVertexBuffers(gl) {
var verticesColors = new Float32Array([
// Vertex coordinates and color
0.0, 1.0, 0.0, 0.4, 0.4, 1.0, // The front blue one
-0.5, -1.0, 0.0, 0.4, 0.4, 1.0, // 蓝色 z-index: 0;
0.5, -1.0, 0.0, 1.0, 0.4, 0.4,
0.0, 1.0, -2.0, 1.0, 1.0, 0.4, // The middle yellow one
-0.5, -1.0, -2.0, 1.0, 1.0, 0.4, // 黄色 z-index: -2;
0.5, -1.0, -2.0, 1.0, 0.4, 0.4,
0.0, 1.0, -4.0, 0.4, 1.0, 0.4, // The back green one
-0.5, -1.0, -4.0, 0.4, 1.0, 0.4, // 绿色 z-index: -4;
0.5, -1.0, -4.0, 1.0, 0.4, 0.4,
]);
var n = 9;
// Create a buffer object
var vertexColorbuffer = gl.createBuffer();
if (!vertexColorbuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Write vertex information to buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorbuffer);
gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW);
var FSIZE = verticesColors.BYTES_PER_ELEMENT;
// Assign the buffer object to a_Position and enable the assignment
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if(a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
return -1;
}
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, FSIZE * 6, 0);
gl.enableVertexAttribArray(a_Position);
// Assign the buffer object to a_Color and enable the assignment
var a_Color = gl.getAttribLocation(gl.program, 'a_Color');
if(a_Color < 0) {
console.log('Failed to get the storage location of a_Color');
return -1;
}
gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 6, FSIZE * 3);
gl.enableVertexAttribArray(a_Color);
// Unbind the buffer object
gl.bindBuffer(gl.ARRAY_BUFFER, null);
return n;
}
效果图
去掉隐藏面消除的效果