-
Notifications
You must be signed in to change notification settings - Fork 0
/
rubik.c
585 lines (506 loc) · 15.4 KB
/
rubik.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
/*
* rubik.c
*
* Created on: 9 DEC 2019
* Author: Connor Berry
*/
#ifdef __APPLE__ // include Mac OS X verions of headers
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else // non-Mac OS X operating systems
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/freeglut_ext.h>
#endif // __APPLE__
#include "initShader.h"
#include "libmatrix.h"
#include "solve_rc.c"
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define BUFFER_OFFSET(offset) ((GLvoid *)(offset))
#define CUBE_SIZE 0.15
#define CUBE_VERTICES 72
#define CUBIES 27
#define VERTICES_SIZE CUBE_VERTICES *CUBIES
#define DEGREE_DELTA 2
#define TROTATION_BOUND DEGREE_DELTA *(M_PI / 180)
#define TCALL_COUNT 90 / DEGREE_DELTA
#define GAP 0.01
#define SCRAMBLE_DEPTH 40
// int DEGREE_DELTA = 2;
int CALL_COUNT = 90 / DEGREE_DELTA;
float ROTATION_BOUND = DEGREE_DELTA * (M_PI / 180);
void keyboard(unsigned char, int, int);
void display(void);
vec4 colors[VERTICES_SIZE];
vec4 vertices[VERTICES_SIZE];
int num_vertices = VERTICES_SIZE;
GLuint ctm_location;
mat4 rotation_matrix;
mat4 origin_matrix;
float eye_degree = 0.005;
mat4 cubies[CUBIES];
int i_up[9] = {6, 15, 24, 7, 16, 25, 8, 17, 26};
int i_down[9] = {2, 11, 20, 1, 10, 19, 0, 9, 18};
int i_right[9] = {26, 25, 24, 23, 22, 21, 20, 19, 18};
int i_left[9] = {6, 7, 8, 3, 4, 5, 0, 1, 2};
int i_back[9] = {24, 15, 6, 21, 12, 3, 18, 9, 0};
int i_front[9] = {8, 17, 26, 5, 14, 23, 2, 11, 20};
char movements[7] = {'U', 'D', 'L', 'R', 'B', 'F', 'U'};
void animation(mat4 rotation, int range[9])
{
for(int i = 0; i < CALL_COUNT; i++)
{
for(int x = 0; x < 9; x++)
{
cubies[range[x]] = matrix_multiply(cubies[range[x]], rotation);
}
display();
}
}
void back()
{
puts("Back");
rotation_matrix = identity();
animation(rotation_z_matrix(-ROTATION_BOUND), i_back);
// change backs
int s = i_back[0];
i_back[0] = i_back[6];
i_back[6] = i_back[8];
i_back[8] = i_back[2];
i_back[2] = s;
s = i_back[1];
i_back[1] = i_back[3];
i_back[3] = i_back[7];
i_back[7] = i_back[5];
i_back[5] = s;
//change rights
i_right[2] = i_back[0];
i_right[5] = i_back[3];
i_right[8] = i_back[6];
//change lefts
i_left[0] = i_back[2];
i_left[3] = i_back[5];
i_left[6] = i_back[8];
//change up
i_up[0] = i_back[2];
i_up[1] = i_back[1];
i_up[2] = i_back[0];
//change down
i_down[6] = i_back[8];
i_down[7] = i_back[7];
i_down[8] = i_back[6];
r_string_back();
}
void front()
{
puts("Front");
rotation_matrix = identity();
animation(rotation_z_matrix(ROTATION_BOUND), i_front);
// change fronts
int s = i_front[0];
i_front[0] = i_front[6];
i_front[6] = i_front[8];
i_front[8] = i_front[2];
i_front[2] = s;
s = i_front[1];
i_front[1] = i_front[3];
i_front[3] = i_front[7];
i_front[7] = i_front[5];
i_front[5] = s;
//change rights
i_right[0] = i_front[2];
i_right[3] = i_front[5];
i_right[6] = i_front[8];
//change lefts
i_left[2] = i_front[0];
i_left[5] = i_front[3];
i_left[8] = i_front[6];
//change up
i_up[6] = i_front[0];
i_up[7] = i_front[1];
i_up[8] = i_front[2];
//change down
i_down[0] = i_front[6];
i_down[1] = i_front[7];
i_down[2] = i_front[8];
r_string_front();
}
void up()
{
puts("Up");
rotation_matrix = identity();
animation(rotation_y_matrix(ROTATION_BOUND), i_up);
int s = i_up[0];
i_up[0] = i_up[6];
i_up[6] = i_up[8];
i_up[8] = i_up[2];
i_up[2] = s;
s = i_up[1];
i_up[1] = i_up[3];
i_up[3] = i_up[7];
i_up[7] = i_up[5];
i_up[5] = s;
//change right
i_right[0] = i_up[8];
i_right[1] = i_up[5];
i_right[2] = i_up[2];
//change left
i_left[0] = i_up[0];
i_left[1] = i_up[3];
i_left[2] = i_up[6];
//change front
i_front[0] = i_up[6];
i_front[1] = i_up[7];
i_front[2] = i_up[8];
//change back
i_back[0] = i_up[2];
i_back[1] = i_up[1];
i_back[2] = i_up[0];
r_string_up();
}
void down()
{
puts("Down");
rotation_matrix = identity();
animation(rotation_y_matrix(-ROTATION_BOUND), i_down);
int s = i_down[0];
i_down[0] = i_down[6];
i_down[6] = i_down[8];
i_down[8] = i_down[2];
i_down[2] = s;
s = i_down[1];
i_down[1] = i_down[3];
i_down[3] = i_down[7];
i_down[7] = i_down[5];
i_down[5] = s;
//change right
i_right[6] = i_down[2];
i_right[7] = i_down[5];
i_right[8] = i_down[8];
//change left
i_left[6] = i_down[6];
i_left[7] = i_down[3];
i_left[8] = i_down[0];
//change front
i_front[6] = i_down[0];
i_front[7] = i_down[1];
i_front[8] = i_down[2];
//change back
i_back[6] = i_down[8];
i_back[7] = i_down[7];
i_back[8] = i_down[6];
r_string_down();
}
void right()
{
puts("Right");
rotation_matrix = identity();
animation(rotation_x_matrix(ROTATION_BOUND), i_right);
int s = i_right[0];
i_right[0] = i_right[6];
i_right[6] = i_right[8];
i_right[8] = i_right[2];
i_right[2] = s;
s = i_right[1];
i_right[1] = i_right[3];
i_right[3] = i_right[7];
i_right[7] = i_right[5];
i_right[5] = s;
//change up
i_up[8] = i_right[0];
i_up[5] = i_right[1];
i_up[2] = i_right[2];
//change down
i_down[2] = i_right[6];
i_down[5] = i_right[7];
i_down[8] = i_right[8];
//change front
i_front[2] = i_right[0];
i_front[5] = i_right[3];
i_front[8] = i_right[6];
//change back
i_back[0] = i_right[2];
i_back[3] = i_right[5];
i_back[6] = i_right[8];
r_string_right();
}
void left()
{
puts("Left");
rotation_matrix = identity();
animation(rotation_x_matrix(-ROTATION_BOUND), i_left);
int s = i_left[0];
i_left[0] = i_left[6];
i_left[6] = i_left[8];
i_left[8] = i_left[2];
i_left[2] = s;
s = i_left[1];
i_left[1] = i_left[3];
i_left[3] = i_left[7];
i_left[7] = i_left[5];
i_left[5] = s;
//up
i_up[0] = i_left[0];
i_up[3] = i_left[1];
i_up[6] = i_left[2];
//down
i_down[6] = i_left[6];
i_down[3] = i_left[7];
i_down[0] = i_left[8];
//front
i_front[0] = i_left[2];
i_front[3] = i_left[5];
i_front[6] = i_left[8];
//back
i_back[2] = i_left[0];
i_back[5] = i_left[3];
i_back[8] = i_left[6];
r_string_left();
}
void shuffle()
{
// CALL_COUNT = 1;
// ROTATION_BOUND = 90 * (M_PI/180);
puts("Scrambling...");
srand(time(NULL));
char s[SCRAMBLE_DEPTH];
for (int i = 0; i < SCRAMBLE_DEPTH; i++)
{
int c = rand() % 6;
s[i] = movements[c];
}
puts(s);
for (int i = 0; i < SCRAMBLE_DEPTH; i++)
{
// printf("%c ", s[i]);
keyboard(s[i], 0, 0);
}
puts("Done scrambling!");
// CALL_COUNT = TCALL_COUNT;
// ROTATION_BOUND = TROTATION_BOUND;
}
void solve()
{
puts("Solving");
char *sol = solve_rc();
puts(sol);
void (*fn)();
int times, index = 0;
char pivot;
int strl = strlen(sol);
for (int i = 0; i < strl; i += 2)
{
pivot = sol[index];
times = (int)(sol[index + 1] - '0');
switch (pivot)
{
case 'U':
fn = up;
break;
case 'D':
fn = down;
break;
case 'L':
fn = left;
break;
case 'R':
fn = right;
break;
case 'F':
fn = front;
break;
case 'B':
fn = back;
break;
default:
fn = up;
}
for (int x = 0; x < times; x++)
{
fn();
}
index += 2;
}
puts("Done solving");
}
void arrays_init(void)
{
for (int i = 0; i < CUBIES; i++)
{
cubies[i] = identity();
}
rotation_matrix = identity();
origin_matrix = translate(-(CUBE_SIZE - GAP) / 2, -(CUBE_SIZE - GAP) / 2, -(CUBE_SIZE - GAP) / 2);
fill_colors(colors, VERTICES_SIZE);
for (int i = 0; i < CUBIES; i++)
{
generate_cube(vertices, VERTICES_SIZE, CUBE_SIZE - GAP, i * CUBE_VERTICES);
uniform_transform(vertices, origin_matrix, i * CUBE_VERTICES, i * CUBE_VERTICES + CUBE_VERTICES);
}
// bottom left row: 0 - CUBE_VERTICES * 3
uniform_transform(vertices, translate(-CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE), 0, CUBE_VERTICES); // b & d
uniform_transform(vertices, translate(-CUBE_SIZE, -CUBE_SIZE, 0.0), CUBE_VERTICES, CUBE_VERTICES * 2); // d
uniform_transform(vertices, translate(-CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE), CUBE_VERTICES * 2, CUBE_VERTICES * 3); // f & d
// middle left row: CUBE_VERTICES*3 - CUBE_VERTICES*6
uniform_transform(vertices, translate(-CUBE_SIZE, 0.0, -CUBE_SIZE), CUBE_VERTICES * 3, CUBE_VERTICES * 4); // b & l
uniform_transform(vertices, translate(-CUBE_SIZE, 0.0, 0.0), CUBE_VERTICES * 4, CUBE_VERTICES * 5); // l
uniform_transform(vertices, translate(-CUBE_SIZE, 0.0, CUBE_SIZE), CUBE_VERTICES * 5, CUBE_VERTICES * 6); // f & l
// top left row: CUBE_VERTICES*6 - CUBE_VERTICES*9
uniform_transform(vertices, translate(-CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE), CUBE_VERTICES * 6, CUBE_VERTICES * 7); // b & u
uniform_transform(vertices, translate(-CUBE_SIZE, CUBE_SIZE, 0.0), CUBE_VERTICES * 7, CUBE_VERTICES * 8); // u & l
uniform_transform(vertices, translate(-CUBE_SIZE, CUBE_SIZE, CUBE_SIZE), CUBE_VERTICES * 8, CUBE_VERTICES * 9); // f & u
// bottom middle row: CUBE_VERTICES*9 - CUBE_VERTICES*12
uniform_transform(vertices, translate(0.0, -CUBE_SIZE, -CUBE_SIZE), CUBE_VERTICES * 9, CUBE_VERTICES * 10); // d & b
uniform_transform(vertices, translate(0.0, -CUBE_SIZE, 0.0), CUBE_VERTICES * 10, CUBE_VERTICES * 11); // d
uniform_transform(vertices, translate(0.0, -CUBE_SIZE, CUBE_SIZE), CUBE_VERTICES * 11, CUBE_VERTICES * 12); // f && d
// middle middle row: CUBE_VERTICES*12 - CUBE_VERTICES*15
uniform_transform(vertices, translate(0.0, 0.0, -CUBE_SIZE), CUBE_VERTICES * 12, CUBE_VERTICES * 13);
uniform_transform(vertices, translate(0.0, 0.0, 0.0), CUBE_VERTICES * 13, CUBE_VERTICES * 14);
uniform_transform(vertices, translate(0.0, 0.0, CUBE_SIZE), CUBE_VERTICES * 14, CUBE_VERTICES * 15); // f
// top middle row: CUBE_VERTICES*15 - CUBE_VERTICES*18
uniform_transform(vertices, translate(0.0, CUBE_SIZE, -CUBE_SIZE), CUBE_VERTICES * 15, CUBE_VERTICES * 16);
uniform_transform(vertices, translate(0.0, CUBE_SIZE, 0.0), CUBE_VERTICES * 16, CUBE_VERTICES * 17);
uniform_transform(vertices, translate(0.0, CUBE_SIZE, CUBE_SIZE), CUBE_VERTICES * 17, CUBE_VERTICES * 18); // f
// bottom right row: CUBE_VERTICES*18 - CUBE_VERTICES*21
uniform_transform(vertices, translate(CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE), CUBE_VERTICES * 18, CUBE_VERTICES * 19);
uniform_transform(vertices, translate(CUBE_SIZE, -CUBE_SIZE, 0.0), CUBE_VERTICES * 19, CUBE_VERTICES * 20);
uniform_transform(vertices, translate(CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE), CUBE_VERTICES * 20, CUBE_VERTICES * 21); // f
// middle right row: CUBE_VERTICES*21 - CUBE_VERTICES*24
uniform_transform(vertices, translate(CUBE_SIZE, 0.0, -CUBE_SIZE), CUBE_VERTICES * 21, CUBE_VERTICES * 22);
uniform_transform(vertices, translate(CUBE_SIZE, 0.0, 0.0), CUBE_VERTICES * 22, CUBE_VERTICES * 23);
uniform_transform(vertices, translate(CUBE_SIZE, 0.0, CUBE_SIZE), CUBE_VERTICES * 23, CUBE_VERTICES * 24); // f
// top right row: CUBE_VERTICES*24 - CUBE_VERTICES*27
uniform_transform(vertices, translate(CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE), CUBE_VERTICES * 24, CUBE_VERTICES * 25);
uniform_transform(vertices, translate(CUBE_SIZE, CUBE_SIZE, 0.0), CUBE_VERTICES * 25, CUBE_VERTICES * 26);
uniform_transform(vertices, translate(CUBE_SIZE, CUBE_SIZE, CUBE_SIZE), CUBE_VERTICES * 26, CUBE_VERTICES * 27); // f
}
void init(void)
{
// initialize shader programs
GLuint program = initShader("vshader_rubik.glsl", "fshader_rubik.glsl");
glUseProgram(program);
// vertex array object initialization and binds it for buffering and rendering
// it is possible to have multiple of these arrays for different sets of vertices
GLuint vao;
// args: 1 array, by reference of vao
glGenVertexArrays(1, &vao);
// bind it to the pipeline
glBindVertexArray(vao);
// transfer vertex attributes into the graphics pipeline
GLuint buffer;
// initialize buffer (one buffer)
glGenBuffers(1, &buffer);
// bind buffer to pipeline
glBindBuffer(GL_ARRAY_BUFFER, buffer);
// allocate the space for the buffer (size of both vertices and the colors)
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) + sizeof(colors), NULL, GL_STATIC_DRAW);
// copies data from the vertices array into the buffer, starting from 0 to the end of vertices
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
// copies data from the colors array into the buffer, starting where vertices ended
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(colors), colors);
// vshader attributes initializations
// locate the attribute named vPosition in the vertex shader program
GLuint vPosition = glGetAttribLocation(program, "vPosition");
// enable this attribute
glEnableVertexAttribArray(vPosition);
// assign a pointer to vPosition
// 1: specifies the index of the generic vertex attribute to be modified
// 2: specifies the number of components for this attribute (vec4)
// 3: specifies the data type of each component
// 4: specifies whether fixed-point data values should be normalized
// 5: specifies the byte offset between consecutive vertex attributes
// 6: specifies an offset of the first component of the first generic
// attribute in the arrray/buffer
glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// locate the attribute named vColor in the shader program
GLuint vColor = glGetAttribLocation(program, "vColor");
// enable this attribute
glEnableVertexAttribArray(vColor);
// see above explanation, except this time we specify that the offset in the buffer
// is after the block of position vertices
glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid *)sizeof(vertices));
// stores the location of one particular uniform variable in from the program
// for use later in displaying and controlling the uniform variable
ctm_location = glGetUniformLocation(program, "ctm");
// enable hidden surface removal
glEnable(GL_DEPTH_TEST);
// set the clearing color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
// set depth range from 1.0 to 0.0
glDepthRange(1, 0);
}
void display(void)
{
// clear color buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set polygon modes to fill triangles in from the front, and mesh lines in the back
glPolygonMode(GL_FRONT, GL_FILL);
glPolygonMode(GL_BACK, GL_LINE);
// set matrix to whatever corresponds to this cubie
// then draw the respective array for that cubie
for (int i = 0; i < CUBIES; i++)
{
glUniformMatrix4fv(ctm_location, 1, GL_FALSE, (GLfloat *)&cubies[i]);
glDrawArrays(GL_TRIANGLES, i * CUBE_VERTICES, CUBE_VERTICES);
}
// right now we will only draw the front, with it's respective CTM
glutSwapBuffers();
}
void keyboard(unsigned char key, int mousex, int mousey)
{
if (key == 'q')
exit(0);
if (key == 'p')
rotation_matrix = matrix_multiply(rotation_y_matrix(eye_degree), rotation_matrix);
if (key == 'P')
rotation_matrix = matrix_multiply(rotation_x_matrix(-eye_degree), rotation_matrix);
if (key == '[')
rotation_matrix = identity();
if (key == '?')
matrix_print(rotation_matrix);
if (key == 'f' || key == 'F')
front();
if (key == 'r' || key == 'R')
right();
if (key == 'u' || key == 'U')
up();
if (key == 'l' || key == 'L')
left();
if (key == 'b' || key == 'B')
back();
if (key == 'd' || key == 'D')
down();
if (key == 's')
shuffle();
if (key == 'S')
solve();
}
void idle(void)
{
for (int i = 0; i < CUBIES; i++)
{
cubies[i] = matrix_multiply(cubies[i], rotation_matrix);
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
puts("(f)ront, (r)ight, (u)p, (l)eft, (b)ack, (d)own, (s)cramble, (S)olve (shift + s)\np and shift + p to spin cube, [ to stop.");
arrays_init();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(512, 512);
glutInitWindowPosition(100, 100);
glutCreateWindow("Rubik's Cube");
glewInit();
init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}