GLViewer.h 22.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 *	(c) LSIIT, UMR CNRS/UdS
 *	Authors: O. Gnevaux, F. Larue.
 *
 *	See licence.txt for additional information.
 */


#ifndef GLVIEWER_H
#define GLVIEWER_H


#include "../GPU/GPU.h"
#include "DisplayableInterface.h"
#include <set>
16
#include <chrono>
17
18
19
#include "MetricGrid.h"
#include "PickedPoint.h"
#include "UIData.h"
20
#include "Frustum.h"
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class InfoBarManager;
class Panel;




class GLViewer : public QOpenGLWidget
{
    Q_OBJECT

    /******************\
    | Internal type(s) |
    \******************/
public:
    struct PrimitiveInfo
    {
        Panel           *panel;
        unsigned int    row;
        unsigned int    count; 
    };

42
43
44
45
46
47
48
    struct RenderState
    {
        DisplayDoF      DoF;

        QVector3D       focusPoint;
        float           fovY;
	    float           distToFocusPoint;
49
50
        float           nearPlaneFactor;
        float           farPlaneFactor;
51
52
53
54
55
56
57
58
59
        float           zNear;
        float           zFar;
        QMatrix4x4      viewRotationMatrix;
        QMatrix4x4      lightRotationMatrix;

        bool            isWireframeEnabled;
        bool            isLightingEnabled;
        bool            isLightTrackingEnabled;
        bool            isCullingEnabled;
60
61
        MetricGrid      metricGrid2D;
        MetricGrid      metricGrid3D;
62
63
64
65

        unsigned int    pointSize;
    };

66
protected:
67
    class DisplayableInfo;
68
69
70
71
72
73

    typedef std::map<QString,DisplayableFactoryInterface*> FactoryMap;
    typedef std::list<DisplayableInfo*> DisplayableList;
    typedef std::map<int,DisplayableList> PriorityMap;
    typedef std::map<const GenericUIData*,DisplayableInfo> DisplayableMap;

74
75
76
    using TimeMeasurement = std::chrono::high_resolution_clock::time_point;


77
78
79
80
81
82
83
84
    class DisplayableInfo
    {
        QMatrix4x4                  userTransform;
        QMatrix4x4                  packingTransform;
        QMatrix4x4                  fullTransform;

    public:
        void                        Initialize( DisplayableInterface *displayable,
85
                                                DisplayableList::iterator priorityPos,
86
87
                                                UIParamSet &params );

88
        inline void                 SetUserTransform( const QMatrix4x4& tr )    { userTransform = tr; fullTransform = tr * packingTransform; }
89
90
91
92
        inline void                 SetPackingTransform( const QMatrix4x4& tr ) { packingTransform = tr; fullTransform = userTransform * tr; }
        inline const QMatrix4x4&    GetTransform() const                        { return fullTransform; }

        DisplayableInterface        *displayable;
93
94
        QVector<SelectionManager*>  selectionMgr;
        SelectionManager*           currentSelectionMgr;
95
96
97
98
        DisplayableList::iterator   posInPriorityList;
        Box3f                       boundingBox;
    };

99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
    enum SelectionToolName
    {
        SELECTION_TOOL_DISK     = 0,
        SELECTION_TOOL_BOX      ,
        SELECTION_TOOL_LASSO    ,
        SELECTION_TOOL_END      ,
    };

    /*******************\
    | Class variable(s) |
    \*******************/
protected:
    static const int                PICKING_WIN_RADIUS = 5;
    static const char*              s_PickingVPG;
    static const char*              s_PickingFPG;

    static const char*              s_ZBufferVPG;
    static const char*              s_ZBufferFPG;

    /********************\
    | Member variable(s) |
    \********************/
protected:
122
    RenderState                     m_View;
123
124
125
126
127
128

    float                           m_SpeedFactor;
    float                           m_CtrlSpeedFactor;
    float                           m_ShiftSpeedFactor;

