快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

AGG第三十一课pattern_perspective样式透明-创新互联

摘自:http://article.gmane.org/gmane.comp.graphics.agg/2911/

创新互联建站是专业的长汀网站建设公司,长汀接单;提供做网站、网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行长汀网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
> I've read the pattern_perspective.cpp sample but I don't understand very
> well how to apply a texture on a shape.
> Could you explain me the stages to do that and what is the role of each
> operation ?

Yes, I must admit the example is difficult to read.

First, it's better to take pattern_resample.cpp that covers automatic 
resampling too.
Now I'll try to explain to you what to do step by step.

1. If your pattern has actual alpha channel, the pixels must be 
premultiplied. In general, all p_w_picpath transformers work only with 
premultiplied colors. If it's RGB or all alphas are 255 you don't need to do 
anything. But if you load a PNG p_w_picpath you most probably will have to 
premultiply it.

2. You get your p_w_picpath somehow, create a rendering buffer and pixel format 
for it:

typedef agg::pixfmt_bgra32 pixfmt;
typedef agg::pixfmt_bgra32_pre pixfmt_pre;
typedef pixfmt::color_type color_type;

In the example agg::rendering_buffer is already created in load_img() 
function:
pixfmt img_pixf(rbuf_img(0));
Note that there's no difference if you use pixfmt or pixfmt_pre for the 
source p_w_picpath.

3. Create a span allocator. It's better to keep it in a class and reuse it 
instead of creating/destroying.
typedef agg::span_allocator span_alloc_type;
span_alloc_type sa;

4. Create a filter LUT
agg::p_w_picpath_filter_bilinear filter_kernel;
agg::p_w_picpath_filter_lut filter(filter_kernel, true);
For some methods there's no need in it. For example, 
span_p_w_picpath_filter_rgba_bilinear has hardcoded bilinear filter.

5. Create an p_w_picpath accessor that defines the logic of pixel wrapping.
typedef agg::wrap_mode_reflect_auto_pow2 wrap_type;
typedef agg::p_w_picpath_accessor_wrap 
img_source_type;
img_source_type img_src(img_pixf);
The possible variants are:
p_w_picpath_accessor_clip - clips the pixels outside of the source p_w_picpath and 
replaces them with a given color.
p_w_picpath_accessor_clone - clones the edge pixels.
p_w_picpath_accessor_wrap - wraps the pixels (it's what actually creates the 
repeating pattern)
The wrapper has template arguments of, what to do by X and Y:
wrap_mode_repeat, wrap_mode_reflect, wrap_mode_repeat_pow2, 
wrap_mode_reflect_pow2, wrap_mode_repeat_auto_pow2, 
wrap_mode_reflect_auto_pow2. If the size of your pattern is power of 2, you 
can use a faster _pow2 version (shifts and masking instead of divisions).

6. Create an interpolator. For affine transformations you can use a fast 
linear interpolator that operates in integers, with a Bresenham-like 
algorithm inside. For perspective transformations you can't use linear 
interpolator. There are two choices:

6.1. exact interpolator:
typedef agg::span_interpolator_trans 
interpolator_type;
interpolator_type interpolator(tr);
Defines an exact interpolator, works slower because it calculates the points 
on every pixel

6.2. Subdivision approximation:
typedef agg::span_interpolator_linear_subdiv 
interpolator_type;
interpolator_type interpolator(tr);
It calculates every 16'th pixel (configurable) and linearly interpolates 
between them. Works faster, but on heavily transformed p_w_picpaths there is 
considerable inaccuracy.

Also, for transformations with automatic resampling (rather slow, but 
produces high quality) you need a special version of the interpolator, 
because you need to know the local scale at every point:
typedef agg::span_interpolator_persp_lerp<> interpolator_type;
typedef agg::span_subdiv_adaptor subdiv_adaptor_type;
interpolator_type interpolator(m_quad.polygon(), g_x1, g_y1, g_x2, g_y2);
subdiv_adaptor_type subdiv_adaptor(interpolator);

7. Then you create a span generator that does the p_w_picpath filtering job. It 
can be one of the following:
span_p_w_picpath_filter_rgba_nn - nearest neigbor filter (box). Actually, no 
filtering
span_p_w_picpath_filter_rgba_bilinear - hardcoded bilinear filter
span_p_w_picpath_filter_rgba_2x2 - arbitrary filter of fixed size 2x2 pixels
span_p_w_picpath_filter_rgba - arbitrary filter of any size, like bicubic, 
gaussian, etc (slow).
span_p_w_picpath_resample_rgba_affine - p_w_picpath resampler for affine 
transformations. It can't be used with perspective transformer.
span_p_w_picpath_resample_rgba - p_w_picpath resampler for arbitrary transformer. The 
interpolator must provide function to calculate (or interpolate) the local 
scale.

8. Create and configure the transformer. It can be a simple affine 
transformer, or perspective one, or even some nonlinear, like in 
distortions.cpp example.
The algorithmic feature is that the transformer must be inverse. That is, it 
should map destination pixels onto source ones.

9. Finally, you feed the rasterizer with an arbitrary path and draw it, for 
example:
g_rasterizer.move_to_d(x1, y1);
g_rasterizer.line_to_d(x2, y2);
g_rasterizer.line_to_d(x3, y3);
g_rasterizer.line_to_d(x4, y4);
typedef span_p_w_picpath_filter_rgba_2x2 span_gen_type;
span_gen_type sg(img_src, interpolator, filter);
agg::render_scanlines_aa(g_rasterizer, g_scanline, rb_pre, sa, sg);

The path can be just a rectangle, or quadrilateral, or whatever. The whole 
point is that it will draw perfectly anti-aliased edges. For example, you 
can draw a circle with pattern inside.

Well, this explanation isn't full, but I hope it will help you together with 
working pattern_resample.cpp

McSeem

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享名称:AGG第三十一课pattern_perspective样式透明-创新互联
文章转载:http://6mz.cn/article/hhsgi.html

其他资讯