<p>I'd like to display 100 floating cubes using <code>DirectX</code> or <code>OpenGL</code>.</p>

<p>I'm looking for either some sample source code, or a description of the technique.  I have trouble getting more one cube to display correctly.</p>

<p>I've combed the net for a good series of tutorials and although they talk about how to do <code>3D</code> primitives, what I can't find is information on how to do large numbers of <code>3D</code> primitives - <code>cubes</code>, <code>spheres</code>, <code>pyramids</code>, and so forth.</p>

[{'answer_id': 1092, 'author': 'Bernard', 'author_id': 61, 'author_profile': 'https://Stackoverflow.com/users/61', 'pm_score': 3, 'selected': False, 'text': "<p>Just use glTranslatef (or the DirectX equivalent) to draw a cube using the same code, but moving the relative point where you draw it. Maybe there's a better way to do it though, I'm fairly new to OpenGL. Be sure to set your viewpoint so you can see them all.</p>\n"}, {'answer_id': 1096, 'author': 'Redbaron', 'author_id': 41, 'author_profile': 'https://Stackoverflow.com/users/41', 'pm_score': 4, 'selected': True, 'text': "<p>You say you have enough trouble getting one cube to display... so I am not sure if you have got one to display or not.</p>\n\n<p>Basically... put your code for writing a cube in one function, then just call that function 100 times.</p>\n\n<pre><code>void DrawCube()\n{\n    //code to draw the cube\n}\n\nvoid DisplayCubes()\n{\n    for(int i = 0; i &lt; 10; ++i)\n    {   \n         for(int j = 0; j &lt; 10; ++j)\n         {\n             glPushMatrix();\n             //alter these values depending on the size of your cubes.\n             //This call makes sure that your cubes aren't drawn overtop of each other\n             glTranslatef(i*5.0, j*5.0, 0);\n             DrawCube();\n             glPopMatrix();\n         }\n    }              \n}\n</code></pre>\n\n<p>That is the basic outline for how you could go about doing this. If you want something more efficient take a look into Display Lists sometime once you have the basics figured out :)</p>\n"}, {'answer_id': 15989, 'author': 'Ali Parr', 'author_id': 1169, 'author_profile': 'https://Stackoverflow.com/users/1169', 'pm_score': 3, 'selected': False, 'text': '<p>Yeah, if you were being efficient you\'d throw everything into the same vertex buffer, but I don\'t think drawing 100 cubes will push any GPU produced in the past 5 years, so you should be fine following the suggestions above.</p>\n\n<p>Write a basic pass through vertex shader, shade however you desire in the pixel shader. Either pass in a world matrix and do the translation in the vertex shader, or just compute the world space vertex positions on the CPU side (do this if your cubes are going to stay fixed).</p>\n\n<p>You could get fancy and do <a href="http://en.wikipedia.org/wiki/Geometry_instancing" rel="noreferrer">geometry instancing</a> etc, but just get the basics going first. </p>\n'}, {'answer_id': 73391536, 'author': 'new QOpenGLWidget', 'author_id': 9194639, 'author_profile': 'https://Stackoverflow.com/users/9194639', 'pm_score': -1, 'selected': False, 'text': '<p>This answer isn\'t just for OP\'s question. It also answers a more general question - displaying many cubes <em>in general</em>.</p>\n<h3>Drawing many cube meshes</h3>\n<p>This is probably the most naive way of doing things. We draw the same cube mesh with many different transformation matrices:</p>\n<pre class="lang-cpp prettyprint-override"><code>prepare();\n\nfor (int i = 0; i &lt; numCubes; i++) {\n    setTransformation(matrices[i]);\n    drawCube();\n}\n/* and so on... */\n</code></pre>\n<p>The nice thing is that this is SUPER easy to implement, and it\'s not <em>too</em> slow (at least for 100 cubes). I\'d recommend this as a starter.</p>\n<h3>The problem</h3>\nOk, but let\'s say you want to make a Minecraft clone, or at least some sort of project that requires thousands, if not tens of thousands of cubes to be rendered. That\'s where the performance starts to go down. The problem is that each drawCube() sends a draw call to the GPU, and the time in each draw call adds up, so that eventually, it\'s unbearable.\n<p>However, we can fix this. The solution is <em>batching</em>, a way to do only <em>one</em> draw call for all of the cubes.</p>\n<h3>Batching</h3>\n<p>We join all the (transformed) cubes into one single mesh. This means that we will have to deal with only one draw call, instead of thousands. Here is some pseudocode for doing so:</p>\n<pre class="lang-cpp prettyprint-override"><code>vector&lt;float&gt; transformedVerts;\n\nfor (int i = 0; i &lt; numCubes; i++) {\n    cubeData = cubes[i];\n    for (int j = 0; j &lt; numVertsPerCube; j++) {\n        vert = verts[j];\n        /* We transform the position by the transformation matrix. */\n        vec3 vposition = matrices[i] * verts.position; \n        transformedVerts.push(vposition);\n\n        /* We don\'t need to transform the colors, so we just directly push them. */\n        transformedVerts.push(vert.color);\n    }\n}\n\n...\nsendDataToBuffer(transformedVerts); \n</code></pre>\n<p>If the cubes are moving, or one of the cubes is added or deleted, you\'ll have to recalculate <code>transformedVerts</code> and then resend it to the buffer - but this is minor.</p>\n<p>Then at the end we draw the entire lumped-together mesh in one draw call, instead of many.</p>\n'}]
['https://Stackoverflow.com/questions/1064', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']