-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
403 lines (341 loc) · 9.17 KB
/
main.cpp
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
#include <windows.h>
#include <stack>
#define FREEGLUT_STATIC
#include <gl\freeglut.h>
#include "RubikCube.h"
#include "SOIL.h"
#include <iostream>
extern double rotateSpeed;
char title[256] = "Rubik's Cube, Created by Narusaki - rotate speed %4f";
using namespace std;
int curWin;
bool fullscreen;
RubikCube rubikCube;
GLuint textures[6];
const double depthShift = 0.0001;
//selected names and sub-cubes
vector<TriInt> selectCubes;
vector<GLuint> selectNames;
// variables to track which key of mouse is pressed
bool leftkey = false;
bool rightkey = false;
//picking buffer used in OpenGL
const int BUFFERSIZE = 512;
GLuint selectBuffer[BUFFERSIZE];
//viewport
GLint viewport[4];
//rotation parameter for one layer
int globalX, globalY;
int direction;
//a lock used to lock the rotating operation when the animation is being performed
bool isRotating = false;
//used for rotating animation
int rotateIteration = -1;
// stack to track the operations
std::stack<TriInt> operationRecords;
double globalShift = 8.0;
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (rotateIteration > 0)
rubikCube.RotateStep(globalX, globalY, direction);
else if(rotateIteration == 0)
{
rubikCube.RotateFinish(globalX, globalY, direction);
isRotating = false;
}
--rotateIteration;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -globalShift);
//first pass, rendering the lines
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDepthRange(0, 1.0 - depthShift);
rubikCube.Render(GL_LINE);
//second pass, rendering the faces
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDepthRange(depthShift, 1.0);
rubikCube.Render(GL_FILL);
glutSwapBuffers();
glutPostRedisplay();
}
void myMouseClick(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON)
{
if (state == GLUT_UP)
{
leftkey = false; rightkey = false;
return;
}
leftkey = true; rightkey = false;
rubikCube.trackBall.mouseClick(Vector2D(x, y));
glutPostRedisplay();
}
else if (button == GLUT_RIGHT_BUTTON)
{
if (isRotating) return;
leftkey = false; rightkey = true;
if (state == GLUT_UP)
{
selectCubes.clear();
for (int i = 0;i < selectNames.size();++i)
{
//decompose the sub-cube's name to integer-coordinates
TriInt curCube;
curCube.x = selectNames[i] % rubikCube.cubeOrder;
selectNames[i] /= rubikCube.cubeOrder; curCube.y = selectNames[i] % rubikCube.cubeOrder;
selectNames[i] /= rubikCube.cubeOrder; curCube.z = selectNames[i] % rubikCube.cubeOrder;
selectCubes.push_back(curCube);
}
//query for the layer and the rotating direction
if(!rubikCube.QueryLayer(selectCubes, globalX, globalY, direction))
{
selectNames.clear();
return;
}
//record the operation
TriInt curOp;
curOp.x = globalX; curOp.y = globalY; curOp.z = direction;
operationRecords.push(curOp);
//mark to start rotating
isRotating = true;
rotateIteration = 90.0f/rotateSpeed;
selectNames.clear();
leftkey = false; rightkey = false;
}
}
}
/************************************************************************/
/*Find the nearest sub-cube when mouse moves
@param hitsNum: hit counts of picking
The select buffer is the global variable selectBuffer. This function returns the name
of the selected (with the smallest z values) object (sub-cube)
*/
/************************************************************************/
GLuint findOutTheNearestCube(GLint hitsNum)
{
/*
TODO: find the nearest sub-cube. You need to compare the depth of all the sub-cubes selected,
and find the one with the minimal depth.
*/
return -1;
}
void myMouseMove(int x, int y)
{
if (leftkey)
{
rubikCube.trackBall.mouseMove(Vector2D(x, y));
glutPostRedisplay();
}
else if (rightkey)
{
if (isRotating) return;
glSelectBuffer(BUFFERSIZE, selectBuffer);
glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble)x, (GLdouble)(viewport[3]-y), 2.0, 2.0, viewport);
gluPerspective(45.0f, (GLdouble)viewport[2]/(GLdouble)viewport[3], 0.1, 100.0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDepthRange(depthShift, 1.0);
rubikCube.Render(GL_FILL);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
//glutPostRedisplay();
GLint hitsNum = glRenderMode(GL_RENDER);
GLuint pickedCube = findOutTheNearestCube(hitsNum);
if(pickedCube == -1) return;
if (selectNames.empty())
selectNames.push_back(pickedCube);
else
{
GLuint lastName = selectNames.back();
if (pickedCube != lastName) selectNames.push_back(pickedCube);
}
}
}
void myReshape(int width, int height)
{
if(height == 0) height = 1;
rubikCube.trackBall.reAdjustTrackBall(width, height);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLdouble)width/(GLdouble)height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetIntegerv(GL_VIEWPORT, viewport);
}
void myKeyboard(unsigned char key, int x, int y)
{
if (isRotating) return;
TriInt prevOp;
switch(key)
{
case VK_ESCAPE:
glutDestroyWindow(curWin);
exit(0);
break;
case '+':
rotateSpeed *= 2;
char curTitle[256];
sprintf(curTitle, title, rotateSpeed);
glutSetWindowTitle(curTitle);
break;
case '-':
rotateSpeed /= 2;
sprintf(curTitle, title, rotateSpeed);
glutSetWindowTitle(curTitle);
break;
case 'z':
if (!operationRecords.empty())
{
prevOp = operationRecords.top(); operationRecords.pop();
globalX = prevOp.x; globalY = prevOp.y; direction = -prevOp.z;
isRotating = true;
rotateIteration = 90.0f/rotateSpeed;
}
break;
case 't':
rubikCube.textureOn = !rubikCube.textureOn;
if (rubikCube.textureOn) glEnable(GL_TEXTURE_2D);
else glDisable(GL_TEXTURE_2D);
break;
case '1':
globalShift += 0.5;
break;
case '2':
globalShift -= 0.5;
break;
case '3':
rubikCube.ReAssignOrder(3);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
case '4':
rubikCube.ReAssignOrder(4);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
case '5':
rubikCube.ReAssignOrder(5);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
case '6':
rubikCube.ReAssignOrder(6);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
case '7':
rubikCube.ReAssignOrder(7);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
case '8':
rubikCube.ReAssignOrder(8);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
case '9':
rubikCube.ReAssignOrder(9);
rubikCube.AssignTextures(textures);
while (!operationRecords.empty()) operationRecords.pop();
break;
}
}
void mySpecial(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_F1:
if(!fullscreen)
{
glutFullScreen();
fullscreen = true;
}
else
{
glutReshapeWindow(640, 480);
fullscreen = false;
}
break;
}
glutPostRedisplay();
}
bool LoadTextures()
{
for (int i = 0; i < 6; ++i)
{
char fileName[255];
sprintf_s(fileName, "Textures/x-men_%d.png", i);
textures[i] = SOIL_load_OGL_texture(fileName,
SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
if (textures[i] == 0) return false;
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
rubikCube.AssignTextures(textures);
return true;
}
// Initialization of OpenGL
void myInit()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
GLfloat ambient[4] = { 0.1, 0.1, 0.1, 0.1 };
GLfloat diffuse[4] = { 0.7, 0.7, 0.7, 0.7 };
GLfloat position[4] = { 0.0, 0.0, 4.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
if (!LoadTextures())
{
cout << "Cannot load textures!" << endl;
return;
}
}
void OutputInformation()
{
cout << "Operations: " << endl;
cout << "Entire rotation: Left mouse + drag" << endl;
cout << "Layer rotation: Right mouse + drag" << endl;
cout << "1/2: zoom out/in" << endl;
cout << "3 to 9: Cube order" << endl;
cout << "t: Enable/Disable textures" << endl;
cout << "z: Undo" << endl;
cout << "+/-: Rotate speed up/slow down" << endl;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
char curTitle[256];
sprintf(curTitle, title, rotateSpeed);
curWin = glutCreateWindow(curTitle);
fullscreen = false;
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutKeyboardFunc(myKeyboard);
glutMouseFunc(myMouseClick);
glutMotionFunc(myMouseMove);
glutSpecialFunc(mySpecial);
myInit();
OutputInformation();
glutMainLoop();
return 0;
}