Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SvgGl.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012, The Cinder Project
3  All rights reserved.
4 
5  This code is designed for use with the Cinder C++ library, http://libcinder.org
6 
7  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
8  the following conditions are met:
9 
10  * Redistributions of source code must retain the above copyright notice, this list of conditions and
11  the following disclaimer.
12  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
13  the following disclaimer in the documentation and/or other materials provided with the distribution.
14 
15  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
16  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
17  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
18  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
19  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
22  POSSIBILITY OF SUCH DAMAGE.
23 */
24 
25 
26 #pragma once
27 
28 #include "cinder/gl/gl.h"
29 #include "cinder/gl/Texture.h"
30 #include "cinder/svg/Svg.h"
31 #include "cinder/Triangulate.h"
32 
33 namespace cinder {
34 
35 class SvgRendererGl : public svg::Renderer {
36  public:
37  SvgRendererGl() : svg::Renderer() {
38  mFillStack.push_back( svg::Paint( Color::black() ) );
39  mStrokeStack.push_back( svg::Paint() );
40  mFillOpacityStack.push_back( 1.0f );
41  mStrokeOpacityStack.push_back( 1.0f );
42  mStrokeWidthStack.push_back( 1.0f );
43  glLineWidth( 1.0f );
44  glMatrixMode( GL_MODELVIEW );
45  glPushMatrix();
47  }
48 
50  glPopMatrix();
51  }
52 
53  void pushGroup( const svg::Group &group, float opacity ) {}
54 
55  void drawPath( const svg::Path &path ) {
56  if( ! mFillStack.back().isNone() ) {
59  gl::draw( Triangulator( path.getShape2d() ).calcMesh( winding ) );
60  }
61  if( ! mStrokeStack.back().isNone() ) {
63  gl::draw( path.getShape2d() );
64  }
65  }
66 
67  void drawPolygon( const svg::Polygon &polygon ) {
68  if( ! mFillStack.back().isNone() ) {
71  gl::draw( Triangulator( polygon.getPolyLine() ).calcMesh( winding ) );
72 
73  }
74  if( ! mStrokeStack.back().isNone() ) {
76  gl::draw( polygon.getPolyLine() );
77  }
78  }
79 
80  void drawPolyline( const svg::Polyline &polyline ) {
81  if( ! mFillStack.back().isNone() ) {
84  gl::draw( Triangulator( polyline.getPolyLine() ).calcMesh( winding ) );
85 
86  }
87  if( ! mStrokeStack.back().isNone() ) {
89  gl::draw( polyline.getPolyLine() );
90  }
91  }
92 
93  void drawLine( const svg::Line &line ) {
94  if( ! mStrokeStack.back().isNone() ) {
96  gl::drawLine( line.getPoint1(), line.getPoint2() );
97  }
98  }
99 
100  void drawRect( const svg::Rect &rect ) {
101  if( ! mFillStack.back().isNone() ) {
103  gl::drawSolidRect( rect.getRect() );
104  }
105  if( ! mStrokeStack.back().isNone() ) {
107  gl::drawStrokedRect( rect.getRect() );
108  }
109  }
110 
111  void drawCircle( const svg::Circle &circle ) {
112  if( ! mFillStack.back().isNone() ) {
114  gl::drawSolidCircle( circle.getCenter(), circle.getRadius() );
115  }
116  if( ! mStrokeStack.back().isNone() ) {
118  gl::drawStrokedCircle( circle.getCenter(), circle.getRadius() );
119  }
120  }
121 
122  void drawEllipse( const svg::Ellipse &ellipse ) {
123  if( ! mFillStack.back().isNone() ) {
125  gl::drawSolidEllipse( ellipse.getCenter(), ellipse.getRadiusX(), ellipse.getRadiusY() );
126  }
127  if( ! mStrokeStack.back().isNone() ) {
129  gl::drawStrokedEllipse( ellipse.getCenter(), ellipse.getRadiusX(), ellipse.getRadiusY() );
130  }
131  }
132 
133  void drawImage( const Surface8u &surface, const Rectf &drawRect ) {
134  gl::color( Color::white() );
135  gl::draw( gl::Texture( surface ), drawRect );
136  }
137 
138  void drawTextSpan( const svg::TextSpan &span ) {
139 
140  }
141 
142  void popGroup() {}
143 
144  void pushMatrix( const MatrixAffine2f &m ) {
145  glPushMatrix();
146  glMultMatrixf( Matrix44f( m ) );
147  }
148  void popMatrix() {
149  glPopMatrix();
150  }
151 
152  void pushFill( const svg::Paint &paint ) { mFillStack.push_back( paint ); }
153  void popFill() { mFillStack.pop_back(); }
154  void pushStroke( const svg::Paint &paint ) { mStrokeStack.push_back( paint ); }
155  void popStroke() { mStrokeStack.pop_back(); }
156  void pushFillOpacity( float opacity ) { mFillOpacityStack.push_back( opacity ); }
157  void popFillOpacity() { mFillOpacityStack.pop_back(); }
158  void pushStrokeOpacity( float opacity ) { mStrokeOpacityStack.push_back( opacity ); }
159  void popStrokeOpacity() { mStrokeOpacityStack.pop_back(); }
160 
161  ColorA getCurFillColor() { ColorA result( mFillStack.back().getColor() ); result.a = mFillOpacityStack.back(); return result; }
162  ColorA getCurStrokeColor() { ColorA result( mStrokeStack.back().getColor() ); result.a = mStrokeOpacityStack.back(); return result; }
163 
164 
165  void pushStrokeWidth( float width ) { mStrokeWidthStack.push_back( width ); glLineWidth( width ); }
166  void popStrokeWidth() { mStrokeWidthStack.pop_back(); glLineWidth( mStrokeWidthStack.back() ); }
167  void pushFillRule( svg::FillRule rule ) { mFillRuleStack.push_back( rule ); }
168  void popFillRule() { mFillRuleStack.pop_back(); }
169 
170 
171  std::vector<svg::Paint> mFillStack, mStrokeStack;
173  std::vector<float> mStrokeWidthStack;
174  std::vector<svg::FillRule> mFillRuleStack;
175 };
176 
177 namespace gl {
178 inline void draw( const svg::Doc &svg )
179 {
180  SvgRendererGl renderGl;
181  svg.render( renderGl );
182 }
183 } // namespace gl
184 
185 } // namespace cinder
std::vector< svg::FillRule > mFillRuleStack
Definition: SvgGl.h:174
Represents a group of SVG elements. http://www.w3.org/TR/SVG/struct.html#Groups.
Definition: Svg.h:731
std::vector< svg::Paint > mFillStack
Definition: SvgGl.h:171
SVG Circle element: http://www.w3.org/TR/SVG/shapes.html#CircleElement.
Definition: Svg.h:491
void drawStrokedEllipse(const Vec2f &center, float radiusX, float radiusY, int numSegments=0)
Renders a stroked circle using a line loop. The default value of zero for numSegments automatically d...
Definition: gl.cpp:733
void drawStrokedCircle(const Vec2f &center, float radius, int numSegments=0)
Renders a stroked circle using a line loop. The default value of zero for numSegments automatically d...
Definition: gl.cpp:689
void color(float r, float g, float b)
Sets the current color and the alpha value to 1.0.
Definition: gl.h:174
void pushStrokeOpacity(float opacity)
Definition: SvgGl.h:158
void popGroup()
Definition: SvgGl.h:142
Definition: SvgGl.h:35
void drawImage(const Surface8u &surface, const Rectf &drawRect)
Definition: SvgGl.h:133
Winding
Definition: Triangulate.h:38
void drawSolidCircle(const Vec2f &center, float radius, int numSegments=0)
Renders a solid circle using triangle fans. The default value of zero for numSegments automatically d...
Definition: gl.cpp:666
GLenum GLsizei width
Definition: GLee.h:969
void drawEllipse(const svg::Ellipse &ellipse)
Definition: SvgGl.h:122
void popMatrix()
Definition: SvgGl.h:148
void pushGroup(const svg::Group &group, float opacity)
Definition: SvgGl.h:53
SVG Polyline Element: http://www.w3.org/TR/SVG/shapes.html#PolylineElement.
Definition: Svg.h:611
Vec2f getCenter() const
Definition: Svg.h:517
void drawPolyline(const svg::Polyline &polyline)
Definition: SvgGl.h:80
ColorA getCurFillColor()
Definition: SvgGl.h:161
Converts an arbitrary Shape2d into a TriMesh2d.
Definition: Triangulate.h:36
SVG Ellipse element: http://www.w3.org/TR/SVG/shapes.html#EllipseElement.
Definition: Svg.h:512
void pushStroke(const svg::Paint &paint)
Definition: SvgGl.h:154
#define GL_MODELVIEW
Definition: gldx.h:754
void pushMatrix(const MatrixAffine2f &m)
Definition: SvgGl.h:144
SVG tspan Element. Generally owned by a svg::Text Node. http://www.w3.org/TR/SVG/text.html#TSpanElement.
Definition: Svg.h:672
SVG Rect element: http://www.w3.org/TR/SVG/shapes.html#RectElement.
Definition: Svg.h:572
std::vector< svg::Paint > mStrokeStack
Definition: SvgGl.h:171
void drawSolidEllipse(const Vec2f &center, float radiusX, float radiusY, int numSegments=0)
Renders a solid ellipse using triangle fans. The default value of zero for numSegments automatically ...
Definition: gl.cpp:710
SVG Paint specification for fill or stroke, including solids and gradients.
Definition: Svg.h:147
void drawStrokedRect(const Rectf &rect)
Renders a stroked rectangle.
Definition: gl.cpp:777
void popFillOpacity()
Definition: SvgGl.h:157
ColorA getCurStrokeColor()
Definition: SvgGl.h:162
Represents an OpenGL Texture. Implicitly shared object.
Definition: Texture.h:41
SVG Line element: http://www.w3.org/TR/SVG/shapes.html#LineElement.
Definition: Svg.h:554
void drawPath(const svg::Path &path)
Definition: SvgGl.h:55
void drawTextSpan(const svg::TextSpan &span)
Definition: SvgGl.h:138
float getRadiusY() const
Definition: Svg.h:519
void popStrokeWidth()
Definition: SvgGl.h:166
void popFill()
Definition: SvgGl.h:153
void drawSolidRect(const Rectf &rect, bool textureRectangle=false)
Renders a solid rectangle. Texture coordinates in the range [0,1] are generated unless textureRectang...
Definition: gl.cpp:754
void render(Renderer &renderer) const
Renders the node and its descendants.
Definition: Svg.cpp:916
float getRadiusX() const
Definition: Svg.h:518
void drawLine(const svg::Line &line)
Definition: SvgGl.h:93
const PolyLine2f & getPolyLine() const
Definition: Svg.h:596
void glMultMatrixf(const cinder::Matrix44f &m)
Global overloads for OpenGL free functions to allow the use of Cinder types natively.
Definition: gl.h:409
std::vector< float > mStrokeWidthStack
Definition: SvgGl.h:173
const Shape2d & getShape2d() const
Definition: Svg.h:539
const Rectf & getRect() const
Definition: Svg.h:577
static ColorT< float > white()
Definition: Color.h:186
const Vec2f & getPoint1() const
Definition: Svg.h:559
void pushStrokeWidth(float width)
Definition: SvgGl.h:165
void drawCircle(const svg::Circle &circle)
Definition: SvgGl.h:111
static ColorT< float > black()
Definition: Color.h:181
SVG Polygon Element: http://www.w3.org/TR/SVG/shapes.html#PolygonElement.
Definition: Svg.h:591
boost::geometry::model::polygon< boost::geometry::model::d2::point_xy< double > > polygon
Definition: ConvexHull.cpp:32
SvgRendererGl()
Definition: SvgGl.h:37
std::vector< float > mStrokeOpacityStack
Definition: SvgGl.h:172
Vec2f getCenter() const
Definition: Svg.h:496
std::vector< float > mFillOpacityStack
Definition: SvgGl.h:172
Renderer()
Definition: Svg.h:68
FillRule
Definition: Svg.h:44
Matrix44< float > Matrix44f
Definition: Matrix44.h:1314
T a
Definition: Color.h:216
void popFillRule()
Definition: SvgGl.h:168
void drawRect(const svg::Rect &rect)
Definition: SvgGl.h:100
const PolyLine2f & getPolyLine() const
Definition: Svg.h:616
void popStroke()
Definition: SvgGl.h:155
Definition: Svg.h:44
SVG Path element: http://www.w3.org/TR/SVG/paths.html#PathElement.
Definition: Svg.h:534
float getRadius() const
Definition: Svg.h:497
void drawLine(const Vec2f &start, const Vec2f &end)
Draws a line from start to end.
Definition: gl.cpp:487
Represents an SVG Document. See SVG Document Structure http://www.w3.org/TR/SVG/struct.html.
Definition: Svg.h:781
Definition: Triangulate.h:38
Base class from which Renderers are derived.
Definition: Svg.h:66
GLenum GLenum GLvoid GLvoid GLvoid * span
Definition: GLee.h:1089
const GLfloat * m
Definition: GLee.h:13493
void pushFillRule(svg::FillRule rule)
Definition: SvgGl.h:167
void pushFill(const svg::Paint &paint)
Definition: SvgGl.h:152
GLclampf f
Definition: GLee.h:15307
const Vec2f & getPoint2() const
Definition: Svg.h:560
void pushFillOpacity(float opacity)
Definition: SvgGl.h:156
Definition: Triangulate.h:38
void popStrokeOpacity()
Definition: SvgGl.h:159
void drawPolygon(const svg::Polygon &polygon)
Definition: SvgGl.h:67
~SvgRendererGl()
Definition: SvgGl.h:49
void draw(const class Sphere &sphere, int segments=12)
Renders a solid sphere. segments defines how many segments the sphere is subdivided into...
Definition: gl.cpp:661