2
// AggPas 2.4 RM3 Demo application
3
// Note: Press F1 key on run to see more info about this demo
5
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
22
agg_platform_support ,
27
agg_rasterizer_scanline_aa ,
33
agg_renderer_scanline ,
34
agg_render_scanlines ,
39
agg_span_gouraud_rgba ,
40
agg_span_gouraud_gray ,
45
{$I pixel_formats.inc }
52
the_application = object(platform_support )
54
m_y : array[0..2 ] of double;
63
m_alpha : slider_ctrl;
65
constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
68
procedure render_gouraud(sl : scanline_ptr; ras : rasterizer_scanline_aa_ptr );
70
procedure on_draw; virtual;
72
procedure on_mouse_move (x ,y : int; flags : unsigned ); virtual;
73
procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
74
procedure on_mouse_button_up (x ,y : int; flags : unsigned ); virtual;
76
procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
81
constructor the_application.Construct;
83
inherited Construct(format_ ,flip_y_ );
85
m_dilation.Construct(5 ,5 ,400 - 5 ,11 ,not flip_y_ );
86
m_gamma.Construct (5 ,5 + 15 ,400 - 5 ,11 + 15 ,not flip_y_ );
87
m_alpha.Construct (5 ,5 + 30 ,400 - 5 ,11 + 30 ,not flip_y_ );
91
m_x[0 ]:=57; m_y[0 ]:=60;
92
m_x[1 ]:=369; m_y[1 ]:=170;
93
m_x[2 ]:=143; m_y[2 ]:=310;
95
add_ctrl(@m_dilation );
99
m_dilation.label_('Dilation=%3.2f' );
100
m_gamma.label_ ('Linear gamma=%3.2f' );
101
m_alpha.label_ ('Opacity=%3.2f' );
103
m_dilation.value_(0.175 );
104
m_gamma.value_ (0.809 );
105
m_alpha.value_ (1.0 );
110
destructor the_application.Destruct;
121
procedure the_application.render_gouraud;
123
alpha ,brc ,d ,xc ,yc ,x1 ,y1 ,x2 ,y2 ,x3 ,y3 : double;
128
span_gen : span_gouraud_gray;
131
span_gen : span_gouraud_rgba;
135
ren_base : renderer_base;
136
span_alloc : span_allocator;
137
ren_gouraud : renderer_scanline_aa;
139
gm_li : gamma_linear;
146
alpha:=m_alpha._value;
149
// Initialize structures
150
pixfmt(pf ,rbuf_window );
152
ren_base.Construct (@pf );
153
span_alloc.Construct;
154
span_gen.Construct (@span_alloc );
155
ren_gouraud.Construct(@ren_base ,@span_gen );
157
gm_li.Construct(0.0 ,m_gamma._value );
160
d:=m_dilation._value;
163
{ rgba.ConstrDbl (1 ,0 ,0 ,alpha );
164
rgbb.ConstrDbl (0 ,1 ,0 ,alpha );
165
rgbc.ConstrDbl (0 ,0 ,1 ,alpha );
166
span_gen.colors (@rgba ,@rgbb ,@rgbc );
167
span_gen.triangle(m_x[0 ] ,m_y[0 ] ,m_x[1 ] ,m_y[1 ] ,m_x[2 ] ,m_y[2 ] ,d );
169
ras.add_path (@span_gen);
170
render_scanlines(ras ,sl ,@ren_gouraud );{}
173
xc:=(m_x[0 ] + m_x[1 ] + m_x[2 ] ) / 3.0;
174
yc:=(m_y[0 ] + m_y[1 ] + m_y[2 ] ) / 3.0;
176
x1:=(m_x[1 ] + m_x[0 ] ) / 2 - (xc - (m_x[1 ] + m_x[0 ] ) / 2 );
177
y1:=(m_y[1 ] + m_y[0 ] ) / 2 - (yc - (m_y[1 ] + m_y[0 ] ) / 2 );
179
x2:=(m_x[2 ] + m_x[1 ] ) / 2 - (xc - (m_x[2 ] + m_x[1 ] ) / 2 );
180
y2:=(m_y[2 ] + m_y[1 ] ) / 2 - (yc - (m_y[2 ] + m_y[1 ] ) / 2 );
182
x3:=(m_x[0 ] + m_x[2 ] ) / 2 - (xc - (m_x[0 ] + m_x[2 ] ) / 2 );
183
y3:=(m_y[0 ] + m_y[2 ] ) / 2 - (yc - (m_y[0 ] + m_y[2 ] ) / 2 );
185
rgba.ConstrDbl (1 ,0 ,0 ,alpha );
186
rgbb.ConstrDbl (0 ,1 ,0 ,alpha );
187
rgbc.ConstrDbl (brc ,brc ,brc ,alpha );
188
span_gen.colors_ (@rgba ,@rgbb ,@rgbc );
189
span_gen.triangle(m_x[0 ] ,m_y[0 ] ,m_x[1 ] ,m_y[1 ] ,xc ,yc ,d );
191
ras.add_path (@span_gen );
192
render_scanlines(ras ,sl ,@ren_gouraud );
194
rgba.ConstrDbl (0 ,1 ,0 ,alpha );
195
rgbb.ConstrDbl (0 ,0 ,1 ,alpha );
196
rgbc.ConstrDbl (brc ,brc ,brc ,alpha );
197
span_gen.colors_ (@rgba ,@rgbb ,@rgbc );
198
span_gen.triangle(m_x[1 ] ,m_y[1 ] ,m_x[2 ] ,m_y[2 ] ,xc ,yc ,d );
200
ras.add_path (@span_gen );
201
render_scanlines(ras ,sl ,@ren_gouraud );
203
rgba.ConstrDbl (0 ,0 ,1 ,alpha );
204
rgbb.ConstrDbl (1 ,0 ,0 ,alpha );
205
rgbc.ConstrDbl (brc ,brc ,brc ,alpha );
206
span_gen.colors_ (@rgba ,@rgbb ,@rgbc );
207
span_gen.triangle(m_x[2 ] ,m_y[2 ] ,m_x[0 ] ,m_y[0 ] ,xc ,yc ,d );
209
ras.add_path (@span_gen );
210
render_scanlines(ras ,sl ,@ren_gouraud );
214
rgba.ConstrDbl (1 ,0 ,0 ,alpha );
215
rgbb.ConstrDbl (0 ,1 ,0 ,alpha );
216
rgbc.ConstrDbl (brc ,brc ,brc ,alpha );
217
span_gen.colors_ (@rgba ,@rgbb ,@rgbc );
218
span_gen.triangle(m_x[0 ] ,m_y[0 ] ,m_x[1 ] ,m_y[1 ] ,x1 ,y1 ,d );
220
ras.add_path (@span_gen );
221
render_scanlines(ras ,sl ,@ren_gouraud );
223
rgba.ConstrDbl (0 ,1 ,0 ,alpha );
224
rgbb.ConstrDbl (0 ,0 ,1 ,alpha );
225
rgbc.ConstrDbl (brc ,brc ,brc ,alpha );
226
span_gen.colors_ (@rgba ,@rgbb ,@rgbc );
227
span_gen.triangle(m_x[1 ] ,m_y[1 ] ,m_x[2 ] ,m_y[2 ] ,x2 ,y2 ,d );
229
ras.add_path (@span_gen);
230
render_scanlines(ras ,sl ,@ren_gouraud );
232
rgba.ConstrDbl (0 ,0 ,1 ,alpha );
233
rgbb.ConstrDbl (1 ,0 ,0 ,alpha );
234
rgbc.ConstrDbl (brc ,brc ,brc ,alpha );
235
span_gen.colors_ (@rgba ,@rgbb ,@rgbc );
236
span_gen.triangle(m_x[2 ] ,m_y[2 ] ,m_x[0 ] ,m_y[0 ] ,x3 ,y3 ,d );
238
ras.add_path (@span_gen );
239
render_scanlines(ras ,sl ,@ren_gouraud );{}
241
// Free AGG resources
247
procedure the_application.on_draw;
251
ren_base : renderer_base;
252
ren_solid : renderer_scanline_aa_solid;
254
ras : rasterizer_scanline_aa;
258
gm_no : vertex_source;
261
// Initialize structures
262
pixfmt(pf ,rbuf_window );
264
ren_base.Construct (@pf );
265
ren_solid.Construct(@ren_base );
267
rgba.ConstrDbl(1 ,1 ,1 );
268
ren_base.clear(@rgba );
274
render_gouraud(@sl ,@ras );
276
// Render the controls
280
render_ctrl(@ras ,@sl ,@ren_solid ,@m_dilation );
281
render_ctrl(@ras ,@sl ,@ren_solid ,@m_gamma );
282
render_ctrl(@ras ,@sl ,@ren_solid ,@m_alpha );
284
// Free AGG resources
291
procedure the_application.on_mouse_move;
296
if flags and mouse_left <> 0 then
303
m_x[1 ]:=m_x[1 ] - (m_x[0 ] - dx );
304
m_y[1 ]:=m_y[1 ] - (m_y[0 ] - dy );
305
m_x[2 ]:=m_x[2 ] - (m_x[0 ] - dx );
306
m_y[2 ]:=m_y[2 ] - (m_y[0 ] - dy );
317
m_x[m_idx ]:=x - m_dx;
318
m_y[m_idx ]:=y - m_dy;
326
on_mouse_button_up(x ,y ,flags );
330
{ ON_MOUSE_BUTTON_DOWN }
331
procedure the_application.on_mouse_button_down;
336
ras : rasterizer_scanline_aa;
337
buf : array[0..99 ] of char;
340
if flags and mouse_right <> 0 then
348
render_gouraud(@sl ,@ras );
350
sprintf (@buf[0 ] ,'Time=%2.2f ms' ,elapsed_time );
358
if flags and mouse_left <> 0 then
364
if Sqrt((x-m_x[i ] ) * (x-m_x[i ] ) + (y-m_y[i ] ) * (y-m_y[i ] ) ) < 10.0 then
379
if point_in_triangle(m_x[0 ] ,m_y[0 ] ,m_x[1 ] ,m_y[1 ] ,m_x[2 ] ,m_y[2 ] ,x ,y ) then
391
{ ON_MOUSE_BUTTON_UP }
392
procedure the_application.on_mouse_button_up;
399
procedure the_application.on_key;
415
m_x[0 ]:=m_x[0 ] + dx;
416
m_y[0 ]:=m_y[0 ] + dy;
417
m_x[1 ]:=m_x[1 ] + dx;
418
m_y[1 ]:=m_y[1 ] + dy;
424
'Gouraud shading. It''s a simple method of interpolating colors in a triangle. '#13 +
425
'There''s no "cube" drawn, there''re just 6 triangles. You define a triangle '#13 +
426
'and colors in its vertices. When rendering, the colors will be linearly interpolated. '#13 +
427
'But there''s a problem that appears when drawing adjacent triangles with Anti-Aliasing.'#13 +
428
'Anti-Aliased polygons do not "dock" to each other correctly, there visual artifacts '#13 +
429
'at the edges appear. I call it "the problem of adjacent edges". AGG has a simple '#13 +
430
'mechanism that allows you to get rid of the artifacts, just dilating the polygons '#13 +
431
'and/or changing the gamma-correction value. But it''s tricky, because the values '#13 +
432
'depend on the opacity of the polygons. In this example you can change the opacity, '#13 +
433
'the dilation value and gamma.'#13#13 +
434
'How to play with:'#13#13 +
435
'Use the left mouse button to drag the Red, Green and Blue corners of the "cube".'#13 +
436
'Use the right mouse button to issue a performance test (100x).' +
437
#13#13'Note: F2 key saves current "screenshot" file in this demo''s directory. ' );
442
app : the_application;
445
app.Construct(pix_format ,flip_y );
446
app.caption_ ('AGG Example. Gouraud Shading (F1-Help)' );
448
if app.init(400 ,320 ,window_resize ) then
b'\\ No newline at end of file'