examples/opengl/hellogl/glwidget.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 examples 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 <QtGui>
       
    43 #include <QtOpenGL>
       
    44 
       
    45 #include <math.h>
       
    46 
       
    47 #include "glwidget.h"
       
    48 #include "qtlogo.h"
       
    49 
       
    50 #ifndef GL_MULTISAMPLE
       
    51 #define GL_MULTISAMPLE  0x809D
       
    52 #endif
       
    53 
       
    54 //! [0]
       
    55 GLWidget::GLWidget(QWidget *parent)
       
    56     : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
       
    57 {
       
    58     logo = 0;
       
    59     xRot = 0;
       
    60     yRot = 0;
       
    61     zRot = 0;
       
    62 
       
    63     qtGreen = QColor::fromCmykF(0.40, 0.0, 1.0, 0.0);
       
    64     qtPurple = QColor::fromCmykF(0.39, 0.39, 0.0, 0.0);
       
    65 }
       
    66 //! [0]
       
    67 
       
    68 //! [1]
       
    69 GLWidget::~GLWidget()
       
    70 {
       
    71 }
       
    72 //! [1]
       
    73 
       
    74 //! [2]
       
    75 QSize GLWidget::minimumSizeHint() const
       
    76 {
       
    77     return QSize(50, 50);
       
    78 }
       
    79 //! [2]
       
    80 
       
    81 //! [3]
       
    82 QSize GLWidget::sizeHint() const
       
    83 //! [3] //! [4]
       
    84 {
       
    85     return QSize(400, 400);
       
    86 }
       
    87 //! [4]
       
    88 
       
    89 static void qNormalizeAngle(int &angle)
       
    90 {
       
    91     while (angle < 0)
       
    92         angle += 360 * 16;
       
    93     while (angle > 360 * 16)
       
    94         angle -= 360 * 16;
       
    95 }
       
    96 
       
    97 //! [5]
       
    98 void GLWidget::setXRotation(int angle)
       
    99 {
       
   100     qNormalizeAngle(angle);
       
   101     if (angle != xRot) {
       
   102         xRot = angle;
       
   103         emit xRotationChanged(angle);
       
   104         updateGL();
       
   105     }
       
   106 }
       
   107 //! [5]
       
   108 
       
   109 void GLWidget::setYRotation(int angle)
       
   110 {
       
   111     qNormalizeAngle(angle);
       
   112     if (angle != yRot) {
       
   113         yRot = angle;
       
   114         emit yRotationChanged(angle);
       
   115         updateGL();
       
   116     }
       
   117 }
       
   118 
       
   119 void GLWidget::setZRotation(int angle)
       
   120 {
       
   121     qNormalizeAngle(angle);
       
   122     if (angle != zRot) {
       
   123         zRot = angle;
       
   124         emit zRotationChanged(angle);
       
   125         updateGL();
       
   126     }
       
   127 }
       
   128 
       
   129 //! [6]
       
   130 void GLWidget::initializeGL()
       
   131 {
       
   132     qglClearColor(qtPurple.dark());
       
   133 
       
   134     logo = new QtLogo(this, 64);
       
   135     logo->setColor(qtGreen.dark());
       
   136 
       
   137     glEnable(GL_DEPTH_TEST);
       
   138     glEnable(GL_CULL_FACE);
       
   139     glShadeModel(GL_SMOOTH);
       
   140     glEnable(GL_LIGHTING);
       
   141     glEnable(GL_LIGHT0);
       
   142     glEnable(GL_MULTISAMPLE);
       
   143     static GLfloat lightPosition[4] = { 0.5, 5.0, 7.0, 1.0 };
       
   144     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
       
   145 }
       
   146 //! [6]
       
   147 
       
   148 //! [7]
       
   149 void GLWidget::paintGL()
       
   150 {
       
   151     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
   152     glLoadIdentity();
       
   153     glTranslatef(0.0, 0.0, -10.0);
       
   154     glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
       
   155     glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
       
   156     glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);
       
   157     logo->draw();
       
   158 }
       
   159 //! [7]
       
   160 
       
   161 //! [8]
       
   162 void GLWidget::resizeGL(int width, int height)
       
   163 {
       
   164     int side = qMin(width, height);
       
   165     glViewport((width - side) / 2, (height - side) / 2, side, side);
       
   166 
       
   167     glMatrixMode(GL_PROJECTION);
       
   168     glLoadIdentity();
       
   169 #ifdef QT_OPENGL_ES_1
       
   170     glOrthof(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
       
   171 #else
       
   172     glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
       
   173 #endif
       
   174     glMatrixMode(GL_MODELVIEW);
       
   175 }
       
   176 //! [8]
       
   177 
       
   178 //! [9]
       
   179 void GLWidget::mousePressEvent(QMouseEvent *event)
       
   180 {
       
   181     lastPos = event->pos();
       
   182 }
       
   183 //! [9]
       
   184 
       
   185 //! [10]
       
   186 void GLWidget::mouseMoveEvent(QMouseEvent *event)
       
   187 {
       
   188     int dx = event->x() - lastPos.x();
       
   189     int dy = event->y() - lastPos.y();
       
   190 
       
   191     if (event->buttons() & Qt::LeftButton) {
       
   192         setXRotation(xRot + 8 * dy);
       
   193         setYRotation(yRot + 8 * dx);
       
   194     } else if (event->buttons() & Qt::RightButton) {
       
   195         setXRotation(xRot + 8 * dy);
       
   196         setZRotation(zRot + 8 * dx);
       
   197     }
       
   198     lastPos = event->pos();
       
   199 }
       
   200 //! [10]