summaryrefslogtreecommitdiffstats
path: root/src/imports/threed/skybox.cpp
blob: aa0edbbc03893e2a14d97dfdb0d7749c5b7a453c (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
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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtQuick3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 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 the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "skybox.h"
#include "qglbuilder.h"
#include "qglcube.h"
#include "qglpainter.h"
#include "qglview.h"
#include "viewport.h"

#include <QDir>
#include <QFileInfo>
#include <QEvent>

/*!
    \qmlclass Skybox Skybox
    \brief The Skybox item provides a simple 6-texture environment.
    \since 4.8
    \ingroup qt3d::qml3d

    The six textures of the Skybox completely encloses the camera,
    giving the illusion of a full 3D environment.  Although the illusion
    is far from perfect it is adequate for many simple purposes.  It is
    most suited to sky, clouds, remote landscapes (such as mountains) or
    possibly city skylines - anything where the textures represent an
    environment that is effectively infinitely distant from the camera.

    To use a skybox create a directory with six appropriately named images
    and place this alongside the main qml file, then specify the directory
    name for the \l{source} property:

    \code
    Viewport {

        Skybox {
            source: "skybox"
        }

        Cube { }
    }
    \endcode

    For an illustration of its use see the flickr3d example.
*/
const char * Skybox::EXPECTED_NAMES[] = {
    "_west", "_up", "_east", "_down", "_south", "_north", 0 };

Skybox::Skybox(QObject *parent)
    : m_ready(false)
    , m_scene(0)
    , m_view(0)
    , m_camera(new QGLCamera(this))
{
    m_view = qobject_cast<Viewport*>(parent);

    //Set the eye to to 0,0,0 and the near plane/view size such that
    //a wide variety of aspect ratios will display without undue
    //cropping against the near plane.
    m_camera->setEye(QVector3D());
    m_camera->setNearPlane(0.1f);
    m_camera->setViewSize(QSizeF(0.17f, 0.17f));

    QGLBuilder builder;
    builder.newSection(QGL::Faceted);
    QVector3D blb(-1.0, -1.0, -1.0);
    QVector3D blf(-1.0, -1.0, 1.0);
    QVector3D tlf(-1.0, 1.0, 1.0);
    QVector3D tlb(-1.0, 1.0, -1.0);
    QVector3D brb(1.0, -1.0, -1.0);
    QVector3D brf(1.0, -1.0, 1.0);
    QVector3D trf(1.0, 1.0, 1.0);
    QVector3D trb(1.0, 1.0, -1.0);
    QVector2D bl(0.0f, 0.0f);
    QVector2D br(1.0f, 0.0f);
    QVector2D tr(1.0f, 1.0f);
    QVector2D tl(0.0f, 1.0f);
    {
        QGeometryData q;   // left
        builder.currentNode()->setObjectName("left");
        q.appendVertex(blf, blb, tlb, tlf);
        q.appendTexCoord(bl, br, tr, tl);
        builder.addQuads(q);
        m_faces[0] = builder.currentNode();
        m_faces[0]->setMaterial(new QGLMaterial);
    }
    {
        builder.newNode();   // top
        builder.currentNode()->setObjectName("top");
        QGeometryData q;
        q.appendVertex(trf, tlf, tlb, trb);
        q.appendTexCoord(bl, br, tr, tl);
        builder.addQuads(q);
        m_faces[1] = builder.currentNode();
        m_faces[1]->setMaterial(new QGLMaterial);
    }
    {
        builder.newNode();    // right
        builder.currentNode()->setObjectName("right");
        QGeometryData q;
        q.appendVertex(brb, brf, trf, trb);
        q.appendTexCoord(bl, br, tr, tl);
        builder.addQuads(q);
        m_faces[2] = builder.currentNode();
        m_faces[2]->setMaterial(new QGLMaterial);
    }
    {
        builder.newNode();    // bottom
        builder.currentNode()->setObjectName("bottom");
        QGeometryData q;
        q.appendVertex(brb, blb, blf, brf);
        q.appendTexCoord(bl, br, tr, tl);
        builder.addQuads(q);
        m_faces[3] = builder.currentNode();
        m_faces[3]->setMaterial(new QGLMaterial);
    }
    {
        builder.newNode();    // front
        builder.currentNode()->setObjectName("front");
        QGeometryData q;
        q.appendVertex(brf, blf, tlf, trf);
        q.appendTexCoord(bl, br, tr, tl);
        builder.addQuads(q);
        m_faces[4] = builder.currentNode();
        m_faces[4]->setMaterial(new QGLMaterial);
    }
    {
        builder.newNode();    // back
        builder.currentNode()->setObjectName("back");
        QGeometryData q;
        q.appendVertex(blb, brb, trb, tlb);
        q.appendTexCoord(bl, br, tr, tl);
        builder.addQuads(q);
        m_faces[5] = builder.currentNode();
        m_faces[5]->setMaterial(new QGLMaterial);
    }
    m_scene = builder.finalizedSceneNode();
    m_scene->setObjectName("SkyboxList");
    m_scene->setEffect(QGL::FlatReplaceTexture2D);
    m_scene->setEffectEnabled(true);
    m_scene->setParent(this);
}

/*!
    \internal
    Destroy this skybox returning all resources
*/
Skybox::~Skybox()
{

}
/*!
    \internal

    Implement the event property to catch when the skybox is placed into a
    viewport parent.
*/
bool Skybox::event(QEvent *event)
{
    if (event->type() == QEvent::ParentChange && !m_view)
    {
        setViewport(qobject_cast<Viewport*>(parent()));
    }
    return QObject::event(event);
}

/*!
    \qmlproperty Viewport Mesh::viewport

    This property holds the viewport which this Skybox will be displayed
    inside.  The skybox will completely fill the viewport, but since it is
    drawn first, and with depth buffering off, all other content in the
    given viewport will overlay the skybox making it appear infinitely
    distant.

    Generally this property should not need to be set as the skybox can be
    simply placed inside the viewport element in which it should display.
*/
void Skybox::setViewport(Viewport *view)
{
    if (m_view != view)
    {
        m_view = view;
        emit viewportChanged();
    }
}

void Skybox::scanLocalDir(const QString &imagePath)
{
    m_imagePath = imagePath;
    QStringList notFound;
    const char **exp = EXPECTED_NAMES;
    for ( ; *exp; ++exp)
        notFound << QLatin1String(*exp);
    QFileInfo info(m_imagePath);
    if (info.exists() && info.isDir())
    {
        QDir imDir(imagePath);
        QFileInfoList files = imDir.entryInfoList(QDir::Files);
        QFileInfoList::const_iterator it = files.constBegin();
        for ( ; it != files.constEnd() && notFound.size() > 0; ++it)
        {
            QFileInfo ent = *it;
            QString tok = ent.baseName().toLower();
            for (int ix = 0; EXPECTED_NAMES[ix]; ++ix)
            {
                if (tok.contains(EXPECTED_NAMES[ix]))
                {
                    QUrl url;
                    url.setScheme("file");
                    url.setPath(ent.absoluteFilePath());
                    m_faces[ix]->material()->setTextureUrl(url);
                    m_faces[ix]->material()->texture()->setHorizontalWrap(QGL::ClampToEdge);
                    m_faces[ix]->material()->texture()->setVerticalWrap(QGL::ClampToEdge);
                    notFound.removeOne(QLatin1String(EXPECTED_NAMES[ix]));
                    break;
                }
            }
        }
        if (notFound.size() > 0)
        {
            qWarning() << imagePath << "did not contain a skybox image for" << notFound;
        }
    }
    else
    {
        qWarning() << imagePath << "was not a directory of skybox images";
    }
}

/*!
    \qmlproperty url Skybox::source

    The location where the 6 textures for the skybox are stored.  The textures are
    in any image format that Qt can load, but must have names that contain the following
    strings:
    \list
        \o "_up"
        \o "_down"
        \o "_west"
        \o "_east"
        \o "_north"
        \o "_south"
    \endlist
    These textures are then attached to the appropriate faces of the skybox.
*/
void Skybox::setSource(const QUrl &source)
{
    if (source != m_source)
    {
        m_source = source;
        if (m_source.scheme() == QLatin1String("file"))
        {
            scanLocalDir(m_source.toLocalFile());
        }
        else
        {
            // TODO - handle network case
            qWarning() << "Network support for skybox images not yet implemented";
        }
        emit sourceChanged();
    }
}

void Skybox::draw(QGLPainter *painter) const
{
    if (!m_view)
        return;
    painter->modelViewMatrix().push();
    painter->modelViewMatrix().setToIdentity();

    QGLCamera *cam = m_view->camera();
    m_camera->setCenter(-cam->eye());
    painter->setCamera(m_camera);

    glDisable(GL_DEPTH_TEST);

    m_scene->draw(painter);

    glEnable(GL_DEPTH_TEST);

    painter->setCamera(cam);
    painter->modelViewMatrix().pop();
}

/*!
    \internal
*/
void Skybox::classBegin()
{
}

/*!
    \internal
    Checks that all loading and initialisation has been finished, and emits the  loaded() signal if
    the component is complete.

    \sa loaded()
*/
void Skybox::componentComplete()
{
}