summaryrefslogtreecommitdiffstats
path: root/modelitem.cpp
blob: 90e37141cd4bfa9a2adbee496c19f17d3c279568 (plain)
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
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "modelitem.h"
#include "model.h"

#include <QtGui>
#include <QtOpenGL>

#include "mazescene.h"

#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif

void ModelItem::updateTransform(const Camera &camera, qreal time)
{
    ProjectedItem::updateTransform(camera, time);

    setTransform(QTransform());
    m_matrix = Matrix4x4::fromTranslation(3, 0, 7) * camera.matrix(time);
}

QRectF ModelItem::boundingRect() const
{
    if (!scene()->views().isEmpty()) {
        QGraphicsView *view = scene()->views().at(0);
        return view->mapToScene(view->rect()).boundingRect();
    }

    return scene()->sceneRect();
}

void ModelItem::updateItem()
{
    ProjectedItem::update();
}

void ModelItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (!m_model)
        return;

    Matrix4x4 projectionMatrix = Matrix4x4::fromProjection(70);
    projectionMatrix *= Matrix4x4::fromQTransform(painter->transform());

    Matrix4x4 modelMatrix;
    const int delta = m_time.elapsed() - m_lastTime;
    m_rotation += m_angularMomentum * (delta / 1000.0);
    m_lastTime += delta;

    Point3d size = m_model->size();
    float extent = qSqrt(2.0);
    float scale = 1 / qMax(size.y, qMax(size.x / extent, size.z / extent));
    modelMatrix *= Matrix4x4::fromScale(scale, -scale, scale);

    modelMatrix *= Matrix4x4::fromRotation(m_rotation.z, Qt::ZAxis);
    modelMatrix *= Matrix4x4::fromRotation(m_rotation.y, Qt::YAxis);
    modelMatrix *= Matrix4x4::fromRotation(m_rotation.x, Qt::XAxis);

    QTimer::singleShot(10, this, SLOT(updateItem()));

    if (painter->paintEngine()->type() != QPaintEngine::OpenGL) {
        m_wireframe->setEnabled(false);
        m_wireframe->setChecked(false);
        m_wireframeEnabled = false;
        painter->setTransform(QTransform());
        painter->setPen(m_modelColor);
        m_model->render(painter, modelMatrix * m_matrix * projectionMatrix, m_normalsEnabled);
        return;
    }

    m_wireframe->setEnabled(true);

    glClearDepth(0);
    glClear(GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    glOrtho(0, painter->device()->width(), painter->device()->height(), 0, -1, 1);
    glMultMatrixf(projectionMatrix.data());

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadMatrixf(m_matrix.data());

    const float ambient[] = { 0.1, 0.1, 0.1, 1 };
    const float pos[] = { 50, -500, 200 };
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
    glLightfv(GL_LIGHT0, GL_POSITION, pos);

    glMultMatrixf(modelMatrix.data());

    glColor4f(m_modelColor.redF(), m_modelColor.greenF(), m_modelColor.blueF(), 1.0f);

    glEnable(GL_MULTISAMPLE);
    m_model->render(m_wireframeEnabled, m_normalsEnabled);
    glDisable(GL_MULTISAMPLE);

    glPopMatrix();

    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
}


ModelItem::ModelItem()
    : ProjectedItem(QRectF(), false)
    , m_wireframeEnabled(false)
    , m_normalsEnabled(false)
    , m_modelColor(153, 255, 0)
    , m_model(0)
    , m_lastTime(0)
    , m_distance(1.4f)
    , m_angularMomentum(0, 40, 0)
{
    QPointF pos(3, 7);
    setPosition(pos, pos);
    setLayout(new QVBoxLayout);

    m_modelButton = new QPushButton(tr("Load model"));
    connect(m_modelButton, SIGNAL(clicked()), this, SLOT(loadModel()));
#ifndef QT_NO_CONCURRENT
    connect(&m_modelLoader, SIGNAL(finished()), this, SLOT(modelLoaded()));
#endif
    layout()->addWidget(m_modelButton);

    m_wireframe = new QCheckBox(tr("Render as wireframe"));
    connect(m_wireframe, SIGNAL(toggled(bool)), this, SLOT(enableWireframe(bool)));
    layout()->addWidget(m_wireframe);

    QCheckBox *normals = new QCheckBox(tr("Display normals vectors"));
    connect(normals, SIGNAL(toggled(bool)), this, SLOT(enableNormals(bool)));
    layout()->addWidget(normals);

    QPushButton *colorButton = new QPushButton(tr("Choose model color"));
    connect(colorButton, SIGNAL(clicked()), this, SLOT(setModelColor()));
    layout()->addWidget(colorButton);

    loadModel(QLatin1String("qt.obj"));
    m_time.start();
}

static Model *loadModel(const QString &filePath)
{
    return new Model(filePath);
}

void ModelItem::loadModel()
{
    loadModel(QFileDialog::getOpenFileName(0, tr("Choose model"), QString(), QLatin1String("*.obj")));
}

void ModelItem::loadModel(const QString &filePath)
{
    if (filePath.isEmpty())
        return;

    m_modelButton->setEnabled(false);
    QApplication::setOverrideCursor(Qt::BusyCursor);
#ifndef QT_NO_CONCURRENT
    m_modelLoader.setFuture(QtConcurrent::run(::loadModel, filePath));
#else
    setModel(::loadModel(filePath));
    modelLoaded();
#endif
}

void ModelItem::modelLoaded()
{
#ifndef QT_NO_CONCURRENT
    setModel(m_modelLoader.result());
#endif
    m_modelButton->setEnabled(true);
    QApplication::restoreOverrideCursor();
}

void ModelItem::setModel(Model *model)
{
    delete m_model;
    m_model = model;

    ProjectedItem::update();
}

void ModelItem::enableWireframe(bool enabled)
{
    m_wireframeEnabled = enabled;
    ProjectedItem::update();
}

void ModelItem::enableNormals(bool enabled)
{
    m_normalsEnabled = enabled;
    ProjectedItem::update();
}

void ModelItem::setModelColor()
{
    const QColor color = QColorDialog::getColor(m_modelColor);
    if (color.isValid()) {
        m_modelColor = color;
        ProjectedItem::update();
    }
}