RTT,渲染到贴图

好乱没整理
// DyncDistort.cpp : 定义应用程序的入口点。
//




#include 
"stdafx.h"
#include 
"DyncDistort.h"
#include 
"MCvMainClass.h"



// test Pbuffer in osg. added by LSL 2011/05/07
#include <osg/Node>
#include 
<osg/Geode>
#include 
<osg/Geometry>
#include 
<osgViewer/Viewer>
#include 
<osgDB/ReadFile>
#include 
<osg/Texture2D>
#include 
<osg/MatrixTransform>

#pragma comment(lib,"osgd.lib")
#pragma comment(lib,"osgViewerd.lib")
#pragma comment(lib,"osgGAd.lib")
#pragma comment(lib,"osgDBd.lib")
#pragma comment(lib,"osgFXd.lib")
#pragma comment(lib,"osgUtild.lib")
// end

struct Rect2D
{
    
float left;
    
float right;
    
float top;
    
float bottom;
    
float depth;
};


osg::Node 
* createTex()
{
    osg::Group 
* parent = new osg::Group;
     osg::Texture
* texture = 0;

     unsigned tex_width 
= 128;
     unsigned tex_height 
= 128;
     osg::Texture2D
* texture2D = new osg::Texture2D;
     texture2D
->setTextureSize(tex_width, tex_height);
     texture2D
->setInternalFormat(GL_RGBA);
     texture2D
->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR);
     texture2D
->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
     texture 
= texture2D;

     
     osg::Node 
* cow = osgDB::readNodeFile("cow.osg");
     osg::MatrixTransform 
* subgraph = new osg::MatrixTransform;
     subgraph
->addChild(cow);

     
// then create the camera node to do the render to texture
         
    osg::Camera
* camera = new osg::Camera;
         {

         
// set up the background color and clear mask.
         camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
         camera
->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

         
const osg::BoundingSphere& bs = subgraph->getBound();
         
if (!bs.valid())
         {
             
return subgraph;
         }

         
float znear = 1.0f*bs.radius();
         
float zfar  = 3.0f*bs.radius();

         
// 2:1 aspect ratio as per flag geometry below.
         float proj_top   = 0.25f*znear;
         
float proj_right = 0.5f*znear;

         znear 
*= 0.9f;
         zfar 
*= 1.1f;

         
// set up projection.
         camera->setProjectionMatrixAsFrustum(-proj_right,proj_right,-proj_top,proj_top,znear,zfar);

         
// set view
         camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
         camera
->setViewMatrixAsLookAt(bs.center()-osg::Vec3(0.0f,2.0f,0.0f)*bs.radius(),bs.center(),osg::Vec3(0.0f,0.0f,1.0f));

         
// set viewport
         camera->setViewport(0,0,tex_width,tex_height);

         
// set the camera to render before the main camera.
         camera->setRenderOrder(osg::Camera::PRE_RENDER);

         
// tell the camera to use OpenGL frame buffer object where supported.
         camera->setRenderTargetImplementation(osg::Camera::PIXEL_BUFFER);

         
// attach the texture and use it as the color buffer.
         camera->attach(osg::Camera::COLOR_BUFFER, texture);
     }

     {

         osg::ref_ptr
<osg::Geometry > geom = new osg::Geometry;

         
// getScreenRectAtDepth(m_frustum, m_frustum.maxdepth+5);    
         Rect2D rect;
         rect.depth 
= 100;
         rect.left 
= 100;
         rect.right 
= 300;
         rect.bottom 
= 100;
         rect.top 
= 200;

         
// vertex
         osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
         geom
->setVertexArray(v);
         v
->push_back(osg::Vec3(rect.left, rect.depth, rect.bottom));
         v
->push_back(osg::Vec3(rect.right, rect.depth, rect.bottom));
         v
->push_back(osg::Vec3(rect.right, rect.depth, rect.top));
         v
->push_back(osg::Vec3(rect.left, rect.depth, rect.top));
         geom
->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));

         
// color
         osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
         geom
->setColorArray(c);
         geom
->setColorBinding(osg::Geometry::BIND_PER_VERTEX);
         c
->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
         c
->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
         c
->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
         c
->push_back(osg::Vec4(1.0,1.0,1.0,1.0));

         
// normal
         osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array ;
         geom
->setNormalArray(n);
         geom
->setNormalBinding(osg::Geometry::BIND_OVERALL);
         n
->push_back(osg::Vec3(0.f,-1.f,0.f));

         
// ---texuture----
         
//osg::ref_ptr<osg::Texture2D> tex = new osg::Texture2D;    
         
//tex->setDataVariance(osg::Object::DYNAMIC);
         
//tex->setResizeNonPowerOfTwoHint(false);
         
//tex->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
         
//tex->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
         
//tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE);
         
//tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE);

         osg::StateSet 
* ss = geom->getOrCreateStateSet();
         
//    ss->setAttributeAndModes(new osg::BlendFunc,osg::StateAttribute::ON);
         
//    ss->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
         ss->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);

         
//osg::ref_ptr<osg::Image> img = osgDB::readImageFile(path);
         
//tex->setImage(img);

         
// tex coordinate
         osg::ref_ptr<osg::Vec2Array> texcoor= new osg::Vec2Array;
         geom
->setTexCoordArray(0,texcoor.get());
         texcoor
->push_back(osg::Vec2(1.,0.));
         texcoor
->push_back(osg::Vec2(0.,0.));
         texcoor
->push_back(osg::Vec2(0.,1.));
         texcoor
->push_back(osg::Vec2(1.,1.));

         osg::Geode
* geode = new osg::Geode();
         geode
->addDrawable(geom);

         parent
->addChild(geode);
     }

     camera
->addChild(subgraph);
     parent
->addChild(camera);
    
return parent;
}


int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     
int       nCmdShow)
{

    osg::ref_ptr
<osgViewer::Viewer> viewer = new osgViewer::Viewer;
    osg::ref_ptr
<osg::Group> root = new osg::Group;
    root
->addChild(createTex());
    viewer
->setSceneData(root.get());
    viewer
->realize();
    viewer
->run();


    
//MCvMainClass l_app;
    
//IAppData AppData;
    
//
    
//AppData.InitWin32(hInstance, hPrevInstance, lpCmdLine, nCmdShow, "videoShowWindow", 0, 0, 1280, 750);

    
//MWinFrameWork g_app;

    
//g_app.Initialize(&AppData, &l_app);
    
//l_app.SetFramePointer(&AppData);
    
//g_app.Run();

    



    
return 0;
}
原文地址:https://www.cnblogs.com/mumuliang/p/2039973.html