00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifndef VectorGraphics_INCLUDE_ONCE
00033 #define VectorGraphics_INCLUDE_ONCE
00034
00035 #include <vlVG/link_config.hpp>
00036 #include <vlCore/Image.hpp>
00037 #include <vlCore/VisualizationLibrary.hpp>
00038 #include <vlGraphics/Actor.hpp>
00039 #include <vlGraphics/Text.hpp>
00040 #include <vlGraphics/FontManager.hpp>
00041 #include <vlGraphics/Effect.hpp>
00042 #include <vlGraphics/SceneManager.hpp>
00043 #include <vlGraphics/Clear.hpp>
00044 #include <vlGraphics/Scissor.hpp>
00045 #include <vlGraphics/Geometry.hpp>
00046 #include <vlGraphics/FontManager.hpp>
00047
00048 namespace vl
00049 {
00051 typedef enum
00052 {
00054 TextureMode_Clamp,
00056 TextureMode_Repeat
00057 } ETextureMode;
00058
00060 typedef enum
00061 {
00063 PolygonStipple_Solid,
00064 PolygonStipple_Dot,
00065 PolygonStipple_Chain,
00066 PolygonStipple_HLine,
00067 PolygonStipple_VLine
00068 } EPolygonStipple;
00069
00071 typedef enum
00072 {
00074 LineStipple_Solid,
00075 LineStipple_Dot,
00076 LineStipple_Dash,
00077 LineStipple_Dash4,
00078 LineStipple_Dash8,
00079 LineStipple_DashDot,
00080 LineStipple_DashDotDot
00081 } ELineStipple;
00082
00083
00084
00085
00106 class VLVG_EXPORT VectorGraphics: public Object
00107 {
00108 private:
00109
00111 class ImageState
00112 {
00113 public:
00114 ImageState(Image* img, ETextureMode mode): mImage(img), mTextureMode(mode) {}
00115
00116 bool operator<(const ImageState& other) const
00117 {
00118 if (mImage != other.mImage)
00119 return mImage < other.mImage;
00120 else
00121 if (mTextureMode != other.mTextureMode)
00122 return mTextureMode < other.mTextureMode;
00123 else
00124 return false;
00125 }
00126 protected:
00127 Image* mImage;
00128 ETextureMode mTextureMode;
00129 };
00130
00132 class State
00133 {
00134 public:
00135 State()
00136 {
00137 mColor = white;
00138 mPointSize = 5;
00139 mImage = NULL;
00140 mTextureMode = TextureMode_Clamp;
00141 mLogicOp = LO_COPY;
00142 mPointSmoothing= true;
00143 mLineSmoothing = true;
00144 mPolygonSmoothing = false;
00145 mLineWidth = 1.0;
00146 mLineStipple = 0xFFFF;
00147 memset(mPolyStipple, 0xFF, 32*32/8);
00148
00149
00150 mBlendEquationRGB = BE_FUNC_ADD;
00151 mBlendEquationAlpha = BE_FUNC_ADD;
00152
00153 mBlendFactorSrcRGB = BF_SRC_ALPHA;
00154 mBlendFactorDstRGB = BF_ONE_MINUS_SRC_ALPHA;
00155 mBlendFactorSrcAlpha = BF_SRC_ALPHA;
00156 mBlendFactorDstAlpha = BF_ONE_MINUS_SRC_ALPHA;
00157
00158 mAlphaFuncRefValue = 0.0f;
00159 mAlphaFunc = FU_ALWAYS;
00160
00161 mFont = defFontManager()->acquireFont("/font/bitstream-vera/VeraMono.ttf", 10, false);
00162
00163
00164 mColorMask = ivec4(1,1,1,1);
00165
00166 mStencilMask = 0xFFFFFFFF;
00167 mStencilTestEnabled = false;
00168 mStencil_SFail = SO_KEEP;
00169 mStencil_SFail = SO_KEEP;
00170 mStencil_DpFail = SO_KEEP;
00171 mStencil_Function = FU_ALWAYS;
00172 mStencil_RefValue = 0;
00173 mStencil_FunctionMask = ~(unsigned int)0;
00174 }
00175
00176 fvec4 mColor;
00177 int mPointSize;
00178 ref<Image> mImage;
00179 ETextureMode mTextureMode;
00180 ELogicOp mLogicOp;
00181 float mLineWidth;
00182 bool mPointSmoothing;
00183 bool mLineSmoothing;
00184 bool mPolygonSmoothing;
00185 unsigned short mLineStipple;
00186 unsigned char mPolyStipple[32*32/8];
00187 EBlendEquation mBlendEquationRGB;
00188 EBlendEquation mBlendEquationAlpha;
00189 EBlendFactor mBlendFactorSrcRGB;
00190 EBlendFactor mBlendFactorDstRGB;
00191 EBlendFactor mBlendFactorSrcAlpha;
00192 EBlendFactor mBlendFactorDstAlpha;
00193 float mAlphaFuncRefValue;
00194 EFunction mAlphaFunc;
00195 ref<Font> mFont;
00196
00197 ivec4 mColorMask;
00198
00199 bool mStencilTestEnabled;
00200 unsigned int mStencilMask;
00201 EStencilOp mStencil_SFail;
00202 EStencilOp mStencil_DpFail;
00203 EStencilOp mStencil_DpPass;
00204 EFunction mStencil_Function;
00205 int mStencil_RefValue;
00206 unsigned int mStencil_FunctionMask;
00207
00208 bool operator<(const State& other) const
00209 {
00210
00211 if (mColor.r() != other.mColor.r())
00212 return mColor.r() < other.mColor.r();
00213 else
00214 if (mColor.g() != other.mColor.g())
00215 return mColor.g() < other.mColor.g();
00216 else
00217 if (mColor.b() != other.mColor.b())
00218 return mColor.b() < other.mColor.b();
00219 else
00220 if (mColor.a() != other.mColor.a())
00221 return mColor.a() < other.mColor.a();
00222 else
00223 if(mPointSize != other.mPointSize)
00224 return mPointSize < other.mPointSize;
00225 else
00226 if(mImage != other.mImage)
00227 return mImage < other.mImage;
00228 else
00229 if (mTextureMode != other.mTextureMode)
00230 return mTextureMode < other.mTextureMode;
00231 else
00232 if (mPolygonSmoothing != other.mPolygonSmoothing)
00233 return mPolygonSmoothing < other.mPolygonSmoothing;
00234 else
00235 if (mPointSmoothing!= other.mPointSmoothing)
00236 return mPointSmoothing < other.mPointSmoothing;
00237 else
00238 if (mLineSmoothing!= other.mLineSmoothing)
00239 return mLineSmoothing < other.mLineSmoothing;
00240 else
00241 if (mLineWidth != other.mLineWidth)
00242 return mLineWidth < other.mLineWidth;
00243 else
00244 if (mLineStipple != other.mLineStipple)
00245 return mLineStipple < other.mLineStipple;
00246 else
00247 if (mLogicOp != other.mLogicOp)
00248 return mLogicOp < other.mLogicOp;
00249 else
00250 if ( memcmp(mPolyStipple, other.mPolyStipple, 32*32/8) != 0 )
00251 return memcmp(mPolyStipple, other.mPolyStipple, 32*32/8) < 0;
00252 else
00253 if ( mBlendEquationRGB != other.mBlendEquationRGB)
00254 return mBlendEquationRGB < other.mBlendEquationRGB;
00255 else
00256 if ( mBlendEquationAlpha != other.mBlendEquationAlpha)
00257 return mBlendEquationAlpha < other.mBlendEquationAlpha;
00258 else
00259 if ( mBlendFactorSrcRGB != other.mBlendFactorSrcRGB)
00260 return mBlendFactorSrcRGB < other.mBlendFactorSrcRGB;
00261 else
00262 if ( mBlendFactorDstRGB != other.mBlendFactorDstRGB)
00263 return mBlendFactorDstRGB < other.mBlendFactorDstRGB;
00264 else
00265 if ( mBlendFactorSrcAlpha != other.mBlendFactorSrcAlpha)
00266 return mBlendFactorSrcAlpha < other.mBlendFactorSrcAlpha;
00267 else
00268 if ( mBlendFactorDstAlpha != other.mBlendFactorDstAlpha)
00269 return mBlendFactorDstAlpha < other.mBlendFactorDstAlpha;
00270 else
00271 if ( mAlphaFuncRefValue != other.mAlphaFuncRefValue)
00272 return mAlphaFuncRefValue < other.mAlphaFuncRefValue;
00273 else
00274 if ( mAlphaFunc != other.mAlphaFunc)
00275 return mAlphaFunc < other.mAlphaFunc;
00276 else
00277 if ( mFont != other.mFont)
00278 return mFont < other.mFont;
00279 else
00280
00281
00282
00283 if ( mColorMask.r() != other.mColorMask.r())
00284 return mColorMask.r() < other.mColorMask.r();
00285 else
00286 if ( mColorMask.g() != other.mColorMask.g())
00287 return mColorMask.g() < other.mColorMask.g();
00288 else
00289 if ( mColorMask.b() != other.mColorMask.b())
00290 return mColorMask.b() < other.mColorMask.b();
00291 else
00292 if ( mColorMask.a() != other.mColorMask.a())
00293 return mColorMask.a() < other.mColorMask.a();
00294 else
00295 if ( mStencilMask != other.mStencilMask)
00296 return mStencilMask < other.mStencilMask;
00297 else
00298 if ( mStencilTestEnabled != other.mStencilTestEnabled)
00299 return mStencilTestEnabled < other.mStencilTestEnabled;
00300 else
00301 if ( mStencil_SFail != other.mStencil_SFail )
00302 return mStencil_SFail < other.mStencil_SFail;
00303 else
00304 if ( mStencil_DpFail != other.mStencil_DpFail )
00305 return mStencil_DpFail < other.mStencil_DpFail;
00306 else
00307 if ( mStencil_DpPass != other.mStencil_DpPass )
00308 return mStencil_DpPass < other.mStencil_DpPass;
00309 else
00310 if ( mStencil_Function != other.mStencil_Function )
00311 return mStencil_Function < other.mStencil_Function;
00312 else
00313 if ( mStencil_RefValue != other.mStencil_RefValue )
00314 return mStencil_RefValue < other.mStencil_RefValue;
00315 else
00316 if ( mStencil_FunctionMask != other.mStencil_FunctionMask )
00317 return mStencil_FunctionMask < other.mStencil_FunctionMask;
00318 else
00319 return false;
00320 }
00321 };
00322
00323
00324 public:
00325 virtual const char* className() { return "vl::VectorGraphics"; }
00326
00327 VectorGraphics();
00328
00330 const ActorCollection* actors() const { return &mActors; }
00331
00333 ActorCollection* actors() { return &mActors; }
00334
00336 Actor* drawLine(double x1, double y1, double x2, double y2);
00337
00339 Actor* drawLines(const std::vector<dvec2>& ln);
00340
00342 Actor* drawLineStrip(const std::vector<dvec2>& ln);
00343
00345 Actor* drawLineLoop(const std::vector<dvec2>& ln);
00346
00348 Actor* fillPolygon(const std::vector<dvec2>& poly);
00349
00351 Actor* fillTriangles(const std::vector<dvec2>& triangles);
00352
00354 Actor* fillTriangleFan(const std::vector<dvec2>& fan);
00355
00357 Actor* fillTriangleStrip(const std::vector<dvec2>& strip);
00358
00360 Actor* fillQuads(const std::vector<dvec2>& quads);
00361
00363 Actor* fillQuadStrip(const std::vector<dvec2>& quad_strip);
00364
00366 Actor* drawPoint(double x, double y);
00367
00369 Actor* drawPoints(const std::vector<dvec2>& pt);
00370
00372 Actor* drawEllipse(double origx, double origy, double xaxis, double yaxis, int segments = 64);
00373
00375 Actor* fillEllipse(double origx, double origy, double xaxis, double yaxis, int segments = 64);
00376
00378 Actor* drawQuad(double left, double bottom, double right, double top);
00379
00381 Actor* fillQuad(double left, double bottom, double right, double top);
00382
00385 void startDrawing() { clear(); }
00386
00389 void continueDrawing();
00390
00393 void endDrawing(bool release_cache=true);
00394
00396 void clear();
00397
00399 void setColor(const fvec4& color) { mState.mColor = color; }
00400
00402 const fvec4& color() const { return mState.mColor; }
00403
00405 void setPointSize(int size) { mState.mPointSize = size; }
00406
00408 int pointSize() const { return mState.mPointSize; }
00409
00411 void setImage(Image* image) { mState.mImage = image; }
00412
00414 const Image* image() const { return mState.mImage.get(); }
00415
00417 Image* image() { return mState.mImage.get(); }
00418
00420 void setPoint(Image* image) { setImage(image); setPointSize(image->width()); }
00421
00423 void setTextureMode(ETextureMode mode) { mState.mTextureMode = mode; }
00424
00426 ETextureMode textureMode() const { return mState.mTextureMode; }
00427
00429 void setLogicOp(ELogicOp op) { mState.mLogicOp = op; }
00430
00432 ELogicOp logicOp() const { return mState.mLogicOp; }
00433
00435 void setLineWidth(float width) { mState.mLineWidth = width; }
00436
00438 float lineWidth() const { return mState.mLineWidth; }
00439
00441 void setPointSmoothing(bool smooth) { mState.mPointSmoothing = smooth; }
00442
00444 bool pointSmoothing() const { return mState.mPointSmoothing; }
00445
00447 void setLineSmoothing(bool smooth) { mState.mLineSmoothing = smooth; }
00448
00450 bool lineSmoothing() const { return mState.mLineSmoothing; }
00451
00453 void setPolygonSmoothing(bool smooth) { mState.mPolygonSmoothing = smooth; }
00454
00456 bool polygonSmoothing() const { return mState.mPolygonSmoothing; }
00457
00459 void setLineStipple(ELineStipple stipple) ;
00460
00462 void setLineStipple(unsigned short stipple) { mState.mLineStipple = stipple; }
00463
00465 unsigned short lineStipple() const { return mState.mLineStipple; }
00466
00468 void setPolygonStipple(EPolygonStipple stipple);
00469
00471 void setPolygonStipple(unsigned char* stipple) { memcpy(mState.mPolyStipple, stipple, 32*32/8); }
00472
00474 const unsigned char* polygonStipple() const { return mState.mPolyStipple; }
00475
00477 unsigned char* polygonStipple() { return mState.mPolyStipple; }
00478
00480 void setAlphaFunc(EFunction func, float ref_value) { mState.mAlphaFuncRefValue=ref_value; mState.mAlphaFunc=func; }
00481
00483 void getAlphaFunc(EFunction& func, float& ref_value) const { ref_value=mState.mAlphaFuncRefValue; func=mState.mAlphaFunc; }
00484
00486 void setBlendFunc(EBlendFactor src_rgb, EBlendFactor dst_rgb, EBlendFactor src_alpha, EBlendFactor dst_alpha);
00487
00489 void getBlendFunc(EBlendFactor& src_rgb, EBlendFactor& dst_rgb, EBlendFactor& src_alpha, EBlendFactor& dst_alpha) const;
00490
00492 void setBlendEquation( EBlendEquation rgb_eq, EBlendEquation alpha_eq );
00493
00495 void getBlendEquation( EBlendEquation& rgb_eq, EBlendEquation& alpha_eq ) const;
00496
00498 void setColorMask(bool r, bool g, bool b, bool a) { mState.mColorMask = ivec4(r?1:0,g?1:0,b?1:0,a?1:0); }
00499
00501 const ivec4& colorMask() const { return mState.mColorMask; }
00502
00503
00504
00505
00507 void setStencilTestEnabled(bool enabled) { mState.mStencilTestEnabled = enabled; }
00508
00510 bool stencilTestEnabled() const { return mState.mStencilTestEnabled; }
00511
00513 void setStencilMask(unsigned int mask) { mState.mStencilMask = mask; }
00514
00516 unsigned int stencilMask() const { return mState.mStencilMask; }
00517
00519 void setStencilOp(EStencilOp sfail, EStencilOp dpfail, EStencilOp dppass);
00520
00522 void getStencilOp(EStencilOp& sfail, EStencilOp& dpfail, EStencilOp& dppass);
00523
00525 void setStencilFunc(EFunction func, int refval, unsigned int mask);
00526
00528 void getStencilFunc(EFunction& func, int& refval, unsigned int& mask);
00529
00531 void setFont(const String& name, int size, bool smooth=false) { mState.mFont = defFontManager()->acquireFont(name,size,smooth); }
00532
00534 void setFont(const Font* font) { setFont(font->filePath(),font->size(),font->smooth()); }
00535
00537 void setDefaultFont() { setFont(defFontManager()->acquireFont("/font/bitstream-vera/VeraMono.ttf", 10, false)); }
00538
00540 const Font* font() const { return mState.mFont.get(); }
00541
00546 void setScissor(int x, int y, int width, int height)
00547 {
00548 mScissor = resolveScissor(x,y,width,height);
00549 }
00550
00552 const Scissor* scissor() const { return mScissor.get(); }
00553
00555 void removeScissor()
00556 {
00557 mScissor = NULL;
00558 }
00559
00564 Actor* clearColor(const fvec4& color, int x=0, int y=0, int w=-1, int h=-1);
00565
00570 Actor* clearStencil(int clear_val, int x=0, int y=0, int w=-1, int h=-1);
00571
00573 Actor* drawText(Text* text);
00574
00577 Actor* drawText(int x, int y, const String& text, int alignment = AlignBottom|AlignLeft);
00578
00580 Actor* drawText(const String& text, int alignment = AlignBottom|AlignLeft);
00581
00585 Actor* drawActor(Actor* actor, Transform* transform=NULL, bool keep_effect=false);
00586
00589 Actor* drawActorCopy(Actor* actor, Transform* transform=NULL);
00590
00592 const dmat4& matrix() const { return mMatrix; }
00593
00595 void setMatrix(const dmat4& matrix) { mMatrix = matrix; }
00596
00598 void resetMatrix() { mMatrix.setIdentity(); }
00599
00601 void rotate(double deg);
00602
00604 void translate(double x, double y, double z=0.0);
00605
00607 void scale(double x, double y, double z=1.0);
00608
00610 void pushMatrix() { mMatrixStack.push_back(matrix()); }
00611
00613 void popMatrix();
00614
00616 const std::vector<dmat4>& matrixStack() const { return mMatrixStack; }
00617
00619 void pushState();
00620
00622 void popState();
00623
00624
00625
00629 void pushScissor(int x, int y, int w, int h);
00630
00632 void popScissor();
00633
00635 const std::vector< ref<Scissor> >& scissorStack() const { return mScissorStack; }
00636
00638 void setTransform(Transform* transform) { for(int i=0; i<actors()->size(); ++i) actors()->at(i)->setTransform(transform); }
00639
00641 Effect* currentEffect() { return currentEffect(mState); }
00642
00643 private:
00644 void generateQuadsTexCoords(Geometry* geom, const std::vector<dvec2>& points);
00645
00646 void generatePlanarTexCoords(Geometry* geom, const std::vector<dvec2>& points);
00647
00648 void generateLinearTexCoords(Geometry* geom);
00649
00650 ref<Geometry> prepareGeometry(const std::vector<dvec2>& ln);
00651
00652 Scissor* resolveScissor(int x, int y, int width, int height);
00653
00654 Texture* resolveTexture(Image* image);
00655
00656 Effect* currentEffect(const State& vgs);
00657
00658 Actor* addActor(Actor* actor) ;
00659
00660 private:
00661
00662 State mState;
00663 dmat4 mMatrix;
00664 ref<Scissor> mScissor;
00665 std::vector<State> mStateStack;
00666 std::vector<dmat4> mMatrixStack;
00667 std::vector< ref<Scissor> > mScissorStack;
00668
00669 std::map<State, ref<Effect> > mVGToEffectMap;
00670 std::map<ImageState, ref<Texture> > mImageToTextureMap;
00671 std::map<RectI, ref<Scissor> > mRectToScissorMap;
00672 ref<Effect> mDefaultEffect;
00673 ActorCollection mActors;
00674 };
00675
00676 }
00677
00678 #endif