Repository navigation
Expand file tree
/
Copy pathrenderwidget.cpp
More file actions
203 lines (146 loc) · 4.22 KB
/
Copy pathrenderwidget.cpp
File metadata and controls
203 lines (146 loc) · 4.22 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
#include "renderwidget.h"
#include <QMouseEvent>
#include "System.h"
#include <math.h>
#define REFRESH_DELAY 25
RenderWidget::RenderWidget(QWidget *parent) :
QOpenGLWidget(parent),
m_system(0),
angularSpeed(0)
{
}
RenderWidget::~RenderWidget()
{
// Make sure the context is current when deleting the texture
// and the buffers.
makeCurrent();
//delete geometries;
doneCurrent();
}
//! [0]
void RenderWidget::mousePressEvent(QMouseEvent *e)
{
// Save mouse press position
mousePressPosition = QVector2D(e->localPos());
}
void RenderWidget::mouseReleaseEvent(QMouseEvent *e)
{
// Mouse release position - mouse press position
QVector2D diff = QVector2D(e->localPos()) - mousePressPosition;
// Rotation axis is perpendicular to the mouse position difference
// vector
QVector3D n = QVector3D(diff.y(), diff.x(), 0.0).normalized();
// Accelerate angular speed relative to the length of the mouse sweep
qreal acc = diff.length() / 100.0;
// Calculate new rotation axis as weighted sum
rotationAxis = (rotationAxis * angularSpeed + n * acc).normalized();
// Increase angular speed
angularSpeed += acc;
}
//! [0]
//! [1]
void RenderWidget::timerEvent(QTimerEvent *)
{
// Decrease angular speed (friction)
angularSpeed *= 0.99;
// Stop rotation when speed goes below threshold
if (angularSpeed < 0.01) {
angularSpeed = 0.0;
} else {
// Update rotation
rotation = QQuaternion::fromAxisAndAngle(rotationAxis, angularSpeed) * rotation;
}
}
//! [1]
void RenderWidget::initializeGL()
{
initializeOpenGLFunctions();
glClearColor(1, 1, 1, 1);
// glEnable(GL_CULL_FACE);
initShaders();
// Enable depth buffer
glEnable(GL_DEPTH_TEST);
// Enable back face culling
glEnable(GL_CULL_FACE);
m_system = new System;
// Use QBasicTimer because its faster than QTimer
timer.start(12, this);
}
//! [3]
void RenderWidget::initShaders()
{
// Compile vertex shader
if (!program.addShaderFromSourceFile(QOpenGLShader::Vertex, ":/vshader.glsl"))
close();
// Compile fragment shader
if (!program.addShaderFromSourceFile(QOpenGLShader::Fragment, ":/fshader.glsl"))
close();
// Link shader pipeline
if (!program.link())
close();
// Bind shader pipeline for use
if (!program.bind())
close();
}
//! [3]
// run each time the windows is resized
void RenderWidget::resizeGL(int w, int h)
{
// Calculate aspect ratio
qreal aspect = qreal(w) / qreal(h ? h : 1);
// Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
const qreal zNear = 0.1, zFar = 50.0, fov = 45.0;
// Set view matrix
view.setToIdentity();
view.translate({0,0,-30});
// Reset projection
projection.setToIdentity();
// Set perspective projection matrix
projection.perspective(fov, aspect, zNear, zFar);
}
//! [5]
void RenderWidget::paintGL()
{
// Clear color 0nd depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Parent matrix model , in order to applicate the same transformation to all its childs
QMatrix4x4 humanoide;
humanoide.rotate(rotation);
humanoide = projection * view * humanoide;
// Calculate model matrix
QMatrix4x4 matrix;
//Set modelview-projection matrix
program.setUniformValue("mvp", humanoide * matrix);
//Draw base geometry
m_system->drawGeometry(&program);
}
System * RenderWidget::getM_system()
{
return m_system;
}
void RenderWidget::startRendering()
{
animationTimer.setSingleShot(false);
connect(&animationTimer, SIGNAL(timeout()), this, SLOT(animate()));
animationTimer.start(REFRESH_DELAY);
}
void RenderWidget::animate()
{
m_system->update_particles(REFRESH_DELAY);
update();
}
void RenderWidget::change_wind(QVector3D * vect){
m_system->change_wind(vect);
}
void RenderWidget::setM_windFactor(f32 value){
m_system->setM_windFactor(value);
}
void RenderWidget::setM_windRotation(u16 value){
m_system->setM_windRotation(value);
}
void RenderWidget::setM_atmDensityFactor(f32 value){
m_system->setM_atmDensityFactor(value);
}
void RenderWidget::setM_gravity(f32 value){
m_system->setM_gravity(value);
}