examples/sensors/cubehouse/material.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 Qt3D module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "material.h"
       
    42 
       
    43 /*!
       
    44     \class Material
       
    45     \brief The Material class describes material properties for OpenGL drawing.
       
    46     \since 4.7
       
    47     \ingroup qt3d
       
    48     \ingroup qt3d::painting
       
    49 */
       
    50 
       
    51 class MaterialPrivate
       
    52 {
       
    53 public:
       
    54     explicit MaterialPrivate();
       
    55 
       
    56     QColor basicColor;
       
    57     QColor ambientColor;
       
    58     QColor diffuseColor;
       
    59     QColor specularColor;
       
    60     QColor emittedLight;
       
    61     qreal shininess;
       
    62 };
       
    63 
       
    64 MaterialPrivate::MaterialPrivate()
       
    65     : basicColor(255, 255, 255, 255),
       
    66       specularColor(0, 0, 0, 255),
       
    67       emittedLight(0, 0, 0, 255),
       
    68       shininess(0)
       
    69 {
       
    70     ambientColor.setRgbF(0.2f, 0.2f, 0.2f, 1.0f);
       
    71     diffuseColor.setRgbF(0.8f, 0.8f, 0.8f, 1.0f);
       
    72 }
       
    73 
       
    74 /*!
       
    75     Constructs a Material object with its default values,
       
    76     and attaches it to \a parent.
       
    77 */
       
    78 Material::Material(QObject *parent)
       
    79     : QObject(parent), d_ptr(new MaterialPrivate)
       
    80 {
       
    81 }
       
    82 
       
    83 /*!
       
    84     Destroys this Material object.
       
    85 */
       
    86 Material::~Material()
       
    87 {
       
    88 }
       
    89 
       
    90 /*!
       
    91     \property Material::basicColor
       
    92     \brief the basic color of the material.  The default value
       
    93     is (1.0, 1.0, 1.0, 1.0).
       
    94 
       
    95     The basic color is used by effects that don't implement
       
    96     material-based lighting.  It is ignored by material-based
       
    97     lighting effects.
       
    98 
       
    99     \sa ambientColor(), diffuseColor(), basicColorChanged(), setColor()
       
   100 */
       
   101 QColor Material::basicColor() const
       
   102 {
       
   103     Q_D(const Material);
       
   104     return d->basicColor;
       
   105 }
       
   106 
       
   107 void Material::setBasicColor(const QColor& value)
       
   108 {
       
   109     Q_D(Material);
       
   110     if (d->basicColor != value) {
       
   111         d->basicColor = value;
       
   112         emit basicColorChanged();
       
   113         emit materialChanged();
       
   114     }
       
   115 }
       
   116 
       
   117 /*!
       
   118     \property Material::ambientColor
       
   119     \brief the ambient color of the material.  The default value
       
   120     is (0.2, 0.2, 0.2, 1.0).
       
   121 
       
   122     \sa diffuseColor(), specularColor(), ambientColorChanged()
       
   123 */
       
   124 QColor Material::ambientColor() const
       
   125 {
       
   126     Q_D(const Material);
       
   127     return d->ambientColor;
       
   128 }
       
   129 
       
   130 void Material::setAmbientColor(const QColor& value)
       
   131 {
       
   132     Q_D(Material);
       
   133     if (d->ambientColor != value) {
       
   134         d->ambientColor = value;
       
   135         emit ambientColorChanged();
       
   136         emit materialChanged();
       
   137     }
       
   138 }
       
   139 
       
   140 /*!
       
   141     \property Material::diffuseColor
       
   142     \brief the diffuse color of the material.  The default value
       
   143     is (0.8, 0.8, 0.8, 1.0).
       
   144 
       
   145     \sa ambientColor(), specularColor(), diffuseColorChanged()
       
   146 */
       
   147 QColor Material::diffuseColor() const
       
   148 {
       
   149     Q_D(const Material);
       
   150     return d->diffuseColor;
       
   151 }
       
   152 
       
   153 void Material::setDiffuseColor(const QColor& value)
       
   154 {
       
   155     Q_D(Material);
       
   156     if (d->diffuseColor != value) {
       
   157         d->diffuseColor = value;
       
   158         emit diffuseColorChanged();
       
   159         emit materialChanged();
       
   160     }
       
   161 }
       
   162 
       
   163 /*!
       
   164     \property Material::specularColor
       
   165     \brief the specular color of the material.  The default value
       
   166     is (0, 0, 0, 1).
       
   167 
       
   168     \sa ambientColor(), diffuseColor(), specularColorChanged()
       
   169 */
       
   170 QColor Material::specularColor() const
       
   171 {
       
   172     Q_D(const Material);
       
   173     return d->specularColor;
       
   174 }
       
   175 
       
   176 void Material::setSpecularColor(const QColor& value)
       
   177 {
       
   178     Q_D(Material);
       
   179     if (d->specularColor != value) {
       
   180         d->specularColor = value;
       
   181         emit specularColorChanged();
       
   182         emit materialChanged();
       
   183     }
       
   184 }
       
   185 
       
   186 /*!
       
   187     \property Material::emittedLight
       
   188     \brief the emitted light intensity of the material.
       
   189     The default value is (0.0, 0.0, 0.0, 1.0), which indicates
       
   190     that the material does not emit any light.
       
   191 
       
   192     \sa emittedLightChanged()
       
   193 */
       
   194 QColor Material::emittedLight() const
       
   195 {
       
   196     Q_D(const Material);
       
   197     return d->emittedLight;
       
   198 }
       
   199 
       
   200 void Material::setEmittedLight(const QColor& value)
       
   201 {
       
   202     Q_D(Material);
       
   203     if (d->emittedLight != value) {
       
   204         d->emittedLight = value;
       
   205         emit emittedLightChanged();
       
   206         emit materialChanged();
       
   207     }
       
   208 }
       
   209 
       
   210 /*!
       
   211     \property Material::color
       
   212     \brief the overall color of the material.  The default value
       
   213     is (1.0, 1.0, 1.0, 1.0).
       
   214 
       
   215     This is a convenience property.  When read it returns basicColor().
       
   216     When written, it sets basicColor() to the value, ambientColor()
       
   217     to 20% of the value, and diffuseColor() to 80% of the value.
       
   218     The result is that regardless of whether lighting is used or not,
       
   219     the material will appear to have a similar color.
       
   220 
       
   221     \sa basicColor(), ambientColor(), diffuseColor()
       
   222 */
       
   223 QColor Material::color() const
       
   224 {
       
   225     Q_D(const Material);
       
   226     return d->basicColor;
       
   227 }
       
   228 
       
   229 void Material::setColor(const QColor& value)
       
   230 {
       
   231     Q_D(Material);
       
   232     d->basicColor = value;
       
   233     d->ambientColor.setRgbF
       
   234         (value.redF() * 0.2f, value.greenF() * 0.2f,
       
   235          value.blueF() * 0.2f, value.alphaF());
       
   236     d->diffuseColor.setRgbF
       
   237         (value.redF() * 0.8f, value.greenF() * 0.8f,
       
   238          value.blueF() * 0.8f, value.alphaF());
       
   239     emit basicColorChanged();
       
   240     emit ambientColorChanged();
       
   241     emit diffuseColorChanged();
       
   242     emit materialChanged();
       
   243 }
       
   244 
       
   245 /*!
       
   246     \property Material::shininess
       
   247     \brief the specular exponent of the material, or how shiny it is.
       
   248     Must be between 0 and 128.  The default value is 0.  A value outside
       
   249     this range will be clamped to the range when the property is set.
       
   250 
       
   251     \sa shininessChanged()
       
   252 */
       
   253 qreal Material::shininess() const
       
   254 {
       
   255     Q_D(const Material);
       
   256     return d->shininess;
       
   257 }
       
   258 
       
   259 void Material::setShininess(qreal value)
       
   260 {
       
   261     Q_D(Material);
       
   262     if (value < 0)
       
   263         value = 0;
       
   264     else if (value > 128)
       
   265         value = 128;
       
   266     if (d->shininess != value) {
       
   267         d->shininess = value;
       
   268         emit shininessChanged();
       
   269         emit materialChanged();
       
   270     }
       
   271 }
       
   272 
       
   273 /*!
       
   274     \fn void Material::basicColorChanged()
       
   275 
       
   276     This signal is emitted when basicColor() changes.
       
   277 
       
   278     \sa basicColor(), setBasicColor(), materialChanged()
       
   279 */
       
   280 
       
   281 /*!
       
   282     \fn void Material::ambientColorChanged()
       
   283 
       
   284     This signal is emitted when ambientColor() changes.
       
   285 
       
   286     \sa ambientColor(), setAmbientColor(), materialChanged()
       
   287 */
       
   288 
       
   289 /*!
       
   290     \fn void Material::diffuseColorChanged()
       
   291 
       
   292     This signal is emitted when diffuseColor() changes.
       
   293 
       
   294     \sa diffuseColor(), setDiffuseColor(), materialChanged()
       
   295 */
       
   296 
       
   297 /*!
       
   298     \fn void Material::specularColorChanged()
       
   299 
       
   300     This signal is emitted when specularColor() changes.
       
   301 
       
   302     \sa specularColor(), setSpecularColor(), materialChanged()
       
   303 */
       
   304 
       
   305 /*!
       
   306     \fn void Material::emittedLightChanged()
       
   307 
       
   308     This signal is emitted when emittedLight() changes.
       
   309 
       
   310     \sa emittedLight(), setEmittedLight(), materialChanged()
       
   311 */
       
   312 
       
   313 /*!
       
   314     \fn void Material::shininessChanged()
       
   315 
       
   316     This signal is emitted when shininess() changes.
       
   317 
       
   318     \sa shininess(), setShininess(), materialChanged()
       
   319 */
       
   320 
       
   321 /*!
       
   322     \fn void Material::materialChanged()
       
   323 
       
   324     This signal is emitted when one of basicColor(), ambientColor(),
       
   325     diffuseColor(), specularColor(), emittedLight(), shiniess(),
       
   326     or texture() changes.
       
   327 
       
   328     \sa basicColorChanged(), ambientColorChanged(), diffuseColorChanged()
       
   329     \sa specularColorChanged(), emittedLightChanged(), shininessChanged()
       
   330     \sa texturesChanged()
       
   331 */