title: Infinite cube world engine (like Minecraft) optimization suggestions?
body: <p>Voxel engine (like Minecraft) optimization suggestions?</p>
  
  <p>As a fun project (and to get my Minecraft-adict son excited for programming) I am building a 3D Minecraft-like voxel engine using C# .NET4.5.1, OpenGL and GLSL 4.x.</p>
  
  <p>Right now my world is built using chunks. Chunks are stored in a dictionary, where I can select them based on a 64bit <code>X | Z&lt;&lt;32</code> key. This allows to create an 'infinite' world that can cache-in and cache-out chunks. </p>
  
  <ul>
  <li><p>Every chunk consists of an array of 16x16x16 block segments. Starting from level 0, bedrock, it can go as high as you want (unlike minecraft where the limit is 256, I think).</p></li>
  <li><p>Chunks are queued for generation on a separate thread when they come in view and need to be rendered. This means that chunks might not show right away. In practice you will not notice this. NOTE: I am not waiting for them to be generated, they will just not be visible immediately.</p></li>
  <li><p>When a chunk needs to be rendered for the first time a VBO (<code>glGenBuffer</code>, <code>GL_STREAM_DRAW</code>, etc.) for that chunk is generated containing the possibly visible/outside faces (neighboring chunks are checked as well). [This means that a chunk potentially needs to be re-tesselated when a neighbor has been modified]. When tesselating first the opaque faces are tesselated for every segment and then the transparent ones. Every segment knows where it starts within that vertex array and how many vertices it has, both for opaque faces and transparent faces.</p></li>
  <li><p>Textures are taken from an array texture.</p></li>
  </ul>
  
  <p>When rendering;</p>
  
  <ul>
  <li>I first take the bounding box of the frustum and map that onto the chunk grid. Using that knowledge I pick every chunk that is within the frustum and within a certain distance of the camera.</li>
  <li>Now I do a distance sort on the chunks. </li>
  <li>After that I determine the ranges (index, length) of the chunks-segments that are actually visible. NOW I know exactly what segments (and what vertex ranges) are 'at least partially' in view. The only excess segments that I have are the ones that are hidden behind mountains or 'sometimes' deep underground.</li>
  <li>Then I start rendering ... first I render the opaque faces [culling and depth test enabled, alpha test and blend disabled] front to back using the known vertex ranges. Then I render the transparent faces back to front [blend enabled]</li>
  </ul>
  
  <p>Now... does anyone know a way of improving this and still allow dynamic generation of an infinite world? I am currently reaching <strong>~80fps@1920x1080</strong>, <strong>~120fps@1024x768</strong> (screenshots: <a href="http://i.stack.imgur.com/t4k30.jpg" rel="nofollow">http://i.stack.imgur.com/t4k30.jpg</a>, <a href="http://i.stack.imgur.com/prV8X.jpg" rel="nofollow">http://i.stack.imgur.com/prV8X.jpg</a>) on an average 2.2Ghz i7 laptop with a ATI HD8600M gfx card. I think it must be possible to increase the number of frames. And I think I have to, as I want to add entity AI, sound and do bump and specular mapping. Could using Occlusion Queries help me out? ... which I can't really imagine based on the nature of the segments. I already minimized the creation of objects, so there is no 'new Object' all over the place. Also as the performance doesn't really change when using Debug or Release mode, I don't think it's the code but more the approach to the problem.</p>
  
  <p>edit: I have been thinking of using <code>GL_SAMPLE_ALPHA_TO_COVERAGE</code> but it doesn't seem to be working?</p>
  
  <pre><code>gl.Enable(GL.DEPTH_TEST);
  gl.Enable(GL.BLEND); // gl.Disable(GL.BLEND);
  gl.Enable(GL.MULTI_SAMPLE);
  gl.Enable(GL.SAMPLE_ALPHA_TO_COVERAGE);
  </code></pre>