# language agnostic - 100 lebegő kocka megjelenítése a DirectX vagy az OpenGL használatával

original title: "language agnostic - Displaying 100 Floating Cubes Using DirectX OR OpenGL"

Translate

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.

100 lebegő kockát szeretnék megjeleníteni DirectX vagy OpenGL használatával. Vagy valamilyen forráskódot vagy a technika leírását keresem. Nekem van egy kocka megjelenítésével.

Ez az összefoglalás a fordítás után. Ha meg szeretné tekinteni a teljes fordítást, kattintson a "fordítás" ikonra

Minden válasz
• Translate

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

• Translate

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.

• Translate

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.