	QPoint		                    m_PrevMousePos;
129
	QPoint		                    m_ClickMousePos;
130
131
132
133
134
135
136
137
138
139
140
141

    FactoryMap                      m_Factories;
    DisplayableMap                  m_Displayables;
    PriorityMap                     m_DisplayablesByPriority;
    DisplayableList                 m_AddingOrder;
    QVBoxLayout                     *m_DisplayOptionsLayout;

    InfoBarManager                  *m_InfoBarManager;
    QMap<QString,PrimitiveInfo>     m_PrimitiveCountMap;

    QList<DisplayableInterface*>    m_AnimationStartingQueue;
    QList<DisplayableInterface*>    m_AnimationStoppingQueue;
142
143
    TimeMeasurement                 m_AnimationFrameTime;
    QMap<DisplayableInterface*,double>   m_ActiveAnimations;
144
145
146
147
148
149
150
151
152
153
    bool                            m_AnimationsRunningAtPreviousFrame;

    bool                            m_SelectionEnabled;
    SelectionToolName               m_SelectionCurrentToolName;
    SelectionTool::Context          m_SelectionContext;
    SelectionTool*                  m_SelectionCurrentTool;
    std::vector<SelectionTool*>     m_SelectionTools;

    GPU::Shader                     m_PickingShader;
    GPU::Shader                     m_ZBufferShader;
154
155
156
    GPU::Texture2D                  m_SelectionDepthTexture;
    GPU::FrameBuffer                m_SelectionDepthBuffer;
    SelectionZBufferUpdateMode      m_SelectionZBufferUpdateMode;
157

158
159
    DisplayableInfo                 *m_BelowCursor;

160
161
162
163
164
165
166
167
168
169
170
171
    // View related transformations.

    bool                            m_MustUpdateFocal;
    double                          m_Focal;

    bool                            m_MustUpdateProjectionMatrix;
    QMatrix4x4                      m_ProjectionMatrix;
    QMatrix4x4                      m_ProjectionMatrixInverse;

    bool                            m_MustUpdateViewMatrix;
    QMatrix4x4                      m_ViewMatrix;
    QMatrix4x4                      m_ViewMatrixInverse;
172
    QMatrix4x4                      m_ViewRotationMatrixInverse;
173
174
175

    QMatrix4x4                      m_ViewProjectionMatrix;
    QMatrix4x4                      m_ViewProjectionMatrixInverse;
176
    QMatrix4x4                      m_PixelToRayMatrix;
177

178
179
    Frustum                         m_Frustum;

180
181
182
    bool                            m_ChangeNearPlaneMode;
    bool                            m_ChangeFarPlaneMode;

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    /*****************************\
    | Constructor(s) / destructor |
    \*****************************/
public:
    GLViewer( DisplayDoF dof,
              QWidget *parent = NULL,
              Qt::WindowFlags f = 0 );
    GLViewer( DisplayDoF dof,
              const QSurfaceFormat &format,
              QWidget *parent = NULL,
              Qt::WindowFlags f = 0 );
    virtual ~GLViewer();

    /********************\
    | Member function(s) |
    \********************/
protected:
200
201
    void                                        initMetricGrids();

202
    virtual void                                enterEvent( QEvent *evt );
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    virtual void	                            mousePressEvent( QMouseEvent *evt );
    virtual void                                mouseReleaseEvent( QMouseEvent *evt );
	virtual void	                            mouseMoveEvent( QMouseEvent *evt );
    virtual void                                wheelEvent( QWheelEvent *evt );
    virtual void                                mouseDoubleClickEvent( QMouseEvent *evt );
    virtual void                                keyPressEvent( QKeyEvent *evt );
    virtual void                                keyReleaseEvent( QKeyEvent *evt );
    virtual bool                                event( QEvent *evt );

    void                                        setupScene();
    virtual void                                releaseScene()                                  {}

