Search code examples
language-agnosticopengl3ddirectx

Displaying 100 Floating Cubes Using DirectX OR OpenGL


I'd like to display 100 floating cubes using DirectX or OpenGL.

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.

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


Solution

  • 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.

    Basically... put your code for writing a cube in one function, then just call that function 100 times.

    void DrawCube()
    {
        //code to draw the cube
    }
    
    void DisplayCubes()
    {
        for(int i = 0; i < 10; ++i)
        {   
             for(int j = 0; j < 10; ++j)
             {
                 glPushMatrix();
                 //alter these values depending on the size of your cubes.
                 //This call makes sure that your cubes aren't drawn overtop of each other
                 glTranslatef(i*5.0, j*5.0, 0);
                 DrawCube();
                 glPopMatrix();
             }
        }              
    }
    

    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 :)