I’m just writing a Rubik cube in C++ and OpenGL. It’s the first time I use OpenGL, so I don’t see clearly what things I have to model manually, and what I can have OpenGL doing it for me.
First thing I did was to draw a Rubik cube. I used Cube objects, each having 6 Side objects, and for each side I keep the information of 4 Vertex objects. So in the end I am drawing those sides using GL_QUADS [Question: I’m not sure I need that Vertex class, or I could simply use an OpenGL data structure for the vertices information]
Now, when I want to rotate a Slice of the Rubik cube, I am not sure if I have to manually rotate all those vertices, or I can have OpenGL doing it for me. The two options I’m thinking about are:
- I have a Slice::rotate that will do a 90º rotation in steps. It runs in a separate thread, so that the display function can be called during the rotation, and each step be shown. For each step, it calls Cube::rotate(Vertex rotationAxis, angle). Then, in Cube::rotate, I have to update the cube’s position, and call Side::rotate for each of the sides, which will basically update all the vertices.
I think I need loads of matrix algebra here: creating vectors (rotationAxis->vertexToRotate), rotating those vectors, and updating them [Question: do you know if boost ublas would be a good option?]
I could simply rotate a slice with some OpenGL code in Slice::rotate. Something like:
glTranslatef(center.getX(), center.getY(), center.getZ());
glRotatef(angle, plane == x, plane == y, plane == z);
glTranslatef(-center.getX(), -center.getY(), -center.getZ());
Then, I would just update my model information (cubes’ positions, side’s vertices) at the end of a rotation. The problem I see here is the display function. If it’s called in the middle of a rotation, I should keep account of those cubes that are being moved, so that when I am about to draw them, I do all those OpenGL transforms.
[Question: what option to go for, if any of these two? My intuition tells me it’ll have to be the first one, but…]