    void                                        updateSelectionMode( const Qt::KeyboardModifiers &modifiers );

    void                                        mouseMotionNavigationEvent( float dx, float dy, QMouseEvent *evt );

    void                                        updatePrimitiveCounts();
    DisplayableMap::iterator                    removeDisplayable_Internal( DisplayableMap::iterator &dmapIt );

    void                                        displayLabel( DisplayableInterface *d );
223
    inline void                                 setLightRotationMatrix( const QMatrix4x4& m )   { m_View.lightRotationMatrix = m; update(); }
224
225
226
227
228

    void                                        repackDisplayables();

    void                                        manageAnimations();

229
230
231
    void                                        findObjectUnderCursor( const QPoint& cursor );
    void                                        clearObjectUnderCursor();

232
233
    QMatrix4x4                                  reframedProjection( float viewportX, float viewportY, float viewportW, float viewportH );

234
235
    void                                        processSelection( DisplayableInfo &dInfo, BaseSelectionProcessor &proc );

236
237
238
public:
	void	                                    init( DisplayDoF dof );

239
240
241
242
    inline void                                 getRenderState( RenderState& state ) const                      { state = m_View; }
    void                                        setRenderState( const RenderState& state );

    inline QVector2D                            screenPosToClipCoord( const QPoint &screenPos )                 { return QVector2D( 2.0f*screenPos.x()/width()-1.0f, 1.0f-2.0f*screenPos.y()/height() ); }
243
244
245
246
247

    virtual void	                            initializeGL();
    void                                        paintGL();
	virtual void	                            resizeGL( int width, int height );

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
    inline float                                speedFactor() const                                             { return m_SpeedFactor; }
    inline float                                ctrlSpeedFactor() const                                         { return m_CtrlSpeedFactor; }
    inline float                                shiftSpeedFactor() const                                        { return m_ShiftSpeedFactor; }

    inline const QVector3D&                     focusPoint() const                                              { return m_View.focusPoint; }
    inline float                                distToFocusPoint() const                                        { return m_View.distToFocusPoint; }
    inline float                                fovY() const                                                    { return m_View.fovY; }
    inline double                               focal() const                                                   { return m_Focal; }
    inline float                                zNear() const                                                   { return m_View.zNear; }
    inline float                                zFar() const                                                    { return m_View.zFar; }
    inline float                                pointSize() const                                               { return m_View.pointSize; }
    inline DisplayDoF                           dof() const                                                     { return m_View.DoF; }

    inline const QMatrix4x4&                    projectionMatrix() const                                        { return m_ProjectionMatrix; }
    inline const QMatrix4x4&                    projectionMatrixInverse() const                                 { return m_ProjectionMatrixInverse; }
    inline const QMatrix4x4&                    viewMatrix() const                                              { return m_ViewMatrix; }
    inline const QMatrix4x4&                    viewMatrixInverse() const                                       { return m_ViewMatrixInverse; }
    inline const QMatrix4x4&                    viewRotationMatrix() const                                      { return m_View.viewRotationMatrix; }
    inline const QMatrix4x4&                    viewRotationMatrixInverse() const                               { return m_ViewRotationMatrixInverse; }
267
    QMatrix4x4                                  modelMatrix( const DisplayableInterface *d ) const;
268
    inline QMatrix4x4                           modelMatrixInverse( const DisplayableInterface *d ) const       { return modelMatrix(d).inverted(); }
269

270
271
272
273
274
275
    inline const QMatrix4x4&                    viewProjectionMatrix() const                                    { return m_ViewProjectionMatrix; }
    inline const QMatrix4x4&                    viewProjectionMatrixInverse() const                             { return m_ViewProjectionMatrixInverse; }
    inline QMatrix4x4                           modelViewMatrix( const DisplayableInterface *d ) const          { return viewMatrix() * modelMatrix(d); }
    inline QMatrix4x4                           modelViewMatrixInverse( const DisplayableInterface *d ) const   { return modelViewMatrix(d).inverted(); }
    inline QMatrix4x4                           MVPMatrix( const DisplayableInterface *d ) const                { return viewProjectionMatrix() * modelMatrix(d); }
    inline QMatrix4x4                           MVPMatrixInverse( const DisplayableInterface *d ) const         { return MVPMatrix(d).inverted(); }
276

277
278
    inline QVector3D                            viewpointLocation() const
    {
279
        const QMatrix4x4& R = viewMatrix();
280
281
282
283
284
285
286
        return R.transposed().mapVector( -R.column(3).toVector3D() );
    }
    inline QVector3D                            pixelRay( int px, int py ) const
    {
        QVector4D clipCoord( 2.0f*px/width() - 1.0f, 2.0f*py/height() - 1.0f, 0.0f, 1.0f );
        return m_PixelToRayMatrix.map( clipCoord ).toVector3D();
    }
287
    QMatrix4x4                                  viewportMatrix( bool flipY = false ) const;
288
289
    inline QMatrix4x4                           normalMatrix( const DisplayableInterface *d ) const             { QMatrix4x4 m = modelViewMatrix(d); m(0,3) = m(1,3) = m(2,3) = 0.0f; return m; }
    inline const QMatrix4x4&                    lightRotationMatrix() const                                     { return m_View.lightRotationMatrix; }
290

291
292
293
294
295
    inline QVector3D                            lightDirection() const                                          { return m_View.lightRotationMatrix.column(2).toVector3D(); }
    inline bool                                 isLightingEnabled() const                                       { return m_View.isLightingEnabled;  }
    inline bool                                 isWireframeEnabled() const                                      { return m_View.isWireframeEnabled; }
    inline bool                                 isCullingEnabled() const                                        { return m_View.isCullingEnabled; }
    inline bool                                 isLightTrackingEye() const                                      { return m_View.isLightTrackingEnabled; }
296

297
    inline void                                 clearAllFactories()                                             { m_Factories.clear(); }
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
    void                                        populateDisplayerFactories( DisplayableFactoryInterface *plugin );
    void                                        populateDisplayerFactories( QList<DisplayableFactoryInterface*> &plugins );

    virtual bool                                addDisplayable( GenericUIData *m );
    virtual void                                removeDisplayable( GenericUIData *m );
    virtual void                                removeAllDisplayables();
    inline bool                                 isDisplayed( GenericUIData *m ) const           { return m_Displayables.find(m) != m_Displayables.end(); }
    inline bool                                 isAbleToDisplay( GenericUIData *m ) const       { return m_Factories.find(m->GetTypeString()) != m_Factories.end(); }
    inline DisplayableFactoryInterface*         getFactory( const QString &type )               { FactoryMap::iterator f=m_Factories.find(type); return f==m_Factories.end()? NULL : f->second; }
    inline const DisplayableFactoryInterface*   getFactory( const QString &type ) const         { FactoryMap::const_iterator f=m_Factories.find(type); return f==m_Factories.end()? NULL : f->second; }
    inline DisplayableInterface*                getDisplayable( const GenericUIData* m )        { DisplayableMap::iterator d=m_Displayables.find(m); return d==m_Displayables.end()? NULL : d->second.displayable; }
    inline DisplayableInterface*                getDisplayable( const GenericUIData* m ) const  { DisplayableMap::const_iterator d=m_Displayables.find(m); return d==m_Displayables.end()? NULL : d->second.displayable; }
    virtual void                                updateDisplayable( GenericUIData *m );
    inline const DisplayableMap&                getDisplayables() const { return m_Displayables; }
    inline DisplayableMap&                      getDisplayables()       { return m_Displayables; }
    void                                        setTransform( GenericUIData *m, const QMatrix4x4 &xf );

    virtual bool                                getPickedPoint( const float x,
                                                                const float y,
                                                                PickedPoint &picked,
                                                                bool accurate );

