File: scanline_boolean.dpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (385 lines) | stat: -rw-r--r-- 8,411 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
//
program
 scanline_boolean ;

{DEFINE AGG_GRAY8 }
{$DEFINE AGG_BGR24 }
{DEFINE AGG_RGB24 }
{DEFINE AGG_BGRA32 }
{DEFINE AGG_RGBA32 }
{DEFINE AGG_ARGB32 }
{DEFINE AGG_ABGR32 }
{DEFINE AGG_RGB565 }
{DEFINE AGG_RGB555 }

uses
 agg_basics ,
 agg_platform_support ,

 agg_ctrl ,
 agg_slider_ctrl ,
 agg_cbox_ctrl ,
 agg_rbox_ctrl ,

 agg_rasterizer_scanline_aa ,
 agg_scanline ,
 agg_scanline_p ,
 agg_scanline_u ,
 agg_scanline_bin ,
 agg_scanline_boolean_algebra ,

 agg_renderer_base ,
 agg_renderer_scanline ,
 agg_render_scanlines ,

 agg_path_storage ,
 agg_ellipse ,
 agg_gamma_functions ,
 interactive_polygon_

{$I pixel_formats.inc }
{$I agg_mode.inc }

const
 flip_y = true;

type
 the_application = object(platform_support )
   m_quad1 ,
   m_quad2 : interactive_polygon;

   m_trans_type : rbox_ctrl;

   m_reset : cbox_ctrl;
   m_mul1  ,
   m_mul2  : slider_ctrl;

   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
   destructor  Destruct;

   procedure on_init; virtual;
   procedure on_draw; virtual;

   procedure on_mouse_move       (x ,y : int; flags : unsigned ); virtual;
   procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
   procedure on_mouse_button_up  (x ,y : int; flags : unsigned ); virtual;

   procedure on_ctrl_change; virtual;

   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;

  end;

{ GENERATE_CIRCLES }
procedure generate_circles(ps : path_storage_ptr; quad : double_ptr; num_circles : unsigned; radius : double );
var
 ell : ellipse;

 i ,j ,n1 ,n2 : unsigned;

begin
 ps.remove_all;
 ell.Construct;

 for i:=0 to 3 do
  begin
   n1:=i * 2;

   if i < 3 then
    n2:=i * 2 + 2
   else
    n2:=0;

   for j:=0 to num_circles - 1 do
    begin
     ell.init(
      double_ptr(ptrcomp(quad ) + n1 * sizeof(double ) )^ +
      (double_ptr(ptrcomp(quad ) + n2 * sizeof(double ) )^ -
       double_ptr(ptrcomp(quad ) + n1 * sizeof(double ) )^ ) *
      j / num_circles ,
      double_ptr(ptrcomp(quad ) + (n1 + 1 ) * sizeof(double ) )^ +
      (double_ptr(ptrcomp(quad ) + (n2 + 1 ) * sizeof(double ) )^ -
       double_ptr(ptrcomp(quad ) + (n1 + 1 ) * sizeof(double ) )^ ) *
      j / num_circles ,
      radius ,radius ,100 );

     ps.add_path(@ell ,0 ,false );

    end;

  end;

end;

{ CONSTRUCT }
constructor the_application.Construct;
begin
 inherited Construct(format_ ,flip_y_ );

 m_quad1.Construct(4 ,5.0 );
 m_quad2.Construct(4 ,5.0 );

 m_trans_type.Construct(420 ,5.0 ,420 + 130.0 ,145.0 ,not flip_y_ );

 m_reset.Construct(350 ,5.0 ,'Reset' ,not flip_y_ );

 m_mul1.Construct(5.0 ,5.0 ,340.0 ,12.0 ,not flip_y_ );
 m_mul2.Construct(5.0 ,20.0 ,340.0 ,27.0 ,not flip_y_ );

 m_trans_type.add_item ('Union' );
 m_trans_type.add_item ('Intersection' );
 m_trans_type.add_item ('Linear XOR' );
 m_trans_type.add_item ('Saddle XOR' );
 m_trans_type.add_item ('Abs Diff XOR' );
 m_trans_type.add_item ('A-B' );
 m_trans_type.add_item ('B-A' );
 m_trans_type.cur_item_(0 );

 add_ctrl(@m_trans_type );
 add_ctrl(@m_reset );
 add_ctrl(@m_mul1 );
 add_ctrl(@m_mul2 );

 m_mul1.value_(1.0 );
 m_mul2.value_(1.0 );
 m_mul1.label_('Opacity1=%.3f' );
 m_mul2.label_('Opacity2=%.3f' );

