|
楼主 |
发表于 2010-10-29 10:51:17
|
显示全部楼层
drawPixels的方法是从《OpenSceneGraph三维渲染引擎设计与实践》P119绘制位图的例子中学到的!
现在我调整了一下代码,可以显示了。但是显示的效果比较暗,而且提示信息:
Scaling image 'desktop.png' from (1440,900) to (1024,1024)
我加载的图片是1440*900的,显示器分辨率也一样。 代码如下,请各位分析一下:- // 创建一个四边形节点
- osg::ref_ptr<osg::Geode> createNode()
- {
- osg::ref_ptr<osg::Geode> geode = new osg::Geode();
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
- //设置顶点
- osg::ref_ptr<osg::Vec3Array> vc = new osg::Vec3Array();
- vc->push_back(osg::Vec3(0.0f, 0.0f, -1.0f));
- vc->push_back(osg::Vec3(1440.0f,0.0f, -1.0f));
- vc->push_back(osg::Vec3(1440.0f,900.0f, -1.0f));
- vc->push_back(osg::Vec3(0.0f, 900.0f, -1.0f));
- geom->setVertexArray(vc.get());
- //设置纹理坐标
- osg::ref_ptr<osg::Vec2Array> vt = new osg::Vec2Array();
- vt->push_back(osg::Vec2(0.0f,0.0f));
- vt->push_back(osg::Vec2(1.0f,0.0f));
- vt->push_back(osg::Vec2(1.0f,1.0f));
- vt->push_back(osg::Vec2(0.0f,1.0f));
- geom->setTexCoordArray(0,vt.get());
- //设置法线
- osg::ref_ptr<osg::Vec3Array> nc = new osg::Vec3Array();
- nc->push_back(osg::Vec3(0.0f,1.0f,0.0f));
-
- geom->setNormalArray(nc.get());
- geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- //添加图元
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
- //绘制
- geode->addDrawable(geom.get());
- return geode.get();
- }
- //创建二维纹理状态对象
- osg::ref_ptr<osg::StateSet> createTexture2DState(osg::ref_ptr<osg::Image> image)
- {
- //创建状态集对象
- osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
- //创建二维纹理对象
- osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D();
- texture->setDataVariance(osg::Object::DYNAMIC);
- //设置贴图
- texture->setImage(image.get());
- stateset->setTextureAttributeAndModes(0,texture.get(),osg::StateAttribute::ON);
- return stateset.get();
- }
- osg::ref_ptr<osg::Camera> createBackground(void)
- {
- osg::ref_ptr<osg::Camera> camera = new osg::Camera;
- camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1440,0,900));
- camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- camera->setViewMatrix(osg::Matrix::identity());
- camera->setClearMask(GL_DEPTH_BUFFER_BIT);
- camera->setRenderOrder(osg::Camera::POST_RENDER);
- camera->setAllowEventFocus(false);
- camera->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
- osg::ref_ptr<osg::Image> image = osgDB::readImageFile("desktop.png");
- //创建几何体
- osg::ref_ptr<osg::Geode> geode = createNode();
- //创建状态集对象
- osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet();
- stateset = createTexture2DState(image.get());
- //使用二维纹理
- geode->setStateSet(stateset.get());
- geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON);
- geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
- osg::ref_ptr<osg::PositionAttitudeTransform> pos = new osg::PositionAttitudeTransform;
- pos->addChild(geode.get());
- pos->setPosition(osg::Vec3(0,0,-1));
- camera->addChild(pos.get());
- return camera.get();
- }
- int main(void)
- {
- osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
- osg::ref_ptr<osg::Group> root = new osg::Group;
- //创建背景
- root->addChild(createBackground());
- viewer->addEventHandler(new osgViewer::StatsHandler);
- viewer->setSceneData(root.get());
- viewer->run();
- return 0;
- }
复制代码 变暗应该是和体系信息相关,大小被强制变化造成。但是我看到的情况确实是满屏显示的,应该又没有拉伸!怪了…… |
|