WebGL-渲染一个接一个的形状时出现问题

WebGL- Issue when rendering one shape after another

我一直在关注教程中提供的 WebGL tutorial series and the point I'm at in the series deals with applying both textures and lighting to a 3D sphere. In order to actually grasp the proper implementation of such programs so I can write them in the future, I'm trying to implement a version of the code without the lighting and textures. I've been able to draw 3D parametric shapes before where one was drawn on a canvas at a time (using a different setup) so I'm essentially adding that code into the program。教程代码遵循一个结构,其中有一个名为 webGLStart() 的函数,该函数在页面加载时调用,该函数本身调用 initGL()(它设置 canvas)、initShaders()initBuffers()(用于要绘制的形状)和``drawScene()```。

我已经能够在 canvas 的中间成功绘制一个形状,但出于某种原因,当我尝试随后绘制另一个形状时,出现错误:Cannot read property '0' of undefined 在我将模型视图矩阵转换到新形状位置的行上。按照教程中的方法,有一个用作堆栈的数组,用于在每个形状的转换之前保存 mvMatrix 的当前状态,并在对每个形状执行最后一次转换后恢复状态。我在想,也许在绘制第一个形状后,mvMatrix 的当前副本没有正确弹出,因为经过一些调试,似乎弹出前后的堆栈帧数量相同。我不认为这是导致错误的原因,但这是我能找到的唯一可能的问题。

我很确定我正在为每个形状正确设置缓冲区,因为我基本上正在做教程 source code so I have no idea what is going wrong. I have a snippet (but still very long segment) of my code here 中为 initBuffers()drawScene() 函数所做的事情.

可能导致问题的原因是什么?

代码是正确的,只是在绘制形状之前 translate() 函数遗漏了一个参数。