WebGL:缺少每个第二个三角形

时间:2014-12-19 19:49:37

标签: javascript webgl

似乎缺少3d对象的每个第二个三角形。例如,绘制立方体时,只有6个三角形,每个面上一个。 Here is a picture of the problem。即使只绘制了一个对象,此问题仍然存在。

以下是我的代码部分,我认为问题可能是:

//start up
gl = canvas.getContext('webgl');
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.clearColor(0.0,0.0,0.0,0.0);
gl.clearDepth(1.0);
//shaders
//vertexShader
'
    attribute vec3 vertexPosition;
    uniform mat4 transformationMatrix;
    attribute vec2 uv;
    varying vec2 vUV;
    void main()
    {
        gl_Position = transformationMatrix * vec4(vertexPosition,1);
        vUV = uv;
    }'
//fragmentShader
//problem is still there even when not using a texture
'
    precision highp float;
    uniform sampler2D sampler;
    varying vec2 vUV;
    void main()
    {
        gl_FragColor = texture2D(sampler,vUV);
    }'
//after shaders are compiled and some getAttribLocation/enableVertex/AttribLocation/etc calls are made, models and textures are loaded
//each model has a vertexBuffer, indexBuffer, and nIndices, among some other things
//models are retrieved in a for loop
//models are exported to json format with a vertex format of [X,Y,Z,Nx,Ny,Nz,U,V]

var retrievedModel = retrieveResourceFile(resourcesToLoad.models[m].source);

//make vertex buffer

resourcesToLoad.models[m].path.vertexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER,resourcesToLoad.models[m].path.vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array(retrievedModel.vertices),gl.STATIC_DRAW);

//make index buffer

resourcesToLoad.models[m].path.indexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,resourcesToLoad.models[m].path.indexBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER,new Uint16Array(retrievedModel.indices),gl.STATIC_DRAW);
resourcesToLoad.models[m].path.nIndices = retrievedModel.indices.length;

//now in draw loop, objects that need to be drawn are stored in Draw.objectsToRender array

gl.bindBuffer(gl.ARRAY_BUFFER,Draw.objectsToRender[i].model.vertexBuffer);

gl.vertexAttribPointer(Shaders.modelShaderProgram.vertexPosition,3,gl.FLOAT,false,4*(3+3+2),0);
gl.vertexAttribPointer(Shaders.modelShaderProgram.uv,2,gl.FLOAT,false,4*(3+3+2),(3+3)*4);

gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,Draw.objectsToRender[i].model.indexBuffer);

gl.drawElements(gl.TRIANGLES,Draw.objectsToRender[i].model.nIndices,gl.UNSIGNED_SHORT,0);

我一直在将我的代码与我使用http://www.webglacademy.com/#6的教程进行比较,(特别是第6部分第8节),我无法发现会导致此问题的任何不同内容。我正在使用的模型是使用本教程第6部分提供的导出脚本从Blender导出的,但我不认为那里存在问题,因为我尝试使用在本教程中工作正常的龙模型数据,当我的代码使用时,它有缺少的三角形问题。非常感谢你。

0 个答案:

没有答案