00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef KGLIMAGEVIEWER_H
00021 #define KGLIMAGEVIEWER_H
00022
00023 #define GL_GLEXT_PROTOTYPES
00024
00025 #ifdef QT4
00026 #include <QGLWidget>
00027 #else
00028 #include <qgl.h>
00029 #endif
00030
00031 class QTimer;
00032 class QStringList;
00033 #ifdef QT4
00034 class QGLPixelBuffer;
00035 #endif
00036
00037 namespace KGLImageView {
00038
00039 class KGLImage;
00040
00044 enum Axis{X = 0, Y, Z};
00045
00067 #ifdef QT4
00068 typedef QList<GLhandleARB> ShaderList;
00069 #else
00070 typedef QValueList<GLhandleARB> ShaderList;
00071 #endif
00072
00073 class KGLImageViewer : public QGLWidget
00074 {
00075 Q_OBJECT
00076
00077 public:
00086 KGLImageViewer( QWidget* parent, const char* name, float fps = 25, bool interactive = true);
00087 ~KGLImageViewer();
00088
00094 inline void setInteractive(bool interactive = true){ _interactive = interactive; }
00095
00102 void setCanvas(const QColor & color, bool update = true);
00103
00109 inline float position(Axis a) const { return _translation[a]; }
00110
00116 inline float rotation(Axis a) const { return _rotation[a]; }
00117
00123 inline float scaleFactor(Axis a) const { return _scale[a]; }
00124
00133 void rotate(Axis a, float degrees, int msecs = 0);
00134
00144 void rotate(float xDegrees, float yDegrees, float zDegrees, bool update = true);
00145
00154 void rotateTo(Axis a, float degrees, int msecs = 0);
00155
00165 void rotateTo(float xDegrees, float yDegrees, float zDegrees, bool update = true);
00166
00174 void scale(Axis a, float percent, int msecs = 0);
00175
00183 void scale(float xPercent, float yPercent, bool update = true);
00184
00192 void scaleTo(Axis a, float percent, int msecs = 0);
00200 void scaleTo(float xPercent, float yPercent, bool update = true);
00201
00202
00203 void move(Axis a, float percent, int msecs = 0);
00204 void move(float xPercent, float yPercent, float zPercent, bool update = true);
00205 void moveTo(Axis a, float percent, int msecs = 0);
00206 void moveTo(float xPercent, float yPercent, float zPercent, bool update = true);
00207
00216 void message(int x, int y, QString message, int msecs, const QColor * color = 0L);
00217 void hideMessage();
00218
00219 #if 0
00220
00230 void mark(const QRect & rect, bool longLines = false, const QColor & color = Qt::white, float lineWidth = 3.0, bool fill = false, const QColor & fillColor = Qt::white, float fillAlpha = 60.0);
00231 #endif
00232
00242 uint load( const KGLImage& image, bool show = false );
00243
00252 uint load( const QImage& img, bool show = false );
00261 int load( const QString& imgPath, bool show = false );
00269 uint load( const QPixmap& pix, bool show = false);
00270
00278 void remove(uint id);
00279
00283 int fpsDelay(){return _fpsDelay;}
00284
00285 inline bool providesShaders(){return _providesShaders; }
00286
00287 #ifdef QT4
00288 typedef QList<KGLImage> KGLImageList;
00289 #else
00290 typedef QValueList<KGLImage> KGLImageList;
00291 #endif
00292
00296 KGLImageList &images(){return _images;}
00297
00298 bool isTimerActive() const;
00299
00307 GLhandleARB loadShader(QString file, GLenum shaderType);
00308
00312 inline ShaderList & shaders(){ return _shaders; };
00313
00314 public slots:
00318 virtual void updateGL();
00319
00320 protected:
00321 friend class KGLImage;
00322 void ensureTimerIsActive();
00323 virtual void mergeCnB(KGLImage & img);
00324
00328 virtual void initializeGL();
00329
00333 virtual void paintGL();
00334
00338 virtual void resizeGL( int w, int h );
00339
00343 virtual void mousePressEvent ( QMouseEvent * e );
00344
00348 virtual void mouseReleaseEvent ( QMouseEvent * e );
00349
00353 virtual void mouseMoveEvent ( QMouseEvent * e );
00354
00358 virtual void wheelEvent ( QWheelEvent * e );
00359
00360 void blur(KGLImage &img);
00361
00362 private:
00363 KGLImageList _images;
00364 typedef QMap<GLuint, uint> ObjectCounter;
00365 ObjectCounter _objectCounter;
00366 GLfloat _scale[3];
00367 GLfloat _desiredScale[3];
00368 GLfloat _scaleStep[3];
00369
00370 GLfloat _translation[3];
00371 GLfloat _desiredTranslation[3];
00372 GLfloat _translationStep[3];
00373
00374 GLfloat _rotation[3];
00375 GLfloat _desiredRotation[3];
00376 GLfloat _rotationStep[3];
00377
00378 QStringList _message;
00379 QPoint _messagePos;
00380 int _messageTimeOut;
00381 GLfloat _messageColor[4];
00382
00383 QPoint _lastPos;
00384 float _scaleTarget[2];
00385
00386 uint _fpsDelay;
00387 int _activeAnimations;
00388 QTimer *_timer;
00389
00390 float _canvasColor[3];
00391
00392
00393 int _rectCoords[4];
00394 float _rectColor[3];
00395 bool _rectFilled;
00396 float _rectFillColor[4];
00397 float _rectThickness;
00398 bool _rectLongLines;
00399 #ifdef QT4
00400 QGLPixelBuffer *_pbuffer;
00401 #endif
00402 bool _interactive;
00403
00404 ShaderList _shaders;
00405 bool _providesShaders;
00406
00407
00408 private:
00409 void handleAnimationsPrivate(float (*value)[3], float (*desiredValue)[3], float (*valueStep)[3], int *animCounter);
00410 uint newUniqueId();
00411
00412 private slots:
00413 void handleAnimations();
00414 };
00415
00423 class KGLImage
00424 {
00425 public:
00429 KGLImage(){}
00430
00431 #if 0
00432
00435
00436 #endif
00437
00443 int width() const;
00444
00450 int height() const;
00451
00456 inline int basicWidth() const { return _basicWidth; }
00457
00462 inline int basicHeight() const { return _basicHeight; }
00463
00468 inline float alpha() const { return _colorI[3]*100.0; }
00469
00475 inline bool hasAlpha() const { return _hasAlpha; }
00476
00484 inline void setInternalColor(const QColor color)
00485 {
00486 _colorI[0] = ((float)color.red())/255.0;
00487 _colorI[1] = ((float)color.green())/255.0;
00488 _colorI[2] = ((float)color.blue())/255.0;
00489 }
00490
00495 inline QColor color() const { return QColor((int)(_color[0]*255), (int)(_color[1]*255), (int)(_color[2]*255)); }
00496
00501 inline float brightness() const { return _brightness*100.0; }
00502
00507 inline float position(Axis a) const { return _translation[a]; }
00508
00513 inline float rotation(Axis a) const { return _rotation[a]; }
00514
00519 inline float scaleFactor(Axis a) const { return _scale[a]; }
00520
00525 inline GLuint glObject() const { return _object; }
00526 inline uint id() const { return _id; }
00527
00528 inline KGLImageViewer *parent() const { return _parent; }
00529
00534 inline bool isShown() const { return _isShown; }
00535
00536
00549 void show(bool update = true);
00550
00556 void hide(bool update = true);
00557
00567 void rotate(Axis a, float degrees, int msecs = 0);
00568
00579 void rotate(float xDegrees, float yDegrees, float zDegrees, bool update = true);
00580
00590 void rotateTo(Axis a, float degrees, int msecs = 0);
00591
00602 void rotateTo(float xDegrees, float yDegrees, float zDegrees, bool update = true);
00603
00612 float scale(Axis a, float percent, int msecs = 0);
00613
00622 void scale(float xPercent, float yPercent, bool update = true);
00623
00635 float scaleTo(Axis a, float percent, int msecs = 0, bool viewRelative = false, float assumedViewScale = -1.0);
00636 void scaleTo(float xPercent, float yPercent, bool update = true, bool viewRelative = false, float assumedViewScaleX = -1.0, float assumedViewScaleY = -1.0);
00637 void move(Axis a, float percent, int msecs = 0);
00638 void move(float xPercent, float yPercent, float zPercent, bool update = true);
00639 void moveTo(Axis a, float percent, int msecs = 0);
00640 void moveTo(float xPercent, float yPercent, float zPercent, bool update = true);
00641
00642 void setAlpha(float percent, int msecs = 0);
00643 void tint(const QColor & color, int msecs = 0);
00644 void setBrightness(float percent, int msecs = 0);
00645
00667 inline void boxBlur(float factor, int msecs = 0) {blur(factor, msecs, 0);}
00671 inline void tunnel(float factor, int msecs = 0) {blur(factor, msecs, 1);}
00672 void blur(float factor, int msecs = 0, int type = 0);
00673
00674 inline float blurrage(){return _blur;}
00675
00676 void setClipRect(int x, int y, int w, int h, bool update = true);
00677 inline void setClipRect(QRect &r, bool update = true) { setClipRect(r.x(), r.y(), r.width(), r.height(), update); }
00678
00679 inline void setClipping(bool enabled = true) { _hasClipping = enabled; }
00680 inline bool hasClipping() const { return _hasClipping; }
00681
00682 void invert(bool inverted = true, bool update = true);
00683 inline bool isInverted() { return _inverted; }
00684
00685
00686 void resize(int width, int height, int msecs = 0, float assumedViewScaleX = -1.0, float assumedViewScaleY = -1.0);
00687
00695 void addShader(GLhandleARB shader, bool linkProgram = true, bool update = true);
00699 inline void addShader(QString file, GLenum shaderType, bool linkProgram = true, bool update = true)
00700 {
00701 addShader(_parent->loadShader(file, shaderType), linkProgram, update);
00702 }
00710 void removeShader(GLhandleARB shader, bool relinkProgram = true, bool update = true);
00711
00715 inline GLhandleARB shaderProgram(){ return _shaderProgram; }
00716
00723 inline void setShaderProgram(KGLImage &img){ _shaderProgram = img._shaderProgram; }
00724
00728 inline ShaderList & shaders() {return _shaders;}
00729
00741 void setShaderUniform(const int var, const int varSize, const float val1, const float val2 = 0.0, const float val3 = 0.0, const float val4 = 0.0);
00742
00747 int setShaderUniform(QString var, const int varSize, const float val1, const float val2 = 0.0, const float val3 = 0.0, const float val4 = 0.0);
00748
00749
00750 protected:
00751 void paint();
00752
00753 private:
00754 KGLImage(KGLImageViewer *parent, const uint id, const GLuint object, GLuint *textures, const int texAmount, int width, int height, bool hasAlpha = false);
00755
00756 friend class KGLImageViewer;
00757 GLfloat _scale[3];
00758 GLfloat _desiredScale[3];
00759 GLfloat _scaleStep[3];
00760
00761 GLfloat _translation[3];
00762 GLfloat _desiredTranslation[3];
00763 GLfloat _translationStep[3];
00764
00765 GLfloat _rotation[3];
00766 GLfloat _desiredRotation[3];
00767 GLfloat _rotationStep[3];
00768
00769 GLfloat _color[3];
00770 GLfloat _colorI[4];
00771 GLfloat _desiredColor[4];
00772 GLfloat _colorStep[4];
00773
00774 GLfloat _brightness;
00775 GLfloat _desiredBrightness;
00776 GLfloat _brightnessStep;
00777
00778 GLfloat _blur;
00779 GLfloat _desiredBlur;
00780 GLfloat _blurStep;
00781 int _blurType;
00782
00783 GLdouble _clip[4][4];
00784
00785 GLint _combineRGB;
00786
00787 uint _id;
00788 GLuint _object;
00789 GLuint _blurObj;
00790 GLuint _blurTex;
00791 int _blurW, _blurH;
00792 GLuint *_textures;
00793 int _texAmount;
00794 int _basicWidth, _basicHeight;
00795 bool _isShown;
00796 int _activeAnimations;
00797 bool _hasAlpha, _hasClipping;
00798 KGLImageViewer *_parent;
00799 float _ratio;
00800 bool _inverted;
00801
00802 ShaderList _shaders;
00803 GLhandleARB _shaderProgram;
00804 GLint _sColor, _sAlpha, _sBrightness, _sInverted;
00805 };
00806
00807 }
00808
00809 #endif //KGLIMAGEVIEWER_H