end;

{ DESTRUCT }
destructor the_application.Destruct;
begin
 inherited Destruct;

 m_quad1.Destruct;
 m_quad2.Destruct;

 m_trans_type.Destruct;
 m_reset.Destruct;
 m_mul1.Destruct;
 m_mul2.Destruct;

end;

{ ON_INIT }
procedure the_application.on_init;
begin
 m_quad1.xn_ptr(0 )^:=50;
 m_quad1.yn_ptr(0 )^:=200 - 20;
 m_quad1.xn_ptr(1 )^:=_width / 2 - 25;
 m_quad1.yn_ptr(1 )^:=200;
 m_quad1.xn_ptr(2 )^:=_width / 2 - 25;
 m_quad1.yn_ptr(2 )^:=_height - 50 - 20;
 m_quad1.xn_ptr(3 )^:=50;
 m_quad1.yn_ptr(3 )^:=_height - 50;

 m_quad2.xn_ptr(0 )^:=_width / 2 + 25;
 m_quad2.yn_ptr(0 )^:=200 - 20;
 m_quad2.xn_ptr(1 )^:=_width - 50;
 m_quad2.yn_ptr(1 )^:=200;
 m_quad2.xn_ptr(2 )^:=_width - 50;
 m_quad2.yn_ptr(2 )^:=_height - 50 - 20;
 m_quad2.xn_ptr(3 )^:=_width / 2 + 25;
 m_quad2.yn_ptr(3 )^:=_height - 50;

end;

{ ON_DRAW }
procedure the_application.on_draw;
var
 pixf : pixel_formats;

 rb : renderer_base;
 r  : renderer_scanline_aa_solid;
 sl : scanline_p8;

 ras  ,
 ras1 ,
 ras2 : rasterizer_scanline_aa;
 rgba : aggclr;

 op : sbool_op_e;
 gm : gamma_multiply;

 ps1 ,
 ps2 : path_storage;

 sl_result ,sl1 ,sl2 : scanline_p8;

 sren : renderer_scanline_aa_solid;

begin
// Initialize structures
 pixfmt(pixf ,rbuf_window );

 rb.Construct(@pixf );
 r.Construct (@rb );

 rgba.ConstrDbl(1 ,1 ,1 );
 rb.clear      (@rgba );

 sl.Construct;
 ras.Construct;
 ras1.Construct;
 ras2.Construct;

// Draw
 op:=sbool_op_e(m_trans_type._cur_item );

 gm.Construct(m_mul1._value );
 ras1.gamma  (@gm );

 gm.Construct(m_mul2._value );
 ras2.gamma  (@gm );

 ras.clip_box(0 ,0 ,_width ,_height );

 ps1.Construct;
 generate_circles(@ps1 ,m_quad1.polygon ,5 ,20 );

 ps2.Construct;
 generate_circles(@ps2 ,m_quad2.polygon ,5 ,20 );

 ras1.filling_rule(fill_even_odd );

// Bottom Layer of Polygon1
 rgba.ConstrInt(240, 255, 200, 100 );
 r.color_      (@rgba );

 ras1.add_path   (@ps1 );
 render_scanlines(@ras1 ,@sl ,@r );

// Bottom Layer of Polygon2
 rgba.ConstrInt(255 ,240 ,240 ,100 );
 r.color_      (@rgba );

 ras2.add_path   (@ps2 );
 render_scanlines(@ras2 ,@sl ,@r );

