【发布时间】:2014-11-08 04:10:48
【问题描述】:
我已尝试遵循作为对this questions 的回答给出的建议,但我仍然无法弄清楚 WebGL 程序的“渲染流程”是如何工作的。
我只是想在画布上绘制两个三角形,它以一种相当不确定的方式工作:有时两个三角形都被渲染,有时只渲染第二个(second 就像 the last一个绘制的)被渲染。
(它似乎取决于渲染时间:奇怪的是,越长渲染两个三角形的几率就越大)。 编辑:不正确,一遍又一遍地尝试刷新,两个三角形有时会出现在非常快速的渲染上(~55ms),有时会出现在运行时间较长的渲染上(~120ms)。 确实 似乎是一个反复出现的模式是,在第一次呈现页面时,两个三角形会显示,而在随后的重复刷新中,红色的一个要么显示良好,要么显示很短的时间时间,然后一闪而逝。
显然我在这里遗漏了一些东西,让我用伪代码解释我的程序流程(如果需要可以包含真实代码),看看我是否做错了什么:
var canvas = new Canvas(/*...*/);
var redTriangle = new Shape(/* vertex positions & colors */);
var blueTriangle = new Shape(/* vertex positions & colors */);
canvas.add(redTriangle, blueTriangle);
canvas.init(); //compiles and links shaders, calls gl.enableVertexAttribArray()
//for vertex attributes "position" and "color"
for(shape in canvas) {
for(bufferType in [PositionBuffer, ColorBuffer]) {
shape.bindBuffer(bufferType); //calls gl.bindBuffer() and gl.bufferData()
//This is equivalent to the initBuffers()
//function in the tutorial
}
}
for(shape in canvas) {
shape.draw();
//calls:
//-gl.bindBuffer() and gl.vertexAttribPointer() for each buffer (position & color),
//-setMatrixUniforms()
//-drawArrays()
//This is equivalent to the drawScene() function in the tutorial
}
尽管我已经将指令封装在对象方法中以尝试使 WebGL 的使用更加面向对象,但在我看来,我完全遵守了 this lesson 上的指令(比较课程的来源和我的自己的代码),因此我无法弄清楚我做错了什么。
我什至尝试只使用一个for(shape in canvas) 循环,如下所示:
for(shape in canvas) {
for(bufferType in [PositionBuffer, ColorBuffer]) {
shape.bindBuffer(bufferType); //calls gl.bindBuffer() and gl.bufferData()
//This is equivalent to the initBuffers()
//function in the tutorial
}
shape.draw();
//calls:
//-gl.bindBuffer() and gl.vertexAttribPointer() for each buffer (position & color),
//-setMatrixUniforms()
//-drawArrays()
//This is equivalent to the drawScene() function in the tutorial
}
但它似乎没有任何效果。 有什么线索吗?
【问题讨论】:
-
发布你的真实代码和小提琴
-
这可能会变得非常棘手,因为我正在开发一个 AngularJS 模块来处理 WebGL,而这又依赖于 UnderscoreJS、Node.js 和 caolan's async library... 我可以创建一个 github回购,强硬我宁愿现在不透露它,因为这是一个应该保密的论文项目,直到我在我的期末论文中提出它......
-
此伪代码无助于调试您的问题,因此您要么发布与绘图相关的代码,要么自行调试。
-
另外,如果你需要 55ms-120ms 来渲染两个三角形,你的绘图循环中还有一些其他严重的问题,只是说。
-
好的,这里是:github.com/axedre/angulargl。感谢您的宝贵时间。
标签: javascript rendering webgl