前段时间,一直在弄osg中的图像放缩和图像拷贝的问题,主要是其中的两个函数: scaleImage和copySubImage,但是使用过程中总是产生一个问题,提示

,
这个问题困扰了很久很久,一直在osgChina上查找都没有找到具体的原因。今天上天保佑,在三人共同编程的情况下,终于找到了解决方案,现在我贴出代码,以供大家分享:
1


class MyGraphicsContext

{
2

public:
3

MyGraphicsContext()
4


{
5

osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
6

traits->x = 0;
7

traits->y = 0;
8

traits->width = 1;
9

traits->height = 1;
10

traits->windowDecoration = false;
11

traits->doubleBuffer = false;
12

traits->sharedContext = 0;
13

traits->pbuffer = true;
14

15

_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
16

17

if (!_gc)
18


{
19

traits->pbuffer = false;
20

_gc = osg::GraphicsContext::createGraphicsContext(traits.get());
21

}
22

23

if (_gc.valid())
24


{
25

_gc->realize();
26

_gc->makeCurrent();
27

}
28

}
29

30


bool valid() const

{ return _gc.valid() && _gc->isRealized(); }
31

32

private:
33

osg::ref_ptr<osg::GraphicsContext> _gc;
34

};
以上是自己的图形上下文,必须要有这个的存在才能正确的调用到上面提到的两个函数,下面是其使用:
1

int main(int argc, char* argv[])
2



{
3

MyGraphicsContext gc;
4

if (!gc.valid())
5


{
6

osg::notify(osg::NOTICE)<<"Unable to create the graphics context required to build 3d image."<<std::endl;
7

return 0;
8

}
9

10

osg::ref_ptr<osg::Image> image = new osg::Image;
11

osg::ref_ptr<osg::Image> image1 = new osg::Image;
12

osg::ref_ptr<osg::Image> image2 = new osg::Image;
13

image1 = osgDB::readImageFile("1.bmp");
14

image2 = osgDB::readImageFile("2.bmp");
15

image->allocateImage(512, 256, 1, image1->getPixelFormat(), image1->getDataType());
16

image->copySubImage(0, 0, 0, image1);
17

image->copySubImage(256,0, 0, image2);
18

19

osgDB::writeImageFile(*image, "0.bmp");
20

21

return 0;
22

}
23

只有这样才能正确的处理图像的压缩或者融合,终于解决了这个问题,上苍保佑!~~