例子:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body onload="webGLStart()">
<canvas id="webgl" height="400" width="400"></canvas>
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
varying vec4 v_Color;
void main(void) {
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
//gl_FragColor = v_Color;
}
</script>
<script id="shader-vs" type="x-shader/x-vertex">
precision highp float;
attribute highp vec4 aVertexPosition;
attribute vec4 a_Color;
uniform highp mat4 uMVMatrix;
uniform highp mat4 uPMatrix;
varying vec4 v_Color;
void main(void) {
gl_Position = uPMatrix *(uMVMatrix *aVertexPosition);
v_Color=a_Color;
}
</script>
<script src="cuon-matrix.js"></script>
<script>
var gl;
canvas = document.getElementById("webgl");
var FSIZE=4;
function initGL(canvas) {
try {
gl = canvas.getContext("experimental-webgl");
} catch (e) {
}
if (!gl) {
alert("Could not initialise WebGL, sorry :-(");
}
}
function getShader(gl, id) {
var shaderScript = document.getElementById(id);
if (!shaderScript) {
return null;
}
var str = "";
var k = shaderScript.firstChild;
while (k) {
if (k.nodeType == 3) {
str += k.textContent;
}
k = k.nextSibling;
}
var shader;
if (shaderScript.type == "x-shader/x-fragment") {
shader = gl.createShader(gl.FRAGMENT_SHADER);
} else if (shaderScript.type == "x-shader/x-vertex") {
shader = gl.createShader(gl.VERTEX_SHADER);
} else {
return null;
}
gl.shaderSource(shader, str);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
alert(gl.getShaderInfoLog(shader));
return null;
}
return shader;
}
var shaderProgram;
function initShaders() {
var fragmentShader = getShader(gl, "shader-fs");
var vertexShader = getShader(gl, "shader-vs");
shaderProgram = gl.createProgram();
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
alert("Could not initialise shaders");
}
gl.useProgram(shaderProgram);
shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
shaderProgram.colorAttribute = gl.getAttribLocation(shaderProgram, "a_Color");
gl.enableVertexAttribArray(shaderProgram.colorAttribute);
shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
}
var modelMatrix=new Matrix4();
modelMatrix.rotate(0,0,0,1);
//modelMatrix.translate(100000,100000,0);
var viewMatrix=new Matrix4();
viewMatrix.setLookAt(0,10000000,0,0,0,0,0,0,1);
var perspectMatrix=new Matrix4();
perspectMatrix.setPerspective(45,1,1,1000000000);
var mvMatrix =viewMatrix.multiply(modelMatrix);
var pMatrix = perspectMatrix;
console.log(pMatrix);
console.log(mvMatrix);
function setMatrixUniforms() {
gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix.elements);
gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix.elements);
}
var triangleVertexPositionBuffer;
var indexBuffer;
function initBuffers() {
triangleVertexPositionBuffer = gl.createBuffer();
indexBuffer=gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
var verticesColor= [
1000000.0, 1000000.0, 1000000.0, 1.0, 1.0, 1.0,
-1000000.0, 1000000.0, 1000000.0, 1.0, 1.0, 1.0,
-1000000.0, -1000000.0, 1000000.0, 1.0, 1.0, 1.0,
1000000.0, -1000000.0, 1000000.0, 1.0, 1.0, 0.0,
1000000.0, -1000000.0, -1000000.0, 0.0, 0.0, 1.0,
1000000.0, 1000000.0, -1000000.0, 1.0, 0.0, 0.0,
-1000000.0,1000000.0,-1000000.0, 0.0, 1.0, 0.0,
-1000000.0,-1000000.0,-1000000.0,0.0, 0.0, 0.0,
];
var index=[
0,1,2,0,2,3,
0,3,4,0,4,5,
0,5,6,0,6,1,
1,6,7,1,7,2,
7,4,3,7,3,2,
4,7,6,4,6,5
];
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(verticesColor), gl.STATIC_DRAW);
triangleVertexPositionBuffer.itemSize = 3;
triangleVertexPositionBuffer.numItems = 36;
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint8Array(index),gl.STATIC_DRAW);
}
function drawScene() {
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, 3, gl.FLOAT, false, FSIZE*6, 0);
gl.vertexAttribPointer(shaderProgram.colorAttribute, 3, gl.FLOAT, false, FSIZE*6, FSIZE*3);
//mvMatrix.rotate(1,0,0,1);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES,36,gl.UNSIGNED_BYTE,0);
requestAnimationFrame(drawScene);
}
function webGLStart() {
initGL(canvas);
initShaders();
initBuffers();
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.enable(gl.DEPTH_TEST);
drawScene();
}
</script>
</body>
</html>
主要需要注意我们开启了深度检测gl.enable(gl.DEPTH_TEST);
.
本例中perspectCamera的near参数设置为1,相机的位置在10000000;
我们可以画出一个正方体。
但是当我们把near参数设置为0.1时,画布上一片黑暗。
这是为什么呢?
可以确定是由near过小引起的,那么near影响了什么呢?当然是投影矩阵
先来看看透视投影的矩阵运算
fovy为角度,aspect为比率,f为远距离,n为近距离
对比两者的投影矩阵
可以发现near=0.1时由-2变为-0.2,那么乘以一个坐标向量[X,Y,Z,1]的值为什么呢?
相乘的结果即为
gl_position=[X,Y,Z,W]
,除以W为[X/W,Y/W,Z/W,W]
,(X/W,Y/W)
即为canvas中定位的坐标。Z/W
为深度信息。(1)当near=1时
Z/W=1-2/z
(2)当near=0.1时
Z/W=1-0.2/z
回到例子的代码我们可以发现相机的位置设置在10000000,导致了z值比较大,而gl_position为vec4类型时4个float类型,精度为小数点后7位,因而当near=0.1时
Z/W=1-0.2/10000000=1
,为1时深度检测超限不会绘制出。解决方法
(1)关闭深度检测
(2)near值设大一点