我以前在这里看到过这种类型的问题,但没有一个给我完整的答案,所以我将发布我的代码,看看是否有人能给我任何见解。基本上,这段代码有效,但我有一个神奇的 z 偏移 (height * 1.205) [与宽度无关],我不知道为什么。
设置大小并创建对齐向量:
gl.viewportWidth = gl.canvas.width;
gl.viewportHeight = gl.canvas.height;
gl.viewportHalfWidth = gl.viewportWidth / 2;
gl.viewportHalfHeight = gl.viewportHeight / 2;
gl.viewportAspect = gl.viewportWidth / gl.viewportHeight;
gl.viewportZoom = -gl.viewportHeight * 1.205;
if (gl.alignmentTest) {
gl.alignmentVertices = (new Buffer( // TL, TM, BM, BR, TR, BL, TL
[ 1.0, -1.0, 0.0,
gl.viewportHalfWidth - 0.5, -1.0, 0.0,
gl.viewportHalfWidth - 0.5, -(gl.viewportHeight - 1.0), 0.0,
gl.viewportWidth - 1.0, -(gl.viewportHeight - 1.0), 0.0,
gl.viewportWidth - 1.0, -1.0, 0.0,
1.0, -(gl.viewportHeight - 1.0), 0.0,
1.0, -1.0, 0.0], 3)).post();
}
绘制对齐向量:
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
mat4.perspective(45, gl.viewportAspect, 0.1, 1000.0, gl.perspective.current);
gl.perspective.post(); // ^ writes directly to current perspective buffer
gl.modelView.resetToIdentity();
gl.enable(gl.BLEND);
gl.blendFunc(gl.ONE, gl.ONE_MINUS_SRC_ALPHA);
// Translate to pixel resolution
gl.modelView.push();
gl.modelView.translate([-gl.viewportHalfWidth, gl.viewportHalfHeight, gl.viewportZoom]);
gl.modelView.post();
// Alignment test?
if (gl.alignmentTest && gl.alignmentVertices) {
gl.red.useAsAttribute(gl.shaderProg.vertexColour);
gl.zero.vec2.useAsAttribute(gl.shaderProg.vertexTextureCoord);
gl.alignmentVertices.drawAsPrimitives(gl.shaderProg.vertexPosition, gl.LINE_STRIP);
}
gl.disable(gl.BLEND);
有问题的行是:
gl.viewportZoom = -gl.viewportHeight * 1.205;
和:
gl.modelView.translate([-gl.viewportHalfWidth, gl.viewportHalfHeight, gl.viewportZoom]);
此代码适用于任何尺寸。
这是 700x300 的屏幕截图:http: //oi49.tinypic.com/2my9ir7.jpg
另一个是 400x600: http ://oi46.tinypic.com/i2irh3.jpg
所以问题是:为什么会存在 1.205 这个神奇的比例因子?或者我可能做错了什么以使其成为必要?