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


#ifndef DISPLAYERFACTORYINTERFACE_H
#define DISPLAYERFACTORYINTERFACE_H


//#include <GL/glew.h>
#include "SelectionTool.h"
#include "Box.h"
#include "UIParam.h"
#include "ToolCategory.h"
18
#include "UIContext.h"
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "UIData.h"
class UIParamSet;
class SelectionManager;


enum DisplayDoF
{
    DISPLAY_DOF_NONE    = 0x00,
    DISPLAY_DOF_2D      = 0x01,
    DISPLAY_DOF_3D      = 0x02,
    DISPLAY_DOF_BOTH    = DISPLAY_DOF_2D | DISPLAY_DOF_3D
};


class DisplayableInterface : public QObject
{
    Q_OBJECT

private:
    GLViewer                    *m_Viewer;
    GenericUIData               *m_Data;
    bool                        m_IsAnimationPaused;
41
	bool						m_IsSelected;
42

43 44 45 46
    QString                     concatenateResourcesContent( const QStringList &resourceNames ) const
    {
        QString res;
        for( auto &rscName : resourceNames )
47
            res += getResourceContent( rscName );
48 49 50
        return res;
    }

51
protected:
52 53 54 55 56 57 58
    inline QString              getResourceContent( const QString &resourceName ) const
    {
        QString content;
        QFile rscFile( resourceName );
        if( !rscFile.open( QIODevice::ReadOnly | QIODevice::Text ) )
            qDebug() << "Unable to load resource \"" << resourceName << "\".";
        else
59
            content = QTextStream( &rscFile ).readAll();
60 61
        return content;
    }
62 63 64
    inline bool                 shaderFromResources( GPU::Shader &shader, 
                                                     const QString &vshResourceName,
                                                     const QString &fshResourceName,
65
                                                     std::string *logs = NULL ) const
66
    {
67 68 69 70 71 72 73 74 75 76
        return shaderFromResources( shader, QStringList(vshResourceName), QStringList(fshResourceName), logs );
    }
    inline bool                 shaderFromResources( GPU::Shader &shader, 
                                                     const QStringList &vshResourceNames,
                                                     const QStringList &fshResourceNames,
                                                     std::string *logs = NULL ) const
    {
        std::string vshSrc = concatenateResourcesContent(vshResourceNames).toStdString();
        std::string fshSrc = concatenateResourcesContent(fshResourceNames).toStdString();
        return GPU::CreateShaderFromSources( shader, vshSrc, fshSrc, logs );
77 78 79 80 81
    }
    inline bool                 shaderFromResources( GPU::Shader &shader, 
                                                     const QString &vshResourceName,
                                                     const QString &gshResourceName,
                                                     const QString &fshResourceName,
82
                                                     std::string *logs = NULL ) const
83
    {
84 85 86 87 88 89 90 91 92 93 94 95
        return shaderFromResources( shader, QStringList(vshResourceName), QStringList(gshResourceName), QStringList(fshResourceName), logs );
    }
    inline bool                 shaderFromResources( GPU::Shader &shader, 
                                                     const QStringList &vshResourceNames,
                                                     const QStringList &gshResourceNames,
                                                     const QStringList &fshResourceNames,
                                                     std::string *logs = NULL ) const
    {
        std::string vshSrc = concatenateResourcesContent(vshResourceNames).toStdString();
        std::string gshSrc = concatenateResourcesContent(gshResourceNames).toStdString();
        std::string fshSrc = concatenateResourcesContent(fshResourceNames).toStdString();
        return GPU::CreateShaderFromSources( shader, vshSrc, gshSrc, fshSrc, logs );
96
    }
97 98 99 100 101

public:
    inline                      DisplayableInterface( GenericUIData *m, GLViewer *v ) : QObject(), m_Data(m), m_Viewer(v), m_IsAnimationPaused(false) {}
    virtual                     ~DisplayableInterface() {}

102 103
    /** Recover the current viewer to which this Displayable is associated. */
    inline GLViewer*            getViewer() const                       { return m_Viewer; }
104 105 106 107 108 109 110 111 112 113 114 115 116
    /** Recover the source data from which this Displayable has been created. */
    inline GenericUIData*       getSource() const                       { return m_Data; }
    /** Recover the current state of the set of display parameters. */
    inline const UIParamSet&    getParameters() const                   { return *m_Data->GetDisplayOptions(); }

    /** Function called once, after the creation of the displayer object but before its first display.
     *  The OpenGL context is active, which means that any OpenGL operation can be done here.
     */
    virtual void                initialize( UIParamSet &params )        {}
    /** Function called once, before the destruction of the displayer object.
     *  The OpenGL context is active, which means that any OpenGL operation can be done here.
     */
    virtual void                release( UIParamSet &params )           {}
117

118
    /** Recover the bounding box of the displayed item in locam frame. */
119
    virtual void                boundingBox( Box3f &box ) const         = 0;
120 121
    /** Recover the transformation matrix of the displayed item from local to global frame. */
    virtual void                transformation( QMatrix4x4 &tr ) const  { tr.setToIdentity(); }
122 123 124 125 126
    /** Recover the label to be displayed. */
    virtual QString             label() const                           { return QString(); }
    /** Recover parameters describing the display options of the current item. */
    virtual void                declareParameters( UIParamSet &params ) {}
    /** Function called when the value of a parameter has changed. */
127
    virtual void                onUpdatingParameter( UIParamSet &params, UIParam *p )       {}
128 129 130 131 132 133 134 135 136 137
    /** Function that effectively performs the display. */
    virtual void                onDisplay( UIParamSet &params )         = 0;
    /** Called when the viewport of the GL widget into which this object is displayed is resized. */
    virtual void                onReshape( UIParamSet &params )         {}
    /** Specify if picking is enabled for this item. */
    virtual bool                isPickable( UIParamSet &params )        { return false; }
    /** Function that performs a rendering pass dedicated to point picking. */
    virtual void                onPicking( UIParamSet &params,
                                           GPU::Shader &pickingShader,
                                           const std::string& pickingShaderVertexAttrib )   {}
138 139
	/** Specify if the item is currently selected. */
	inline bool					isSelected() const						{ return m_IsSelected; }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

    /** Specify if this item can be animated. */
    virtual bool                isAnimated()                            { return false; }
    /** Specify a fixed timestep (in sec.), if needed. In this case, the onAnimationStep function is called only when elapsed time since the last time step exceeds the specified duration. */
    virtual float               animationTimeStepValue()                { return 0.0f; }
    /** Function called when the animation is started. */
    virtual void                onAnimationStart()                      {}
    /** Function called when the animation is stopped. */
    virtual void                onAnimationStop()                       {}
    /** Function called at each animation frame, providing the time step since the last call. */
    virtual void                onAnimationStep( float elapsedTimeSec ) {}
    /** Function called when the button "Step forward" is clicked. */
    virtual void                onAnimationStepForward()                {}
    /** Function called when the button "Step backward" is clicked. */
    virtual void                onAnimationStepBackward()               {}

    inline bool                 isAnimationPaused() const               { return m_IsAnimationPaused; }

158 159 160
    /** Recover the managers describing the selection behaviour for every kind of selectable entity. */
    virtual void                selectionManagers( QVector<SelectionManager*> &mngr ) {}
    /** Apply a functor to every selected entity. */
161 162
    virtual void                processSelection( SelectionManager *mngr, BaseSelectionProcessor &proc ) {}

163 164 165 166 167
    /** Specify if this item can be grabbed and moved in the 3D viewer. */
    virtual bool                isGrabbable() const                                 { return false; }
    /** Function called when the item grabbing motion has been validated in the 3D viewer. */
    virtual void                onUpdatingTransformation( const QMatrix4x4 &tr )    {}

168 169 170 171 172 173
signals:
    void                        registerAnimation( DisplayableInterface *d );
    void                        unregisterAnimation( DisplayableInterface *d );
    void                        displayNeedUpdate();

public slots:
174
	inline void					setSelected( bool selected )			{ m_IsSelected = selected; }
175
    inline void                 updateParameter( UIParam* p )           { onUpdatingParameter(*p->Group(),p); }
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    inline void                 startAnimation()                        { m_IsAnimationPaused = false; emit registerAnimation(this); }
    inline void                 pauseAnimation()                        { m_IsAnimationPaused = true; }
    inline void                 stopAnimation()                         { m_IsAnimationPaused = false; emit unregisterAnimation(this); }
    inline void                 animationStepForward()                  { onAnimationStepForward();  emit displayNeedUpdate(); }
    inline void                 animationStepBackward()                 { onAnimationStepBackward(); emit displayNeedUpdate(); }
};


class DisplayableFactoryInterface
{
public:
    virtual                         ~DisplayableFactoryInterface() {}

    /** Type identifiers of the data that can be managed by this displayer. */
    virtual void                    acceptedDataTypes( QVector<QString> &types ) const = 0;
    /** Determines the display order of all objects with respect to each other. Higher priorities are
     *  displayed after lower ones. May be useful when transparent objects have to be displayed.
     */
    virtual unsigned int            displayPriority() const { return 0; }
    /** Defines the degrees of freedom (2D or 3D) required for the viewer to display this object. */
    virtual DisplayDoF              viewerDoF() const = 0;
    /** Creates an instance of a displayer object with the correct type. */
    virtual DisplayableInterface*   newDisplayer( GenericUIData *m, GLViewer *v ) const = 0;
199 200 201 202 203 204 205 206 207 208

    inline void                     allocateDisplayOptions( GenericUIData *d ) const
    {
        DisplayableInterface *disp = newDisplayer( d, NULL );
        UIParamSet *params = new UIParamSet( d->GetBaseName(), NULL );
        disp->declareParameters( *params );
        params->updateChildrenLists( d );
        d->SetDisplayOptions( params );
        delete disp;
    }
209 210 211
};


212 213
//Q_DECLARE_INTERFACE( DisplayableInterface, "PluginsInterface.Displayable" )
Q_DECLARE_INTERFACE( DisplayableFactoryInterface, "PluginsInterface.DisplayableFactory" )
214 215 216 217 218




#endif // DISPLAYERFACTORYINTERFACE_H