• Create Account

Need scary sound effects or creepy audio loops for your next horror-themed game? Check out Highscore Vol.3 - The Horror Edition in our marketplace. 50 sounds and 10 loops for only \$9.99!

### #Actualmhagain

Posted 03 July 2012 - 04:46 PM

The array of indexes can be completely static. It's always going to be 0/1/2, 0/2/3, 4/5/6, 4/6/7, 8/9/10, 8/10/11, etc. So pick an arbitrary max number of quads that you're going to draw, pre-populate the array up to this number during startup (a simple loop can handle this), (optionally) put it in a GL_ELEMENT_ARRAY_BUFFER, and you're done.

The code to pre-populate the array will look something like this (assuming a selected max of 1024 quads):
unsigned short quadindexes[1024 * 6];

for (int i = 0, fv = 0; i < 1024; i++, fv += 4, ndx += 6)
{
ndx[0] = fv;
ndx[1] = fv + 1;
ndx[2] = fv + 2;
ndx[3] = fv;
ndx[4] = fv + 2;
ndx[5] = fv + 3;
}

The other option - if you're not averse to using deprecated functionality - is to use glDrawArrays with GL_QUADS. That will enable you to do away with indexes altogether. I assume that you don't really want to do this though as you explicitly mentioned that you want to build this around glDrawElements.

Either way will give you the real advantage of drawing with arrays which is to be able to draw multiple quads in a single draw call. You won't get that from glTranslate for each quad.

By the way - you don't need to go all the way to VBO/VAO with this code if you're not feeling ready for it yet. In particular, since you have dynamic data, you need to build up a good understanding of pipeline stalls and how the CPU and GPU sync up before doing so, otherwise you risk running substantially slower (you should consider getting this understanding and making the switch eventually though). I really wish that people would stop recommending "use a VBO" for every single snippet of OpenGL problem code without analysing the problem properly first.........

### #3mhagain

Posted 03 July 2012 - 03:41 PM

The array of indexes can be completely static. It's always going to be 0/1/2, 0/2/3, 4/5/6, 4/6/7, 8/9/10, 8/10/11, etc. So pick an arbitrary max number of quads that you're going to draw, pre-populate the array up to this number during startup (a simple loop can handle this), (optionally) put it in a GL_ELEMENT_ARRAY_BUFFER, and you're done.

The code to pre-populate the array will look something like this (assuming a selected max of 1024 quads):
unsigned short quadindexes[1024 * 6];

for (int i = 0, fv = 0; i < 1024; i++, fv += 4, ndx += 6)
{
ndx[0] = fv;
ndx[1] = fv + 1;
ndx[2] = fv + 2;
ndx[3] = fv;
ndx[4] = fv + 2;
ndx[5] = fv + 3;
}

The other option - if you're not averse to using deprecated functionality - is to use glDrawArrays with GL_QUADS. That will enable you to do away with indexes altogether. I assume that you don't really want to do this though as you explicitly mentioned that you want to build this around glDrawElements.

Either way will give you the real advantage of drawing with arrays which is to be able to draw multiple quads in a single draw call. You won't get that from glTranslate for each quad.

### #2mhagain

Posted 03 July 2012 - 03:36 PM

The array of indexes can be completely static. It's always going to be 0/1/2, 0/2/3, 4/5/6, 4/6/7, 8/9/10, 8/10/11, etc. So pick an arbitrary max number of quads that you're going to draw, pre-populate the array up to this number during startup (a simple loop can handle this), put it in a GL_ELEMENT_ARRAY_BUFFER, and you're done.

The code to pre-populate the array will look something like this (assuming a selected max of 1024 quads):
unsigned short quadindexes[1024 * 6];

for (int i = 0, fv = 0; i < 1024; i++, fv += 4, ndx += 6)
{
ndx[0] = fv;
ndx[1] = fv + 1;
ndx[2] = fv + 2;
ndx[3] = fv;
ndx[4] = fv + 2;
ndx[5] = fv + 3;
}

The other option - if you're not averse to using deprecated functionality - is to use glDrawArrays with GL_QUADS.

### #1mhagain

Posted 03 July 2012 - 03:31 PM

The array of indexes can be completely static. It's always going to be 0/1/2, 0/2/3, 4/5/6, 4/6/7, 8/9/10, 8/10/11, etc. So pick an arbitrary max number of quads that you're going to draw, pre-populate the array up to this number during startup (a simple loop can handle this), put it in a GL_ELEMENT_ARRAY_BUFFER, and you're done.

The other option - if you're not averse to using deprecated functionality - is to use glDrawArrays with GL_QUADS.

PARTNERS