سؤال

I have written a SpriteBatch utility that can batch sprites together and render them using GL_TRIANGLES ( 6 vertices per sprite ). I would also like to be able to have a way to draw other primitives, such as GL_LINES, using a similar method. How should I batch this other kind of geometry together? Would it be a good idea to have Batch classes for each of these types?

هل كانت مفيدة؟

المحلول

For any primitive type which is not connected (such as GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN), you can pack as many independent geometries as you want into the same buffer and draw them in one go, provided matrices, colors, textures etc are the same. If you have subsets which need to be rendered differently, you can still pack all the geometries into the same buffer, and then use glDrawRangeElements to only render a portion at the time.

If you need a more specific answer, you'll need to elaborate on what your are trying to do.

Clarification concerning "non-connected independent geometries": This only applies if you really want to render all the geometries in one go. You can still pack all the vertex geometries for any primitive type (i.e. even GL_TRIANGLE_FAN) in the same buffer, and then just render the correct range of the buffer using glDrawRangeElements.

Example: You want ro render a triangle fan of four vertices (A, B, C, D) and a triangle of three vertices (E, F, G). Your could then construct your buffers as follows:

  • vertex buffer: [A, B, C, D, E, F, G]
  • index buffer: [0, 1, 2, 3, 4, 5, 6]

Then, to draw the triangle fan, you'd call

glDrawRangeElements(GL_TRIANGLE_FAN, 0, 3, 4, GL_UNSIGNED_INT, 0);

and to draw the triangle, you'd call

glDrawRangeElements(GL_TRIANGLES, 4, 6, 3, GL_UNSIGNED_INT, 0);

نصائح أخرى

I think its also worth pointing out that you can achieve advanced batching with geometry instancing, AKA instanced rendering. Check out the links: 1, 2.

With geometry instancing you can not only pack several objects into a single VB but also draw them all with a single draw call.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top