Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vao usage in examples #44

Open
kswope opened this issue Nov 22, 2018 · 1 comment
Open

vao usage in examples #44

kswope opened this issue Nov 22, 2018 · 1 comment

Comments

@kswope
Copy link

kswope commented Nov 22, 2018

I've already come across two examples where I can completely comment out the lines with vao and not change the running of the program at all.

https://webgl2fundamentals.org/webgl/lessons/webgl-fundamentals.html
https://webgl2fundamentals.org/webgl/lessons/webgl-2d-translation.html

I'm just beginning with webgl, so I'm not sure whats going on, either VAOs aren't being used correctly, the newest browsers are filling in some missing parts, or the tutorial examples are written in a way that is completely uninstructive as to the use of vaos.

@greggman
Copy link
Member

greggman commented Nov 22, 2018

The reason you can take the lines out is because there is only one VAO in those samples. As soon as you have 2 VAOs you'd have to put the lines back. The samples are the way they are to show the normal case (more than 1 VAO) not the exceptional case (just 1 VAO)

The normal case is explained here

https://webgl2fundamentals.org/webgl/lessons/webgl-drawing-multiple-things.html

This is no different than a 2D sample. Let's say you wanted to draw a moving box in red. You could do this

 ctx.fillStyle = "red";
 render(time) {
     ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
     ctx.fillRect(150 * Math.sin(time * .001) + 150, 75);
     requestAnimationFrame(render);
}
requestAnimationFrame(render);

But setting the fillStyle outside the loop is not normal. So the sample would show something like

 render(time) {
     ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
     ctx.fillStyle = "red";
     ctx.fillRect(150 * Math.sin(time * .001) + 150, 75);
     requestAnimationFrame(render);
}
requestAnimationFrame(render);

Even though for such a small sample fillStyle only needs to be set once.

It's the same with VAOs. Because there happens to be just one you could only bind once. But that's not the normal case. Just like drawing just one thing in one color is not the normal case

 render(time) {
     ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
     ctx.fillStyle = "red";
     ctx.fillRect(150 * Math.sin(time * .001) + 150, 75);
     ctx.fillStyle = "blue";
     ctx.fillRect(150 * Math.sin(time * -.001) + 150, 75);
     requestAnimationFrame(render);
}
requestAnimationFrame(render);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants