尝试在 webgl 中渲染 3d 模型时出现奇怪的问题

发布于 2024-11-15 14:34:03 字数 812 浏览 4 评论 0原文

所以基本上我正在尝试制作一个模型加载器,它将接收wavefont obj 文件并在 webgl 中渲染它们。最终我希望能够旋转、平移和缩放这些对象。

我已经完成了界面的所有设置,并且运行良好。但是我在渲染方面遇到了问题。

我已经接受了一个 obj 文件,并检查了所有数组是否具有正确数量的元素,我什至使用 chromes webgl 调试插件进行了检查,并且看起来数组匹配(甚至元素值也匹配)。

顶点数:10932 索引数量:18960

无论如何当我运行 gl.drawElements(gl.TRIANGLES, numItems, gl.UNSIGNED_SHORT, 0); 我没有收到 chrome 错误,但在 webgl 插件调试中我收到“INVALID_OPERATION”,没有任何其他信息。

我发现通过将 numItems (通常是索引数 / 18960)更改为低得多的数字,它将呈现一个茶壶(略有错误)。由于某种原因,幸运数字是 11034,如果我超过这个数字,它就不会渲染,如果我低于它,它会渲染我稍微错误的茶壶。我需要这个数字确实是索引的完整数量,因为显然我无法对数字进行硬编码。

所以我很困惑为什么会发生这种情况,对于我的完整调试代码: :

渲染部分代码 http://webdesignscript.net/assignment/graphics_a3/scripts/webglengine.js

已加载的茶壶模型:

干杯,乔什

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://webdesignscript.net/assignment/graphics_a3/scripts/webglengine.js

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

Cheers, Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一杯敬自由 2024-11-22 14:34:03

我希望您还记得 OBJ 文件中的面使用从 1 开始的顶点索引,而不是从 0 开始。因此,也许后面的面(导致其崩溃或无法工作)只是引用了无效的顶点(越过末尾的顶点)。如果是这样,只需在从文件读取后从面的顶点索引中减去 1 即可。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文