This is haunting me since several weeks, I didn’t end my researches on it because I’m currently overloaded and it’s keeping me in behind the first-year CS (opengl) college lessons that first made me research this: how to draw all the faces of a cube with only one for loop.
The exercise said us to list the coordinates of all the vertices of a square, then of a cube, then to draw one. I quickly noticed since it was a cube and all lengths were equal this mapped to binary. I noticed the square vertices coordinates naturally mapped to gray code which I studied formerly in digital electronics in first semester. I wondered if it was doing something for the cube and it in fact was going through all contiguuous vertices, what I then found to be called a hamiltonian path or an eulerian cycle (I’m not sure, I saw that linked to the snake in a box problem on these pages, including gray code), but it didn’t allow me to compute each (preferably contiguous, so I can use GL_QUADS or even GL_QUAD_STRIP) face in order to factorize my drawing of the cube (though it should allow me to compute a — I guess — specially-ordered array of all lines, maybe then from there I can compute each face?).
I also tried to do it with opengl transformations, as we kept going in the lesson learning them, as I guess it may defer more calculations to the gpu instead of the cpu, and almost succeeded, yet my for loop takes 8 iterations so there are 2 useless hidden faces somewhere:
void
face (void)
{
glPushMatrix();
{
glTranslatef(0, 0, 0.5);
glRectf(-0.5, -0.5, 0.5, 0.5);
}
glPopMatrix();
}
void
cube (void)
{
for (unsigned int i=0; i < 8 ; ++i)
{
glColor3f(!(i&4), !(i&2), !(i&1));
face();
glRotatef(180, !!(i&4), !!(i&2), !!(i&1));
}
}
I’m somewhat trying to continue my researches, though I’m lacking time and usually-finally-failing ideas keep popping in my mind regularly about this. And I now use to loose more than 1 hour on continuing this each time I’m being ask to draw a cube for something.
Something that thrilled me also is that if I succeed to factorize the procedurale computation of a cube, that might be abstracted from the notion of dimension and then generalizing the same algorithm to the nth dimension should be easy, thus giving an easy, natural and simple way to draw a tesseract or any hypercube… That match a long way of experiment I regularly go through of trying to generalize everything that’s got to be given to me at the nth-dimension (first did that when they asked to draw a spiral in 2D).
PS: for everything related to maths of dimensional space, n-dimensions, distribution of vertices, lines, faces in an ordered way, and relation to gray code, should I also ask on math.stackexchange.com?
The 6 sides of the cube can be generated by alternating 90 degree rotations around the x- and y-axis:
+---+
| 1 |
+---+---+
| 2 | 3 |
+---+---+---+
| 4 | 5 |
+---+---+
| 6 |
+---+
This can be coded like this:
for( int i=0; i<6; ++i)
{
glColor3f(!(i&4), !(i&2), !(i&1));
face();
glRotatef(90.0f, (float)(i%2), (float)(1-(i%2)), 0.0f);
}
Preview:
void face (void)
{
glPushMatrix();
glTranslatef(0, 0, 0.5);
glRectf(-0.3, -0.3, 0.3, 0.3);
glPopMatrix();
}
The following gives a similar result. It is not the same as the above one, because the sides of the cube are twisted around their normal vectors, too.
glRotatef(180.0f, (float)(i%2), (float)(1-(i%2)), 1.0f);