void src_shape(RenBase& rbase, color c1, color c2, double x1, double y1, double x2, double y2) { typedef RenBase renderer_base_type; typedef agg::gradient_x gradient_func_type; typedef agg::gradient_linear_color<color> color_func_type; typedef agg::span_interpolator_linear<> interpolator_type; typedef agg::span_allocator<color> span_allocator_type; typedef agg::span_gradient<color, interpolator_type, gradient_func_type, color_func_type> span_gradient_type; gradient_func_type gradient_func; // The gradient function agg::trans_affine gradient_mtx = gradient_affine(x1, y1, x2, y2, 100); interpolator_type span_interpolator(gradient_mtx); // Span interpolator span_allocator_type span_allocator; // Span Allocator color_func_type color_func(c1, c2); span_gradient_type span_gradient(span_interpolator, gradient_func, color_func, 0, 100); agg::rasterizer_scanline_aa<> ras; agg::scanline_u8 sl; agg::rounded_rect shape(x1, y1, x2, y2, 40); // agg::ellipse shape((x1+x2)/2, (y1+y2)/2, fabs(x2-x1)/2, fabs(y2-y1)/2, 100); ras.add_path(shape); agg::render_scanlines_aa(ras, sl, rbase, span_allocator, span_gradient); }
void radial_shape(RenBase& rbase, const ColorRamp& colors, double x1, double y1, double x2, double y2) { typedef RenBase renderer_base_type; typedef agg::gradient_radial gradient_func_type; typedef ColorRamp color_func_type; typedef agg::span_interpolator_linear<> interpolator_type; typedef agg::span_allocator<color> span_allocator_type; typedef agg::span_gradient<color, interpolator_type, gradient_func_type, color_func_type> span_gradient_type; gradient_func_type gradient_func; // The gradient function agg::trans_affine gradient_mtx; interpolator_type span_interpolator(gradient_mtx); // Span interpolator span_allocator_type span_allocator; // Span Allocator span_gradient_type span_gradient(span_interpolator, gradient_func, colors, 0, 100); double cx = (x1 + x2) / 2.0; double cy = (y1 + y2) / 2.0; double r = 0.5 * (((x2 - x1) < (y2 - y1)) ? (x2 - x1) : (y2 - y1)); gradient_mtx *= agg::trans_affine_scaling(r / 100.0); gradient_mtx *= agg::trans_affine_translation(cx, cy); gradient_mtx *= trans_affine_resizing(); gradient_mtx.invert(); agg::ellipse ell(cx, cy, r, r, 100); agg::conv_transform<agg::ellipse> trans(ell, trans_affine_resizing()); m_ras.add_path(trans); agg::render_scanlines_aa(m_ras, m_sl, rbase, span_allocator, span_gradient); }
void circle(RenBase& rbase, color c1, color c2, double x1, double y1, double x2, double y2, double shadow_alpha) { typedef RenBase renderer_base_type; typedef agg::gradient_x gradient_func_type; typedef agg::gradient_linear_color<color> color_func_type; typedef agg::span_interpolator_linear<> interpolator_type; typedef agg::span_allocator<color> span_allocator_type; typedef agg::span_gradient<color, interpolator_type, gradient_func_type, color_func_type> span_gradient_type; gradient_func_type gradient_func; // The gradient function agg::trans_affine gradient_mtx = gradient_affine(x1, y1, x2, y2, 100); interpolator_type span_interpolator(gradient_mtx); // Span interpolator span_allocator_type span_allocator; // Span Allocator color_func_type color_func(c1, c2); span_gradient_type span_gradient(span_interpolator, gradient_func, color_func, 0, 100); agg::rasterizer_scanline_aa<> ras; agg::scanline_u8 sl; double r = agg::calc_distance(x1, y1, x2, y2) / 2; agg::ellipse ell((x1+x2)/2+5, (y1+y2)/2-3, r, r, 100); ras.add_path(ell); agg::render_scanlines_aa_solid(ras, sl, rbase, agg::rgba(0.6, 0.6, 0.6, 0.7*shadow_alpha)); ell.init((x1+x2)/2, (y1+y2)/2, r, r, 100); ras.add_path(ell); agg::render_scanlines_aa(ras, sl, rbase, span_allocator, span_gradient); }
virtual void on_draw() { typedef agg::renderer_base<pixfmt> base_ren_type; pixfmt 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_type::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_type::from_double(m_alpha.value(i / 255.0)); } 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_draw() { pixfmt pixf(rbuf_window()); renderer_base rb(pixf); renderer_solid rs(rb); rb.clear(agg::rgba(1, 1, 1)); // When Gamma changes rebuild the gamma and gradient LUTs //------------------ if(m_old_gamma != m_gamma.value()) { m_gamma_lut.gamma(m_gamma.value()); build_gradient_lut(); m_old_gamma = m_gamma.value(); } // Gradient center. All gradient functions assume the // center being in the origin (0,0) and you can't // change it. But you can apply arbitrary transformations // to the gradient (see below). //------------------ double cx = initial_width() / 2; double cy = initial_height() / 2; double r = 100; // Focal center. Defined in the gradient coordinates, // that is, with respect to the origin (0,0) //------------------ double fx = m_mouse_x - cx; double fy = m_mouse_y - cy; gradient_func_type gradient_func(r, fx, fy); gradient_adaptor_type gradient_adaptor(gradient_func); agg::trans_affine gradient_mtx; // Making the affine matrix. Move to (cx,cy), // apply the resizing transformations and invert // the matrix. Gradients and images always assume the // inverse transformations. //------------------ gradient_mtx.translate(cx, cy); gradient_mtx *= trans_affine_resizing(); gradient_mtx.invert(); interpolator_type span_interpolator(gradient_mtx); span_gradient_type span_gradient(span_interpolator, gradient_adaptor, m_gradient_lut, 0, r); // Form the simple rectangle //------------------ m_rasterizer.reset(); m_rasterizer.move_to_d(0,0); m_rasterizer.line_to_d(width(), 0); m_rasterizer.line_to_d(width(), height()); m_rasterizer.line_to_d(0, height()); // Render the gradient to the whole screen and measure the time //------------------ start_timer(); agg::render_scanlines_aa(m_rasterizer, m_scanline, rb, m_alloc, span_gradient); double tm = elapsed_time(); // Draw the transformed circle that shows the gradient boundary //------------------ agg::ellipse e(cx, cy, r, r); agg::conv_stroke<agg::ellipse> estr(e); agg::conv_transform< agg::conv_stroke< agg::ellipse> > etrans(estr, trans_affine_resizing()); m_rasterizer.add_path(etrans); agg::render_scanlines_aa_solid(m_rasterizer, m_scanline, rb, agg::rgba(1,1,1)); // Show the gradient time //------------------ char buf[64]; agg::gsv_text t; t.size(10.0); agg::conv_stroke<agg::gsv_text> pt(t); pt.width(1.5); sprintf(buf, "%3.2f ms", tm); t.start_point(10.0, 35.0); t.text(buf); m_rasterizer.add_path(pt); agg::render_scanlines_aa_solid(m_rasterizer, m_scanline, rb, agg::rgba(0,0,0)); #if !LINEAR_RGB // Show the controls //------------------ agg::render_ctrl(m_rasterizer, m_scanline, rb, m_gamma); // Apply the inverse gamma to the whole buffer // (transform the colors to the perceptually uniform space) //------------------ pixf.apply_gamma_inv(m_gamma_lut); #endif }