Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to implement batching in my game (Sprite batching and 3D Mesh batching), I've read a lot of documentation about that but I don't understand something:

  • When each object have is own transformation (rotation, position, scale), how can I merge it with others geometries?
  • When each object use blending, how can I achieve depth with batching?

Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Transformation In Batching

When the geometry your batching is simple enough - such as with sprites where only 4 vertices are used - you usually translate to a shared coordinate system before putting the vertices in a batch. Often this is camera space or screen space.

The simplicity of the geometry means that doing this operation CPU side is still a negligible process and is easily parallelised. As the geometry becomes more complex the cost/benefit ratio drops and suddenly separate draw calls looks more appealing.

One solution to this, which I haven't tried, is to supply your vertex shader with the worldview matrix for each mesh your batching and tag each vertex with an id so you know which one to use for each vertex.

Alpha Blending

I'm not quite sure what you mean by "achieve depth". You can still leave depth testing and writing to the depth buffer turned on in this instance but this would cause transparent geometry to occlude other geometry unless the other geometry was drawn first. In other words a window might stop some things being drawn to the screen. For more information check out this article by Shawn Hargreaves.

So it's typical, when drawing transparent geometry, to disable writing to the depth buffer, but keep depth testing active. This means the transparent geometry can still be occluded by opaque geometry but will not occlude anything itself. You have to be careful though, if you draw the transparent geometry, without writing to the z-buffer, then draw opaque geometry, you might end up drawing over the top.

Imagine drawing a skybox and then a window. If you drew a tree behind the window later with no alpha blending it would be drawn over the top of the window.

As Shawn states;

The most common approach:

  1. Set DepthBufferEnable and DepthBufferWriteEnable to true
  2. Draw all opaque geometry
  3. Leave DepthBufferEnable set to true, but change DepthBufferWriteEnable to false
  4. Sort alpha blended objects by distance from the camera, then draw them in order from back to front
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.