-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGLWidget.cpp
More file actions
379 lines (297 loc) · 11.2 KB
/
GLWidget.cpp
File metadata and controls
379 lines (297 loc) · 11.2 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "GLWidget.h"
#include <iostream>
#include <random>
#include <math.h>
#include <QGLFormat>
#include <QSvgGenerator>
#include "VertexData.h"
#include "SystemParams.h"
GLWidget::GLWidget(QGLFormat format, QWidget *parent) :
QGLWidget(format, parent),
_isMouseDown(false),
_zoomFactor(50.0),
_shaderProgram(0),
_patternGenerator(0),
_img_width(50),
_img_height(50)
{
}
GLWidget::~GLWidget()
{
if(_shaderProgram) delete _shaderProgram;
if(_patternGenerator) delete _patternGenerator;
}
void GLWidget::initializeGL()
{
QGLFormat glFormat = QGLWidget::format();
if (!glFormat.sampleBuffers()) { std::cerr << "Could not enable sample buffers." << std::endl; return; }
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
QVector3D backColor = SystemParams::background_color;
glClearColor( backColor.x(), backColor.y(), backColor.z(), 1.0 );
glEnable(GL_DEPTH_TEST);
_shaderProgram = new QOpenGLShaderProgram();
if (!_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, "../IslamicStarPatterns/shader.vert"))
{ std::cerr << "Cannot load vertex shader." << std::endl; return; }
if (!_shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, "../IslamicStarPatterns/shader.frag"))
{ std::cerr << "Cannot load fragment shader." << std::endl; return; }
if ( !_shaderProgram->link() )
{ std::cerr << "Cannot link shaders." << std::endl; return; }
_shaderProgram->bind();
_mvpMatrixLocation = _shaderProgram->uniformLocation("mvpMatrix");
_colorLocation = _shaderProgram->attributeLocation("vertexColor");
_vertexLocation = _shaderProgram->attributeLocation("vert");
_use_color_location = _shaderProgram->uniformLocation("use_color");
if(_patternGenerator) { delete _patternGenerator; }
_patternGenerator = new PatternGenerator();
_patternGenerator->_shaderProgram = _shaderProgram;
_patternGenerator->_colorLocation = _colorLocation;
_patternGenerator->_vertexLocation = _vertexLocation;
_patternGenerator->_use_color_location = _use_color_location;
_patternGenerator->_img_width = _img_width;
_patternGenerator->_img_height = _img_height;
_patternGenerator->InitTiling();
_patternGenerator->GeneratePattern(SystemParams::default_tiling);
//_patternGenerator->GeneratePattern("test");
}
void GLWidget::GeneratePattern(std::string tilingName)
{
_patternGenerator->GeneratePattern(tilingName);
}
bool GLWidget::event( QEvent * event )
{
return QGLWidget::event(event);
}
// This is an override function from Qt but I can't find its purpose
void GLWidget::resizeGL(int width, int height)
{
}
void GLWidget::paintGL()
{
QVector3D backColor = SystemParams::background_color;
glClearColor( backColor.x(), backColor.y(), backColor.z(), 1.0 );
//glClearColor( 0, 0, 0, 1.0 );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, this->width(), this->height());
int current_width = width();
int current_height = height();
// Set orthographic Matrix
QMatrix4x4 orthoMatrix;
orthoMatrix.ortho(0.0 + _scrollOffset.x(),
(float)current_width + _scrollOffset.x(),
(float)current_height + _scrollOffset.y(),
0.0 + _scrollOffset.y(),
-100, 100);
// Translate the view to the middle
QMatrix4x4 transformMatrix;
transformMatrix.setToIdentity();
transformMatrix.scale(_zoomFactor);
_shaderProgram->setUniformValue(_mvpMatrixLocation, orthoMatrix * transformMatrix);
_patternGenerator->Paint(_zoomFactor);
//PaintCurve();
}
// Mouse is pressed
void GLWidget::mousePressEvent(int x, int y)
{
_isMouseDown = true;
double dx = x + _scrollOffset.x();
dx /= _zoomFactor;
double dy = y + _scrollOffset.y();
dy /= _zoomFactor;
this->repaint();
}
// Mouse is moved
void GLWidget::mouseMoveEvent(int x, int y)
{
double dx = x + _scrollOffset.x();
dx /= _zoomFactor;
double dy = y + _scrollOffset.y();
dy /= _zoomFactor;
// your stuff
this->repaint();
}
// Mouse is released
void GLWidget::mouseReleaseEvent(int x, int y)
{
_isMouseDown = false;
double dx = x + _scrollOffset.x();
dx /= _zoomFactor;
double dy = y + _scrollOffset.y();
dy /= _zoomFactor;
// your stuff
this->repaint();
}
void GLWidget::mouseDoubleClick(int x, int y)
{
double dx = x + _scrollOffset.x();
dx /= _zoomFactor;
double dy = y + _scrollOffset.y();
dy /= _zoomFactor;
// your stuff
this->repaint();
}
void GLWidget::HorizontalScroll(int val) { _scrollOffset.setX(val); }
void GLWidget::VerticalScroll(int val) { _scrollOffset.setY(val); }
void GLWidget::ZoomIn() { this->_zoomFactor += 10.0f; }
void GLWidget::ZoomOut() { this->_zoomFactor -= 10.0f; if(this->_zoomFactor < 1.0f) _zoomFactor = 1.0f; }
void GLWidget::ResizeLines(std::vector<ALine> &lines, AVector offsetVec, float scaleFactor)
{
for(int a = 0; a < lines.size(); a++)
{
lines[a].XA -= offsetVec.x;
lines[a].YA -= offsetVec.y;
lines[a].XB -= offsetVec.x;
lines[a].YB -= offsetVec.y;
lines[a].XA *= scaleFactor;
lines[a].YA *= scaleFactor;
lines[a].XB *= scaleFactor;
lines[a].YB *= scaleFactor;
}
}
void GLWidget::SaveToSvg()
{
std::vector<ALine> tilingLines = _patternGenerator->GetTilingLines();
std::vector<ALine> uLines = _patternGenerator->GetULines();
std::vector<ALine> oLines = _patternGenerator->GetOLines();
std::vector<ALine> triangleLines = _patternGenerator->GetTriangleLines();
std::vector<ALine> backTriangleLines = _patternGenerator->GetBackTriangleLines();
std::vector<ALine> addTriangleLines = _patternGenerator->GetAddTriangleLines();
float xLeft = 0 + _scrollOffset.x();
float yTop = 0 + _scrollOffset.y();
float invScale = 1.0 / this->_zoomFactor;
xLeft *= invScale;
yTop *= invScale;
AVector offsetVec(xLeft, yTop);
ResizeLines(tilingLines, offsetVec, _zoomFactor);
ResizeLines(uLines, offsetVec, _zoomFactor);
ResizeLines(oLines, offsetVec, _zoomFactor);
ResizeLines(triangleLines, offsetVec, _zoomFactor);
ResizeLines(backTriangleLines, offsetVec, _zoomFactor);
ResizeLines(addTriangleLines, offsetVec, _zoomFactor);
QSvgGenerator generator;
generator.setFileName("image.svg");
generator.setSize(QSize(this->width(), this->height()));
generator.setViewBox(QRect(0, 0, this->width(), this->height()));
generator.setTitle(tr("Islamic Star Pattern"));
generator.setDescription(tr("Islamic Star Pattern"));
QPainter painter;
painter.begin(&generator);
painter.setClipRect(QRect(0, 0, this->width(), this->height()));
QBrush myBrush;
QVector3D backVec = SystemParams::background_color;
QColor backCol(backVec.x() * 255, backVec.y() * 255, backVec.z() * 255);
myBrush.setColor(backCol);
myBrush.setStyle(Qt::SolidPattern);
painter.fillRect(QRect(0, 0, this->width(), this->height()), myBrush);
QVector3D ribVec = SystemParams::ribbon_color;
QVector3D lineVec = SystemParams::interlacing_color;
QVector3D starVec = SystemParams::star_color;
QColor starCol(starVec.x() * 255, starVec.y() * 255, starVec.z() * 255);
QColor ribCol(ribVec.x() * 255, ribVec.y() * 255, ribVec.z() * 255);
QColor lineCol(lineVec.x() * 255, lineVec.y() * 255, lineVec.z() * 255);
// star triangles
myBrush.setColor(starCol);
myBrush.setStyle(Qt::SolidPattern);
painter.setPen(QPen(starCol, 0.75, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < triangleLines.size(); a += 3)
{
ALine line1 = triangleLines[a];
ALine line2 = triangleLines[a+1];
ALine line3 = triangleLines[a+2];
QPolygonF poly;
poly << QPointF(line1.XA, line1.YA)
<< QPointF(line1.XB, line1.YB)
<< QPointF(line3.XA, line3.YA);
QPainterPath path;
path.addPolygon(poly);
painter.drawPolygon(poly);
painter.fillPath(path, myBrush);
}
// back triangles
myBrush.setColor(backCol);
myBrush.setStyle(Qt::SolidPattern);
painter.setPen(QPen(backCol, 0.75, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < backTriangleLines.size(); a += 3)
{
ALine line1 = backTriangleLines[a];
ALine line2 = backTriangleLines[a+1];
ALine line3 = backTriangleLines[a+2];
QPolygonF poly;
poly << QPointF(line1.XA, line1.YA)
<< QPointF(line1.XB, line1.YB)
<< QPointF(line3.XA, line3.YA);
QPainterPath path;
path.addPolygon(poly);
painter.drawPolygon(poly);
painter.fillPath(path, myBrush);
}
// add triangles
myBrush.setColor(starCol);
myBrush.setStyle(Qt::SolidPattern);
painter.setPen(QPen(starCol, 0.75, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < addTriangleLines.size(); a += 3)
{
ALine line1 = addTriangleLines[a];
ALine line2 = addTriangleLines[a+1];
ALine line3 = addTriangleLines[a+2];
QPolygonF poly;
poly << QPointF(line1.XA, line1.YA)
<< QPointF(line1.XB, line1.YB)
<< QPointF(line3.XA, line3.YA);
QPainterPath path;
path.addPolygon(poly);
painter.drawPolygon(poly);
painter.fillPath(path, myBrush);
}
// under ribbons
myBrush.setColor(ribCol);
myBrush.setStyle(Qt::SolidPattern);
painter.setPen(QPen(ribCol, 0.25, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < uLines.size(); a += 2)
{
ALine line1 = uLines[a];
ALine line2 = uLines[a+1];
QPolygonF poly;
poly << QPointF(line1.XA, line1.YA)
<< QPointF(line1.XB, line1.YB)
<< QPointF(line2.XB, line2.YB)
<< QPointF(line2.XA, line2.YA);
QPainterPath path;
path.addPolygon(poly);
painter.drawPolygon(poly);
painter.fillPath(path, myBrush);
}
painter.setPen(QPen(lineCol, SystemParams::line_width * _zoomFactor, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < uLines.size(); a++)
{
ALine aLine = uLines[a];
painter.drawLine(QPointF(aLine.XA, aLine.YA),
QPointF(aLine.XB, aLine.YB));
}
myBrush.setColor(ribCol);
myBrush.setStyle(Qt::SolidPattern);
painter.setPen(QPen(ribCol, 0.25, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < oLines.size(); a += 2)
{
ALine line1 = oLines[a];
ALine line2 = oLines[a+1];
QPolygonF poly;
poly << QPointF(line1.XA, line1.YA)
<< QPointF(line1.XB, line1.YB)
<< QPointF(line2.XB, line2.YB)
<< QPointF(line2.XA, line2.YA);
QPainterPath path;
path.addPolygon(poly);
painter.drawPolygon(poly);
painter.fillPath(path, myBrush);
}
painter.setPen(QPen(lineCol, SystemParams::line_width * _zoomFactor, Qt::SolidLine, Qt::RoundCap));
for(int a = 0; a < oLines.size(); a++)
{
ALine aLine = oLines[a];
painter.drawLine(QPointF(aLine.XA, aLine.YA),
QPointF(aLine.XB, aLine.YB));
}
painter.end();
}