summaryrefslogtreecommitdiffstats
path: root/src/threed/materials/qgltwosidedmaterial.cpp
blob: 348a208380f1b4c8326f0b78e8b679ffce344dae (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
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: http://www.qt-project.org/
**
** This file is part of the Qt3D 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 "qgltwosidedmaterial.h"
#include "qglpainter.h"
#include "qgllightmodel.h"

QT_BEGIN_NAMESPACE

/*!
    \class QGLTwoSidedMaterial
    \since 4.8
    \brief The QGLTwoSidedMaterial class implemented two-sided materials for 3D rendering.
    \ingroup qt3d
    \ingroup qt3d::materials

    Two-sided materials consist of a front() material and a back()
    material.  The specific material rendered is determined by the
    direction faced by a fragment when it is rendered.  Fragments
    that point towards the viewer are rendered with front(),
    and fragments that point away from the viewer are rendered
    with back().  In both cases, any textures that are used to
    render the material are taken from front().

    If front() and back() are the same, then the same material
    will be used on both sides of the fragment.  However, this
    is not exactly the same as using a one-sided QGLMaterial in
    place of the two-sided material.  One-sided materials will
    render the back of the fragment as black because the normal
    is always pointing away from the viewer.  Two-sided materials
    reverse the back-facing normal so that back() is lit as
    though it was on a front-facing face.

    \sa QGLMaterial
*/

class QGLTwoSidedMaterialPrivate
{
public:
    QGLTwoSidedMaterialPrivate() : front(0), back(0), defaultMaterial(0) {}

    QGLMaterial *front;
    QGLMaterial *back;
    QGLMaterial *defaultMaterial;
};

/*!
    Constructs a two-sided material object and attaches it to \a parent.

    \sa setFront(), setBack()
*/
QGLTwoSidedMaterial::QGLTwoSidedMaterial(QObject *parent)
    : QGLAbstractMaterial(parent)
    , d_ptr(new QGLTwoSidedMaterialPrivate)
{
}

/*!
    Destroys this two-sided material object.
*/
QGLTwoSidedMaterial::~QGLTwoSidedMaterial()
{
}

/*!
    \property QGLTwoSidedMaterial::front
    \brief the material for the front side of the object's fragments.

    The default value is null, which will result in a default QGLMaterial
    object being used when bind() is called.

    \sa back(), frontChanged(), materialChanged()
*/

QGLMaterial *QGLTwoSidedMaterial::front() const
{
    Q_D(const QGLTwoSidedMaterial);
    return d->front;
}

void QGLTwoSidedMaterial::setFront(QGLMaterial *material)
{
    Q_D(QGLTwoSidedMaterial);
    if (d->front != material) {
        if (d->front && d->front != d->back) {
            disconnect(d->front, SIGNAL(materialChanged()),
                       this, SIGNAL(materialChanged()));
        }
        d->front = material;
        if (d->front && d->front != d->back) {
            connect(d->front, SIGNAL(materialChanged()),
                    this, SIGNAL(materialChanged()));
        }
        emit frontChanged();
        emit materialChanged();
    }
}

/*!
    \property QGLTwoSidedMaterial::back
    \brief the material for the back side of the object's fragments.

    The default value is null, which indicates that front() should
    be used on both the front and back sides of the object's fragments.

    Textures are taken from the front() material.  Any textures that
    appear in the back() material are ignored.  Only the material
    lighting parameters from back() will be used.

    \sa front(), backChanged(), materialChanged()
*/

QGLMaterial *QGLTwoSidedMaterial::back() const
{
    Q_D(const QGLTwoSidedMaterial);
    return d->back;
}

void QGLTwoSidedMaterial::setBack(QGLMaterial *material)
{
    Q_D(QGLTwoSidedMaterial);
    if (d->back != material) {
        if (d->back && d->back != d->front) {
            disconnect(d->back, SIGNAL(materialChanged()),
                       this, SIGNAL(materialChanged()));
        }
        d->back = material;
        if (d->back && d->back != d->front) {
            connect(d->back, SIGNAL(materialChanged()),
                    this, SIGNAL(materialChanged()));
        }
        emit backChanged();
        emit materialChanged();
    }
}

/*!
    \reimp
*/
bool QGLTwoSidedMaterial::isTransparent() const
{
    Q_D(const QGLTwoSidedMaterial);
    if (d->front && d->front->isTransparent())
        return true;
    return d->back && d->back->isTransparent();
}

/*!
    \reimp
*/
void QGLTwoSidedMaterial::bind(QGLPainter *painter)
{
    Q_D(QGLTwoSidedMaterial);
    QGLMaterial *front = d->front;
    if (!front) {
        // We need to have something for the front material.
        front = d->defaultMaterial;
        if (!front) {
            d->defaultMaterial = new QGLMaterial(this);
            front = d->defaultMaterial;
        }
    }
    const_cast<QGLLightModel *>(painter->lightModel())
        ->setModel(QGLLightModel::TwoSided); // FIXME
    if (d->back && d->back != front) {
        painter->setFaceMaterial(QGL::FrontFaces, front);
        painter->setFaceMaterial(QGL::BackFaces, d->back);
    } else {
        painter->setFaceMaterial(QGL::AllFaces, front);
    }
    front->bindTextures(painter);
}

/*!
    \reimp
*/
void QGLTwoSidedMaterial::release(QGLPainter *painter, QGLAbstractMaterial *next)
{
    Q_D(const QGLTwoSidedMaterial);
    if (d->front)
        d->front->release(painter, next);
    else if (d->defaultMaterial)
        d->defaultMaterial->release(painter, next);
}

/*!
    \reimp
*/
void QGLTwoSidedMaterial::prepareToDraw
    (QGLPainter *painter, const QGLAttributeSet &attributes)
{
    Q_D(QGLTwoSidedMaterial);
    QGLMaterial *front = d->front;
    if (!front)
        front = d->defaultMaterial;
    front->bindEffect(painter, attributes, true);
}

/*!
    \fn void QGLTwoSidedMaterial::frontChanged()

    Signal that is emitted when the front() material pointer changes.

    This signal is not emitted when a property on front() changes.
    Use materialChanged() for that purpose instead.

    \sa front(), backChanged()
*/

/*!
    \fn void QGLTwoSidedMaterial::backChanged()

    Signal that is emitted when the back() material pointer changes.

    This signal is not emitted when a property on back() changes.
    Use materialChanged() for that purpose instead.

    \sa back(), frontChanged()
*/

QT_END_NAMESPACE