看看这个声明:
agg::pixfmt_rgb24 pixf(rbuf);
这里我们创建了一个底层的像素渲染对象(pixel renderingobject)并将它附着到渲染内存区(renderingbuffer)上,它是这样定义的:
typedef pixel_formats_rgb24<order_rgb24>pixfmt_rgb24;
pixel_formats_rgb24(rendering_buffer&rb);
像素格式渲染器(pixecl formatrenderers)的构造函数需要一已经创建并良好初始化的rendering_buffer对象的引用。这个构建工作的开销很小,基本上只是初始化一个指针。
Member Functions
像素格式渲染器(pixecl formatrenderers)必须要实现以下这些接口:
unsigned width() const { return m_rbuf->width(); }
unsigned height() const { returnm_rbuf->height(); }
返回内存区的宽和高(以像素数来衡量)
color_type pixel(int x, int y);
返回(x,y)坐标处的像素的颜色
void copy_pixel(int x, int y, constcolor_type& c);
void blend_pixel(int x, int y, constcolor_type& c, int8ucover);
void copy_hline(int x, int y, unsigned len,const color_type&c);
void copy_vline(int x, int y, unsigned len,const color_type&c);
使用某种颜色描画一条水平或是垂直的线。
void blend_hline(int x, int y, unsignedlen, const color_type&c, int8u cover);
void blend_vline(int x, int y, unsignedlen, const color_type&c, int8u cover);
void blend_solid_hspan(int x, int y,unsigned len,
const color_type& c,const int8u* covers);
void blend_solid_vspan(int x, int y,unsigned len,
const color_type& c,const int8u* covers);
void blend_color_hspan(int x, int y,unsigned len,
const color_type*colors, const int8u* covers);
void blend_color_vspan(int x, int y, unsignedlen,
const color_type*colors, const int8u* covers);
下面这个例子是描画阳光的光谱。rgba 类包含有 4 个浮点数的颜色部分(包括alpha),这个类有一个静态函数 from_wavelength ,以及相应的构造函数。rgba8 可以用 rgba 来构造(在 AGG 中这是一个常见的策略,也就是任何的颜色类型都可以用 rgba 来构造)。
该类是从像素的角度,来填充渲染缓存,通过指定rgb8结构体,来填充渲染缓存的像素值。
这里多说一点:rgb8是颜色值是8位的,范围是(0,255),rgb是颜色值为浮点型(0,1).
分析代码:
agg::rendering_buffer &rbuf = rbuf_window();
agg::pixfmt_bgr24 pixf(rbuf);
typedef agg::renderer_base<agg::pixfmt_bgr24> renderer_base_type;
renderer_base_type renb(pixf);
agg::renderer_primitives<renderer_base_type>primitive(renb);
像素格式渲染器和基础渲染器对象的构建开销是很小的,仅仅是获取渲染缓存的指针,对渲染缓存进行渲染的操作,没有任何的内存分配。比如生成像素格式渲染器pixfmt_bgr24,仅仅是将渲染缓存的指针赋值给pixfmt_bgr24的成员变量m_buf.
windows平台下,申请的渲染缓存的颜色分量是排列如下:BGR,而不是平常的RGB。所以采用了pixfmt_bgr24,而不是pixfmt_rgb24, 对于OpenGL设置像素的时候,也是一样的道理。
agg::rendering_buffer &rbuf =rbuf_window();
agg::pixfmt_bgr24 pixf(rbuf);
agg::pixfmt_rgb24pixf1(rbuf);
for (int i = 20;i<250;i++)
{//渲染出红色
pixf.copy_hline(20,i,300,agg::rgba8(255,0,0));
}
for (int i = 20;i<250;i++)
{//渲染出蓝色
pixf1.copy_hline(420,i,300,agg::rgba8(255,0,0));
}
提供如下的一种方式直接操作渲染缓存,查看是否是颜色序列不一样导致的??
agg::rendering_buffer &rbuf =rbuf_window();
renb.clear(agg::rgba8(255,255,255));
for (int i =0;i < rbuf.height();i++)
{
unsigned char* p = rbuf.row_ptr(i);
for (int j=0;j<rbuf.width();j++)
{
*p++ = 255;//显示结果为蓝色
*p++ = 0;
*p++ = 0;
}
}
for (int i =0;i < rbuf.height();i++)
{
unsigned char* p = rbuf.row_ptr(i);
for (int j=0;j<rbuf.width();j++)
{
*p++ = 0;
*p++ = 0;
*p++ = 255;
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。