demos/boxes/scene.h
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 demonstration applications 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 #ifndef SCENE_H
       
    43 #define SCENE_H
       
    44 
       
    45 //#include <GL/glew.h>
       
    46 #include "glextensions.h"
       
    47 
       
    48 #include <QtGui>
       
    49 #include <QtOpenGL>
       
    50 
       
    51 #include "roundedbox.h"
       
    52 #include "gltrianglemesh.h"
       
    53 #include "trackball.h"
       
    54 #include "glbuffers.h"
       
    55 #include "qtbox.h"
       
    56 
       
    57 #define PI 3.14159265358979
       
    58 
       
    59 QT_BEGIN_NAMESPACE
       
    60 class QMatrix4x4;
       
    61 QT_END_NAMESPACE
       
    62 
       
    63 class ParameterEdit : public QWidget
       
    64 {
       
    65 public:
       
    66     virtual void emitChange() = 0;
       
    67 };
       
    68 
       
    69 class ColorEdit : public ParameterEdit
       
    70 {
       
    71     Q_OBJECT
       
    72 public:
       
    73     ColorEdit(QRgb initialColor, int id);
       
    74     QRgb color() const {return m_color;}
       
    75     virtual void emitChange() {emit colorChanged(m_color, m_id);}
       
    76 public slots:
       
    77     void editDone();
       
    78 signals:
       
    79     void colorChanged(QRgb color, int id);
       
    80 protected:
       
    81     virtual void mousePressEvent(QMouseEvent *event);
       
    82     void setColor(QRgb color); // also emits colorChanged()
       
    83 private:
       
    84     QGraphicsScene *m_dialogParentScene;
       
    85     QLineEdit *m_lineEdit;
       
    86     QFrame *m_button;
       
    87     QRgb m_color;
       
    88     int m_id;
       
    89 };
       
    90 
       
    91 class FloatEdit : public ParameterEdit
       
    92 {
       
    93     Q_OBJECT
       
    94 public:
       
    95     FloatEdit(float initialValue, int id);
       
    96     float value() const {return m_value;}
       
    97     virtual void emitChange() {emit valueChanged(m_value, m_id);}
       
    98 public slots:
       
    99     void editDone();
       
   100 signals:
       
   101     void valueChanged(float value, int id);
       
   102 private:
       
   103     QGraphicsScene *m_dialogParentScene;
       
   104     QLineEdit *m_lineEdit;
       
   105     float m_value;
       
   106     int m_id;
       
   107 };
       
   108 
       
   109 class GraphicsWidget : public QGraphicsProxyWidget
       
   110 {
       
   111 public:
       
   112     GraphicsWidget() : QGraphicsProxyWidget(0, Qt::Window) {}
       
   113 protected:
       
   114     virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value);
       
   115     virtual void resizeEvent(QGraphicsSceneResizeEvent *event);
       
   116     virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
       
   117 };
       
   118 
       
   119 class TwoSidedGraphicsWidget : public QObject
       
   120 {
       
   121     Q_OBJECT
       
   122 public:
       
   123     TwoSidedGraphicsWidget(QGraphicsScene *scene);
       
   124     void setWidget(int index, QWidget *widget);
       
   125     QWidget *widget(int index);
       
   126 public slots:
       
   127     void flip();
       
   128 protected slots:
       
   129     void animateFlip();
       
   130 private:
       
   131     GraphicsWidget *m_proxyWidgets[2];
       
   132     int m_current;
       
   133     int m_angle; // angle in degrees
       
   134     int m_delta;
       
   135 };
       
   136 
       
   137 class RenderOptionsDialog : public QDialog
       
   138 {
       
   139     Q_OBJECT
       
   140 public:
       
   141     RenderOptionsDialog();
       
   142     int addTexture(const QString &name);
       
   143     int addShader(const QString &name);
       
   144     void emitParameterChanged();
       
   145 protected slots:
       
   146     void setColorParameter(QRgb color, int id);
       
   147     void setFloatParameter(float value, int id);
       
   148 signals:
       
   149     void dynamicCubemapToggled(int);
       
   150     void colorParameterChanged(const QString &, QRgb);
       
   151     void floatParameterChanged(const QString &, float);
       
   152     void textureChanged(int);
       
   153     void shaderChanged(int);
       
   154     void doubleClicked();
       
   155 protected:
       
   156     virtual void mouseDoubleClickEvent(QMouseEvent *event);
       
   157 
       
   158     QVector<QByteArray> m_parameterNames;
       
   159     QComboBox *m_textureCombo;
       
   160     QComboBox *m_shaderCombo;
       
   161     QVector<ParameterEdit *> m_parameterEdits;
       
   162 };
       
   163 
       
   164 class ItemDialog : public QDialog
       
   165 {
       
   166     Q_OBJECT
       
   167 public:
       
   168     enum ItemType {
       
   169         QtBoxItem,
       
   170         CircleItem,
       
   171         SquareItem,
       
   172     };
       
   173 
       
   174     ItemDialog();
       
   175 public slots:
       
   176     void triggerNewQtBox();
       
   177     void triggerNewCircleItem();
       
   178     void triggerNewSquareItem();
       
   179 signals:
       
   180     void doubleClicked();
       
   181     void newItemTriggered(ItemDialog::ItemType type);
       
   182 protected:
       
   183     virtual void mouseDoubleClickEvent(QMouseEvent *event);
       
   184 };
       
   185 
       
   186 class Scene : public QGraphicsScene
       
   187 {
       
   188     Q_OBJECT
       
   189 public:
       
   190     Scene(int width, int height, int maxTextureSize);
       
   191     ~Scene();
       
   192     virtual void drawBackground(QPainter *painter, const QRectF &rect);
       
   193 
       
   194 public slots:
       
   195     void setShader(int index);
       
   196     void setTexture(int index);
       
   197     void toggleDynamicCubemap(int state);
       
   198     void setColorParameter(const QString &name, QRgb color);
       
   199     void setFloatParameter(const QString &name, float value);
       
   200     void newItem(ItemDialog::ItemType type);
       
   201 protected:
       
   202     void renderBoxes(const QMatrix4x4 &view, int excludeBox = -2);
       
   203     void setStates();
       
   204     void setLights();
       
   205     void defaultStates();
       
   206     void renderCubemaps();
       
   207 
       
   208     virtual void mousePressEvent(QGraphicsSceneMouseEvent *event);
       
   209     virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
       
   210     virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
       
   211     virtual void wheelEvent(QGraphicsSceneWheelEvent * event);
       
   212 private:
       
   213     void initGL();
       
   214     QPointF pixelPosToViewPos(const QPointF& p);
       
   215 
       
   216     QTime m_time;
       
   217     int m_lastTime;
       
   218     int m_mouseEventTime;
       
   219     int m_distExp;
       
   220     int m_frame;
       
   221     int m_maxTextureSize;
       
   222 
       
   223     int m_currentShader;
       
   224     int m_currentTexture;
       
   225     bool m_dynamicCubemap;
       
   226     bool m_updateAllCubemaps;
       
   227 
       
   228     RenderOptionsDialog *m_renderOptions;
       
   229     ItemDialog *m_itemDialog;
       
   230     QTimer *m_timer;
       
   231     GLRoundedBox *m_box;
       
   232     TrackBall m_trackBalls[3];
       
   233     QVector<GLTexture *> m_textures;
       
   234     GLTextureCube *m_environment;
       
   235     GLTexture3D *m_noise;
       
   236     GLRenderTargetCube *m_mainCubemap;
       
   237     QVector<GLRenderTargetCube *> m_cubemaps;
       
   238     QVector<QGLShaderProgram *> m_programs;
       
   239     QGLShader *m_vertexShader;
       
   240     QVector<QGLShader *> m_fragmentShaders;
       
   241     QGLShader *m_environmentShader;
       
   242     QGLShaderProgram *m_environmentProgram;
       
   243 };
       
   244 
       
   245 #endif