1 #include <math.h>
2 #include <stdio.h>
3 #include "agg_basics.h"
4 #include "agg_ellipse.h"
5 #include "agg_gamma_lut.h"
6 #include "agg_rendering_buffer.h"
7 #include "agg_rasterizer_scanline_aa.h"
8 #include "agg_rasterizer_compound_aa.h"
9 #include "agg_conv_curve.h"
10 #include "agg_conv_stroke.h"
11 #include "agg_scanline_u.h"
12 #include "agg_renderer_scanline.h"
13 #include "agg_span_allocator.h"
14 #include "agg_pixfmt_rgba.h"
15 #include "ctrl/agg_slider_ctrl.h"
16 #include "ctrl/agg_cbox_ctrl.h"
17 #include "platform/agg_platform_support.h"
18
19
20 enum flip_y_e { flip_y = true };
21
22
23 //-------------------------------------------------
24 class style_handler
25 {
26 public:
style_handler(const agg::rgba8 * styles,unsigned count)27 style_handler(const agg::rgba8* styles, unsigned count) :
28 m_transparent(0, 0, 0, 0),
29 m_styles(styles),
30 m_count(count)
31 {}
32
is_solid(unsigned style) const33 bool is_solid(unsigned style) const { return true; }
34
color(unsigned style) const35 const agg::rgba8& color(unsigned style) const
36 {
37 if (style < m_count)
38 return m_styles[style];
39
40 return m_transparent;
41 }
42
generate_span(agg::rgba8 * span,int x,int y,unsigned len,unsigned style)43 void generate_span(agg::rgba8* span, int x, int y, unsigned len, unsigned style)
44 {
45 }
46
47 private:
48 agg::rgba8 m_transparent;
49 const agg::rgba8* m_styles;
50 unsigned m_count;
51 };
52
53
54 class the_application : public agg::platform_support
55 {
56 agg::slider_ctrl<agg::rgba8> m_width;
57 agg::slider_ctrl<agg::rgba8> m_alpha1;
58 agg::slider_ctrl<agg::rgba8> m_alpha2;
59 agg::slider_ctrl<agg::rgba8> m_alpha3;
60 agg::slider_ctrl<agg::rgba8> m_alpha4;
61 agg::cbox_ctrl<agg::rgba8> m_invert_order;
62 agg::path_storage m_path;
63
64 public:
the_application(agg::pix_format_e format,bool flip_y)65 the_application(agg::pix_format_e format, bool flip_y) :
66 agg::platform_support(format, flip_y),
67 m_width(180 + 10.0, 5.0, 130 + 300.0, 12, !flip_y),
68 m_alpha1(5, 5, 180, 12, !flip_y),
69 m_alpha2(5, 25, 180, 32, !flip_y),
70 m_alpha3(5, 45, 180, 52, !flip_y),
71 m_alpha4(5, 65, 180, 72, !flip_y),
72 m_invert_order(190, 25, "Invert Z-Order")
73 {
74 add_ctrl(m_width);
75 m_width.range(-20.0, 50.0);
76 m_width.value(10.0);
77 m_width.label("Width=%1.2f");
78
79 add_ctrl(m_alpha1);
80 m_alpha1.range(0, 1);
81 m_alpha1.value(1);
82 m_alpha1.label("Alpha1=%1.3f");
83
84 add_ctrl(m_alpha2);
85 m_alpha2.range(0, 1);
86 m_alpha2.value(1);
87 m_alpha2.label("Alpha2=%1.3f");
88
89 add_ctrl(m_alpha3);
90 m_alpha3.range(0, 1);
91 m_alpha3.value(1);
92 m_alpha3.label("Alpha3=%1.3f");
93
94 add_ctrl(m_alpha4);
95 m_alpha4.range(0, 1);
96 m_alpha4.value(1);
97 m_alpha4.label("Alpha4=%1.3f");
98
99 add_ctrl(m_invert_order);
100 }
101
compose_path()102 void compose_path()
103 {
104 m_path.remove_all();
105 m_path.move_to(28.47, 6.45);
106 m_path.curve3(21.58, 1.12, 19.82, 0.29);
107 m_path.curve3(17.19, -0.93, 14.21, -0.93);
108 m_path.curve3(9.57, -0.93, 6.57, 2.25);
109 m_path.curve3(3.56, 5.42, 3.56, 10.60);
110 m_path.curve3(3.56, 13.87, 5.03, 16.26);
111 m_path.curve3(7.03, 19.58, 11.99, 22.51);
112 m_path.curve3(16.94, 25.44, 28.47, 29.64);
113 m_path.line_to(28.47, 31.40);
114 m_path.curve3(28.47, 38.09, 26.34, 40.58);
115 m_path.curve3(24.22, 43.07, 20.17, 43.07);
116 m_path.curve3(17.09, 43.07, 15.28, 41.41);
117 m_path.curve3(13.43, 39.75, 13.43, 37.60);
118 m_path.line_to(13.53, 34.77);
119 m_path.curve3(13.53, 32.52, 12.38, 31.30);
120 m_path.curve3(11.23, 30.08, 9.38, 30.08);
121 m_path.curve3(7.57, 30.08, 6.42, 31.35);
122 m_path.curve3(5.27, 32.62, 5.27, 34.81);
123 m_path.curve3(5.27, 39.01, 9.57, 42.53);
124 m_path.curve3(13.87, 46.04, 21.63, 46.04);
125 m_path.curve3(27.59, 46.04, 31.40, 44.04);
126 m_path.curve3(34.28, 42.53, 35.64, 39.31);
127 m_path.curve3(36.52, 37.21, 36.52, 30.71);
128 m_path.line_to(36.52, 15.53);
129 m_path.curve3(36.52, 9.13, 36.77, 7.69);
130 m_path.curve3(37.01, 6.25, 37.57, 5.76);
131 m_path.curve3(38.13, 5.27, 38.87, 5.27);
132 m_path.curve3(39.65, 5.27, 40.23, 5.62);
133 m_path.curve3(41.26, 6.25, 44.19, 9.18);
134 m_path.line_to(44.19, 6.45);
135 m_path.curve3(38.72, -0.88, 33.74, -0.88);
136 m_path.curve3(31.35, -0.88, 29.93, 0.78);
137 m_path.curve3(28.52, 2.44, 28.47, 6.45);
138 m_path.close_polygon();
139
140 m_path.move_to(28.47, 9.62);
141 m_path.line_to(28.47, 26.66);
142 m_path.curve3(21.09, 23.73, 18.95, 22.51);
143 m_path.curve3(15.09, 20.36, 13.43, 18.02);
144 m_path.curve3(11.77, 15.67, 11.77, 12.89);
145 m_path.curve3(11.77, 9.38, 13.87, 7.06);
146 m_path.curve3(15.97, 4.74, 18.70, 4.74);
147 m_path.curve3(22.41, 4.74, 28.47, 9.62);
148 m_path.close_polygon();
149 }
150
151
152
on_draw()153 virtual void on_draw()
154 {
155 typedef agg::renderer_base<agg::pixfmt_bgra32> ren_base;
156 typedef agg::renderer_base<agg::pixfmt_bgra32_pre> ren_base_pre;
157
158 agg::gamma_lut<agg::int8u, agg::int8u> lut(2.0);
159
160 agg::pixfmt_bgra32 pixf(rbuf_window());
161 ren_base renb(pixf);
162
163 agg::pixfmt_bgra32_pre pixf_pre(rbuf_window());
164 ren_base_pre renb_pre(pixf_pre);
165
166 // Clear the window with a gradient
167 agg::pod_vector<agg::rgba8> gr(pixf_pre.width());
168 unsigned i;
169 for(i = 0; i < pixf.width(); i++)
170 {
171 gr.add(agg::rgba8(255, 255, 0).gradient(agg::rgba8(0, 255, 255),
172 double(i) / pixf.width()));
173 }
174 for(i = 0; i < pixf.height(); i++)
175 {
176 renb.copy_color_hspan(0, i, pixf.width(), &gr[0]);
177 }
178 pixf.apply_gamma_dir(lut);
179
180
181 agg::rasterizer_scanline_aa<> ras;
182 agg::rasterizer_compound_aa<agg::rasterizer_sl_clip_dbl> rasc;
183 agg::scanline_u8 sl;
184 agg::span_allocator<agg::rgba8> alloc;
185
186 // Draw two triangles
187 ras.move_to_d(0, 0);
188 ras.line_to_d(width(), 0);
189 ras.line_to_d(width(), height());
190 agg::render_scanlines_aa_solid(ras, sl, renb,
191 agg::rgba8(lut.dir(0),
192 lut.dir(100),
193 lut.dir(0)));
194
195 ras.move_to_d(0, 0);
196 ras.line_to_d(0, height());
197 ras.line_to_d(width(), 0);
198 agg::render_scanlines_aa_solid(ras, sl, renb,
199 agg::rgba8(lut.dir(0),
200 lut.dir(100),
201 lut.dir(100)));
202
203 agg::trans_affine mtx;
204 mtx *= agg::trans_affine_scaling(4.0);
205 mtx *= agg::trans_affine_translation(150, 100);
206
207 agg::conv_transform<agg::path_storage> trans(m_path, mtx);
208 agg::conv_curve<agg::conv_transform<agg::path_storage> > curve(trans);
209
210 agg::conv_stroke
211 <agg::conv_curve
212 <agg::conv_transform
213 <agg::path_storage> > > stroke(curve);
214
215 compose_path();
216
217 agg::rgba8 styles[4];
218
219 if(m_invert_order.status())
220 {
221 rasc.layer_order(agg::layer_inverse);
222 }
223 else
224 {
225 rasc.layer_order(agg::layer_direct);
226 }
227
228 styles[3] = agg::rgba8(lut.dir(255),
229 lut.dir(0),
230 lut.dir(108),
231 200).premultiply();
232
233 styles[2] = agg::rgba8(lut.dir(51),
234 lut.dir(0),
235 lut.dir(151),
236 180).premultiply();
237
238 styles[1] = agg::rgba8(lut.dir(143),
239 lut.dir(90),
240 lut.dir(6),
241 200).premultiply();
242
243 styles[0] = agg::rgba8(lut.dir(0),
244 lut.dir(0),
245 lut.dir(255),
246 220).premultiply();
247
248 style_handler sh(styles, 4);
249
250 stroke.width(m_width.value());
251
252 rasc.reset();
253 rasc.master_alpha(3, m_alpha1.value());
254 rasc.master_alpha(2, m_alpha2.value());
255 rasc.master_alpha(1, m_alpha3.value());
256 rasc.master_alpha(0, m_alpha4.value());
257
258 agg::ellipse ell(220.0, 180.0, 120.0, 10.0, 128, false);
259 agg::conv_stroke<agg::ellipse> str_ell(ell);
260 str_ell.width(m_width.value() / 2);
261
262 rasc.styles(3, -1);
263 rasc.add_path(str_ell);
264
265 rasc.styles(2, -1);
266 rasc.add_path(ell);
267
268 rasc.styles(1, -1);
269 rasc.add_path(stroke);
270
271 rasc.styles(0, -1);
272 rasc.add_path(curve);
273
274 agg::render_scanlines_compound_layered(rasc, sl, renb_pre, alloc, sh);
275 agg::render_ctrl(ras, sl, renb, m_width);
276 agg::render_ctrl(ras, sl, renb, m_alpha1);
277 agg::render_ctrl(ras, sl, renb, m_alpha2);
278 agg::render_ctrl(ras, sl, renb, m_alpha3);
279 agg::render_ctrl(ras, sl, renb, m_alpha4);
280 agg::render_ctrl(ras, sl, renb, m_invert_order);
281
282 pixf.apply_gamma_inv(lut);
283 }
284
285 };
286
287
288
agg_main(int argc,char * argv[])289 int agg_main(int argc, char* argv[])
290 {
291 the_application app(agg::pix_format_bgra32, flip_y);
292 app.caption("AGG Example. Compound Rasterizer -- Geometry Flattening");
293
294 if(app.init(440, 330, 0))
295 {
296 return app.run();
297 }
298 return 1;
299 }
300