I am using OpenGL in C++ (technically EGL, on a Jetson Nano.)
Let's say I want to draw N Quads. Imagine just a list of colored rectangles. There may be a few thousand such rectangles in the frame.
I want to use two vertex buffers:
- One that defines the geometry of each quad.
- One that defines the properties common to each quad.
The first vertex buffer should define the geometry of each quad. It should have only 4 vertices in it and its data would be just the corners of a quad. Something like:
0, 0, // top left
1, 0, // top right
0, 1, // bottom left
1, 1, // bottom right
Then the second vertex buffer should have just the x,y,width,height of all the rectangles.
x1, y1, width1, height1, color1,
x2, y2, width2, height2, color2,
x3, y3, width3, height3, color3,
x4, y4, width4, height4, color4,
x5, y5, width5, height5, color5,
x6, y6, width6, height6, color6,
... etc.
The thing is that each one of the items in my rectangle buffer should apply to 4 vertices in the vertex buffer.
Is there a way to set this up so that it keeps reusing the same 4 quad vertices over and over for each rectangle and applies the same rectangle properties to 4 vertices at a time?
I'm imagining there's something I can do so that I say that the first vertex buffer should use one element per vertex and wraps around, but the second vertex buffer uses one element per every four vertices or something like that.
How do I set this up?
What I do now:
Right now I need one vertex buffer that just has the quad vertices repeated over and over as many times as I have instances.
0, 0, // (1) top left
1, 0, //
0, 1, //
1, 1 //
0, 0, // (2) top left
1, 0, //
0, 1, //
1, 1, //
0, 0, // (3) top left
1, 0, //
0, 1, //
1, 1, //
... etc
And my second buffer duplicates its data for each vertex:
x1, y1, width1, height1, color1,
x1, y1, width1, height1, color1,
x1, y1, width1, height1, color1,
x1, y1, width1, height1, color1,
x2, y2, width2, height2, color2,
x2, y2, width2, height2, color2,
x2, y2, width2, height2, color2,
x2, y2, width2, height2, color2,
x3, y3, width3, height3, color3,
x3, y3, width3, height3, color3,
x3, y3, width3, height3, color3,
x3, y3, width3, height3, color3,
x4, y4, width4, height4, color4,
x4, y4, width4, height4, color4,
x4, y4, width4, height4, color4,
x4, y4, width4, height4, color4,
x5, y5, width5, height5, color5,
x5, y5, width5, height5, color5,
x5, y5, width5, height5, color5,
x5, y5, width5, height5, color5,
x6, y6, width6, height6, color6,
x6, y6, width6, height6, color6,
x6, y6, width6, height6, color6,
x6, y6, width6, height6, color6,
... etc.
This seems really inefficient and I just want to specify the first 4 vertices once and have it keep reusing them somehow rather than duplicating these 4 vertices N times to have a total of 4*N vertices in my first buffer. And I only want to specify the x,y,width,height,color attributes once for each quad for a total of N vertices, and not once for each overall vertex for a total of 4*N vertices.
What do I do?