AGG入門(六) - 練習(xí)和細(xì)節(jié)
- platform_support
- rendering_buffer
- rgba8
- pixfmt_rgb24
- rect_i
- renderer_base
一、基本框架
#include <agg_renderer_base.h>
#include <platform/agg_platform_support.h>
class the_application : public agg::platform_support
{
public:
the_application(agg::pix_format_e format, bool flip_y) :
agg::platform_support(format, flip_y),
pix_fmt(rbuf_window()),
ren_bas(pix_fmt) //初始化渲染器
{ }
virtual void on_draw()
{
ren_bas.reset_clipping(true);
private:
agg::pixfmt_rgb24 pix_fmt;
agg::renderer_base<agg::pixfmt_rgb24> ren_bas;
};
int agg_main(int argc, char* argv[])
{
the_application app(agg::pix_format_bgr24, true);
app.caption("AGG Test");
if(app.init(500, 500, agg::window_resize)) {
return app.run();
}
return -1;
}
二、畫線函數(shù)
編寫如下函數(shù),實(shí)現(xiàn)在渲染緩存中畫線的功能(無(wú)需反鋸齒):
inline void stroke_line(int x1, int y1, int x2, int y2, agg::rgba8& color);
參數(shù):
- x1, y1, x2, y2分別是兩個(gè)端點(diǎn)的坐標(biāo);
- color是顏色;
三、畫圓函數(shù)
編寫如下函數(shù),實(shí)現(xiàn)在渲染緩存中畫圓的功能(無(wú)需反鋸齒):
void stroke_round(int r, int C_x, int C_y, agg::rgba8& color, float step = 0.01)
參數(shù):
- C_x, C_y 是圓心的坐標(biāo);
- color是顏色;
- step是步長(zhǎng),也就是吧圓細(xì)分成1/step邊形;
四、答案
- 畫線函數(shù)
inline void stroke_line(int x1, int y1, int x2, int y2, agg::rgba8& color)
{
double precision = max(abs(x1 - x2), abs(y1 - y2));
//精度,也就是畫多少個(gè)點(diǎn)
for(int i=0; i <= precision; i++)
ren_bas.copy_pixel( x1 + ( x2 - x1 ) / precision * i, //x
y1 + ( y2 - y1 ) / precision * i, //y
color);
} - 畫圓函數(shù)
void stroke_round(int r, int C_x, int C_y, agg::rgba8& color, float step = 0.01)
{
int prev_x = int(r * cos(-0.01)) + C_x,
prev_y = int(r * sin(-0.01)) + C_y; //保存上一個(gè)點(diǎn)
int x, y; //保存當(dāng)前的點(diǎn)
for(double rad = 0; rad < 2 * PI + step; rad+= step) {
x = int(r * cos(rad)) + C_x;
y = int(r * sin(rad)) + C_y; //計(jì)算弧度為rad時(shí)的坐標(biāo)
stroke_line(x, y, prev_x, prev_y, color);
prev_x = x; prev_y = y;
}
}
可能有的人會(huì)覺(jué)得奇怪的是,為什么在畫線函數(shù)中,不用pix_fmt.copy_pixel()而用ren_bas.copy_pixel()呢?因?yàn)椋趐ix_fmt中,混合器不進(jìn)行檢查,像素拷貝的時(shí)候會(huì)拷貝到剪裁區(qū)域以外,這樣會(huì)造成很奇怪的情況,以至于如果寫到了緩存以外,還會(huì)出現(xiàn)異常。注意,剪裁盒功能是基礎(chǔ)渲染器級(jí)別才提供的,更加底層的操作,比如像素格式混合和直接操作緩存,高層次的渲染器是無(wú)從管理的。為了安全起見(jiàn),建議少碰基礎(chǔ)渲染器以下的工具……
posted on 2012-07-24 16:30 Shihira 閱讀(4229) 評(píng)論(1) 編輯 收藏 引用 所屬分類: 圖形編程