-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.c
226 lines (174 loc) · 4.23 KB
/
grid.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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
#include "windows.h"
#endif
#include "GL/glut.h"
#include "sor.h"
#include "grid.h"
const int w = 1024;
const int h = 768;
const int ROWS = N, COLS = N;
const int BORDER = 24;
const int DELAY = 100;
int CW, CH;
int xoffset;
int stepDelay;
char convtext[128];
char itertext[32];
cell_colour_t gridcolour = RED;
unsigned char converged = 0, paused = 1, boundary = 0;
void *font = GLUT_BITMAP_HELVETICA_12;
const char *convstr = "Convergence in %d iterations for %dx%d grid with tolerance %5.3f and omega %.8f";
const char *iterstr = "Iteration %d e: %.8f";
void drawcell(int x, int y, double v)
{
if(gridcolour == RED)
glColor3f(v, v/2.0, v/3.5); /* Flame */
else if(gridcolour == GREEN)
glColor3f(v/2.5, v, v/2.0); /* Absinthe */
else if(gridcolour == BLUE)
glColor3f(v/3.0, v/2.5, v); /* Cherenkov */
glBegin(GL_QUADS);
glVertex2i(x*CW+BORDER+xoffset, y*CH+BORDER);
glVertex2i((x+1)*CW+BORDER+xoffset, y*CH+BORDER);
glVertex2i((x+1)*CW+BORDER+xoffset, (y+1)*CH+BORDER);
glVertex2i(x*CW+BORDER+xoffset, (y+1)*CH+BORDER);
glEnd();
}
void bitmapString(void *font, char *str, int x, int y)
{
glColor3f(1.0, 1.0, 1.0);
glRasterPos2i(x, y);
if(str && strlen(str)){
while(*str){
glutBitmapCharacter(font, *str);
str++;
}
}
}
void initRendering(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
gluOrtho2D(0, w, h, 0);
}
void handleKeyPress(unsigned char key, int wx, int wy)
{
(void)wx; /* Unused */
(void)wy; /* Unused */
switch(key){
/* q */ case 'q':
/* ESC */ case 27:
exit(0);
break;
/* Space */ case 32:
/* p */ case 'p':
paused = (paused ? 0 : 1);
break;
/* b */ case 'b': boundary = (boundary ? 0 : 1);
break;
/* s */ case 's': paused = 1;
if((epsilon = calcerror()) >= tol){
runsor();
glutPostRedisplay();
}else{
paused = 0;
}
break;
/* r */ case 'r': iter = 0;
paused = 1;
converged = 0;
epsilon = 0.0;
stepDelay = DELAY;
initgrid();
glutPostRedisplay();
break;
/* c */ case 'c': gridcolour = ((gridcolour << 1) % 7);
break;
/* i */ case 'i': stepDelay = (stepDelay ? 0 : DELAY);
paused = 0;
/* Can time here... glutTimerFunc(stepDelay, update, 0); */
break;
}
}
void handleResize(int wx, int hx)
{
glViewport(0, 0, wx, hx);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, w, h, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void drawScene(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
for(i=0; i<N+boundary; i++){
for(j=0; j<N+boundary; j++){
drawcell(j, i, x[i+1][j+1]);
}
}
glBegin(GL_LINES);
glColor3f(1.0, 1.0, 1.0);
glVertex2i(xoffset-BORDER, BORDER);
glVertex2i(w-xoffset+BORDER, BORDER);
glVertex2i(w-xoffset+BORDER, BORDER);
glVertex2i(w-xoffset+BORDER, ROWS*CH+BORDER);
glVertex2i(w-xoffset+BORDER, ROWS*CH+BORDER);
glVertex2i(xoffset-BORDER, ROWS*CH+BORDER);
glVertex2i(xoffset-BORDER, ROWS*CH+BORDER);
glVertex2i(xoffset-BORDER, BORDER);
glEnd();
snprintf(itertext, 32, iterstr, iter, epsilon);
bitmapString(font, itertext, w-w/5, h-BORDER/3);
if(converged){
snprintf(convtext, 128, convstr, iter, N, N, tol, omega);
bitmapString(font, convtext, 48, h-BORDER/3);
}
if(paused){
bitmapString(font, "-=: PAUSED :=-", w/2-40, BORDER/2);
}
glutSwapBuffers();
}
void update(int v)
{
(void)v; /* Unused */
if(paused){
glutTimerFunc(stepDelay, update, 0);
glutPostRedisplay();
return;
}
if((epsilon = calcerror()) >= tol){
runsor();
glutPostRedisplay();
glutTimerFunc(stepDelay, update, 0);
}else{
converged = 1;
glutTimerFunc(stepDelay, update, 0);
glutPostRedisplay();
}
}
int main(int argc, char *argv[])
{
CW = (MIN(w,h)-2*BORDER)/COLS;
CH = CW;
xoffset = (w-(CW*COLS))/2;
stepDelay = DELAY;
initgrid();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(w, h);
glutCreateWindow("SOR Grid");
initRendering();
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeyPress);
glutReshapeFunc(handleResize);
glutTimerFunc(stepDelay, update, 0);
glutMainLoop();
return 0;
}