examples/opengl/samplebuffers/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 "glwidget.h"
       
    43 #include <math.h>
       
    44 
       
    45 #ifndef GL_MULTISAMPLE
       
    46 #define GL_MULTISAMPLE  0x809D
       
    47 #endif
       
    48 
       
    49 GLWidget::GLWidget(QWidget *parent)
       
    50     : QGLWidget(QGLFormat(QGL::SampleBuffers), parent)
       
    51 {
       
    52     startTimer(40);
       
    53     setWindowTitle(tr("Sample Buffers"));
       
    54 }
       
    55 
       
    56 void GLWidget::initializeGL()
       
    57 {
       
    58     glMatrixMode(GL_PROJECTION);
       
    59     glLoadIdentity();
       
    60     glOrtho(-.5, .5, .5, -.5, -1000, 1000);
       
    61     glMatrixMode(GL_MODELVIEW);
       
    62     glLoadIdentity();
       
    63     glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
       
    64 
       
    65     makeObject();
       
    66 }
       
    67 
       
    68 void GLWidget::resizeGL(int w, int h)
       
    69 {
       
    70     glViewport(0, 0, w, h);
       
    71 }
       
    72 
       
    73 void GLWidget::paintGL()
       
    74 {
       
    75     static float rot = 0.0;
       
    76     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       
    77 
       
    78     glMatrixMode(GL_MODELVIEW);
       
    79     glPushMatrix();
       
    80     glEnable(GL_MULTISAMPLE);
       
    81     glTranslatef(-0.25f, -0.10f, 0.0f);
       
    82     glScalef(0.75f, 1.15f, 0.0f);
       
    83     glRotatef(rot, 0.0f, 0.f, 1.0f);
       
    84     glCallList(list);
       
    85     glPopMatrix();
       
    86 
       
    87     glPushMatrix();
       
    88     glDisable(GL_MULTISAMPLE);
       
    89     glTranslatef(0.25f, -0.10f, 0.0f);
       
    90     glScalef(0.75f, 1.15f, 0.0f);
       
    91     glRotatef(rot, 0.0f, 0.0f, 1.0f);
       
    92     glCallList(list);
       
    93     glPopMatrix();
       
    94 
       
    95     rot += 0.2f;
       
    96 
       
    97     qglColor(Qt::black);
       
    98     renderText(-0.35, 0.4, 0.0, "Multisampling enabled");
       
    99     renderText(0.15, 0.4, 0.0, "Multisampling disabled");
       
   100 }
       
   101 
       
   102 void GLWidget::timerEvent(QTimerEvent *)
       
   103 {
       
   104     update();
       
   105 }
       
   106 
       
   107 void GLWidget::makeObject()
       
   108 {
       
   109     QColor qtGreen(QColor::fromCmykF(0.40, 0.0, 1.0, 0.0));
       
   110     const double Pi = 3.14159265358979323846;
       
   111     const int NumSectors = 15;
       
   112     GLdouble x1 = +0.06;
       
   113     GLdouble y1 = -0.14;
       
   114     GLdouble x2 = +0.14;
       
   115     GLdouble y2 = -0.06;
       
   116     GLdouble x3 = +0.08;
       
   117     GLdouble y3 = +0.00;
       
   118     GLdouble x4 = +0.30;
       
   119     GLdouble y4 = +0.22;
       
   120 
       
   121     list = glGenLists(1);
       
   122     glNewList(list, GL_COMPILE);
       
   123     {
       
   124         for (int i = 0; i < NumSectors; ++i) {
       
   125             double angle1 = (i * 2 * Pi) / NumSectors;
       
   126             GLdouble x5 = 0.30 * sin(angle1);
       
   127             GLdouble y5 = 0.30 * cos(angle1);
       
   128             GLdouble x6 = 0.20 * sin(angle1);
       
   129             GLdouble y6 = 0.20 * cos(angle1);
       
   130 
       
   131             double angle2 = ((i + 1) * 2 * Pi) / NumSectors;
       
   132             GLdouble x7 = 0.20 * sin(angle2);
       
   133             GLdouble y7 = 0.20 * cos(angle2);
       
   134             GLdouble x8 = 0.30 * sin(angle2);
       
   135             GLdouble y8 = 0.30 * cos(angle2);
       
   136 
       
   137             qglColor(qtGreen);
       
   138             quad(GL_QUADS, x5, y5, x6, y6, x7, y7, x8, y8);
       
   139             qglColor(Qt::black);
       
   140             quad(GL_LINE_LOOP, x5, y5, x6, y6, x7, y7, x8, y8);
       
   141         }
       
   142 
       
   143         qglColor(qtGreen);
       
   144         quad(GL_QUADS, x1, y1, x2, y2, y2, x2, y1, x1);
       
   145         quad(GL_QUADS, x3, y3, x4, y4, y4, x4, y3, x3);
       
   146 
       
   147         qglColor(Qt::black);
       
   148         quad(GL_LINE_LOOP, x1, y1, x2, y2, y2, x2, y1, x1);
       
   149         quad(GL_LINE_LOOP, x3, y3, x4, y4, y4, x4, y3, x3);
       
   150     }
       
   151     glEndList();
       
   152 }
       
   153 
       
   154 void GLWidget::quad(GLenum primitive, GLdouble x1, GLdouble y1, GLdouble x2, GLdouble y2,
       
   155                     GLdouble x3, GLdouble y3, GLdouble x4, GLdouble y4)
       
   156 {
       
   157     glBegin(primitive);
       
   158     {
       
   159         glVertex2d(x1, y1);
       
   160         glVertex2d(x2, y2);
       
   161         glVertex2d(x3, y3);
       
   162         glVertex2d(x4, y4);
       
   163     }
       
   164     glEnd();
       
   165 }