-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
310 lines (261 loc) · 5.89 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
#include <GL/glew.h>
#define USE_GLEW 1
#include <FL/gl.h>
#include <FL/Fl.h>
#include "clu.h"
#include <iostream>
#include <chrono>
#include "Settings.h"
#include "Simulation.h"
#include "SimWindow.h"
#include "ContWindow.h"
#include "ToolWindow.h"
#include "Grid.h"
#include "Lbm.h"
Grid* sim;
int window = 0;
GLsizei width = 800, height = 600;
bool fullscreen = false;
bool paused = false;
int fps = 30;
std::chrono::system_clock::time_point ticks;
std::chrono::system_clock::time_point newTicks;
//std::chrono::duration<int,std::milli> dt;
void initGL(int argc, char** argv);
void render();
void timer(int ms);
void inputKeys(unsigned char key, int x, int y);
void inputSpecial(int key, int x, int y);
void inputMouse(int button, int state, int x, int y);
void inputMotion(int x, int y);
void resize(GLsizei w, GLsizei h);
void fullToggle();
void cleanup();
void pauseCB(Fl_Widget* w);
void restartCB(Fl_Widget* w);
void proceedCB(Fl_Widget* w);
void settingsCB(Fl_Widget* w);
void toolsCB(Fl_Widget* w);
int mouseX, mouseY;
int mouseBut = 0;
float rotX = 0.0, rotY = 0.0;
float transZ = -1.f;
SimWindow* mainWin;
ToolWindow* toolWin;
ContWindow* contWin;
namespace display
{
bool help = true;
}
int main(int argc, char** argv)
{
settings = new Settings("./data/settings.json");
//settings->setInt("$.width", 10);
std::cout<<settings->getInt("$.width")<<std::endl;
width = settings->getInt("$.width");
height = settings->getInt("$.height");
fps = settings->getInt("$.fps");
//Fl::wait();
//initGL(argc, argv);
Fl::visual(FL_DOUBLE|FL_INDEX);
Fl::gl_visual(FL_RGB);
mainWin = new SimWindow(settings->getInt("$.width"),settings->getInt("$.height"),"LBM");
toolWin = new ToolWindow(500,500,"Tools");
contWin = new ContWindow(300,30,"Controller");
mainWin->show(argc, argv);
//toolWin->show();
contWin->show();
mainWin->draw();
//mainWin.hello->callback(restart);
mainWin->make_current();
Simulation::init();
sim = new Grid(width, height, mainWin);
sim->createKernel(std::vector<std::string>({
"data/kernels/heatmap.cl",
"data/kernels/lattice.cl"
}));
sim->initData();
Lbm* lbm = new Lbm(
Simulation::platforms[settings->getInt("$.OpenCL.platform")],
Simulation::devices[settings->getInt("$.OpenCL.platform")][settings->getInt("$.OpenCL.device")],
std::vector<std::string>({
"data/kernels/heatmap.cl",
"data/kernels/lattice.cl"
})
);
mainWin->sim = sim;
contWin->play->callback(pauseCB);
paused = !(contWin->play->value());
contWin->restart->callback(restartCB);
contWin->proceed->callback(proceedCB);
contWin->settings->callback(settingsCB);
contWin->tools->callback(toolsCB);
ticks = std::chrono::system_clock::now();//glutGet(GLUT_ELAPSED_TIME);
//glutMainLoop();
while(mainWin->shown())
{
newTicks = std::chrono::system_clock::now();
auto dt = newTicks-ticks;
if(!paused)
sim->step(dt.count()/1000.f);
mainWin->make_current();
sim->render();
mainWin->draw();
ticks = newTicks;
if(!contWin->shown())
{
contWin->show();
}
Fl::check();
}
//return Fl::run();
}
void pauseCB(Fl_Widget* w)
{
paused = !((Fl_Toggle_Button*)w)->value();
}
void restartCB(Fl_Widget* w)
{
sim->restart();
}
void proceedCB(Fl_Widget* w)
{
sim->step(1.f);
}
void settingsCB(Fl_Widget* w)
{
}
void toolsCB(Fl_Widget* w)
{
if(((Fl_Toggle_Button*)w)->value())
{
toolWin->show();
}else
{
toolWin->hide();
}
}
void initGL(int argc, char** argv)
{
//glutInit(&argc, argv);
//glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(width, height);
//window = glutCreateWindow("LBM");
//glutDisplayFunc(render);
/*glutTimerFunc(1000/fps, timer, 1000/fps);
glutKeyboardFunc(inputKeys);
glutSpecialFunc(inputSpecial);
glutMouseFunc(inputMouse);
glutMotionFunc(inputMotion);
glutReshapeFunc(resize);*/
glewInit();
glEnable(GL_DEBUG_OUTPUT);
//glDebugMessageCallback(errorCallbackGL, NULL); //This thing
//glClearColor(0.0, 0.0, 0.0, 1.0);
//glViewport(0, 0, glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
}
void render()
{
//newTicks = std::chrono::system_clock::now();//glutGet(GLUT_ELAPSED_TIME);
auto dt = newTicks-ticks;
//std::cout<<dt/1000.f<<std::endl;
if(!paused)
sim->step(dt.count()/1000.f);
sim->render();
//ticks = newTicks;
//glutSwapBuffers();
}
void timer(int ms)
{
//glutTimerFunc(ms, timer, ms);
//glutPostRedisplay();
}
void inputKeys(unsigned char key, int x, int y)
{
//std::cout<<key<<":"<<(int)key<<std::endl;
switch(key)
{
case 27: //Esc
case 17: //Ctrl + Q
case 3: //Ctrl + C
cleanup(); //Quit
break;
case 6: //Ctrl + F
fullToggle(); //Fullscreen
break;
case 'p':
case 'P':
paused = !paused; //Pause
break;
case 'r':
case 'R':
sim->restart();
break;
case 'h':
case 'H':
display::help = !display::help;
break;
case '=':
case '+': //Next demo
sim->nextDemo();
break;
case '-':
case '_': //Previous demo
sim->nextDemo(true);
break;
}
}
void inputSpecial(int key, int x, int y)
{
switch(key)
{
case 1://GLUT_KEY_F11:
fullToggle();
break;
}
}
void inputMouse(int button, int state, int x, int y)
{
if(state == 1)//GLUT_DOWN)
{
mouseBut |= 1<<button;
}
else if(state == 1)//GLUT_UP)
{
mouseBut = 0;
}
mouseX = x;
mouseY = y;
inputMotion(x,y);
}
// A simple Bresenham algorithm for line drawing
void inputMotion(int x1, int y1)
{
}
void resize(GLsizei w, GLsizei h)
{
width = w;
height = h;
glViewport(0, 0, width, height);
sim->resize(w, h);
//glutPostRedisplay();
}
void fullToggle()
{
if(fullscreen)
{
//glutReshapeWindow(width, height);
fullscreen = false;
}
else
{
//glutFullScreen();
fullscreen = true;
}
}
void cleanup()
{
delete sim;
if(window)
;//glutDestroyWindow(window);
}