webgl中的多个对象

时间:2014-02-17 17:50:59

标签: javascript webgl

我正在尝试将一些对象渲染到画布上,而我在理解什么不起作用时遇到了一些麻烦。

我正在构建两个对象,表示我想渲染的两个网格物体。如果我创建一个网格,代码工作正常,我认为问题是当我构建两个或更多时数据被搞砸了。

以下是网格数据的示例:

"name":"cone",
"buffers":{
    "vertexPosition":{}, // Buffer
    "vertexIndex":{} // Buffer
},
"mesh":{
    "vertices":[], // emptied it to fit on page
     "faces":[] // emptied it to fit on page
},
"mvMatrix": Float32Array[16],
"itemSize":3,
"numItems":12,
"program":{
    "vertexPosAttrib":0,
    "mvMatrixUniform":{},
    "pMatrixUniform":{}
}

这是从这个函数构建的:

buildMeshData: function(){

this.mvMatrix = mat4.create();

this.buffers.vertexPosition = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffers.vertexPosition);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.mesh.vertices), gl.STATIC_DRAW);

this.buffers.vertexIndex = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.buffers.vertexIndex);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.mesh.faces), gl.STATIC_DRAW);

this.itemSize = 3;
this.numItems = this.mesh.faces.length;

var vertexProps = {

    attributes: ['vec3', 'VertexPosition'],
    uniforms: ['mat4', 'MVMatrix', 'mat4', 'PMatrix'],
    varyings: ['vec3', 'TexCoord']
}
var vertexShaderFunction = 'vTexCoord = aVertexPosition + 0.5; gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1);';
var vshaderInput = utils.buildVertexShader(vertexProps, vertexShaderFunction);

var fragmentProps = {

    attributes: [],
    uniforms: [],
    varyings: ['vec3', 'TexCoord']
}
var fragmentShaderFunction = 'gl_FragColor = vec4(vTexCoord, 1);';
var fshaderInput = utils.buildFragmentShader(fragmentProps, fragmentShaderFunction);

this.program = gl.createProgram();

var vshader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vshader, vshaderInput);
gl.compileShader(vshader);

var fshader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fshader, fshaderInput);
gl.compileShader(fshader);

gl.attachShader(this.program, vshader);
gl.attachShader(this.program, fshader);

gl.linkProgram(this.program);


gl.useProgram(this.program);

this.program.vertexPosAttrib = gl.getAttribLocation(this.program, 'aVertexPosition');
gl.vertexAttribPointer(this.program.vertexPosAttrib, this.itemSize, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.program.vertexPosAttrib);

this.program.mvMatrixUniform = gl.getUniformLocation(this.program, "uMVMatrix");
this.program.pMatrixUniform = gl.getUniformLocation(this.program, "uPMatrix");

scene.add(this);
}

,渲染功能如下:

function render(){

currentTime = new Date().getTime();
deltaTime = (currentTime - initialTime) / 1000; // in seconds

gl.viewport(0, 0, stage.width, stage.height);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

for(var i in scene.meshes){

    (function(mesh){

        mat4.translate(mesh.mvMatrix, mesh.mvMatrix, [0, 2 * i, -10 - (10 * i)]);

        gl.useProgram(mesh.program);

        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);

        gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, mesh.buffers.vertexIndex);

        gl.vertexAttribPointer(mesh.program.vertexPosAttrib, mesh.itemSize, gl.FLOAT, false, 0, 0);
        gl.enableVertexAttribArray(mesh.program.vertexPosAttrib);

        gl.uniformMatrix4fv(mesh.program.mvMatrixUniform, false, mesh.mvMatrix);
        gl.uniformMatrix4fv(mesh.program.pMatrixUniform, false, scene.pMatrix);

        gl.drawElements(gl.TRIANGLES, mesh.numItems, gl.UNSIGNED_SHORT, 0);

        gl.disableVertexAttribArray(mesh.program.vertexPosAttrib);

    })(scene.meshes[i])

}

// requestAnimationFrame(render);
}

结果是第二个对象被正确绘制但第一个导致错误:

[.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glDrawElements: attempt to access out of range vertices in attribute 0

......因此没有画出来。

问题所在的任何想法。希望从代码中获得足够的信息,我不想忍受太多,但如果你需要看到别的什么我会更新。

1 个答案:

答案 0 :(得分:7)

此代码

gl.vertexAttribPointer(this.program.vertexPosAttrib, this.itemSize, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(this.program.vertexPosAttrib);

绘制每个网格时需要调用,而不是现在调用它。此外,在为gl.vertexAttribPointer致电this.program.vertexPosAttrib之前,您需要致电

gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.vertexPosition);

因为gl.vertexAttribPointer将当前绑定到gl.ARRAY_BUFFER的缓冲区绑定到指定的属性。

换句话说

gl.bindBuffer(gl.ARRAY_BUFFER, mesh.buffers.vertexPosition);
gl.vertexAttribPointer(mesh.program.vertexPosAttrib, mesh.itemSize, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(mesh.program.vertexPosAttrib);