简体   繁体   English

在WebGL中动态更改顶点缓冲区

[英]Change vertex buffer dynamically in WebGL

I'm developing a cloth simulator in WebGL, got all the physics and the animation ready but I just can't render it. 我正在开发WebGL中的布料模拟器,准备好所有物理和动画,但我无法渲染它。 I am used to use glVertex in Opengl, so in every iteration I can change the position of the vertex and it will move, but in WebGL (OpenGL ES) there is not such method. 我习惯在Opengl中使用glVertex,所以在每次迭代中我都可以改变顶点的位置并且它会移动,但是在WebGL(OpenGL ES)中没有这样的方法。

This is my code: 这是我的代码:

//Initialization:
    puntosBuffer= gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, puntosBuffer);
    telaVertices3=new Array(12);
    telaVertices3=[
    0.0,0.0,0.0,
    2.0,0.0,0.0,
    1.0,1.7,0.0,
    0.0,1.0,0.0
    ];
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(telaVertices3), gl.DYNAMIC_DRAW);

//loop:
    posx=posx+0.2;
    telaVertices3[0]=posx;
    gl.bindBuffer(gl.ARRAY_BUFFER, puntosBuffer);
    gl.bufferSubData(gl.ARRAY_BUFFER, 0, new Float32Array(telaVertices3));

But it always renders at the same points!!! 但它始终呈现在同一点!

I use additional calls to gl.bufferData(...) instead of gl.bufferSubData(...) . 我使用gl.bufferData(...)而不是gl.bufferSubData(...)其他调用。 I don't know if this matters or not, but I think it's the only thing I'm doing differently from your example. 我不知道这是否重要,但我认为这是我与你的例子不同的唯一方式。

You can see my current implementation of buffer management at jax/buffer.js#L48 and an older version at webgl/buffer.js#L26 . 您可以在jax / buffer.js#L48上查看我当前的缓冲区管理实现,在webgl / buffer.js#L26上查看旧版本。 As you can see, I'm not doing anything special: 如你所见,我没有做任何特别的事情:

gl.bindBuffer(self.bufferType, buffer);
gl.bufferData(self.bufferType, instance, self.drawType);

You can see the animation in a live demo at webgldemos/md2 . 您可以在webgldemos / md2的现场演示中看到动画。

However 然而

If you're planning to update a lot of vertices at once then this is probably not the best method. 如果您计划一次更新大量顶点,那么这可能不是最好的方法。 Instead, I'd propose sending the relevant data down to the video card and performing animation directly within GLSL, so that you aren't limited by JavaScript slowness and can take advantage of hardware acceleration. 相反,我建议将相关数据发送到视频卡并直接在GLSL中执行动画,这样您就不会受到JavaScript速度的限制,并且可以利用硬件加速。 In fact, I'm transitioning most of my code to work this way. 事实上,我正在将大部分代码转换为以这种方式工作。 Two live examples of vertex manipulation on the video card are available at jax/blobular and jax/meadow . jax / blobularjax / meadow上可以看到视频卡上两个顶点操作的实例。 The source code for those demos is freely available here and here , respectively. 这些演示的源代码分别在这里这里免费提供。

I hope this was helpful to you. 我希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM