C++ : Designing a Rubiks Cube

jackson18249

Senior member
Jan 24, 2005
232
0
76
I am designing a Rubiks Cube for my final project in a basic OpenGL class and have gotten stuck in my project. Here is the basic code that I am having a problem with:


I created an array with 54 elements and basically each time the drawCube function is used (54 times) i want to store the verticies to the corresponding element of the array, but I have no idea how to do that. I need to do this in order to write a rotate function that will change the position of the squares each time the cube is rotated. I would greatly appreciate any help and will answer any questions asap. Thanks.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Of what data type are the vertices? Where is your array declared?

Also, it would be immensely helpful if you'd use the Attach Code function instead of pasting it into a post, as with the latter you lose indentation and risk code corruption.

So you just want to store (x, y, z) vertices for 54 cubes in an array?

If the vertices were of type double, you could declare an array of size 54*3 and access the array by using a data type of size double*3 via typecasting. That way, [0] would really correspond to [0]~[2], and [1] would correspond to [3]~[5], and so on. You could also use vectors, or just declare three different arrays of size 54 to store x, y, and z.
 

jackson18249

Senior member
Jan 24, 2005
232
0
76
So I attached the whole code, thanks for letting me know about that by the way xtknight. Right now the verticies are ints. I'm thinking that I just want to store the x, y, and z verticies for each of the four points for all 54 squares of the cube. I'm also not familiar with typecasting...
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Well, this is the easiest way to do it: use arrays. When you declare an array you need to specify the number of elements (n) you would like the array to contain. The elements in the array are addressed from zero (0) to (n-1).

int VARNAME[n]; creates an int array with n number of elements. first element: VARNAME[0], last element: VARNAME[n-1]

e.g. for x, y, and z:
int xs[54];
int ys[54];
int zs[54];

I assume when you speak of "four points" you are talking about the GL_QUADS (that is the only reference I could find to "four" in your code). If you need 4 sets of x,y,z vertices stored (4*3=12 elements total), then you must use a multidimensional array, like so:

int xs[54][4]; // allocates space for 54*4 ints

For each of the 54 elements, there are 4 integers that can be set.

xs[0][0] = 5;

Why don't you just make a struct for a 'vertex' also? That'll save a dimension in our array.

typedef struct VERTEX {
int x, y, z;
} vertex;

Now, you can make an array of 54*4 of these custom-defined 'vertex' types, can't you?

vertex arrStorage[54][4];

arrStorage[0][1].x = 13;
arrStorage[0][1].y = 14;
arrStorage[0][1].z = 43; // vertices for (1, 2): (10, 5, 8)

arrStorage[4][2].x = 10;
arrStorage[4][2].y = 5;
arrStorage[4][2].z = 8; // vertices for (5, 3): (10, 5, 8)

The total storage used for the array was 54*4*(sizeof(int)*3) bytes. Formatted code attached.

Edit: yuck, I meant a struct not a union. sorry about that!