|
楼主 |
发表于 2009-6-21 14:32:14
|
显示全部楼层
下面是主要的代码:
其中preRender是入口函数 只要提供一个节点给他就可以了.
createPreNode是做RTT效果的函数.- #include "HowtoPreRender.h"
- HowtoPreRender::HowtoPreRender(Image* image)
- {
- mImage=image;
- }
- HowtoPreRender::~HowtoPreRender(void)
- {
- }
- void HowtoPreRender::operator ()(const Camera& camera)const{
- if(mImage
- &&mImage->getPixelFormat()==GL_RGBA
- )
- {
- osgDB::writeImageFile(*mImage,"C:\\image.bmp");
- mImage->dirty();
- }
- }
- Node* HowtoPreRender::createPreNode(Node *graph,
- unsigned txt_width,
- unsigned txt_height,
- bool useImage)
- {
- if(!graph)return NULL;
- Group* parent=new Group();
- Texture2D *txt2D=new Texture2D;
- txt2D->setTextureSize(txt_width,txt_height);
- txt2D->setInternalFormat(GL_RGBA);
- txt2D->setFilter(Texture2D::MIN_FILTER,Texture2D::LINEAR);
- txt2D->setFilter(Texture2D::MAG_FILTER,Texture2D::LINEAR);
- Texture* txt=txt2D;
- Geometry* geom=new Geometry;
- geom->setSupportsDisplayList(false);
- float width=1024;
- float height=512;
- Vec3Array *vertices=new Vec3Array;
- vertices->push_back(Vec3(0.f,0.f,0.f));
- vertices->push_back(Vec3(width,0.f,0.f));
- vertices->push_back(Vec3(width,0.f,height));
- vertices->push_back(Vec3(0.f,0.f,height));
- geom->setVertexArray(vertices);
- Vec2Array *txtCoords=new Vec2Array;
- txtCoords->push_back(Vec2(0.f,0.f));
- txtCoords->push_back(Vec2(1.f,0.f));
- txtCoords->push_back(Vec2(1.f,1.f));
- txtCoords->push_back(Vec2(0.f,1.f));
- VertexBufferObject *vbo=new VertexBufferObject;
- vertices->setVertexBufferObject(vbo);
- geom->setUseVertexBufferObjects(true);
- geom->setTexCoordArray(0,txtCoords);
- Vec4Array *colors=new Vec4Array;
- colors->push_back(Vec4(1.f,1.f,1.f,0.5f));
- geom->setColorArray(colors);
- geom->setColorBinding(Geometry::BIND_OVERALL);
- geom->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS,0,vertices->size()));
- StateSet *states=new StateSet;
- states->setTextureAttributeAndModes(0,txt,StateAttribute::ON);
- geom->setStateSet(states);
- Geode* geode=new Geode;
- geode->addDrawable(geom);
- parent->addChild(geode);
- Camera* camera=new Camera;
- camera->setClearColor(Vec4(0.1f,0.1f,0.3f,1.0f));
- camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- BoundingSphere bs=graph->getBound();
- if(!bs.valid())
- {
- return graph;
- }
- float znear=bs.radius();
- float zfar=3*bs.radius();
- float proj_top=0.25f*znear;
- float proj_right=0.5*znear;
- znear*=0.9f;
- zfar*=1.1f;
- camera->setProjectionMatrixAsFrustum(-proj_right,proj_right,-proj_top,proj_top,znear,zfar);
- camera->setReferenceFrame(Transform::ABSOLUTE_RF);
- camera->setViewMatrixAsLookAt(bs.center()-Vec3(0.f,2.f,0.f)*bs.radius(),
- bs.center(),
- Vec3(0.f,0.f,1.f));
- camera->setViewport(0,0,txt_width,txt_height);
- camera->setRenderOrder(Camera::POST_RENDER);
- camera->setRenderTargetImplementation(Camera::FRAME_BUFFER_OBJECT);
- if(useImage)
- {
- Image *img=new Image;
- img->allocateImage(txt_width,txt_height,1,GL_RGBA,GL_FLOAT);
- camera->attach(Camera::COLOR_BUFFER,img);
- camera->setPostDrawCallback(new HowtoPreRender(img));
- txt->setImage(0,img);
- }else{
- camera->attach(Camera::COLOR_BUFFER,txt);
- }
- camera->addChild(graph);
- parent->addChild(camera);
- return parent;
- }
- void HowtoPreRender::preRender(Group* model)
- {
- Viewer* viewer=new Viewer;
- unsigned txt_width=512;
- unsigned txt_height=512;
- bool useImage=false;
- MatrixTransform * modelEx=new MatrixTransform;
- Matrix mat;
- mat.makeRotate(90.f,1.0f,0.0f,0.f);
- modelEx->setMatrix(mat);
- modelEx->addChild(model);
- Group* rootNode=new Group;
- Node* howtoNode=HowtoPreRender::createPreNode(modelEx,
- txt_width,txt_height,useImage);
- rootNode->addChild(howtoNode);
- osgUtil::Optimizer optimizer;
- optimizer.optimize(rootNode);
- viewer->setSceneData(rootNode);
- viewer->realize();
- viewer->run();
- }
复制代码
[ 本帖最后由 zhuliangxiong 于 2009-6-21 14:33 编辑 ] |
|