// Combine shapes
 sl_result.Construct;
 sl1.Construct;
 sl2.Construct;
 sren.Construct(@rb );

 rgba.ConstrInt(0 ,0 ,0 );
 sren.color_   (@rgba );

 sbool_combine_shapes_aa(op ,@ras1 ,@ras2 ,@sl1 ,@sl2 ,@sl_result ,@sren );

// Render the "quad" tools
 rgba.ConstrDbl(0 ,0.3 ,0.5 ,0.6 );
 r.color_      (@rgba );

 ras.add_path    (@m_quad1 );
 render_scanlines(@ras ,@sl ,@r );

 ras.add_path    (@m_quad2 );
 render_scanlines(@ras ,@sl ,@r );

// Render the controls
 render_ctrl(@ras ,@sl ,@r ,@m_trans_type);
 render_ctrl(@ras ,@sl ,@r ,@m_reset);
 render_ctrl(@ras ,@sl ,@r ,@m_mul1);
 render_ctrl(@ras ,@sl ,@r ,@m_mul2);

// Free AGG resources
 ps1.Destruct;
 ps2.Destruct;

 ras2.Destruct;
 ras1.Destruct;

 sl_result.Destruct;
 sl1.Destruct;
 sl2.Destruct;

 ras.Destruct;
 sl.Destruct;

end;

{ ON_MOUSE_MOVE }
procedure the_application.on_mouse_move;
begin
 if flags and mouse_left <> 0 then
  if m_quad1.on_mouse_move(x ,y ) or
     m_quad2.on_mouse_move(x ,y ) then
   force_redraw;

 if flags and mouse_left = 0 then
  on_mouse_button_up(x ,y ,flags );

end;

{ ON_MOUSE_BUTTON_DOWN }
procedure the_application.on_mouse_button_down;
begin
 if flags and mouse_left <> 0 then
  if m_quad1.on_mouse_button_down(x ,y ) or
     m_quad2.on_mouse_button_down(x ,y ) then
   force_redraw;

end;

{ ON_MOUSE_BUTTON_UP }
procedure the_application.on_mouse_button_up;
begin
 if m_quad1.on_mouse_button_up(x ,y ) or
    m_quad2.on_mouse_button_up(x ,y ) then
  force_redraw;

end;

{ ON_CTRL_CHANGE }
procedure the_application.on_ctrl_change;
begin
 if m_reset._status then
  begin
   on_init;
   m_reset.status_(false );
   force_redraw;

  end;

end;

{ ON_KEY }
procedure the_application.on_key;
begin
 if key = key_f1 then
  message_(
   'A new method to perform boolean operations on polygons (Union, Intersection, XOR, '#13 +
   'and Difference). It uses the scanline approach and in typical screen resolutions '#13 +
   'works much faster (about 10 times) than vectorial algorithms like General Polygon '#13 +
   'Clipper. It preserves perfect Anti-Aliasing and besides, can work with translucency. '#13 +
   'There are two XOR operations, Linear XOR and Saddle XOR. The only difference is in '#13 +
   'the formula of XORing of the two cells with Anti-Aliasing. The first one is:'#13#13 +
   'cover = a+b; if(cover > 1) cover = 2.0 - cover;'#13#13 +
   'The second uses the classical "Saddle" formula:'#13#13 +
   'cover = 1.0 - (1.0 - a + a*b) * (1.0 - b + a*b);'#13#13 +
   'The Linear XOR produces more correct intersections and works constistently with the'#13 +
   'scanline rasterizer algorithm. The Saddle XOR works better with semi-transparent polygons.   ' +
   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );

end;

VAR
 app : the_application;

BEGIN
 app.Construct(pix_format ,flip_y );
 app.caption_ ('AGG Example. Scanline Boolean (F1-Help)' );

 if app.init(800 ,600 ,window_resize ) then
  app.run;

 app.Destruct;

END.