-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHyper.CC
473 lines (378 loc) · 11.8 KB
/
Hyper.CC
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
// Greg Kaiser
//
// CS290 with Prof. Francis
// 4D Tetris
//
// Hyper.C
// Last modified: April 24, 1996
//
// (C) 1996 Board of Trustees University of Illinois
#include <iostream> // for debugging only
//#define DEBUG_HYPER
#include "Hyper.h"
#include "DList.h"
DList<Hyper> junk; // is this how to instantiate this template??
extern Board *board;
extern float *LEN;
extern int *DIM;
extern float *botcorner;
extern int SHADEUNDER;
// Given: Nothing
// Task: Just the default constructor
// Return: Nothing
Hyper::Hyper()
{
pts = new FourD*[16];
}
// Given: A low, high, and new board position
// Task: Construct a new hypercube whose real coordinates are based
// off of high and low, and whose board position is newpos
// Return: Nothing, but the hypercube has been constructed
Hyper::Hyper(float low, float high, int *newpos)
{
pts = new FourD*[16];
float temp[4];
for (int ii = 0; ii < 16; ii++) {
for (int jj = 0; jj < 4; jj++)
temp[jj] = ((ii & (1<<(3-jj))) ? low : high);
pts[ii] = new FourD(temp);
}
boardpos = new int[4];
for (int ii = 0; ii < 4; ii++) {
if (ii != VW)
Assert(newpos[ii] < DIM[ii], "Piece doesn't fit on board");
boardpos[ii] = newpos[ii];
}
}
// Given: A list of lows for each coordinate, a list of highs for each
// coordinate, and a new board position
// Task: Construct a new hypercube whose real coordinates are based
// off of the highs and lows, and whose board position is newpos
// Return: Nothing, but the hypercube has been constructed
Hyper::Hyper(float *low, float *high, int *newpos)
{
pts = new FourD*[16];
float temp[4];
for (int ii = 0; ii < 16; ii++) {
for (int jj = 0; jj < 4; jj++)
temp[jj] = ((ii & (1<<(3-jj))) ? low[jj] : high[jj]);
pts[ii] = new FourD(temp);
}
boardpos = new int[4];
for (int ii = 0; ii < 4; ii++) {
if (ii != VW)
Assert((newpos[ii] < DIM[ii]) && (newpos[ii] >= 0),
"Piece doesn't fit on board");
boardpos[ii] = newpos[ii];
}
}
// Given: Nothing
// Task: Delete the memory used by this hypercube
// Return: Nothing, but the memory has been cleared
Hyper::~Hyper()
{
for (int i = 0; i < 16; i++)
delete pts[i];
delete pts;
delete boardpos;
}
// Given: A color
// Task: Make the given color this color for this cube
// Return: Nothing, but the color of the cube has been set
void Hyper::SetColor(int newcolor)
{
color = newcolor;
}
// Given: Nothing
// Task: Dim the color of the cube, by halving each of the red, green
// and blue components
// Return: Nothing, but the color of the cube has been dimmed
void Hyper::DimColor()
{
int temp = color;
int rr = (temp & 0x000000ff) / 2;
temp = temp >> 8;
int gg = (temp & 0x000000ff) / 2;
temp = temp >> 8;
int bb = (temp & 0x000000ff) / 2;
color = 0xff000000 + (bb << 16) + (gg << 8) + rr;
}
// Given: Nothing
// Task: Nothing
// Return: The board position of this hypercube
int *Hyper::GetPos()
{
return boardpos;
}
// This is for debugging only
void Hyper::Output()
{
#ifdef DEBUG_HYPER
cout << "Begin Hyper Output" << endl;
#endif
for (int i = 0; i < 16; i++)
pts[i]->Output();
#ifdef DEBUG_HYPER
cout << "End Hyper Output" << endl;
#endif
}
// Given: A direction, and a sign
// Task: Translate this hypercube one unit (in terms of board coordinate) in
// the direction and sign designated
// Return: Nothing, but the hypercube has been translated
void Hyper::Translate(int dir, int sign)
{
float xx = ((dir == VX) ? (float)(sign)*LEN[VX] : 0.0);
float yy = ((dir == VY) ? (float)(sign)*LEN[VY] : 0.0);
float zz = ((dir == VZ) ? (float)(sign)*LEN[VZ] : 0.0);
float ww = ((dir == VW) ? (float)(sign)*LEN[VW] : 0.0);
for (int i = 0; i < 16; i++)
pts[i]->Translate(xx, yy, zz, ww);
boardpos[dir] += sign;
}
// Given: The three axes to draw, in a bitmask
// Task: Draw this hypercube in the three axes projection specified
// Return: Nothing, but the cube has been drawn
void Hyper::Drawit(int axes)
{
#ifdef DEBUG_HYPER
// cout << "Begin Hyper::Drawit()" << endl;
#endif
if ((boardpos[VW] < DIM[VW]) || (axes == 7)) {
// face 1, +x, +y
bgntmesh(); Plot(0, axes); Plot(1, axes);
Plot(2, axes); Plot(3, axes); endtmesh();
// face 2, +x, -y
bgntmesh(); Plot(4, axes); Plot(5, axes);
Plot(6, axes); Plot(7, axes); endtmesh();
// face 3, -x, +y
bgntmesh(); Plot(8, axes); Plot(9, axes);
Plot(10, axes); Plot(11, axes); endtmesh();
// face 4, -x, -y
bgntmesh(); Plot(12, axes); Plot(13, axes);
Plot(14, axes); Plot(15, axes); endtmesh();
// face 5, +x, +z
bgntmesh(); Plot(0, axes); Plot(1, axes);
Plot(4, axes); Plot(5, axes); endtmesh();
// face 6, +x, -z
bgntmesh(); Plot(2, axes); Plot(3, axes);
Plot(6, axes); Plot(7, axes); endtmesh();
// face 7, -x, +z
bgntmesh(); Plot(8, axes); Plot(9, axes);
Plot(12, axes); Plot(13, axes); endtmesh();
// face 8, -x, -z
bgntmesh(); Plot(10, axes); Plot(11, axes);
Plot(14, axes); Plot(15, axes); endtmesh();
// face 9, +x, +w
bgntmesh(); Plot(0, axes); Plot(2, axes);
Plot(4, axes); Plot(6, axes); endtmesh();
// face 10, +x, -w
bgntmesh(); Plot(1, axes); Plot(3, axes);
Plot(5, axes); Plot(7, axes); endtmesh();
// face 11, -x, +w
bgntmesh(); Plot(8, axes); Plot(10, axes);
Plot(12, axes); Plot(14, axes); endtmesh();
// face 12, -x, -w
bgntmesh(); Plot(9, axes); Plot(11, axes);
Plot(13, axes); Plot(15, axes); endtmesh();
// face 13, +y, +z
bgntmesh(); Plot(0, axes); Plot(1, axes);
Plot(8, axes); Plot(9, axes); endtmesh();
// face 14, +y, -z
bgntmesh(); Plot(2, axes); Plot(3, axes);
Plot(10, axes); Plot(11, axes); endtmesh();
// face 15, -y, +z
bgntmesh(); Plot(4, axes); Plot(5, axes);
Plot(12, axes); Plot(13, axes); endtmesh();
// face 16, -y, -z
bgntmesh(); Plot(6, axes); Plot(7, axes);
Plot(14, axes); Plot(15, axes); endtmesh();
// face 17, +y, +w
bgntmesh(); Plot(0, axes); Plot(2, axes);
Plot(8, axes); Plot(10, axes); endtmesh();
// face 18, +y, -w
bgntmesh(); Plot(1, axes); Plot(3, axes);
Plot(9, axes); Plot(11, axes); endtmesh();
// face 19, -y, +w
bgntmesh(); Plot(4, axes); Plot(6, axes);
Plot(12, axes); Plot(14, axes); endtmesh();
// face 20, -y, -w
bgntmesh(); Plot(5, axes); Plot(7, axes);
Plot(13, axes); Plot(15, axes); endtmesh();
// face 21, +z, +w
bgntmesh(); Plot(0, axes); Plot(4, axes);
Plot(8, axes); Plot(12, axes); endtmesh();
// face 22, +z, -w
bgntmesh(); Plot(1, axes); Plot(5, axes);
Plot(9, axes); Plot(13, axes); endtmesh();
// face 23, -z, +w
bgntmesh(); Plot(2, axes); Plot(6, axes);
Plot(10, axes); Plot(14, axes); endtmesh();
// face 24, -z, -w
bgntmesh(); Plot(3, axes); Plot(7, axes);
Plot(11, axes); Plot(15, axes); endtmesh();
}
#ifdef DEBUG_HYPER
// cout << "End Hyper::Drawit()" << endl;
#endif
}
// Given: The index number of a point, and the three axes for projection
// Task: Plot pt[index] in the given projection, using my cheesy shading
// scheme for the point
// Return: Nothing, but the point has been plotted
void Hyper::Plot(int index, int axes)
{
int temp = color;
int rr = temp & 0x000000ff;
temp = temp >> 8;
int gg = temp & 0x000000ff;
temp = temp >> 8;
int bb = temp & 0x000000ff;
int ans = index%2;
ans += ((index+1)%4) ? 0 : 1;
ans += ((index+1)%8) ? 0 : 1;
ans += ((index+1)%16) ? 0 : 1;
rr -= ans*rr/5;
gg -= ans*gg/5;
bb -= ans*bb/5;
cpack((0xff000000 + (bb << 16) + (gg << 8) + rr));
//int *axis, notused;
//axis = new int[3];
int axis[3], notused;
switch (axes) {
case 7:
axis[0] = VX;
axis[1] = VY;
axis[2] = VZ;
notused = VW;
break;
case 11:
axis[0] = VW;
axis[1] = VX;
axis[2] = VY;
notused = VZ;
break;
case 13:
axis[0] = VZ;
axis[1] = VW;
axis[2] = VX;
notused = VY;
break;
case 14:
axis[0] = VY;
axis[1] = VZ;
axis[2] = VW;
notused = VX;
break;
}
//float *dist = new float[3];
float dist[3];
for (int i = 0; i < 3; i++) {
dist[i] = 1.0;
if ((*pts[0])[axis[i]] > (*pts[15])[axis[i]]) {
if ((*pts[index])[axis[i]] != (*pts[0])[axis[i]])
dist[i] = -1.0;
} else
if ((*pts[index])[axis[i]] != (*pts[15])[axis[i]])
dist[i] = -1.0;
dist[i] *= (float)(DIM[notused] - boardpos[notused]);
}
pts[index]->Plot(axis, dist);
//delete axis;
//delete dist;
}
// Given: The three axes for projection
// Task: Draw the shadow of this cube on the floor of the playing surface
// Return: Nothing, but the shadow has been drawn
void Hyper::Drawit2D(int axes)
{
float wcoord = botcorner[VW] - .005;
float wcoord2 = botcorner[VW] - .02;
float mypoints[8][3];
int i;
switch (axes) {
case 11: // wxy
for (int i = 0; i < 4; i++)
mypoints[i][0] = wcoord;
for (int i = 4; i < 8; i++)
mypoints[i][0] = wcoord2;
for (int i = 0; i < 4; i++)
mypoints[2*i][1] = (*pts[0])[VX];
for (int i = 0; i < 4; i++)
mypoints[2*i+1][1] = (*pts[15])[VX];
//mypoints[0][1] = mypoints[2][1] = (*pts[0])[VX];
//mypoints[1][1] = mypoints[3][1] = (*pts[15])[VX];
mypoints[0][2] = mypoints[1][2] = mypoints[4][2] = mypoints[5][2] =
(*pts[0])[VY];
mypoints[2][2] = mypoints[3][2] = mypoints[6][2] = mypoints[7][2] =
(*pts[15])[VY];
break;
case 13: // zwx
for (int i = 0; i < 4; i++)
mypoints[i][1] = wcoord;
for (int i = 4; i < 8; i++)
mypoints[i][1] = wcoord2;
for (int i = 0; i < 4; i++)
mypoints[2*i][0] = (*pts[0])[VZ];
for (int i = 0; i < 4; i++)
mypoints[2*i+1][0] = (*pts[15])[VZ];
//mypoints[0][0] = mypoints[2][0] = (*pts[0])[VZ];
//mypoints[1][0] = mypoints[3][0] = (*pts[15])[VZ];
mypoints[0][2] = mypoints[1][2] = mypoints[4][2] = mypoints[5][2] =
(*pts[0])[VX];
mypoints[2][2] = mypoints[3][2] = mypoints[6][2] = mypoints[7][2] =
(*pts[15])[VX];
break;
case 14: // yzw
for (int i = 0; i < 4; i++)
mypoints[i][2] = wcoord;
for (int i = 4; i < 8; i++)
mypoints[i][2] = wcoord2;
for (int i = 0; i < 4; i++)
mypoints[2*i][0] = (*pts[0])[VY];
for (int i = 0; i < 4; i++)
mypoints[2*i+1][0] = (*pts[15])[VY];
//mypoints[0][0] = mypoints[2][0] = (*pts[0])[VY];
//mypoints[1][0] = mypoints[3][0] = (*pts[15])[VY];
mypoints[0][1] = mypoints[1][1] = mypoints[4][1] = mypoints[5][1] =
(*pts[0])[VZ];
mypoints[2][1] = mypoints[3][1] = mypoints[6][1] = mypoints[7][1] =
(*pts[15])[VZ];
break;
}
//cpack(0xff000000);
// glEnable(GL_BLEND);
// blendfunction(BF_SA, BF_MSA);
// glColor4f(.2,.2,.2,.1);
glColor4f(1,1,1,.1);
bgntmesh();
for (int i = 0; i < 4; i++)
v3f(mypoints[i]);
endtmesh();
if (SHADEUNDER) {
bgntmesh();
for (int i = 4; i < 8; i++)
v3f(mypoints[i]);
endtmesh();
}
// glDisable(GL_BLEND);
}
// Given: A new board position, a center for rotation, and the two
// axes and the sign for the rotation
// Task: Set the board position to the one which was passed in, and
// change the points of the cube by rotating them about the
// given center in the given axes in the given direction
// Return: Nothing, but the rotation has been performed
void Hyper::Rotate(int *newpos, FourD *center, int dir, int dude, int sign)
{
#ifdef DEBUG_HYPER
cout << "Begin Hyper::Rotate(newpos, center, "<<dir<<", "<<sign<<")"<<endl;
#endif
for (int i = 0; i < 16; i++)
pts[i]->Rotate(center, dir, dude, sign);
for (int i = 0; i < 4; i++)
boardpos[i] = newpos[i];
#ifdef DEBUG_HYPER
cout << "End Hyper::Rotate(...)" << endl;
#endif
}