its quite a few years when I learned 3D graphics ...
All of the sites I used are gone now (as usual on the WEB but there are tons of new ones). I am OpenGL user so I recommend to use that. Many of mine students liked NEHE tutorials so look here. Also look at 4x4 transform matrices (homogenous coordinates).
Z-Buffer is automatic in OpenGL just create context with Z-Buffer (all tutorials use it) and call glEnable(GL_DEPTH_TEST);
Face culling (skip the other side of object) is done by glEnable(GL_CULL_FACE);
and drawing faces in the same polygon winding (CW
or CCW
)
Here few related questions:
- Understanding 4x4 homogenous transform matrices
- 3D (software) render pipeline computation
- some transform matrices 3x3 and 4x4 insights
- how to project points and vectors ... fixed pipeline OpenGL will do it for you
- code for inverse 4x4 matrix
- this is mine clean OpenGL app example in borland/embarcadero BDS2006 Turbo C++
- some projection math
And some good sites to look at (strongly recommend):
Sorry for that list of links but I think they are relevant and copy their stuff here would be too much. Now back to your question how to render more cubes. I see few options (in OpenGL):
every cube has its own transform matrix
Representing its position and orientation in space then just before rendering each cube change
GL_MODELVIEW
matrix and draw cube (with the same code for each). If you have too much objects/cubes it will consume big chunk of memory (+16 floats per cube).every cube is aligned to each other
in this case you need to know just the 3D position (+3 floats per cube) so just do something like this:
glMatrixMode(GL_MODELVIEW); // this store original matrix glPushMatrix(); glTranslatef(x[i],y[i],z[i]); // position of i-th cube // here render cube (like glBegin(GL_QUADS); ... or use glDraw... ) glMatrixMode(GL_MODELVIEW); // this restore original matrix glPopMatrix();
use of shaders
in modern shader pipeline you can use geometry shader to emit cube on receiving point but this is too much for OpenGL beginner. But in this case you would draw just points and the shader will convert them to cubes on GPU which is much faster ...
use VBO or VAO
- VAO is vertex array object (list of VBO's)
- VBO is vertex buffer object
VBO is basically an array of parameters copied to GPU as one chunk and not by individual calls likeglVertex,glColor,glNormal
... which is much much faster. This allows you to create model of your space (all cubes) and draw it at once with enough speed unless you hit the GPU/CPU/MEM speed limitsVAO similarly groups more VBOs together so you need to bind just single VAO per object instead of one VBO for each one parameter further reducing the API calls number needed for rendering.