-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphsimulation.cpp
More file actions
316 lines (270 loc) · 6.6 KB
/
sphsimulation.cpp
File metadata and controls
316 lines (270 loc) · 6.6 KB
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
#include "sphsimulation.h"
#include <qdebug.h>
#include <qstring.h>
#include <qopenglshaderprogram.h>
#include "Vertex.h"
#include "Particle.h"
/*
static const Vertex sg_vertexes[] = {
Vertex(QVector3D( 0.00f, 0.75f, 1.00f), QVector3D(1.00f, 0.00f ,0.00f)),
Vertex(QVector3D( 0.75f, -0.75f, 1.00f), QVector3D(0.00f, 1.00f, 0.00f)),
Vertex(QVector3D(-0.75f, -0.75f, 1.00f), QVector3D(0.00f, 0.00f, 1.00f))
};
*/
SPHSimulation::SPHSimulation(QWidget *parent):
QOpenGLWidget(parent)
{
connect(&timer, SIGNAL(timeout()), this, SLOT(update()));
//timer.start(16);
}
SPHSimulation::~SPHSimulation()
{
makeCurrent();
teardownGL();
}
void printList(std::vector<Vertex> &list1)
{
for (int i = 0; i < list1.size(); i++)
{
qDebug() << qPrintable("Position ") << i << qPrintable(": ") << list1[i].position() << endl;
qDebug() << qPrintable("Color ") << i << qPrintable(": ") << list1[i].color() << endl;
}
}
void SPHSimulation::initializeGL()
{
vList.clear();
pList.clear();
timer.start(16);
initializeOpenGLFunctions();
printContextInformation();
sph.generateParticleList();
pList = sph.partList();
// vList = sph.vList();
// qDebug() << vList.size() << " Particles extracted";
// printList(vList);
//OpenGL 2.0 syntax here
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
//glColor3f(0.0f, 1.0f, 0.0f);
glPointSize(5.0);
// Trying OpenGL 4.0 but couldn't get vertex buffers to work.
/* {
m_program = new QOpenGLShaderProgram();
m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, "Shaders/simple.vert");
m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, "Shaders/simple.frag");
m_program->link();
m_program->bind();
m_vertex.create();
m_vertex.bind();
m_vertex.setUsagePattern(QOpenGLBuffer::StaticDraw);
// m_vertex.allocate(sg_vertexes, sizeof(sg_vertexes));
m_vertex.allocate(&vList, vList.size()*sizeof(Vertex));
m_object.create();
m_object.bind();
m_program->enableAttributeArray(0);
m_program->enableAttributeArray(1);
m_program->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
m_program->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());
m_object.release();
m_vertex.release();
m_program->release();
}
*/
}
void SPHSimulation::initializeValues()
{
}
void SPHSimulation::resizeGL(int w, int h)
{
int side = qMin(w, h);
glViewport((w-side)/2, (h-side)/2, side, side);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
qreal aspectratio = qreal(w) / qreal(h);
glOrtho(-1.0, 1.0, -1.0, 1.0, 4, -2);
glMatrixMode(GL_MODELVIEW);
}
void SPHSimulation::paintGL()
{
pList.clear();
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
glBegin(GL_POINTS);
pList = sph.partList();
for (std::pair<int,Particle> element : pList)
{
int id = element.first;
Particle p = element.second;
QVector3D col = p.vertex().color();
glColor3f(col.x(),col.y(),col.z());
glVertex2f(p.vertex().position().x(), p.vertex().position().y());
}
glEnd();
//openGL 4.0 stuff
/* m_program->bind();
{
m_object.bind();
//glEnable(GL_POINT_SMOOTH);
glDrawArrays(GL_POINTS,0, vList.size());
qDebug() << vList.size() * sizeof(Vertex) << " Particles Drawn";
m_object.release();
}
m_program->release();
*/
glDisable(GL_BLEND);
glDisable(GL_POINT_SMOOTH);
glFlush();
}
void SPHSimulation::teardownGL()
{
//m_object.destroy();
//m_vertex.destroy();
//delete m_program;
}
void SPHSimulation::update()
{
if (pause == false && timer.isActive())
{
sph.simulate();
// timer.stop();
}
QOpenGLWidget::update();
}
void SPHSimulation::pauseSimulation()
{
if (pause == false) {
qDebug() << qPrintable("Pausing Simulation");
pause = true;
timer.stop();
}
else
{
qDebug() << qPrintable("Resuming Simulation");
pause = false;
timer.start();
}
}
void SPHSimulation::restartSimulation()
{
pList.clear();
sph.generateParticleList();
pList = sph.partList();
repaint();
}
void SPHSimulation::settimestep(float t)
{
if (sph.dt() != t )
{
sph.settimestep(t);
qDebug() << "TimeStep Set to: " << double(t)<<endl;
emit timestepChanged(t);
}
}
void SPHSimulation::setparticleCount(size_t pCount)
{
if (pCount != sph.nParticles())
{
sph.setparticleCount(pCount);
qDebug() << "Particle Count Set to: " << int(pCount) << endl;
emit pCountChanged(pCount);
}
}
void SPHSimulation::setmass(float m)
{
if (m != sph.mass())
{
sph.setmass(m);
qDebug() << "Mass Set to: " << double(m) << endl;
emit massChanged(m);
}
}
void SPHSimulation::setradius(float r)
{
if (r != sph.radius())
{
sph.setradius(r);
qDebug() << "Radius set to: " << double(r) << endl;
emit radiusChanged(r);
}
}
void SPHSimulation::setgravity(float gravY)
{
if (gravY != sph.gravity().y())
{
sph.setgravity(gravY);
qDebug() << "Gravity Set to: " << double(gravY) << endl;
emit gravityChanged(gravY);
}
}
void SPHSimulation::setpresConst(float pConst)
{
if (pConst != sph.presConst())
{
sph.setpresConst(pConst);
qDebug() << "Pressure Constant Set to: " << double(pConst) << endl;
emit presConstChanged(pConst);
}
}
void SPHSimulation::setpresGamma(float gamma)
{
if (gamma != sph.presGamma())
{
sph.setpresGamma(gamma);
qDebug() << "presGamma Set to: " << double(gamma) << endl;
emit presGammaChanged(gamma);
}
}
void SPHSimulation::setdensity(float rho)
{
if (rho != sph.initDensity())
{
sph.setdensity(rho);
qDebug() << "initDensity Set to: " << double(rho) << endl;
emit densityChanged(rho);
}
}
void SPHSimulation::setviscConst(float vConst)
{
if (vConst != sph.viscConst())
{
sph.setviscConst(vConst);
qDebug() << "vConst Set to: " << double(vConst) << endl;
emit viscConstChanged(vConst);
}
}
void SPHSimulation::setviscE(float vE)
{
if (vE != sph.viscE())
{
sph.setviscE(vE);
qDebug() << "ViscE Set to: " << double(vE) << endl;
emit viscEChanged(vE);
}
}
void SPHSimulation::setWallSticky(float wSticky)
{
if (wSticky != sph.wallSticky())
{
sph.setWallSticky(wSticky);
qDebug() << "Wall Sticky Set to: " << double(wSticky) << endl;
emit wallStickyChanged(wSticky);
}
}
void SPHSimulation::printContextInformation()
{
QString glType;
QString glVersion;
QString glProfile;
glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL";
glVersion = reinterpret_cast<const char*>(glGetString(GL_VERSION));
#define CASE(c) case QSurfaceFormat::c: glProfile = #c; break
switch (format().profile())
{
CASE(NoProfile);
CASE(CoreProfile);
CASE(CompatibilityProfile);
}
#undef CASE
qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" << qPrintable(glProfile) << ")";
}