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 am currently working on a basic minecraft like game. I have a list of coordinates using which i render instances of a cube model. To optimize my rendering, i need to merge similar adjacent cubes into a single largest possible cube so that the total number of polygons that needs to be rendered is reduced

Any suggestions?

share|improve this question
2  
You usually don't gain much in these kinds of optimizations. The GPU is so fast at churning vertices, so unless you have a shortage of vertex memory or really long vertex shaders this is a quite pointless optimization. –  Bjorn Wesen Nov 25 '11 at 1:22
    
Thanks Bjorn. I thought that I would do the merging to lower the cube count. then I would do occlusion culling. –  Rishav Sharan Nov 25 '11 at 9:53
1  
I don't have a solid idea of how this works, but I think hidden surface removal would be something to look into. –  Ray Dey Nov 25 '11 at 20:09
    
+ 1 on hidden surface removal. Might just be best to batch all your cubes with similar textures in one buffer, and cull the hidden faces from the buffer before sending to the GPU. –  ChrisC Sep 7 '12 at 15:04
add comment

1 Answer

up vote 2 down vote accepted

Assuming you already know which cubes are adjacent, you can:

  1. Throw all 12 (if they have shared vertices) or 16 (if not) vertices into a collection

  2. Find the extents and center of the collection

  3. Remove the original cubes from your index list

  4. Calculate the maximum dimension from the extents you already found

  5. Add 8 new vertices to define the new cube centered at the center you already calculated

  6. Add the indices connecting those vertices to your index lists

If you're looking for coplanar faces that can be removed to create rectangular prisms (to remove hidden faces), just zip through your index list and find pairs of triangles that use the same vertices, though probably in opposite order (clockwise vs counter clockwise), and remove both triangles assuming they face each other.

@BjornWesen's comment is correct in that this can be overkill unless your mesh is pretty complex.

However, removing unnecessary faces potentially saves a lot of time in the pixel shader. Any time you draw multiple primitives in back-to-front order (typically a function of your camera location relative to the mesh), each primitive must be rendered, then overwritten or discarded (based on depth comparison) by any closer primitives when their turn comes. Obviously this takes unnecessary time which could be used for other things.

In short, if you feel that any performance issues you're seeing may be due to overdraw, I think this is a good place to start looking. (The profiler is your friend! Don't assume you know where your bottleneck is unless it's written in 40 foot (12.2 meter) flaming letters in your mind.

share|improve this answer
add comment

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.