#ifdef __APPLE__ #include #else #include #endif #include #include #include #include #define WINDOW_WIDTH 1200 #define WINDOW_HEIGHT 1200 #define LATTICE_WIDTH 1000 #define LATTICE_HEIGHT 707 #define LENGTH sizeof(short) #define LAST LENGTH-1 #define POINT_SIZE 2 #define SPACING 1 short c1[LATTICE_WIDTH][LATTICE_HEIGHT]; short c2[LATTICE_WIDTH][LATTICE_HEIGHT]; short c3[LATTICE_WIDTH][LATTICE_HEIGHT]; short c4[LATTICE_WIDTH][LATTICE_HEIGHT]; short c5[LATTICE_WIDTH][LATTICE_HEIGHT]; short c6[LATTICE_WIDTH][LATTICE_HEIGHT]; short k1[LATTICE_WIDTH][LATTICE_HEIGHT]; short k2[LATTICE_WIDTH][LATTICE_HEIGHT]; short k3[LATTICE_WIDTH][LATTICE_HEIGHT]; short k4[LATTICE_WIDTH][LATTICE_HEIGHT]; short k5[LATTICE_WIDTH][LATTICE_HEIGHT]; short k6[LATTICE_WIDTH][LATTICE_HEIGHT]; short boundary[LATTICE_WIDTH][LATTICE_HEIGHT]; int mouse_x, mouse_y; unsigned int next_rand; int random_int() { next_rand = next_rand * 1103515245 + 12345; return (unsigned int) (next_rand / 65536); } int flip_coin() { int r = random_int(); return r % 2; } void next() { int x, y; short a, b, c, d, e, f, tripple, db1, db2, db3, ro; /* * 3 2 * c b * \ / * \ / * \ / * \ / * 4 ---[]--- 1 * d ---[]--- a * / \ * / \ * / \ * / \ * 5 6 * e f * */ /************************************* ** COLLISION ** *************************************/ for (x = 0; x < LATTICE_WIDTH; x++) { for (y = 0; y < LATTICE_HEIGHT; y++) { a = c1[x][y]; b = c2[x][y]; c = c3[x][y]; d = c4[x][y]; e = c5[x][y]; f = c6[x][y]; tripple = (a ^ b) & (b ^ c) & (c ^ d) & (d ^ e) & (e ^ f); db1 = (a & d & ~(b | c | e | f)); db2 = (b & e & ~(a | c | d | f)); db3 = (c & f & ~(a | b | d | e)); ro = flip_coin( ); k1[x][y]= c1[x][y] ^ ((tripple | db1 | (ro & db2) | (~ro & db3)) & boundary[x][y]); k2[x][y]= c2[x][y] ^ ((tripple | db2 | (ro & db1) | (~ro & db3)) & boundary[x][y]); k3[x][y]= c3[x][y] ^ ((tripple | db3 | (ro & db1) | (~ro & db2)) & boundary[x][y]); k4[x][y]= c4[x][y] ^ ((tripple | db1 | (ro & db2) | (~ro & db3)) & boundary[x][y]); k5[x][y]= c5[x][y] ^ ((tripple | db2 | (ro & db1) | (~ro & db3)) & boundary[x][y]); k6[x][y]= c6[x][y] ^ ((tripple | db3 | (ro & db1) | (~ro & db2)) & boundary[x][y]); } } /************************************* ** CAN BE OPTIMIZED ** *************************************/ /************************************* ** PROPAGATION ** *************************************/ for (x = 0; x < LATTICE_WIDTH; x++) { for (y = 0; y < LATTICE_HEIGHT; y++) { c1[(x + 1 + LATTICE_WIDTH) % LATTICE_WIDTH][(y + LATTICE_HEIGHT) % LATTICE_HEIGHT] = k1[x][y]; c2[(x + LATTICE_WIDTH) % LATTICE_WIDTH][(y + 1 + LATTICE_HEIGHT) % LATTICE_HEIGHT] = k2[x][y]; c3[(x - 1 + LATTICE_WIDTH) % LATTICE_WIDTH][(y + 1 + LATTICE_HEIGHT) % LATTICE_HEIGHT] = k3[x][y]; c4[(x - 1 + LATTICE_WIDTH) % LATTICE_WIDTH][(y + LATTICE_HEIGHT) % LATTICE_HEIGHT] = k4[x][y]; c5[(x + LATTICE_WIDTH) % LATTICE_WIDTH][(y - 1 + LATTICE_HEIGHT) % LATTICE_HEIGHT] = k5[x][y]; c6[(x + 1 + LATTICE_WIDTH) % LATTICE_WIDTH][(y - 1 + LATTICE_HEIGHT) % LATTICE_HEIGHT] = k6[x][y]; } } } void draw_point(int x, int y, short value) { if(value > 0) { glColor3f((float)value/(pow(2.0, LENGTH)*6.0), (float)value/(pow(2.0, LENGTH) * 6.0), 0.9); } else { glColor3f(0.0, 0.0, 0.0); } glVertex3f(x + WINDOW_WIDTH / 8, y + WINDOW_HEIGHT / 8, 0.0); } void display() { int i, j; int x, y; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_POINTS); for (x = 0; x < LATTICE_WIDTH; x++) { for (y = 0; y < LATTICE_HEIGHT; y++) { i = x * SPACING; j = y * SPACING; glColor3f(0.2, 0.0, 0.0); draw_point(i, j, c1[x][y] + c2[x][y] + c3[x][y] + c4[x][y] + c5[x][y] + c6[x][y]); if (boundary[x][y] == 0) { glColor3f(1.0, 1.0, 1.0); glVertex3f(i + WINDOW_WIDTH / 8, j + 1 + WINDOW_HEIGHT / 8, 0.0); glVertex3f(i - 1 + WINDOW_WIDTH / 8, j - 1 + WINDOW_HEIGHT / 8, 0.0); glVertex3f(i + 1 + WINDOW_WIDTH / 8, j - 1 + WINDOW_HEIGHT / 8, 0.0); glVertex3f(i + WINDOW_WIDTH / 8, j - 1 + WINDOW_HEIGHT / 8, 0.0); glColor3f(0.0, 0.0, 0.0); } } } glEnd(); glutSwapBuffers(); } void idle() { int i, j; /* for (i = 0; i < LATTICE_HEIGHT / 20; i++) { for (j = 0; j < LATTICE_WIDTH / 20; j++) { if (1) { c1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c2[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c3[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c4[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c5[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c6[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; } } } */ /* for (i = 0; i < LATTICE_HEIGHT / 20; i++) { for (j = 0; j < LATTICE_WIDTH / 20; j++) { if (1) { c1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c2[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c3[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c4[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c5[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c6[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); } } } */ /* printf("boundary at i=%d is %d\n", WINDOW_WIDTH/3, boundary[WINDOW_WIDTH/3][0]);*/ next(); glutPostRedisplay(); } void init() { int i, j, k; next_rand = 0; for (i = 0; i < LATTICE_WIDTH; i++) { for (j = 0; j < LATTICE_HEIGHT; j++) { c1[i][j] = 0; c2[i][j] = 0; c3[i][j] = 0; c4[i][j] = 0; c5[i][j] = 0; c6[i][j] = 0; /* if(random_int( ) % 999 == 0) { c1[i][j] = random_int( ) % (int) pow(2.0, LENGTH); c2[i][j] = random_int( ) % (int) pow(2.0, LENGTH); c3[i][j] = random_int( ) % (int) pow(2.0, LENGTH); c4[i][j] = random_int( ) % (int) pow(2.0, LENGTH); c5[i][j] = random_int( ) % (int) pow(2.0, LENGTH); c6[i][j] = random_int( ) % (int) pow(2.0, LENGTH); } */ boundary[i][j] = 0xF; } } for (i = 0; i < LATTICE_WIDTH; i++) { boundary[i][LATTICE_HEIGHT - 1] = 0; boundary[i][0] = 0; } for (j = 0; j < LATTICE_HEIGHT; j++) { boundary[0][j] = 0; boundary[LATTICE_WIDTH - 1][j] = 0; } for(k = 0; k < 10; k++) { for (i = 0; i < LATTICE_HEIGHT / 20; i++) { for (j = 0; j < LATTICE_WIDTH / 20; j++) { if (1) { c1[k*i][k*j] = 0xF; //random_int( ) % 16; c2[k*i][k*j] = 0xF; //random_int( ) % 16; c3[k*i][k*j] = 0xF; //random_int( ) % 16; c4[k*i][k*j] = 0xF; //random_int( ) % 16; c5[k*i][k*j] = 0xF; //random_int( ) % 16; c6[k*i][k*j] = 0xF; //random_int( ) % 16; } } } } for (i = 0; i < LATTICE_HEIGHT / 20; i++) { for (j = 0; j < LATTICE_WIDTH / 20; j++) { if (1) { c1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c2[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c3[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c4[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c5[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; c6[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = pow(2.0, LENGTH) - 1; //random_int( ) % 16; } } } /* for (i = 0; i < LATTICE_HEIGHT / 20; i++) { for (j = 0; j < LATTICE_WIDTH / 20; j++) { if (1) { c1[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c2[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c3[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c4[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c5[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); c6[i + LATTICE_WIDTH / 2][j + LATTICE_HEIGHT / 2] = random_int( ) % (int) pow(2.0, LENGTH); } } } */ glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT, -20.0, 20.0); glMatrixMode(GL_MODELVIEW); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glShadeModel(GL_SMOOTH); glPointSize(POINT_SIZE); } void mouse(int button, int state, int x, int y) { mouse_x = x; mouse_y = y; } int main(int argc, char **argv) { mouse_x = 0; mouse_y = 0; glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutCreateWindow("fhp"); glEnable(GL_DEPTH_TEST); init(); glutDisplayFunc(display); glutIdleFunc(idle); glutMainLoop(); return 0; }