summaryrefslogtreecommitdiffstats
path: root/main.cpp
blob: fd70298ecb9b6c041688e5de5b536477afe4d5cb (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <QtGui>
#include <QtOpenGL>

#include <QFuture>
#include <QFutureWatcher>

#include "model.h"

class GraphicsScene : public QGraphicsScene
{
    Q_OBJECT

public:
    GraphicsScene();

    void drawBackground(QPainter *painter, const QRectF &rect);

public slots:
    void setModel(Model *model);
    void enableAutoRotate(bool enabled);
    void enableWireframe(bool enabled);
    void enableNormals(bool enabled);
    void setLightPosition(int pos);
    void setColor(QRgb color);

private:
    bool m_wireframeEnabled;
    bool m_normalsEnabled;
    bool m_autoRotate;

    float m_angle;
    float m_lightPos;

    QRgb m_color;

    Model *m_model;
};

class Controls : public QGroupBox
{
    Q_OBJECT

public:
    Controls(GraphicsScene *scene);

private slots:
    void loadModel(const QString &model);
    void modelLoaded();
    void setColor(bool showDialog = true);

private:
    GraphicsScene *m_scene;
    QFutureWatcher<Model *> m_modelLoader;
    QComboBox *m_models;
    QRgb m_color;
};

Controls::Controls(GraphicsScene *scene)
    : m_scene(scene)
    , m_models(new QComboBox)
    , m_color(qRgb(180, 100, 255))
{
    QVBoxLayout *layout = new QVBoxLayout(this);

    layout->addWidget(new QLabel("Model:"));

    QDir dir("models");
    dir.setNameFilters(QStringList() << "*.obj");
    m_models->addItems(dir.entryList());
    connect(m_models, SIGNAL(currentIndexChanged(const QString &)), this, SLOT(loadModel(const QString &)));
    connect(&m_modelLoader, SIGNAL(finished()), this, SLOT(modelLoaded()));

    if (m_models->count() > 0)
        loadModel(m_models->currentText());
    layout->addWidget(m_models);

    QCheckBox *autoRotate = new QCheckBox("Auto-rotate");
    autoRotate->setChecked(true);
    connect(autoRotate, SIGNAL(toggled(bool)), m_scene, SLOT(enableAutoRotate(bool)));
    layout->addWidget(autoRotate);

    QCheckBox *wireframe = new QCheckBox("Render as wireframe");
    wireframe->setChecked(true);
    connect(wireframe, SIGNAL(toggled(bool)), m_scene, SLOT(enableWireframe(bool)));
    layout->addWidget(wireframe);

    QCheckBox *normals = new QCheckBox("Display normals vectors");
    wireframe->setChecked(false);
    connect(normals, SIGNAL(toggled(bool)), m_scene, SLOT(enableNormals(bool)));
    layout->addWidget(normals);

    layout->addWidget(new QLabel("Light position:"));
    QSlider *lightPosition = new QSlider(Qt::Horizontal);
    lightPosition->setRange(-1000, 1000);
    connect(lightPosition, SIGNAL(valueChanged(int)), m_scene, SLOT(setLightPosition(int)));
    layout->addWidget(lightPosition);

    m_scene->setLightPosition(lightPosition->value());

    QPushButton *colorButton = new QPushButton("Set model color");
    connect(colorButton, SIGNAL(pressed()), this, SLOT(setColor()));
    layout->addWidget(colorButton);
    setColor(false);
}

Model *loadModel(const QString &filename)
{
    return new Model(QString("models/") + filename);
}

void Controls::loadModel(const QString &filename)
{
    m_modelLoader.setFuture(QtConcurrent::run(::loadModel, filename));
    m_models->setEnabled(false);
    QApplication::setOverrideCursor(Qt::BusyCursor);
}

void Controls::modelLoaded()
{
    m_scene->setModel(m_modelLoader.result());
    m_models->setEnabled(true);
    QApplication::restoreOverrideCursor();
}

void Controls::setColor(bool showDialog)
{
    if (showDialog)
        m_color = QColorDialog::getRgba(m_color);

    m_scene->setColor(m_color);
}

GraphicsScene::GraphicsScene()
    : m_wireframeEnabled(true)
    , m_normalsEnabled(false)
    , m_autoRotate(true)
    , m_angle(0)
    , m_model(0)
{
    Controls *controls = new Controls(this);
    controls->setWindowOpacity(0.8);

    addWidget(controls)->translate(10, 10);
    setSceneRect(QRect(0, 0, 1024, 768));
}

void GraphicsScene::drawBackground(QPainter *painter, const QRectF &)
{
    painter->save();

    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    bool useMultisample = static_cast<QGLWidget *>(painter->device())->format().sampleBuffers();

    if (m_model) {
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        gluPerspective(70, painter->device()->width() / float(painter->device()->height()), 0.01, 1000);

        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();

        float pos[] = { m_lightPos, 5, 2, 0 };
        glLightfv(GL_LIGHT0, GL_POSITION, pos);
        glColor4f(qRed(m_color)/255.0f, qGreen(m_color)/255.0f, qBlue(m_color)/255.0f, 1.0f);

        if (useMultisample)
            glEnable(GL_MULTISAMPLE);

        glTranslatef(0, 0, -1.5);
        glRotatef(m_angle, 0.5, 1, 0.1);

        if (m_autoRotate)
            m_angle += 0.4;

        m_model->render(m_wireframeEnabled, m_normalsEnabled);

        if (useMultisample)
            glDisable(GL_MULTISAMPLE);

        glPopMatrix();

        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
    }

    painter->restore();

    QTimer::singleShot(20, this, SLOT(update()));
}

void GraphicsScene::setModel(Model *model)
{
    delete m_model;
    m_model = model;
    update();
}

void GraphicsScene::enableAutoRotate(bool enabled)
{
    m_autoRotate = enabled;
    update();
}

void GraphicsScene::enableWireframe(bool enabled)
{
    m_wireframeEnabled = enabled;
}

void GraphicsScene::enableNormals(bool enabled)
{
    m_normalsEnabled = enabled;
}

void GraphicsScene::setLightPosition(int pos)
{
    m_lightPos = pos * 0.005;
    update();
}

void GraphicsScene::setColor(QRgb color)
{
    m_color = color;
    update();
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QGraphicsView view;
    view.setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));

    view.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    view.setTransformationAnchor(QGraphicsView::NoAnchor);
    view.setViewportUpdateMode(QGraphicsView::FullViewportUpdate);

    view.setScene(new GraphicsScene);
    view.show();

    return app.exec();
}

#include "main.moc"