| 線段產生器(Span Generator) 我們前面舉的例子使用的都是簡單的單一實色,如藍色的圓、黑色的線等。這是因為在例子裡我們一直使用renderer_scanline_aa_solid或render_scanlines_aa_solid。 在上篇文章(http://www.cppprog.com/2009/0821/150.html)的渲染器一節中除了renderer_scanline_aa_solid外,還提到有一個renderer_scanline_aa,這裡再寫一遍它的聲明:
- template<class BaseRenderer, class SpanAllocator, class SpanGenerator>
- class renderer_scanline_aa;
另外,還有一個函數版本:
- template<class Rasterizer, class Scanline, class BaseRenderer,
- class SpanAllocator, class SpanGenerator>
- void render_scanlines_aa(Rasterizer& ras, Scanline& sl, BaseRenderer& ren,
- SpanAllocator& alloc, SpanGenerator& span_gen);
renderer_scanline_aa (還有一個兄弟版本renderer_scanline_bin)可以按指定的圖案或不同的顏色(如漸層)填充頂點源裡的多邊形。其中的模板參數 SpanAllocator用於準備span,我們直接使用agg::span_allocator就行。這裡的SpanGenerator就是本節要說的線段產生器,它決定了最終用什麼東西填到rendering_buffer裡。 線段產生器品種很多,常用的在致可以分成圖案類和色彩類兩大部分:圖案類線段產生器使用已有映像作為span來源;色彩類線段產生器使用指定的顏色作為span來源。 圖案類線段產生器標頭檔
- #include <agg_span_image_filter_gray.h>
- #include <agg_span_image_filter_rgb.h>
- #include <agg_span_image_filter_rgba.h>
- #include <agg_span_pattern_gray.h>
- #include <agg_span_pattern_rgb.h>
- #include <agg_span_pattern_rgba.h>
類型
- template<class Source, class Interpolator>
- span_image_filter_[gray|rgb|rgba]
- template<class Source, class Interpolator>
- span_image_filter_[gray|rgb|rgba]_2x2
- template<class Source, class Interpolator>
- span_image_filter_[gray|rgb|rgba]_bilinear
- template<class Source, class Interpolator>
- span_image_filter_[gray|rgb|rgba]_bilinear_clip
- template<class Source, class Interpolator>
- span_image_filter_[gray|rgb|rgba]_nn
- template<class Source, class Interpolator>
- span_image_resample_[gray|rgb|rgba]
- template<class Source>
- span_image_resample_[gray|rgb|rgba]_affine
- template<class Source>
- class agg::span_pattern_[gray|rgb|rgba]
上面這些線段產生器類的模板參數都比較相似:Source用於指定映像來源,可以是PixelFormat renderer或agg::image_accessor_clip(由不同的線段產生器類決定);Interpolator是一種插值器,用於填充映像間隙。我們先寫一段範例程式碼,先看一下線段產生器的作用,也為後面的各種實驗做準備。 範例程式碼,使用span_image_filter_rgb_bilinear_clip還是基於這個代碼(http://www.cppprog.com/2009/0816/146.html),加入下面的標頭檔
- #include <platform/win32/agg_win32_bmp.h>
- #include "agg_span_allocator.h"
- #include "agg_span_image_filter_rgb.h"
在on_draw()方法的最後加上下面這些代碼
- ...
- // 以映像填充
- agg::pixel_map pm_img;
- if(pm_img.load_from_bmp("d://spheres.bmp"))
- {
- // pm_img裡的圖案作為填充來源
- agg::rendering_buffer rbuf_img(
- pm_img.buf(),
- pm_img.width(), pm_img.height(),
- -pm_img.stride());
- agg::pixfmt_bgr24 pixf_img(rbuf_img);// 我用的bmp是24位的
- // 線段分配器
- typedef agg::span_allocator<agg::rgba8> span_allocator_type;//分配器類型
- span_allocator_type span_alloc; // span_allocator
- // 插值器
- typedef agg::span_interpolator_linear<> interpolator_type; //插值器類型
- agg::trans_affine img_mtx; // 變換矩陣
- interpolator_type ip(img_mtx); // 插值器
- // 線段產生器
- typedef agg::span_image_filter_rgb_bilinear_clip<agg::pixfmt_bgr24,
- interpolator_type > span_gen_type; // 這個就是Span Generator
-
- span_gen_type span_gen(pixf_img, agg::rgba(0,1,0), ip);
- // 組合成渲染器
- agg::renderer_scanline_aa<
- renderer_base_type,
- span_allocator_type,
- span_gen_type
- > my_renderer(renb, span_alloc, span_gen);
- // 插值器的矩陣變換
- img_mtx.scale(0.5);
- img_mtx.translate(40,40);
- img_mtx.invert(); //注意這裡
- // 用我們的渲染器畫圓
- ras.add_path(ell);
- agg::render_scanlines(ras,sl,my_renderer);
- }
其中的d://spheres.bmp(下載)是我預先放在D盤裡的24位bmp映像,作為填充的來源。 顯示效果:
- 在第19行的span_gen_type之前,所有的事情都在為定義這個線段產生器做準備。
- 首先是用pixel_map讀取bmp檔案,然後產生rendering_buffer和pixfmt_bgr24作為這個線段產生器的"Source"。
- 然後是線段分配器,這個沒什麼特殊要求的話用span_allocator就可以了。
- 接著是插值器類型,插值器也有幾個類型(後面會介紹),它的建構函式需要一個變換矩陣對象,於是我們得為它裝備一個。
- 現在,終於可以組合成一個我們的線段產生器了。這裡使用的是span_image_filter_rgb_bilinear_clip,它的Source是PixelFormat Renderer,如本例的pixfmt_bgr24。
- span_image_filter_rgb_bilinear_clip的建構函式有三個參數,分別是Source對象,填充來源範圍之外的顏色和插值器對象。
- 我們可以改變插值器的矩陣來變換填充映像,象這裡的img_mtx.scale(0.5)和img_mtx.translate(40,40)。要注意的是,插值器的矩陣運算是從目標位置向源位置計算的(即根據目標位置變換得到對應的填充源位置),所以想對源映像變換的話,要記得最後調用矩陣的invert()方法取反。
- 最後,畫圓。由於ell是ellipse對象,沒有被conv_stroke轉換的ellipse對象是實心的(多邊形而不是多義線),於是填充之。
作者:毛毛 來源:www.cppprog.com |