|
//创建一个小型场景..并将俯视图渲染到纹理 保存为bmp图像文件。并使用该纹理渲染一个矩形.
//以前没学过opengl 没怎么用过.不知道texturesize需要是2^n*2^m.在设置texturesize时候 没设置正确 导致错误..
//看来要学好osg得先掌握好opengl- #include<osgViewer/Viewer>
- #include <osgViewer/CompositeViewer>
- #include<osg/Node>
- #include<osg/Geode>
- #include<osg/Group>
- #include<osg/Geometry>
- #include<osg/Matrixd>
- #include<osg/Shape>
- #include<osg/ShapeDrawable>
- #include<osg/PositionAttitudeTransform>
- #include<osgDB/WriteFile>
- #include<osg/Texture2D>
- #include<iostream>
- struct MyCameraPostDrawCallback : public osg::Camera::DrawCallback
- {
- MyCameraPostDrawCallback(osg::Image* image):
- _image(image)
- {
- }
- virtual void operator () (const osg::Camera& /*camera*/) const
- {
- osgDB::writeImageFile(*_image,"skyup.bmp");
- }
- osg::Image* _image;
- };
- osg::ref_ptr<osg::Group> create()
- {
- osg::ref_ptr<osg::Group>root=new osg::Group;
- osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> v=new osg::Vec3Array;
- geom->setVertexArray(v.get());
- GLint count=0;
- for(float i=-100;i<=100;i++)
- {
- count+=4;
- v->push_back(osg::Vec3(-100.f,i,0.f));
- v->push_back(osg::Vec3(100.f,i,0.f));
-
- v->push_back(osg::Vec3(i,-100.f,0.f));
- v->push_back(osg::Vec3(i,100.f,0.f));
- }
- osg::ref_ptr<osg::Vec4Array> c=new osg::Vec4Array;
- geom->setColorArray(c.get());
- geom->setColorBinding(osg::Geometry::BIND_OVERALL);
- c->push_back(osg::Vec4(0.0f,10.0f,10.0f,1.f));
- osg::ref_ptr<osg::Vec3Array> n= new osg::Vec3Array;
- geom->setNormalArray(n.get());
- geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- n->push_back(osg::Vec3(0.f,1.f,0.f));
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,0,count));
-
- osg::Geode *geode =new osg::Geode;
- geode->addDrawable(geom.get());
- root->addChild(geode);
- osg::Box* unitCube = new osg::Box( osg::Vec3(1,1,2), 4);
- osg::ShapeDrawable* unitCubeDrawable = new osg::ShapeDrawable(unitCube);
- // 声明Geode类的实例
- osg::Geode* basicShapesGeode = new osg::Geode();
- // 将单位立方体添加到Geode中
- basicShapesGeode->addDrawable(unitCubeDrawable);
- // 将Geode添加到场景中
- root->addChild(basicShapesGeode);
- osg::Sphere* unitSphere = new osg::Sphere( osg::Vec3(1,1,2), 3);
- osg::ShapeDrawable* unitSphereDrawable = new osg::ShapeDrawable(unitSphere);
- osg::PositionAttitudeTransform* sphereXForm = new osg::PositionAttitudeTransform();
- sphereXForm->setPosition(osg::Vec3(20,-20,1.5));
- osg::Geode* unitSphereGeode = new osg::Geode();
- root->addChild(sphereXForm);
- sphereXForm->addChild(unitSphereGeode);
- unitSphereGeode->addDrawable(unitSphereDrawable);
- return root.get();
- }
- osg::Group* createHUD(osg::ref_ptr<osg::Group> root)
- {
- osg::Group* group=new osg::Group();
- osg::Camera* camera = new osg::Camera;
- osg::Geode *geode =new osg::Geode;
- osg::Texture2D* texture2D = new osg::Texture2D;
- texture2D->setTextureSize(256,256);
- texture2D->setInternalFormat(GL_RGBA);
- texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
- texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
- osg::ref_ptr<osg::Geometry> geom=new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> v=new osg::Vec3Array;
- geom->setVertexArray(v.get());
- v->push_back(osg::Vec3(-100,-100,0));
- v->push_back(osg::Vec3(100,-100,0));
- v->push_back(osg::Vec3(100,100,0));
- v->push_back(osg::Vec3(-100,100,0));
- osg::ref_ptr<osg::Vec4Array> c=new osg::Vec4Array;
- geom->setColorArray(c.get());
- geom->setColorBinding(osg::Geometry::BIND_OVERALL);
- c->push_back(osg::Vec4(1.0f,1.0f,1.0f,0.5f));
- osg::ref_ptr<osg::Vec3Array> n= new osg::Vec3Array;
- geom->setNormalArray(n.get());
- geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
- n->push_back(osg::Vec3(0.f,0.f,1.f));
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
- osg::Vec2Array* texcoords = new osg::Vec2Array(4);
- (*texcoords)[0].set(0.0f,0.0f);
- (*texcoords)[1].set(1.0f,0.0f);
- (*texcoords)[2].set(1.0f,1.0f);
- (*texcoords)[3].set(0.0f,1.0f);
- geom->setTexCoordArray(0,texcoords);
- geom->setDataVariance(osg::Object::DYNAMIC);
- osg::StateSet* state = new osg::StateSet();
- state->setTextureAttributeAndModes(0,texture2D,osg::StateAttribute::ON);
- geom->setStateSet(state);
- geode->addDrawable(geom.get());
- group->addChild(geode);
- camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
- camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- camera->setProjectionMatrix(osg::Matrix::ortho2D(-100,100,-100,100));
- camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
- osg::Matrixd mat;
- osg::Vec3f _eye=osg::Vec3f(0.0,0.0,9.0);
- osg::Vec3f _center=osg::Vec3f(0.0,0.0,0.0);
- osg::Vec3f _up=osg::Vec3f(0.0,1.0,0.0);
- mat.makeLookAt(_eye,_center,_up);
- camera->setViewMatrix(mat);
- camera->setViewport(0,0,256,256);
- camera->setRenderOrder(osg::Camera::POST_RENDER);
- camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
- //camera->attach(osg::Camera::COLOR_BUFFER,texture, 0, 0, false,0,0);
- osg::Image* image=new osg::Image();
- image->allocateImage(256, 256, 1, GL_RGBA, GL_UNSIGNED_BYTE);
- camera->attach(osg::Camera::COLOR_BUFFER,image,0,0);
- texture2D->setImage(0,image);
- camera->setPostDrawCallback(new MyCameraPostDrawCallback(image));
- camera->addChild(root.get());
- group->addChild(camera);
- return group;
- }
- int main()
- {
- osg::ref_ptr<osg::Group> root=create();
- osgViewer::Viewer viewer;
- osg::Group* ans=new osg::Group();
- ans->addChild(createHUD(root));
- viewer.setSceneData(ans);
- viewer.run();
- }
复制代码
[ 本帖最后由 acmiyou 于 2009-6-6 01:58 编辑 ] |
|