From: Maxim S. <mc...@an...> - 2005-11-29 19:46:43
|
> double para [ 6 ] = { 0,100 , 0,0 , 100,0 } ; // 3 points (0,100) (0,0) > and (100,0) > agg::trans_affine mtx(para, 0, 0, imgwd, imght); > agg::path_storage path; // partner rectangle > path.move_to( x,y); > path.line_to( x+imgwd, y ); > path.line_to( x+imgwd, y+imght); > path.line_to( x, y+imght); > path.close_polygon(); > agg::conv_transform<agg::path_storage, agg::trans_affine> trans(path, > mtx); > > pf.add_path(trans); // Note you add "trans" > > Then, if you want your image to fit exactly your parallelogram path (you > also may want to do differently!), you need to create a copy of the matrix > and invert it: > > agg::trans_affine img_mtx(mtx); > img_mtx.invert(); I'm sorry, Ken, this is not correct; I have confused myself, so, please discard the code above. :) So, suppose you have an image of imgwd, imght and a destination parallelogram. To define the parallelogram you need 3 points, x1,y1 - bottom left, x2,y2 - bottom right, x3,y3 - top right. The parallelogram can also define a 2D affine matrix: rotation, scaling, translation and skewing. You can rasterize your destination parallelogram directly: agg::rasterizer_scanline_aa<> ras; ras.move_to_d(x1,y1); ras.line_to_d(x2,y2); ras.line_to_d(x3,y3); ras.line_to_d(x1 + x3 - x2, y1 + y3 - y2); So that, you can draw a solid parallelogram (well, you can also use the path_storage if you want). To map an image to it you need to create the matrix: double para[6] = {x1,y1,x2,y2,x3,y3}; agg::trans_affine img_mtx(0, 0, imgwd, imght, para); img_mtx.invert(); Or, which is the same: double para[6] = {x1,y1,x2,y2,x3,y3}; agg::trans_affine img_mtx(para, 0, 0, imgwd, imght); The first one construicts a matrix to transform a rectangle to a a parellelogram, the second one - parallelogram to rectangle. The image transformer requires namely inverse matrix, so that, you transform your parallelogram (destination) to rectangle (image). Technically that's it. But you may want to apply additional transformations. To do that you will need two matrices: agg::trans_affine master_mtx; master_mtx *= agg::trans_affine_translation(. . .); master_mtx *= agg::trans_affine_rotation(. . .); . . . agg::rasterizer_scanline_aa<> ras; agg::path_storage path; // partner rectangle path.move_to(x1,y1); path.line_to(x2,y2); path.line_to(x3,y3); path.line_to(x1 + x3 - x2, y1 + y3 - y2); path.close_polygon(); agg::conv_transform<agg::path_storage, agg::trans_affine> trans(path, master_mtx); Then you prepare the image matrix: double para[6] = {x1,y1,x2,y2,x3,y3}; agg::trans_affine img_mtx(0, 0, imgwd, imght, para); img_mtx *= master_mtx; //!!!!!!!!!!!!! Integrate the master transforms img_mtx.invert(); ras.add_path(trans); . . .Render Now, whatever transformations you use in the master_mtxÀ they will be synchronized with the image. McSeem |