Displaying 100 Floating Cubes Using DirectX OR OpenGL
Asked Answered
T

3

19

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.

Tropicalize answered 4/8, 2008 at 6:51 Comment(0)
C
8

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

Carty answered 4/8, 2008 at 9:12 Comment(0)
B
7

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.

Big answered 4/8, 2008 at 9:4 Comment(0)
M
6

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.

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

You could get fancy and do geometry instancing etc, but just get the basics going first.

Macilroy answered 19/8, 2008 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.