开发者

Strange problem when attempting to render 3d models in webgl

开发者 https://www.devze.com 2023-03-12 19:51 出处:网络
So basically I am trying to make a model loader, that will take in wavefont obj files and render them in webgl. Eventually I would like to be able to rotate, translate and scale these objects.

So basically I am trying to make a model loader, that will take in wavefont obj files and render them in webgl. Eventually I would like to be able to rotate, translate and scale these objects.

I have the interface all setup and it works nicely. However I am having problems with rendering.

I have taken in an obj file, and checked the arrays all have the correct numbers of elements and I even checked using chromes webgl debug plugin, and it appears the arrays match up (even element values match up).

Number of Vertices: 10932 Number of Indices: 18960

Anyway When I run gl.drawElements(gl.TRIANGLES, numItems, gl.UNSIGNED_SHORT, 0); I get no chrome error but in the webgl plugin debug i get 'INVALID_OPERATION' with no additional information.

I have found that by changing numItems (which is usually the number of indices / 18960) to a much lower number, it will render a teapot (slightly wrong). The lucky number for some reason is 11034, if I go above this, it wont render, if I go below it will render my slightly wrong teapot. I need this number to really be the full number of indices, as obviously i cannot hard-code the numbers.

So I am very confused as to why this is happening, for my full code for debug: http://webdesignscript.net/assignment/graphics_a3/

Rendering part of the code: http://webdesignscri开发者_如何学编程pt.net/assignment/graphics_a3/scripts/webglengine.js

Teapot model that is loaded: http://webdesignscript.net/assignment/graphics_a3/models/teapot.obj

Cheers, Josh


I hope you remembered that the faces in OBJ files use vertex indices that start at 1, rather than 0. So perhaps those later faces (that make it crash or not work) just reference an invalid vertex (one past the end). If so, just subtract 1 from the faces' vertex indices after read from the file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号