qtmobility/examples/sensors/cubehouse/painter_fixed.cpp
changeset 4 90517678cc4f
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
       
     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:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "painter.h"
       
    43 #include "light.h"
       
    44 #include "lightmodel.h"
       
    45 #include "material.h"
       
    46 
       
    47 FixedFunctionPainter::FixedFunctionPainter()
       
    48 {
       
    49     glEnable(GL_LIGHTING);
       
    50     glEnable(GL_LIGHT0);
       
    51     glDisable(GL_CULL_FACE);
       
    52 }
       
    53 
       
    54 static void setMatrix(GLenum type, const QMatrix4x4 &matrix)
       
    55 {
       
    56     glMatrixMode(type);
       
    57     if (sizeof(qreal) == sizeof(GLfloat)) {
       
    58         glLoadMatrixf(reinterpret_cast<const GLfloat *>(matrix.constData()));
       
    59     } else {
       
    60         GLfloat mat[16];
       
    61         const qreal *m = matrix.constData();
       
    62         for (int index = 0; index < 16; ++index)
       
    63             mat[index] = m[index];
       
    64         glLoadMatrixf(mat);
       
    65     }
       
    66 }
       
    67 
       
    68 void FixedFunctionPainter::setMatrices
       
    69     (const QMatrix4x4 &mv, const QMatrix4x4 &proj)
       
    70 {
       
    71     setMatrix(GL_PROJECTION, proj);
       
    72     setMatrix(GL_MODELVIEW, mv);
       
    73 }
       
    74 
       
    75 static void setLight(int light, const Light *parameters,
       
    76                      const QMatrix4x4& transform = QMatrix4x4())
       
    77 {
       
    78     GLfloat params[4];
       
    79 
       
    80     glMatrixMode(GL_MODELVIEW);
       
    81     glPushMatrix();
       
    82     glLoadIdentity();
       
    83 
       
    84     QColor color = parameters->ambientColor();
       
    85     params[0] = color.redF();
       
    86     params[1] = color.greenF();
       
    87     params[2] = color.blueF();
       
    88     params[3] = color.alphaF();
       
    89     glLightfv(light, GL_AMBIENT, params);
       
    90 
       
    91     color = parameters->diffuseColor();
       
    92     params[0] = color.redF();
       
    93     params[1] = color.greenF();
       
    94     params[2] = color.blueF();
       
    95     params[3] = color.alphaF();
       
    96     glLightfv(light, GL_DIFFUSE, params);
       
    97 
       
    98     color = parameters->specularColor();
       
    99     params[0] = color.redF();
       
   100     params[1] = color.greenF();
       
   101     params[2] = color.blueF();
       
   102     params[3] = color.alphaF();
       
   103     glLightfv(light, GL_SPECULAR, params);
       
   104 
       
   105     QVector4D vector = parameters->eyePosition(transform);
       
   106     params[0] = vector.x();
       
   107     params[1] = vector.y();
       
   108     params[2] = vector.z();
       
   109     params[3] = vector.w();
       
   110     glLightfv(light, GL_POSITION, params);
       
   111 
       
   112     QVector3D spotDirection = parameters->eyeSpotDirection(transform);
       
   113     params[0] = spotDirection.x();
       
   114     params[1] = spotDirection.y();
       
   115     params[2] = spotDirection.z();
       
   116     glLightfv(light, GL_SPOT_DIRECTION, params);
       
   117 
       
   118     params[0] = parameters->spotExponent();
       
   119     glLightfv(light, GL_SPOT_EXPONENT, params);
       
   120 
       
   121     params[0] = parameters->spotAngle();
       
   122     glLightfv(light, GL_SPOT_CUTOFF, params);
       
   123 
       
   124     params[0] = parameters->constantAttenuation();
       
   125     glLightfv(light, GL_CONSTANT_ATTENUATION, params);
       
   126 
       
   127     params[0] = parameters->linearAttenuation();
       
   128     glLightfv(light, GL_LINEAR_ATTENUATION, params);
       
   129 
       
   130     params[0] = parameters->quadraticAttenuation();
       
   131     glLightfv(light, GL_QUADRATIC_ATTENUATION, params);
       
   132 
       
   133     glPopMatrix();
       
   134 }
       
   135 
       
   136 static void setLightModel(const LightModel *lightModel)
       
   137 {
       
   138     GLfloat values[4];
       
   139 #ifdef GL_LIGHT_MODEL_TWO_SIDE
       
   140     if (lightModel->model() == LightModel::TwoSided)
       
   141         values[0] = 1.0f;
       
   142     else
       
   143         values[0] = 0.0f;
       
   144     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, values);
       
   145 #endif
       
   146 #ifdef GL_LIGHT_MODEL_COLOR_CONTROL
       
   147     if (lightModel->colorControl() == LightModel::SeparateSpecularColor)
       
   148         values[0] = GL_SEPARATE_SPECULAR_COLOR;
       
   149     else
       
   150         values[0] = GL_SINGLE_COLOR;
       
   151     glLightModelfv(GL_LIGHT_MODEL_COLOR_CONTROL, values);
       
   152 #endif
       
   153 #ifdef GL_LIGHT_MODEL_LOCAL_VIEWER
       
   154     if (lightModel->viewerPosition() == LightModel::LocalViewer)
       
   155         values[0] = 1.0f;
       
   156     else
       
   157         values[0] = 0.0f;
       
   158     glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, values);
       
   159 #endif
       
   160 #ifdef GL_LIGHT_MODEL_AMBIENT
       
   161     QColor color = lightModel->ambientSceneColor();
       
   162     values[0] = color.redF();
       
   163     values[1] = color.blueF();
       
   164     values[2] = color.greenF();
       
   165     values[3] = color.alphaF();
       
   166     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, values);
       
   167 #endif
       
   168 }
       
   169 
       
   170 static void setMaterial(int face, const Material *parameters)
       
   171 {
       
   172     GLfloat params[17];
       
   173 
       
   174     QColor mcolor = parameters->ambientColor();
       
   175     params[0] = mcolor.redF();
       
   176     params[1] = mcolor.greenF();
       
   177     params[2] = mcolor.blueF();
       
   178     params[3] = mcolor.alphaF();
       
   179 
       
   180     mcolor = parameters->diffuseColor();
       
   181     params[4] = mcolor.redF();
       
   182     params[5] = mcolor.greenF();
       
   183     params[6] = mcolor.blueF();
       
   184     params[7] = mcolor.alphaF();
       
   185 
       
   186     mcolor = parameters->specularColor();
       
   187     params[8] = mcolor.redF();
       
   188     params[9] = mcolor.greenF();
       
   189     params[10] = mcolor.blueF();
       
   190     params[11] = mcolor.alphaF();
       
   191 
       
   192     mcolor = parameters->emittedLight();
       
   193     params[12] = mcolor.redF();
       
   194     params[13] = mcolor.greenF();
       
   195     params[14] = mcolor.blueF();
       
   196     params[15] = mcolor.alphaF();
       
   197 
       
   198     params[16] = parameters->shininess();
       
   199 
       
   200     glMaterialfv(face, GL_AMBIENT, params);
       
   201     glMaterialfv(face, GL_DIFFUSE, params + 4);
       
   202     glMaterialfv(face, GL_SPECULAR, params + 8);
       
   203     glMaterialfv(face, GL_EMISSION, params + 12);
       
   204     glMaterialfv(face, GL_SHININESS, params + 16);
       
   205 }
       
   206 
       
   207 void FixedFunctionPainter::selectMaterial(Material *material)
       
   208 {
       
   209     ::setLight(GL_LIGHT0, light);
       
   210     ::setLightModel(lightModel);
       
   211     ::setMaterial(GL_FRONT_AND_BACK, material);
       
   212     glDisable(GL_TEXTURE_2D);
       
   213     glEnableClientState(GL_VERTEX_ARRAY);
       
   214     glEnableClientState(GL_NORMAL_ARRAY);
       
   215     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
       
   216 }
       
   217 
       
   218 void FixedFunctionPainter::selectTexturedMaterial(Material *material)
       
   219 {
       
   220     ::setLight(GL_LIGHT0, light);
       
   221     ::setLightModel(lightModel);
       
   222     ::setMaterial(GL_FRONT_AND_BACK, material);
       
   223     glEnable(GL_TEXTURE_2D);
       
   224     glEnableClientState(GL_VERTEX_ARRAY);
       
   225     glEnableClientState(GL_NORMAL_ARRAY);
       
   226     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
       
   227     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
       
   228 }
       
   229 
       
   230 void FixedFunctionPainter::setVertices(const float *array, int stride)
       
   231 {
       
   232     glVertexPointer(3, GL_FLOAT, stride * sizeof(float), array);
       
   233 }
       
   234 
       
   235 void FixedFunctionPainter::setTexCoords(const float *array, int stride)
       
   236 {
       
   237     glTexCoordPointer(2, GL_FLOAT, stride * sizeof(float), array);
       
   238 }
       
   239 
       
   240 void FixedFunctionPainter::setNormals(const float *array, int stride)
       
   241 {
       
   242     glNormalPointer(GL_FLOAT, stride * sizeof(float), array);
       
   243 }