-
Notifications
You must be signed in to change notification settings - Fork 0
/
YS3Dview (original).cpp
312 lines (259 loc) · 9.52 KB
/
YS3Dview (original).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
//
// YS3DView.cpp
// GL view of a nucleus
//
// Created by Yves Schutz on 15/12/13.
// Copyright (c) 2013 Yves Schutz. All rights reserved.
//
#include "qglbuilder.h"
#include "qglsphere.h"
#include <QtOpenGL/QGLFramebufferObject>
#include <QDebug>
#include <QKeyEvent>
#include <QtMath>
#include <QPropertyAnimation>
#include <QUrl>
#include <QVector3D>
#include "YSCollision.h"
#include "YS3DView.h"
#include "YSNucleus.h"
//______________________________________________________________________________
YS3DView::YS3DView(QWindow *parent, YSNucleus *nucleus)
: QGLView(parent),
mCollision(false), mCollDist(0.0), mFBO(NULL), mNanimation(NULL), mNpos(0.0), mQanimation(NULL), mQpos(0.0)
{
//ctor to draw one nucleus
QGLBuilder builder;
// the nucleus
mNucleus1 = nucleus;
builder.newSection(QGL::Faceted);
builder << QGLSphere(mNucleus1->RadiusMean() * 2);
mNucleusEnv = builder.currentNode();
mNucleusEnv->setObjectName("nucleus");
InitNucleonAndQuark(builder);
mScene = builder.finalizedSceneNode();
mScene->setParent(this);
mInnerCamera = new QGLCamera(this);
}
//______________________________________________________________________________
YS3DView::YS3DView(QWindow *parent, YSCollision *collision)
: QGLView(parent), mCollision(true),mCollDist(30.0), mNanimation(NULL), mQanimation(NULL)
{
// ctor to draw the collision
setWidth(1000);
QGLBuilder builder;
// nucleus 1
mNucleus1 = collision->Nucleus(1);
builder.newSection(QGL::Faceted);
builder << QGLSphere(mNucleus1->RadiusMean() * 2);
mNucleusEnv = builder.currentNode();
mNucleusEnv->setObjectName("nucleus1");
// nucleus 2
mNucleus2 = collision->Nucleus(2);
builder.newSection(QGL::Faceted);
builder << QGLSphere(mNucleus1->RadiusMean() * 2);
mNucleusEnv = builder.currentNode();
mNucleusEnv->setObjectName("nucleus2");
InitNucleonAndQuark(builder);
mScene = builder.finalizedSceneNode();
mScene->setParent(this);
mInnerCamera = new QGLCamera(this);
}
//______________________________________________________________________________
YS3DView::~YS3DView()
{
delete mFBO;
delete mQanimation;
delete mNanimation;
}
//______________________________________________________________________________
void YS3DView::initializeGL(QGLPainter *)
{
// create the framebuffer needed to draw the nested view
mFBO = new QOpenGLFramebufferObject(512, 512, QOpenGLFramebufferObject::Depth);
mFBOSurface.setFramebufferObject(mFBO);
glEnable(GL_BLEND);
}
//______________________________________________________________________________
void YS3DView::keyPressEvent(QKeyEvent * event)
{
// stop the animation
if (event->key() == Qt::Key_Space && !event->isAutoRepeat()) {
if (!mQanimation) {
mQanimation = new QPropertyAnimation(this, "QRot", this);
mQanimation->setStartValue(0.0f);
mQanimation->setEndValue(360.0f);
mQanimation->setDuration(4500);
mQanimation->setLoopCount(-1);
}
if (mQanimation->state() == QAbstractAnimation::Running)
mQanimation->stop();
else
mQanimation->start();
} else if (event->key() == Qt::Key_Return && !event->isAutoRepeat()) {
if (mCollision) {
if(!mNanimation) {
mNanimation = new QPropertyAnimation(this, "NTrans", this);
mNanimation->setDuration(4500);
mNanimation->setStartValue(0.0f);
mNanimation->setEndValue(mCollDist * 2.);
}
if (mNanimation->state() == QAbstractAnimation::Running)
mNanimation->stop();
else
mNanimation->start();
}
}
}
//______________________________________________________________________________
void YS3DView::DrawNucleon(QGLPainter *painter, const QVector3D &posn)
{
// Draw a transparent nucleon
painter->modelViewMatrix().push();
painter->setFaceColor(QGL::AllFaces, QColor(0, 160, 202, 125));
painter->setStandardEffect(QGL::LitDecalTexture2D);
glBindTexture(GL_TEXTURE_2D, mFBO->texture());
glEnable(GL_TEXTURE_2D);
painter->modelViewMatrix().translate(posn);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
mNucleonNode->draw(painter);
glCullFace(GL_BACK);
mNucleonNode->draw(painter);
glDisable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
painter->modelViewMatrix().pop();
}
//______________________________________________________________________________
void YS3DView::DrawNucleonAndQuarks(QGLPainter *painter, const QVector3D &posn)
{
// draw a nucleon with quarks inside
DrawQuark(painter, posn, Qt::red);
DrawQuark(painter, posn, Qt::blue);
DrawQuark(painter, posn, Qt::green);
DrawNucleon(painter, posn);
}
//______________________________________________________________________________
void YS3DView::DrawNucleus(QGLPainter *painter, const QVector3D &posn, YSNucleus *nucleus)
{
// draw nucleus
QVector3D * nucleons = nucleus->GetNucleons();
for (int index = 0; index < nucleus->A(); index++) {
QVector3D pos = nucleons[index] + posn;
DrawNucleonAndQuarks(painter, pos);
}
painter->modelViewMatrix().push();
painter->setFaceColor(QGL::AllFaces, QColor(0, 160, 202, 125));
painter->setStandardEffect(QGL::LitDecalTexture2D);
glBindTexture(GL_TEXTURE_2D, mFBO->texture());
glEnable(GL_TEXTURE_2D);
painter->modelViewMatrix().translate(posn);
if (nucleus->A() != 1) {
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
mNucleusEnv->draw(painter);
glCullFace(GL_BACK);
mNucleusEnv->draw(painter);
glDisable(GL_CULL_FACE);
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
painter->modelViewMatrix().pop();
}
//______________________________________________________________________________
void YS3DView::DrawQuark(QGLPainter *painter, const QVector3D &posn, const QColor color)
{
// Draw a quark inside a nucleon
painter->modelViewMatrix().push();
painter->setFaceColor(QGL::AllFaces, QColor(color));
painter->setStandardEffect(QGL::LitDecalTexture2D);
glBindTexture(GL_TEXTURE_2D, mFBO->texture());
glEnable(GL_TEXTURE_2D);
qreal r = (qreal)qrand() / (qreal)RAND_MAX * YSNucleus::kgProtonRadius;
qreal phi = (qreal)qrand() / (qreal)RAND_MAX * qDegreesToRadians(360.);
qreal theta = (qreal)qrand() / (qreal)RAND_MAX * qDegreesToRadians(180.);
qreal rx = r * qCos(phi) * qSin(theta) + posn.x();
qreal ry = r * qSin(phi) * qSin(theta) + posn.y();
qreal rz = r * qCos(theta) + posn.z();
QVector3D posq(rx, ry, rz);
painter->modelViewMatrix().translate(posq);
glCullFace(GL_FRONT);
glEnable(GL_CULL_FACE);
mQuarkNode->draw(painter);
glCullFace(GL_BACK);
mQuarkNode->draw(painter);
glDisable(GL_CULL_FACE);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
painter->modelViewMatrix().pop();
}
//______________________________________________________________________________
void YS3DView::InitNucleonAndQuark(QGLBuilder &builder)
{
// define nucleon and quark
// the nucleon
builder.newSection(QGL::Faceted);
builder << QGLSphere(YSNucleus::kgProtonRadius * 2);
mNucleonNode = builder.currentNode();
mNucleonNode->setObjectName("nucleon");
// the quark
builder.newSection();
builder << QGLSphere(YSNucleus::kgProtonRadius / 20.); // the quark is in fact 1000 times smaller than the nucleon
mQuarkNode = builder.currentNode();
mQuarkNode->setObjectName("quark");
}
//______________________________________________________________________________
void YS3DView::mousePressEvent(QMouseEvent *event)
{
// select impact parameter by dragging one nucleus
qDebug() << "YS3DView::mousePressEvent" << event->x() << event->y();
}
//______________________________________________________________________________
void YS3DView::paintGL(QGLPainter *painter)
{
painter->modelViewMatrix().push();
painter->projectionMatrix().push();
painter->pushSurface(&mFBOSurface);
painter->setCamera(mInnerCamera);
painter->setFaceColor(QGL::AllFaces, QColor(170, 202, 0));
painter->setStandardEffect(QGL::LitMaterial);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
painter->popSurface();
painter->projectionMatrix().pop();
painter->modelViewMatrix().pop();
// drawing sequence of the quarks, nucleons, nucleus draw from back to front
qreal unzoom;
if (mCollision)
unzoom = -100.;
else
unzoom = -50;
// unzoom
QMatrix4x4 mat = painter->worldMatrix();
QVector4D row = mat.row(2);
if ( row[2,3] > -10 ) {
row.setW(unzoom);
mat.setRow(2, row);
painter->modelViewMatrix() = mat;
}
//scale x, y, z
QVector3D scaleV(1, 1, 1);
painter->modelViewMatrix().scale(scaleV);
qreal x1displacement = 0.0, x2displacement = 0.0;
if (mCollision) {
x1displacement = mCollDist;
x2displacement = -mCollDist;
}
qreal zoomScale = mat(2,3);
if (zoomScale > 1.0) { // if zoom too large show only one nucleon
QVector3D nucleonpos(0., 0., 0.);
DrawNucleonAndQuarks(painter, nucleonpos);
} else {
QVector3D nucleus1pos(x1displacement - mNpos, 0., 0.);
DrawNucleus(painter, nucleus1pos, mNucleus1);
if (mCollision) {
QVector3D nucleus2pos(x2displacement + mNpos, 0., 0.);
DrawNucleus(painter, nucleus2pos, mNucleus2);
}
}
}