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
|
#include <stdio.h>
#include "agg_basics.h"
#include "agg_rendering_buffer.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_scanline_u.h"
#include "agg_renderer_scanline.h"
#include "agg_span_gradient.h"
#include "agg_span_gradient_alpha.h"
#include "agg_span_interpolator_linear.h"
#include "agg_span_allocator.h"
#include "agg_span_converter.h"
#include "agg_ellipse.h"
#include "agg_pixfmt_rgb.h"
#include "agg_vcgen_stroke.h"
#include "platform/agg_platform_support.h"
#include "ctrl/agg_spline_ctrl.h"
enum flip_y_e { flip_y = true };
#define pix_format agg::pix_format_bgr24
typedef agg::pixfmt_bgr24 pixfmt_type;
typedef pixfmt_type::color_type color_type;
typedef color_type::value_type color_value_type;
class the_application : public agg::platform_support
{
double m_x[3];
double m_y[3];
double m_dx;
double m_dy;
int m_idx;
agg::spline_ctrl<color_type> m_alpha;
public:
the_application(agg::pix_format_e format, bool flip_y) :
agg::platform_support(format, flip_y),
m_idx(-1),
m_alpha(2, 2, 200, 30, 6, !flip_y)
{
m_x[0] = 257; m_y[0] = 60;
m_x[1] = 369; m_y[1] = 170;
m_x[2] = 143; m_y[2] = 310;
m_alpha.point(0, 0.0, 0.0);
m_alpha.point(1, 1.0/5.0, 1.0 - 4.0/5.0);
m_alpha.point(2, 2.0/5.0, 1.0 - 3.0/5.0);
m_alpha.point(3, 3.0/5.0, 1.0 - 2.0/5.0);
m_alpha.point(4, 4.0/5.0, 1.0 - 1.0/5.0);
m_alpha.point(5, 1.0, 1.0);
m_alpha.update_spline();
add_ctrl(m_alpha);
}
// A simple function to form the gradient color array
// consisting of 3 colors, "begin", "middle", "end"
//---------------------------------------------------
template<class ColorArrayT>
static void fill_color_array(ColorArrayT& array,
color_type begin,
color_type middle,
color_type end)
{
unsigned i;
for(i = 0; i < 128; ++i)
{
array[i] = begin.gradient(middle, i / 128.0);
}
for(; i < 256; ++i)
{
array[i] = middle.gradient(end, (i - 128) / 128.0);
}
}
virtual void on_draw()
{
typedef agg::renderer_base<pixfmt_type> base_ren_type;
pixfmt_type pf(rbuf_window());
base_ren_type ren_base(pf);
ren_base.clear(agg::rgba(1,1,1));
agg::scanline_u8 sl;
agg::rasterizer_scanline_aa<> ras;
// Draw some background
agg::ellipse ell;
srand(1234);
unsigned i;
unsigned w = unsigned(width());
unsigned h = unsigned(height());
for(i = 0; i < 100; i++)
{
ell.init(rand() % w, rand() % h, rand() % 60 + 5, rand() % 60 + 5, 50);
ras.add_path(ell);
agg::render_scanlines_aa_solid(ras, sl, ren_base,
agg::rgba(rand() / double(RAND_MAX),
rand() / double(RAND_MAX),
rand() / double(RAND_MAX),
rand() / double(RAND_MAX) / 2.0));
}
double parallelogram[6];
parallelogram[0] = m_x[0];
parallelogram[1] = m_y[0];
parallelogram[2] = m_x[1];
parallelogram[3] = m_y[1];
parallelogram[4] = m_x[2];
parallelogram[5] = m_y[2];
// Gradient shape function (linear, radial, custom, etc)
//-----------------
typedef agg::gradient_circle gradient_func_type;
// Alpha gradient shape function (linear, radial, custom, etc)
//-----------------
typedef agg::gradient_xy gradient_alpha_func_type;
// Span interpolator. This object is used in all span generators
// that operate with transformations during iterating of the spans,
// for example, image transformers use the interpolator too.
//-----------------
typedef agg::span_interpolator_linear<> interpolator_type;
// Span allocator is an object that allocates memory for
// the array of colors that will be used to render the
// color spans. One object can be shared between different
// span generators.
//-----------------
typedef agg::span_allocator<color_type> span_allocator_type;
// Gradient colors array adaptor
//-----------------
typedef agg::pod_auto_array<color_type, 256> gradient_colors_type;
// Finally, the gradient span generator working with the color_type
// color type.
//-----------------
typedef agg::span_gradient<color_type,
interpolator_type,
gradient_func_type,
gradient_colors_type> span_gradient_type;
// Gradient alpha array adaptor
//-----------------
typedef agg::pod_auto_array<color_value_type, 256> gradient_alpha_type;
// The alpha gradient span converter working with the color_type
// color type.
//-----------------
typedef agg::span_gradient_alpha<color_type,
interpolator_type,
gradient_alpha_func_type,
gradient_alpha_type> span_gradient_alpha_type;
// Span converter type
//-----------------
typedef agg::span_converter<span_gradient_type,
span_gradient_alpha_type> span_conv_type;
// The gradient objects declarations
//----------------
gradient_func_type gradient_func; // The gradient function
gradient_alpha_func_type alpha_func; // The gradient function
agg::trans_affine gradient_mtx; // Gradient affine transformer
agg::trans_affine alpha_mtx; // Alpha affine transformer
interpolator_type span_interpolator(gradient_mtx); // Span gradient interpolator
interpolator_type span_interpolator_alpha(alpha_mtx); // Span alpha interpolator
span_allocator_type span_allocator; // Span Allocator
gradient_colors_type color_array; // The gradient colors
// Declare the gradient span itself.
// The last two arguments are so called "d1" and "d2"
// defining two distances in pixels, where the gradient starts
// and where it ends. The actual meaning of "d1" and "d2" depands
// on the gradient function.
//----------------
span_gradient_type span_gradient(span_interpolator,
gradient_func,
color_array,
0, 150);
// Declare the gradient span itself.
// The last two arguments are so called "d1" and "d2"
// defining two distances in pixels, where the gradient starts
// and where it ends. The actual meaning of "d1" and "d2" depands
// on the gradient function.
//----------------
gradient_alpha_type alpha_array;
span_gradient_alpha_type span_gradient_alpha(span_interpolator_alpha,
alpha_func,
alpha_array,
0, 100);
// Span converter declaration
span_conv_type span_conv(span_gradient, span_gradient_alpha);
// Finally we can draw a circle.
//----------------
gradient_mtx *= agg::trans_affine_scaling(0.75, 1.2);
gradient_mtx *= agg::trans_affine_rotation(-agg::pi/3.0);
gradient_mtx *= agg::trans_affine_translation(width()/2, height()/2);
gradient_mtx.invert();
alpha_mtx.parl_to_rect(parallelogram, -100, -100, 100, 100);
fill_color_array(color_array,
agg::rgba(0, 0.19, 0.19),
agg::rgba(0.7, 0.7, 0.19),
agg::rgba(0.31, 0, 0));
// Fill Alpha array
//----------------
for(i = 0; i < 256; i++)
{
alpha_array[i] = color_value_type(m_alpha.value(i / 255.0) * double(color_type::base_mask));
}
ell.init(width()/2, height()/2, 150, 150, 100);
ras.add_path(ell);
// Render the circle with gradient plus alpha-gradient
agg::render_scanlines_aa(ras, sl, ren_base, span_allocator, span_conv);
// Draw the control points and the parallelogram
//-----------------
agg::rgba color_pnt(0, 0.4, 0.4, 0.31);
ell.init(m_x[0], m_y[0], 5, 5, 20);
ras.add_path(ell);
agg::render_scanlines_aa_solid(ras, sl, ren_base, color_pnt);
ell.init(m_x[1], m_y[1], 5, 5, 20);
ras.add_path(ell);
agg::render_scanlines_aa_solid(ras, sl, ren_base, color_pnt);
ell.init(m_x[2], m_y[2], 5, 5, 20);
ras.add_path(ell);
agg::render_scanlines_aa_solid(ras, sl, ren_base, color_pnt);
agg::vcgen_stroke stroke;
stroke.add_vertex(m_x[0], m_y[0], agg::path_cmd_move_to);
stroke.add_vertex(m_x[1], m_y[1], agg::path_cmd_line_to);
stroke.add_vertex(m_x[2], m_y[2], agg::path_cmd_line_to);
stroke.add_vertex(m_x[0]+m_x[2]-m_x[1], m_y[0]+m_y[2]-m_y[1], agg::path_cmd_line_to);
stroke.add_vertex(0, 0, agg::path_cmd_end_poly | agg::path_flags_close);
ras.add_path(stroke);
agg::render_scanlines_aa_solid(ras, sl, ren_base, agg::rgba(0, 0, 0));
agg::render_ctrl(ras, sl, ren_base, m_alpha);
}
virtual void on_mouse_button_down(int x, int y, unsigned flags)
{
unsigned i;
if(flags & agg::mouse_left)
{
for (i = 0; i < 3; i++)
{
if(sqrt( (x-m_x[i]) * (x-m_x[i]) + (y-m_y[i]) * (y-m_y[i]) ) < 10.0)
{
m_dx = x - m_x[i];
m_dy = y - m_y[i];
m_idx = i;
break;
}
}
if(i == 3)
{
if(agg::point_in_triangle(m_x[0], m_y[0],
m_x[1], m_y[1],
m_x[2], m_y[2],
x, y))
{
m_dx = x - m_x[0];
m_dy = y - m_y[0];
m_idx = 3;
}
}
}
}
virtual void on_mouse_move(int x, int y, unsigned flags)
{
if(flags & agg::mouse_left)
{
if(m_idx == 3)
{
double dx = x - m_dx;
double dy = y - m_dy;
m_x[1] -= m_x[0] - dx;
m_y[1] -= m_y[0] - dy;
m_x[2] -= m_x[0] - dx;
m_y[2] -= m_y[0] - dy;
m_x[0] = dx;
m_y[0] = dy;
force_redraw();
return;
}
if(m_idx >= 0)
{
m_x[m_idx] = x - m_dx;
m_y[m_idx] = y - m_dy;
force_redraw();
}
}
else
{
on_mouse_button_up(x, y, flags);
}
}
virtual void on_mouse_button_up(int x, int y, unsigned flags)
{
m_idx = -1;
}
virtual void on_key(int x, int y, unsigned key, unsigned flags)
{
double dx = 0;
double dy = 0;
switch(key)
{
case agg::key_left: dx = -0.1; break;
case agg::key_right: dx = 0.1; break;
case agg::key_up: dy = 0.1; break;
case agg::key_down: dy = -0.1; break;
}
m_x[0] += dx;
m_y[0] += dy;
m_x[1] += dx;
m_y[1] += dy;
force_redraw();
}
};
int agg_main(int argc, char* argv[])
{
the_application app(pix_format, flip_y);
app.caption("AGG Example. Alpha channel gradient");
if(app.init(400, 320, agg::window_resize))
{
return app.run();
}
return 1;
}
|