    void                                        frameBox( Box3f &box, const QMatrix4x4 *xf = NULL );
    virtual void                                frameItem( GenericUIData* item );
    virtual void                                frameItems( std::list<GenericUIData*> &items );
    virtual void                                frameAll();

    inline bool                                 isSelectionEnabled() const                      { return m_SelectionEnabled; }
    inline const SelectionTool&                 currentSelectionTool() const                    { return *m_SelectionCurrentTool; }
    void                                        processSelection( BaseSelectionProcessor &proc );
328
    void                                        processSelection( QList<BaseSelectionProcessor*> &procList );
329
    void                                        processSelection( GenericUIData *m, BaseSelectionProcessor &proc );
330
    void                                        processSelection( DisplayableInterface *d, BaseSelectionProcessor &proc );
331

332
    void                                        renderCoords( double x, double y, double z, double *screenX, double *screenY );
333
334
    void                                        renderText( double x, double y, const QString &text, const QFont &font = QFont(), Qt::Alignment align = Qt::AlignLeft );
    void                                        renderText( double x, double y, double z, const QString &text, const QFont &font = QFont(), Qt::Alignment align = Qt::AlignLeft );
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351

    bool                                        imageOfCurrentDisplay( const GenericUIData *m, QImage &renderResult );


    /**************\
    | QT signal(s) |
    \**************/
signals:
    void                                        distToFocusPointChanged( const float d );
    void                                        fovYChanged( const float fovY );
    void                                        focusPointChanged( const QVector3D &p );
    void                                        viewRotationMatrixUpdated( const QMatrix4x4 &deltaRot );
    void                                        cullingToggled( const bool cullingEnabled );
    void                                        screenSpaceTranslation( const QVector2D &tra );
    void                                        wireframeToggled( const bool wireframeEnabled );
    void                                        lightingToggled( const bool lightingEnabled );
    void                                        lightTrackingToggled( const bool trackingEnabled );
352
    void                                        keyPressed( QKeyEvent *evt );
353
    void                                        dataSelected( GenericUIData *d, Qt::KeyboardModifiers modifiers );
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401

    /************\
    | QT slot(s) |
    \************/
public slots:
    void                                        centerView();

    void                                        setSpeedFactor( const float speedFactor,
                                                                const float ctrlSpeedFactor,
                                                                const float shiftSpeedFactor );
    void                                        setSpeedFactor( const float speedFactor );
    void                                        setCtrlSpeedFactor( const float ctrlSpeedFactor );
    void                                        setShiftSpeedFactor( const float shiftSpeedFactor );

    void                                        increasePointSize();
    void                                        decreasePointSize();

    void                                        setFocusPoint( const QVector3D& p );
    void                                        setDistToFocusPoint( const float d );
    void                                        setViewRotationMatrix( const QMatrix4x4& m );
    void                                        setFovY( float fovY );

    void                                        setSelectionEnabled( bool enabled );
    void                                        setSelectionDisabled( bool disabled );
    void                                        toggleSelection();
    void                                        updateSelectionZBuffer();
    void                                        setSelectionEntity( SelectableEntity entity );
    void                                        clearSelection( SelectableEntity entities );
    void                                        updateSelection( SelectionTool &tool );

    void                                        setCullingEnabled( const bool enabled );
    void                                        toggleCulling();

    void                                        setWireframeEnabled( const bool enabled );
    void                                        setLightingEnabled( const bool enabled );
    void                                        setLightTrackingEnabled( const bool enabled );
    void                                        toggleWireframe();
    void                                        toggleLighting();
    void                                        toggleLightTracking();

    inline void                                 registerAnimation( DisplayableInterface *anim )     { m_AnimationStartingQueue.push_back( anim ); update(); }
    inline void                                 unregisterAnimation( DisplayableInterface *anim )   { m_AnimationStoppingQueue .push_back( anim ); update(); }
};




#endif //GLVIEWER_H