【发布时间】:2019-06-05 11:55:00
【问题描述】:
我想在 WebGl 中分别转换两个 3D 对象,现在我的代码只更改相机的位置和旋转。我正在使用 glMatrix 进行矢量数学运算。“缓冲区”是一个包含对象数据的数组。 buffers[0] 和 buffers[1] 是两个独立的对象。平移和旋转在drawScene函数中完成
function drawScene(gl, programInfo, buffers, deltaTime) {
gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque
gl.clearDepth(1.0); // Clear everything
gl.enable(gl.DEPTH_TEST); // Enable depth testing
gl.depthFunc(gl.LEQUAL); // Near things obscure far things
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
var fieldOfView = 45 * Math.PI / 180;
var aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
var zNear = 0.1;
var zFar = 100.0;
var projectionMatrix = mat4.create();
mat4.perspective(projectionMatrix,fieldOfView,aspect,zNear,zFar);
modelViewMatrix = mat4.create();
// Camera Movement
mat4.translate(modelViewMatrix,modelViewMatrix,[-0.0 + cubeTranslate, 0.0, -6.0]);
mat4.rotate(modelViewMatrix,modelViewMatrix,cubeRotation,[0, 0, 1]);
for( i = 0; i < 2; i++ ){
var numComponents = 3;
var type = gl.FLOAT;
var normalize = false;
var stride = 0;
var offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers[i].position);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfo.attribLocations.vertexPosition);
var numComponents = 4;
var type = gl.FLOAT;
var normalize = false;
var stride = 0;
var offset = 0;
gl.bindBuffer(gl.ARRAY_BUFFER, buffers[i].color);
gl.vertexAttribPointer(
programInfo.attribLocations.vertexColor,
numComponents,
type,
normalize,
stride,
offset);
gl.enableVertexAttribArray(
programInfo.attribLocations.vertexColor);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers[i].indices);
gl.useProgram(programInfo.program);
gl.uniformMatrix4fv(
programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix);
gl.uniformMatrix4fv(
programInfo.uniformLocations.modelViewMatrix,
false,
modelViewMatrix);
var vertexCount = 36;
var type = gl.UNSIGNED_SHORT;
var offset = 0;
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
}
cubeRotation += deltaTime;
cubeTranslate += 0.01
}
【问题讨论】:
-
查看 youtube.com/watch?v=3yLL9ADo-ko
标签: javascript webgl