source: trunk/demos/boxes/scene.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 35.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the demonstration applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QDebug>
43#include "scene.h"
44#include <QtGui/qmatrix4x4.h>
45#include <QtGui/qvector3d.h>
46
47#include "3rdparty/fbm.h"
48
49void checkGLErrors(const QString& prefix)
50{
51 switch (glGetError()) {
52 case GL_NO_ERROR:
53 //qDebug() << prefix << tr("No error.");
54 break;
55 case GL_INVALID_ENUM:
56 qDebug() << prefix << QObject::tr("Invalid enum.");
57 break;
58 case GL_INVALID_VALUE:
59 qDebug() << prefix << QObject::tr("Invalid value.");
60 break;
61 case GL_INVALID_OPERATION:
62 qDebug() << prefix << QObject::tr("Invalid operation.");
63 break;
64 case GL_STACK_OVERFLOW:
65 qDebug() << prefix << QObject::tr("Stack overflow.");
66 break;
67 case GL_STACK_UNDERFLOW:
68 qDebug() << prefix << QObject::tr("Stack underflow.");
69 break;
70 case GL_OUT_OF_MEMORY:
71 qDebug() << prefix << QObject::tr("Out of memory.");
72 break;
73 default:
74 qDebug() << prefix << QObject::tr("Unknown error.");
75 break;
76 }
77}
78
79//============================================================================//
80// ColorEdit //
81//============================================================================//
82
83ColorEdit::ColorEdit(QRgb initialColor, int id)
84 : m_color(initialColor), m_id(id)
85{
86 QHBoxLayout *layout = new QHBoxLayout;
87 setLayout(layout);
88 layout->setContentsMargins(0, 0, 0, 0);
89
90 m_lineEdit = new QLineEdit(QString::number(m_color, 16));
91 layout->addWidget(m_lineEdit);
92
93 m_button = new QFrame;
94 QPalette palette = m_button->palette();
95 palette.setColor(QPalette::Window, QColor(m_color));
96 m_button->setPalette(palette);
97 m_button->setAutoFillBackground(true);
98 m_button->setMinimumSize(32, 0);
99 m_button->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
100 m_button->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
101 layout->addWidget(m_button);
102
103 connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(editDone()));
104}
105
106void ColorEdit::editDone()
107{
108 bool ok;
109 QRgb newColor = m_lineEdit->text().toUInt(&ok, 16);
110 if (ok)
111 setColor(newColor);
112}
113
114void ColorEdit::mousePressEvent(QMouseEvent *event)
115{
116 if (event->button() == Qt::LeftButton) {
117 QColor color(m_color);
118 QColorDialog dialog(color, 0);
119 dialog.setOption(QColorDialog::ShowAlphaChannel, true);
120// The ifdef block is a workaround for the beta, TODO: remove when bug 238525 is fixed
121#ifdef Q_WS_MAC
122 dialog.setOption(QColorDialog::DontUseNativeDialog, true);
123#endif
124 dialog.move(280, 120);
125 if (dialog.exec() == QDialog::Rejected)
126 return;
127 QRgb newColor = dialog.selectedColor().rgba();
128 if (newColor == m_color)
129 return;
130 setColor(newColor);
131 }
132}
133
134void ColorEdit::setColor(QRgb color)
135{
136 m_color = color;
137 m_lineEdit->setText(QString::number(m_color, 16)); // "Clean up" text
138 QPalette palette = m_button->palette();
139 palette.setColor(QPalette::Window, QColor(m_color));
140 m_button->setPalette(palette);
141 emit colorChanged(m_color, m_id);
142}
143
144//============================================================================//
145// FloatEdit //
146//============================================================================//
147
148FloatEdit::FloatEdit(float initialValue, int id)
149 : m_value(initialValue), m_id(id)
150{
151 QHBoxLayout *layout = new QHBoxLayout;
152 setLayout(layout);
153 layout->setContentsMargins(0, 0, 0, 0);
154
155 m_lineEdit = new QLineEdit(QString::number(m_value));
156 layout->addWidget(m_lineEdit);
157
158 connect(m_lineEdit, SIGNAL(editingFinished()), this, SLOT(editDone()));
159}
160
161void FloatEdit::editDone()
162{
163 bool ok;
164 float newValue = m_lineEdit->text().toFloat(&ok);
165 if (ok) {
166 m_value = newValue;
167 m_lineEdit->setText(QString::number(m_value)); // "Clean up" text
168 emit valueChanged(m_value, m_id);
169 }
170}
171
172//============================================================================//
173// TwoSidedGraphicsWidget //
174//============================================================================//
175
176TwoSidedGraphicsWidget::TwoSidedGraphicsWidget(QGraphicsScene *scene)
177 : QObject(scene)
178 , m_current(0)
179 , m_angle(0)
180 , m_delta(0)
181{
182 for (int i = 0; i < 2; ++i)
183 m_proxyWidgets[i] = 0;
184}
185
186void TwoSidedGraphicsWidget::setWidget(int index, QWidget *widget)
187{
188 if (index < 0 || index >= 2)
189 {
190 qWarning("TwoSidedGraphicsWidget::setWidget: Index out of bounds, index == %d", index);
191 return;
192 }
193
194 GraphicsWidget *proxy = new GraphicsWidget;
195 proxy->setWidget(widget);
196
197 if (m_proxyWidgets[index])
198 delete m_proxyWidgets[index];
199 m_proxyWidgets[index] = proxy;
200
201 proxy->setCacheMode(QGraphicsItem::ItemCoordinateCache);
202 proxy->setZValue(1e30); // Make sure the dialog is drawn on top of all other (OpenGL) items
203
204 if (index != m_current)
205 proxy->setVisible(false);
206
207 qobject_cast<QGraphicsScene *>(parent())->addItem(proxy);
208}
209
210QWidget *TwoSidedGraphicsWidget::widget(int index)
211{
212 if (index < 0 || index >= 2)
213 {
214 qWarning("TwoSidedGraphicsWidget::widget: Index out of bounds, index == %d", index);
215 return 0;
216 }
217 return m_proxyWidgets[index]->widget();
218}
219
220void TwoSidedGraphicsWidget::flip()
221{
222 m_delta = (m_current == 0 ? 9 : -9);
223 animateFlip();
224}
225
226void TwoSidedGraphicsWidget::animateFlip()
227{
228 m_angle += m_delta;
229 if (m_angle == 90) {
230 int old = m_current;
231 m_current ^= 1;
232 m_proxyWidgets[old]->setVisible(false);
233 m_proxyWidgets[m_current]->setVisible(true);
234 m_proxyWidgets[m_current]->setGeometry(m_proxyWidgets[old]->geometry());
235 }
236
237 QRectF r = m_proxyWidgets[m_current]->boundingRect();
238 m_proxyWidgets[m_current]->setTransform(QTransform()
239 .translate(r.width() / 2, r.height() / 2)
240 .rotate(m_angle - 180 * m_current, Qt::YAxis)
241 .translate(-r.width() / 2, -r.height() / 2));
242
243 if ((m_current == 0 && m_angle > 0) || (m_current == 1 && m_angle < 180))
244 QTimer::singleShot(25, this, SLOT(animateFlip()));
245}
246
247QVariant GraphicsWidget::itemChange(GraphicsItemChange change, const QVariant &value)
248{
249 if (change == ItemPositionChange && scene()) {
250 QRectF rect = boundingRect();
251 QPointF pos = value.toPointF();
252 QRectF sceneRect = scene()->sceneRect();
253 if (pos.x() + rect.left() < sceneRect.left())
254 pos.setX(sceneRect.left() - rect.left());
255 else if (pos.x() + rect.right() >= sceneRect.right())
256 pos.setX(sceneRect.right() - rect.right());
257 if (pos.y() + rect.top() < sceneRect.top())
258 pos.setY(sceneRect.top() - rect.top());
259 else if (pos.y() + rect.bottom() >= sceneRect.bottom())
260 pos.setY(sceneRect.bottom() - rect.bottom());
261 return pos;
262 }
263 return QGraphicsProxyWidget::itemChange(change, value);
264}
265
266void GraphicsWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
267{
268 setCacheMode(QGraphicsItem::NoCache);
269 setCacheMode(QGraphicsItem::ItemCoordinateCache);
270 QGraphicsProxyWidget::resizeEvent(event);
271}
272
273void GraphicsWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
274{
275 painter->setRenderHint(QPainter::Antialiasing, false);
276 QGraphicsProxyWidget::paint(painter, option, widget);
277 //painter->setRenderHint(QPainter::Antialiasing, true);
278}
279
280//============================================================================//
281// RenderOptionsDialog //
282//============================================================================//
283
284RenderOptionsDialog::RenderOptionsDialog()
285 : QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint)
286{
287 setWindowOpacity(0.75);
288 setWindowTitle(tr("Options (double click to flip)"));
289 QGridLayout *layout = new QGridLayout;
290 setLayout(layout);
291 layout->setColumnStretch(1, 1);
292
293 int row = 0;
294
295 QCheckBox *check = new QCheckBox(tr("Dynamic cube map"));
296 check->setCheckState(Qt::Unchecked);
297 // Dynamic cube maps are only enabled when multi-texturing and render to texture are available.
298 check->setEnabled(glActiveTexture && glGenFramebuffersEXT);
299 connect(check, SIGNAL(stateChanged(int)), this, SIGNAL(dynamicCubemapToggled(int)));
300 layout->addWidget(check, 0, 0, 1, 2);
301 ++row;
302
303 QPalette palette;
304
305 // Load all .par files
306 // .par files have a simple syntax for specifying user adjustable uniform variables.
307 QSet<QByteArray> uniforms;
308 QList<QString> filter = QStringList("*.par");
309 QList<QFileInfo> files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
310
311 foreach (QFileInfo fileInfo, files) {
312 QFile file(fileInfo.absoluteFilePath());
313 if (file.open(QIODevice::ReadOnly)) {
314 while (!file.atEnd()) {
315 QList<QByteArray> tokens = file.readLine().simplified().split(' ');
316 QList<QByteArray>::const_iterator it = tokens.begin();
317 if (it == tokens.end())
318 continue;
319 QByteArray type = *it;
320 if (++it == tokens.end())
321 continue;
322 QByteArray name = *it;
323 bool singleElement = (tokens.size() == 3); // type, name and one value
324 char counter[10] = "000000000";
325 int counterPos = 8; // position of last digit
326 while (++it != tokens.end()) {
327 m_parameterNames << name;
328 if (!singleElement) {
329 m_parameterNames.back() += "[";
330 m_parameterNames.back() += counter + counterPos;
331 m_parameterNames.back() += "]";
332 int j = 8; // position of last digit
333 ++counter[j];
334 while (j > 0 && counter[j] > '9') {
335 counter[j] = '0';
336 ++counter[--j];
337 }
338 if (j < counterPos)
339 counterPos = j;
340 }
341
342 if (type == "color") {
343 layout->addWidget(new QLabel(m_parameterNames.back()));
344 bool ok;
345 ColorEdit *colorEdit = new ColorEdit(it->toUInt(&ok, 16), m_parameterNames.size() - 1);
346 m_parameterEdits << colorEdit;
347 layout->addWidget(colorEdit);
348 connect(colorEdit, SIGNAL(colorChanged(QRgb,int)), this, SLOT(setColorParameter(QRgb,int)));
349 ++row;
350 } else if (type == "float") {
351 layout->addWidget(new QLabel(m_parameterNames.back()));
352 bool ok;
353 FloatEdit *floatEdit = new FloatEdit(it->toFloat(&ok), m_parameterNames.size() - 1);
354 m_parameterEdits << floatEdit;
355 layout->addWidget(floatEdit);
356 connect(floatEdit, SIGNAL(valueChanged(float,int)), this, SLOT(setFloatParameter(float,int)));
357 ++row;
358 }
359 }
360 }
361 file.close();
362 }
363 }
364
365 layout->addWidget(new QLabel(tr("Texture:")));
366 m_textureCombo = new QComboBox;
367 connect(m_textureCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(textureChanged(int)));
368 layout->addWidget(m_textureCombo);
369 ++row;
370
371 layout->addWidget(new QLabel(tr("Shader:")));
372 m_shaderCombo = new QComboBox;
373 connect(m_shaderCombo, SIGNAL(currentIndexChanged(int)), this, SIGNAL(shaderChanged(int)));
374 layout->addWidget(m_shaderCombo);
375 ++row;
376
377 layout->setRowStretch(row, 1);
378}
379
380int RenderOptionsDialog::addTexture(const QString &name)
381{
382 m_textureCombo->addItem(name);
383 return m_textureCombo->count() - 1;
384}
385
386int RenderOptionsDialog::addShader(const QString &name)
387{
388 m_shaderCombo->addItem(name);
389 return m_shaderCombo->count() - 1;
390}
391
392void RenderOptionsDialog::emitParameterChanged()
393{
394 foreach (ParameterEdit *edit, m_parameterEdits)
395 edit->emitChange();
396}
397
398void RenderOptionsDialog::setColorParameter(QRgb color, int id)
399{
400 emit colorParameterChanged(m_parameterNames[id], color);
401}
402
403void RenderOptionsDialog::setFloatParameter(float value, int id)
404{
405 emit floatParameterChanged(m_parameterNames[id], value);
406}
407
408void RenderOptionsDialog::mouseDoubleClickEvent(QMouseEvent *event)
409{
410 if (event->button() == Qt::LeftButton)
411 emit doubleClicked();
412}
413
414//============================================================================//
415// ItemDialog //
416//============================================================================//
417
418ItemDialog::ItemDialog()
419 : QDialog(0, Qt::CustomizeWindowHint | Qt::WindowTitleHint)
420{
421 setWindowTitle(tr("Items (double click to flip)"));
422 setWindowOpacity(0.75);
423 resize(160, 100);
424
425 QVBoxLayout *layout = new QVBoxLayout;
426 setLayout(layout);
427 QPushButton *button;
428
429 button = new QPushButton(tr("Add Qt box"));
430 layout->addWidget(button);
431 connect(button, SIGNAL(clicked()), this, SLOT(triggerNewQtBox()));
432
433 button = new QPushButton(tr("Add circle"));
434 layout->addWidget(button);
435 connect(button, SIGNAL(clicked()), this, SLOT(triggerNewCircleItem()));
436
437 button = new QPushButton(tr("Add square"));
438 layout->addWidget(button);
439 connect(button, SIGNAL(clicked()), this, SLOT(triggerNewSquareItem()));
440
441 layout->addStretch(1);
442}
443
444void ItemDialog::triggerNewQtBox()
445{
446 emit newItemTriggered(QtBoxItem);
447}
448
449void ItemDialog::triggerNewCircleItem()
450{
451 emit newItemTriggered(CircleItem);
452}
453
454void ItemDialog::triggerNewSquareItem()
455{
456 emit newItemTriggered(SquareItem);
457}
458
459void ItemDialog::mouseDoubleClickEvent(QMouseEvent *event)
460{
461 if (event->button() == Qt::LeftButton)
462 emit doubleClicked();
463}
464
465//============================================================================//
466// Scene //
467//============================================================================//
468
469const static char environmentShaderText[] =
470 "uniform samplerCube env;"
471 "void main() {"
472 "gl_FragColor = textureCube(env, gl_TexCoord[1].xyz);"
473 "}";
474
475Scene::Scene(int width, int height, int maxTextureSize)
476 : m_distExp(600)
477 , m_frame(0)
478 , m_maxTextureSize(maxTextureSize)
479 , m_currentShader(0)
480 , m_currentTexture(0)
481 , m_dynamicCubemap(false)
482 , m_updateAllCubemaps(true)
483 , m_box(0)
484 , m_vertexShader(0)
485 , m_environmentShader(0)
486 , m_environmentProgram(0)
487{
488 setSceneRect(0, 0, width, height);
489
490 m_trackBalls[0] = TrackBall(0.05f, QVector3D(0, 1, 0), TrackBall::Sphere);
491 m_trackBalls[1] = TrackBall(0.005f, QVector3D(0, 0, 1), TrackBall::Sphere);
492 m_trackBalls[2] = TrackBall(0.0f, QVector3D(0, 1, 0), TrackBall::Plane);
493
494 m_renderOptions = new RenderOptionsDialog;
495 m_renderOptions->move(20, 120);
496 m_renderOptions->resize(m_renderOptions->sizeHint());
497
498 connect(m_renderOptions, SIGNAL(dynamicCubemapToggled(int)), this, SLOT(toggleDynamicCubemap(int)));
499 connect(m_renderOptions, SIGNAL(colorParameterChanged(QString,QRgb)), this, SLOT(setColorParameter(QString,QRgb)));
500 connect(m_renderOptions, SIGNAL(floatParameterChanged(QString,float)), this, SLOT(setFloatParameter(QString,float)));
501 connect(m_renderOptions, SIGNAL(textureChanged(int)), this, SLOT(setTexture(int)));
502 connect(m_renderOptions, SIGNAL(shaderChanged(int)), this, SLOT(setShader(int)));
503
504 m_itemDialog = new ItemDialog;
505 connect(m_itemDialog, SIGNAL(newItemTriggered(ItemDialog::ItemType)), this, SLOT(newItem(ItemDialog::ItemType)));
506
507 TwoSidedGraphicsWidget *twoSided = new TwoSidedGraphicsWidget(this);
508 twoSided->setWidget(0, m_renderOptions);
509 twoSided->setWidget(1, m_itemDialog);
510
511 connect(m_renderOptions, SIGNAL(doubleClicked()), twoSided, SLOT(flip()));
512 connect(m_itemDialog, SIGNAL(doubleClicked()), twoSided, SLOT(flip()));
513
514 addItem(new QtBox(64, width - 64, height - 64));
515 addItem(new QtBox(64, width - 64, 64));
516 addItem(new QtBox(64, 64, height - 64));
517 addItem(new QtBox(64, 64, 64));
518
519 initGL();
520
521 m_timer = new QTimer(this);
522 m_timer->setInterval(20);
523 connect(m_timer, SIGNAL(timeout()), this, SLOT(update()));
524 m_timer->start();
525
526 m_time.start();
527}
528
529Scene::~Scene()
530{
531 if (m_box)
532 delete m_box;
533 foreach (GLTexture *texture, m_textures)
534 if (texture) delete texture;
535 if (m_mainCubemap)
536 delete m_mainCubemap;
537 foreach (QGLShaderProgram *program, m_programs)
538 if (program) delete program;
539 if (m_vertexShader)
540 delete m_vertexShader;
541 foreach (QGLShader *shader, m_fragmentShaders)
542 if (shader) delete shader;
543 foreach (GLRenderTargetCube *rt, m_cubemaps)
544 if (rt) delete rt;
545 if (m_environmentShader)
546 delete m_environmentShader;
547 if (m_environmentProgram)
548 delete m_environmentProgram;
549}
550
551void Scene::initGL()
552{
553 m_box = new GLRoundedBox(0.25f, 1.0f, 10);
554
555 m_vertexShader = new QGLShader(QGLShader::Vertex);
556 m_vertexShader->compileSourceFile(QLatin1String(":/res/boxes/basic.vsh"));
557
558 QStringList list;
559 list << ":/res/boxes/cubemap_posx.jpg" << ":/res/boxes/cubemap_negx.jpg" << ":/res/boxes/cubemap_posy.jpg"
560 << ":/res/boxes/cubemap_negy.jpg" << ":/res/boxes/cubemap_posz.jpg" << ":/res/boxes/cubemap_negz.jpg";
561 m_environment = new GLTextureCube(list, qMin(1024, m_maxTextureSize));
562 m_environmentShader = new QGLShader(QGLShader::Fragment);
563 m_environmentShader->compileSourceCode(environmentShaderText);
564 m_environmentProgram = new QGLShaderProgram;
565 m_environmentProgram->addShader(m_vertexShader);
566 m_environmentProgram->addShader(m_environmentShader);
567 m_environmentProgram->link();
568
569 const int NOISE_SIZE = 128; // for a different size, B and BM in fbm.c must also be changed
570 m_noise = new GLTexture3D(NOISE_SIZE, NOISE_SIZE, NOISE_SIZE);
571 QRgb *data = new QRgb[NOISE_SIZE * NOISE_SIZE * NOISE_SIZE];
572 memset(data, 0, NOISE_SIZE * NOISE_SIZE * NOISE_SIZE * sizeof(QRgb));
573 QRgb *p = data;
574 float pos[3];
575 for (int k = 0; k < NOISE_SIZE; ++k) {
576 pos[2] = k * (0x20 / (float)NOISE_SIZE);
577 for (int j = 0; j < NOISE_SIZE; ++j) {
578 for (int i = 0; i < NOISE_SIZE; ++i) {
579 for (int byte = 0; byte < 4; ++byte) {
580 pos[0] = (i + (byte & 1) * 16) * (0x20 / (float)NOISE_SIZE);
581 pos[1] = (j + (byte & 2) * 8) * (0x20 / (float)NOISE_SIZE);
582 *p |= (int)(128.0f * (noise3(pos) + 1.0f)) << (byte * 8);
583 }
584 ++p;
585 }
586 }
587 }
588 m_noise->load(NOISE_SIZE, NOISE_SIZE, NOISE_SIZE, data);
589 delete[] data;
590
591 m_mainCubemap = new GLRenderTargetCube(512);
592
593 QStringList filter;
594 QList<QFileInfo> files;
595
596 // Load all .png files as textures
597 m_currentTexture = 0;
598 filter = QStringList("*.png");
599 files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
600
601 foreach (QFileInfo file, files) {
602 GLTexture *texture = new GLTexture2D(file.absoluteFilePath(), qMin(256, m_maxTextureSize), qMin(256, m_maxTextureSize));
603 if (texture->failed()) {
604 delete texture;
605 continue;
606 }
607 m_textures << texture;
608 m_renderOptions->addTexture(file.baseName());
609 }
610
611 if (m_textures.size() == 0)
612 m_textures << new GLTexture2D(qMin(64, m_maxTextureSize), qMin(64, m_maxTextureSize));
613
614 // Load all .fsh files as fragment shaders
615 m_currentShader = 0;
616 filter = QStringList("*.fsh");
617 files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
618 foreach (QFileInfo file, files) {
619 QGLShaderProgram *program = new QGLShaderProgram;
620 QGLShader* shader = new QGLShader(QGLShader::Fragment);
621 shader->compileSourceFile(file.absoluteFilePath());
622 // The program does not take ownership over the shaders, so store them in a vector so they can be deleted afterwards.
623 program->addShader(m_vertexShader);
624 program->addShader(shader);
625 if (!program->link()) {
626 qWarning("Failed to compile and link shader program");
627 qWarning("Vertex shader log:");
628 qWarning() << m_vertexShader->log();
629 qWarning() << "Fragment shader log ( file =" << file.absoluteFilePath() << "):";
630 qWarning() << shader->log();
631 qWarning("Shader program log:");
632 qWarning() << program->log();
633
634 delete shader;
635 delete program;
636 continue;
637 }
638
639 m_fragmentShaders << shader;
640 m_programs << program;
641 m_renderOptions->addShader(file.baseName());
642
643 program->bind();
644 m_cubemaps << ((program->uniformLocation("env") != -1) ? new GLRenderTargetCube(qMin(256, m_maxTextureSize)) : 0);
645 program->release();
646 }
647
648 if (m_programs.size() == 0)
649 m_programs << new QGLShaderProgram;
650
651 m_renderOptions->emitParameterChanged();
652}
653
654static void loadMatrix(const QMatrix4x4& m)
655{
656 GLfloat mat[16];
657 const qreal *data = m.constData();
658 for (int index = 0; index < 16; ++index)
659 mat[index] = data[index];
660 glLoadMatrixf(mat);
661}
662
663static void multMatrix(const QMatrix4x4& m)
664{
665 GLfloat mat[16];
666 const qreal *data = m.constData();
667 for (int index = 0; index < 16; ++index)
668 mat[index] = data[index];
669 glMultMatrixf(mat);
670}
671
672// If one of the boxes should not be rendered, set excludeBox to its index.
673// If the main box should not be rendered, set excludeBox to -1.
674void Scene::renderBoxes(const QMatrix4x4 &view, int excludeBox)
675{
676 QMatrix4x4 invView = view.inverted();
677
678 // If multi-texturing is supported, use three saplers.
679 if (glActiveTexture) {
680 glActiveTexture(GL_TEXTURE0);
681 m_textures[m_currentTexture]->bind();
682 glActiveTexture(GL_TEXTURE2);
683 m_noise->bind();
684 glActiveTexture(GL_TEXTURE1);
685 } else {
686 m_textures[m_currentTexture]->bind();
687 }
688
689 glDisable(GL_LIGHTING);
690 glDisable(GL_CULL_FACE);
691
692 QMatrix4x4 viewRotation(view);
693 viewRotation(3, 0) = viewRotation(3, 1) = viewRotation(3, 2) = 0.0f;
694 viewRotation(0, 3) = viewRotation(1, 3) = viewRotation(2, 3) = 0.0f;
695 viewRotation(3, 3) = 1.0f;
696 loadMatrix(viewRotation);
697 glScalef(20.0f, 20.0f, 20.0f);
698
699 // Don't render the environment if the environment texture can't be set for the correct sampler.
700 if (glActiveTexture) {
701 m_environment->bind();
702 m_environmentProgram->bind();
703 m_environmentProgram->setUniformValue("tex", GLint(0));
704 m_environmentProgram->setUniformValue("env", GLint(1));
705 m_environmentProgram->setUniformValue("noise", GLint(2));
706 m_box->draw();
707 m_environmentProgram->release();
708 m_environment->unbind();
709 }
710
711 loadMatrix(view);
712
713 glEnable(GL_CULL_FACE);
714 glEnable(GL_LIGHTING);
715
716 for (int i = 0; i < m_programs.size(); ++i) {
717 if (i == excludeBox)
718 continue;
719
720 glPushMatrix();
721 QMatrix4x4 m;
722 m.rotate(m_trackBalls[1].rotation());
723 multMatrix(m);
724
725 glRotatef(360.0f * i / m_programs.size(), 0.0f, 0.0f, 1.0f);
726 glTranslatef(2.0f, 0.0f, 0.0f);
727 glScalef(0.3f, 0.6f, 0.6f);
728
729 if (glActiveTexture) {
730 if (m_dynamicCubemap && m_cubemaps[i])
731 m_cubemaps[i]->bind();
732 else
733 m_environment->bind();
734 }
735 m_programs[i]->bind();
736 m_programs[i]->setUniformValue("tex", GLint(0));
737 m_programs[i]->setUniformValue("env", GLint(1));
738 m_programs[i]->setUniformValue("noise", GLint(2));
739 m_programs[i]->setUniformValue("view", view);
740 m_programs[i]->setUniformValue("invView", invView);
741 m_box->draw();
742 m_programs[i]->release();
743
744 if (glActiveTexture) {
745 if (m_dynamicCubemap && m_cubemaps[i])
746 m_cubemaps[i]->unbind();
747 else
748 m_environment->unbind();
749 }
750 glPopMatrix();
751 }
752
753 if (-1 != excludeBox) {
754 QMatrix4x4 m;
755 m.rotate(m_trackBalls[0].rotation());
756 multMatrix(m);
757
758 if (glActiveTexture) {
759 if (m_dynamicCubemap)
760 m_mainCubemap->bind();
761 else
762 m_environment->bind();
763 }
764
765 m_programs[m_currentShader]->bind();
766 m_programs[m_currentShader]->setUniformValue("tex", GLint(0));
767 m_programs[m_currentShader]->setUniformValue("env", GLint(1));
768 m_programs[m_currentShader]->setUniformValue("noise", GLint(2));
769 m_programs[m_currentShader]->setUniformValue("view", view);
770 m_programs[m_currentShader]->setUniformValue("invView", invView);
771 m_box->draw();
772 m_programs[m_currentShader]->release();
773
774 if (glActiveTexture) {
775 if (m_dynamicCubemap)
776 m_mainCubemap->unbind();
777 else
778 m_environment->unbind();
779 }
780 }
781
782 if (glActiveTexture) {
783 glActiveTexture(GL_TEXTURE2);
784 m_noise->unbind();
785 glActiveTexture(GL_TEXTURE0);
786 }
787 m_textures[m_currentTexture]->unbind();
788}
789
790void Scene::setStates()
791{
792 //glClearColor(0.25f, 0.25f, 0.5f, 1.0f);
793
794 glEnable(GL_DEPTH_TEST);
795 glEnable(GL_CULL_FACE);
796 glEnable(GL_LIGHTING);
797 //glEnable(GL_COLOR_MATERIAL);
798 glEnable(GL_TEXTURE_2D);
799 glEnable(GL_NORMALIZE);
800
801 glMatrixMode(GL_PROJECTION);
802 glPushMatrix();
803 glLoadIdentity();
804
805 glMatrixMode(GL_MODELVIEW);
806 glPushMatrix();
807 glLoadIdentity();
808
809 setLights();
810
811 float materialSpecular[] = {0.5f, 0.5f, 0.5f, 1.0f};
812 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, materialSpecular);
813 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 32.0f);
814}
815
816void Scene::setLights()
817{
818 glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
819 //float lightColour[] = {1.0f, 1.0f, 1.0f, 1.0f};
820 float lightDir[] = {0.0f, 0.0f, 1.0f, 0.0f};
821 //glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColour);
822 //glLightfv(GL_LIGHT0, GL_SPECULAR, lightColour);
823 glLightfv(GL_LIGHT0, GL_POSITION, lightDir);
824 glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
825 glEnable(GL_LIGHT0);
826}
827
828void Scene::defaultStates()
829{
830 //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
831
832 glDisable(GL_DEPTH_TEST);
833 glDisable(GL_CULL_FACE);
834 glDisable(GL_LIGHTING);
835 //glDisable(GL_COLOR_MATERIAL);
836 glDisable(GL_TEXTURE_2D);
837 glDisable(GL_LIGHT0);
838 glDisable(GL_NORMALIZE);
839
840 glMatrixMode(GL_MODELVIEW);
841 glPopMatrix();
842
843 glMatrixMode(GL_PROJECTION);
844 glPopMatrix();
845
846 glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 0.0f);
847 float defaultMaterialSpecular[] = {0.0f, 0.0f, 0.0f, 1.0f};
848 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defaultMaterialSpecular);
849 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f);
850}
851
852void Scene::renderCubemaps()
853{
854 // To speed things up, only update the cubemaps for the small cubes every N frames.
855 const int N = (m_updateAllCubemaps ? 1 : 3);
856
857 QMatrix4x4 mat;
858 GLRenderTargetCube::getProjectionMatrix(mat, 0.1f, 100.0f);
859
860 glMatrixMode(GL_PROJECTION);
861 glPushMatrix();
862 loadMatrix(mat);
863
864 glMatrixMode(GL_MODELVIEW);
865 glPushMatrix();
866
867 QVector3D center;
868
869 for (int i = m_frame % N; i < m_cubemaps.size(); i += N) {
870 if (0 == m_cubemaps[i])
871 continue;
872
873 float angle = 2.0f * PI * i / m_cubemaps.size();
874
875 center = m_trackBalls[1].rotation().rotatedVector(QVector3D(cos(angle), sin(angle), 0.0f));
876
877 for (int face = 0; face < 6; ++face) {
878 m_cubemaps[i]->begin(face);
879
880 GLRenderTargetCube::getViewMatrix(mat, face);
881 QVector4D v = QVector4D(-center.x(), -center.y(), -center.z(), 1.0);
882 mat.setColumn(3, mat * v);
883
884 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
885 renderBoxes(mat, i);
886
887 m_cubemaps[i]->end();
888 }
889 }
890
891 for (int face = 0; face < 6; ++face) {
892 m_mainCubemap->begin(face);
893 GLRenderTargetCube::getViewMatrix(mat, face);
894
895 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
896 renderBoxes(mat, -1);
897
898 m_mainCubemap->end();
899 }
900
901 glPopMatrix();
902
903 glMatrixMode(GL_PROJECTION);
904 glPopMatrix();
905
906 m_updateAllCubemaps = false;
907}
908
909void Scene::drawBackground(QPainter *painter, const QRectF &)
910{
911 float width = float(painter->device()->width());
912 float height = float(painter->device()->height());
913
914 painter->beginNativePainting();
915 setStates();
916
917 if (m_dynamicCubemap)
918 renderCubemaps();
919
920 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
921
922 glMatrixMode(GL_PROJECTION);
923 gluPerspective(60.0, width / height, 0.01, 15.0);
924
925 glMatrixMode(GL_MODELVIEW);
926
927 QMatrix4x4 view;
928 view.rotate(m_trackBalls[2].rotation());
929 view(2, 3) -= 2.0f * exp(m_distExp / 1200.0f);
930 renderBoxes(view);
931
932 defaultStates();
933 ++m_frame;
934
935 painter->endNativePainting();
936}
937
938QPointF Scene::pixelPosToViewPos(const QPointF& p)
939{
940 return QPointF(2.0 * float(p.x()) / width() - 1.0,
941 1.0 - 2.0 * float(p.y()) / height());
942}
943
944void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
945{
946 QGraphicsScene::mouseMoveEvent(event);
947 if (event->isAccepted())
948 return;
949
950 if (event->buttons() & Qt::LeftButton) {
951 m_trackBalls[0].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
952 event->accept();
953 } else {
954 m_trackBalls[0].release(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
955 }
956
957 if (event->buttons() & Qt::RightButton) {
958 m_trackBalls[1].move(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
959 event->accept();
960 } else {
961 m_trackBalls[1].release(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
962 }
963
964 if (event->buttons() & Qt::MidButton) {
965 m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), QQuaternion());
966 event->accept();
967 } else {
968 m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion());
969 }
970}
971
972void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
973{
974 QGraphicsScene::mousePressEvent(event);
975 if (event->isAccepted())
976 return;
977
978 if (event->buttons() & Qt::LeftButton) {
979 m_trackBalls[0].push(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
980 event->accept();
981 }
982
983 if (event->buttons() & Qt::RightButton) {
984 m_trackBalls[1].push(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
985 event->accept();
986 }
987
988 if (event->buttons() & Qt::MidButton) {
989 m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), QQuaternion());
990 event->accept();
991 }
992}
993
994void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
995{
996 QGraphicsScene::mouseReleaseEvent(event);
997 if (event->isAccepted())
998 return;
999
1000 if (event->button() == Qt::LeftButton) {
1001 m_trackBalls[0].release(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
1002 event->accept();
1003 }
1004
1005 if (event->button() == Qt::RightButton) {
1006 m_trackBalls[1].release(pixelPosToViewPos(event->scenePos()), m_trackBalls[2].rotation().conjugate());
1007 event->accept();
1008 }
1009
1010 if (event->button() == Qt::MidButton) {
1011 m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion());
1012 event->accept();
1013 }
1014}
1015
1016void Scene::wheelEvent(QGraphicsSceneWheelEvent * event)
1017{
1018 QGraphicsScene::wheelEvent(event);
1019 if (!event->isAccepted()) {
1020 m_distExp += event->delta();
1021 if (m_distExp < -8 * 120)
1022 m_distExp = -8 * 120;
1023 if (m_distExp > 10 * 120)
1024 m_distExp = 10 * 120;
1025 event->accept();
1026 }
1027}
1028
1029void Scene::setShader(int index)
1030{
1031 if (index >= 0 && index < m_fragmentShaders.size())
1032 m_currentShader = index;
1033}
1034
1035void Scene::setTexture(int index)
1036{
1037 if (index >= 0 && index < m_textures.size())
1038 m_currentTexture = index;
1039}
1040
1041void Scene::toggleDynamicCubemap(int state)
1042{
1043 if ((m_dynamicCubemap = (state == Qt::Checked)))
1044 m_updateAllCubemaps = true;
1045}
1046
1047void Scene::setColorParameter(const QString &name, QRgb color)
1048{
1049 // set the color in all programs
1050 foreach (QGLShaderProgram *program, m_programs) {
1051 program->bind();
1052 program->setUniformValue(program->uniformLocation(name), QColor(color));
1053 program->release();
1054 }
1055}
1056
1057void Scene::setFloatParameter(const QString &name, float value)
1058{
1059 // set the color in all programs
1060 foreach (QGLShaderProgram *program, m_programs) {
1061 program->bind();
1062 program->setUniformValue(program->uniformLocation(name), value);
1063 program->release();
1064 }
1065}
1066
1067void Scene::newItem(ItemDialog::ItemType type)
1068{
1069 QSize size = sceneRect().size().toSize();
1070 switch (type) {
1071 case ItemDialog::QtBoxItem:
1072 addItem(new QtBox(64, rand() % (size.width() - 64) + 32, rand() % (size.height() - 64) + 32));
1073 break;
1074 case ItemDialog::CircleItem:
1075 addItem(new CircleItem(64, rand() % (size.width() - 64) + 32, rand() % (size.height() - 64) + 32));
1076 break;
1077 case ItemDialog::SquareItem:
1078 addItem(new SquareItem(64, rand() % (size.width() - 64) + 32, rand() % (size.height() - 64) + 32));
1079 break;
1080 default:
1081 break;
1082 }
1083}
Note: See TracBrowser for help on using the repository browser.