-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisual.cpp
325 lines (250 loc) · 5.46 KB
/
visual.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include <cuda_runtime.h>
#include "visual.h"
#include "grains.h"
#include "bmp.h"
// glut parameters
char name[] = "cuggs"; // window title
int width = 256, height = 256; // window size
int oldx, oldy; // old mouse coordinates
int frames = 0; // frame counter
int t0 = 0, t1; // timer to compute fps
int dims, step = 0;
// volume vis parameters
int nslices = 250;
int rotateVolume = 0;
double rx=0, ry=0;
int autorot = 0;
int pause = 1;
// control parameters
int visual=0;
int stat=0, verb=0, dump=0, term=-1;
const char *savefilename;
void display2d()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
// setup 2d pixel plotting camera
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (GLfloat)width, 0.0f, (GLfloat)height, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
glDepthRange(0.0f, 1.0f);
// load texture from pbo
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBindTexture(GL_TEXTURE_2D, texid);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_ABGR_EXT, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0, 0.0);
glVertex2i(0.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex2i(width, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex2i(width, height);
glTexCoord2f(0.0, 1.0);
glVertex2i(0.0, height);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
glutSwapBuffers();
}
void display3d()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, (float)width/height, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, width, height);
gluLookAt(0.0, 0.0, 1.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// x-ray
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
// load texture from pbo
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo);
glBindTexture(GL_TEXTURE_3D, texid);
glTexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 0, w, h, d, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glEnable(GL_TEXTURE_3D);
for (int i=1; i<=nslices; i++)
{
double z = (i*1.0/(nslices+1)-0.5);
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5, 0.5, 0.5);
glScalef(1.75, 1.75, 1.75);
glRotatef(rx, 1, 0, 0);
glRotatef(ry, 0, 1, 0);
glTranslatef(0, 0, z);
glMatrixMode(GL_MODELVIEW);
glBegin(GL_QUADS);
glColor4f(1, 1, 1, 0.04);
glVertex3f(-0.5, -0.5, z);
glVertex3f(-0.5, 0.5, z);
glVertex3f(0.5, 0.5, z);
glVertex3f(0.5, -0.5, z);
glEnd();
}
glDisable(GL_TEXTURE_3D);
glPopMatrix();
glutSwapBuffers();
}
void update()
{
// output only if not paused
if (!pause || !visual)
{
// do statistics output
if (stat)
{
download_grains();
if (step == 0)
{
init_stats();
// print stats
if (verb)
print_grains(step);
else
print_stats(step);
}
// every n simulation steps
else if ((step % stat) == 0)
{
// compute statistics
update_stats();
// print stats
if (verb)
print_grains(step);
else
print_stats(step);
}
}
// store grains after termination time was reached
if ((term >= 0) && (step >= term))
{
download_grains();
store_grains(savefilename);
exit(0);
}
if (dump && (step % dump == 0))
{
char dumpfile[80];
if (dims == 2)
sprintf(dumpfile, "dump%d.bmp\0", step);
else
sprintf(dumpfile, "dump%d.bm3\0", step);
download_grains();
store_grains(dumpfile);
}
// do one monte carlo step in 2d or 3d
if (dims == 2)
run_mcs2d(w, h, kT);
else
run_mcs3d(w, h, d, kT);
// increment simulation step
step++;
}
// redraw
glutPostRedisplay();
// count frame per second
frames++;
t1 = glutGet(GLUT_ELAPSED_TIME);
// every second approximately
if (t1-t0 >= 1000)
{
char title[80];
sprintf(title, "%s %.1f fps", name, (1000.0*frames/(t1-t0)));
glutSetWindowTitle(title);
frames = 0;
t0 = t1;
}
// autorotate feature
rx += autorot*0.4;
ry += autorot*0.4;
}
void mouse(int button, int state, int x, int y)
{
rotateVolume = 0;
if (state == GLUT_DOWN)
{
if (button == GLUT_LEFT_BUTTON)
{
rotateVolume = 1;
oldx = x;
oldy = y;
}
if (button == GLUT_RIGHT_BUTTON)
{
}
}
}
void motion(int x, int y)
{
if (rotateVolume)
{
rx += oldy-y;
ry += oldx-x;
oldx = x;
oldy = y;
}
}
void reshape(int w, int h)
{
width = w;
height = h;
}
void keyboard(unsigned char key, int x, int y)
{
if (key == 27)
{
exit(0);
}
else if (key == 'r')
{
autorot = 1-autorot;
}
else if (key == ' ')
pause = 1-pause;
}
void cleanup()
{
destroy_grains();
}
void run_cuggs(int argc, char *argv[], const char *loadfile, const char *savefile)
{
atexit(cleanup);
savefilename = savefile;
// init glut
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow(name);
glewInit();
dims = init_grains(loadfile);
if (visual & dims == 2)
{
glutDisplayFunc(display2d);
}
else if (visual & dims == 3)
{
glutDisplayFunc(display3d);
glutMouseFunc(mouse);
glutMotionFunc(motion);
}
glutIdleFunc(update);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
}