|
发表于 2010-6-7 14:13:17
|
显示全部楼层
嗯,我也是这么做的,但是程序运行之后出现访问冲突的问题,不知道怎么回事?下面是我的代码:
osg::Texture2D *backtexture(osg::ref_ptr<osg::Node> terrainnode)
{
int tex_width = 128;
int tex_height = 128;
// osg::Texture* texture = 0;
osg::Texture2D* texture2D = new osg::Texture2D;
texture2D->setTextureSize(tex_width, tex_height);
texture2D->setInternalFormat(GL_RGBA);
texture2D->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D:INEAR);
texture2D->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
texture2D->setDataVariance(osg::Object:YNAMIC);
// texture = texture2D;
// 代码代码
osg::ref_ptr<osg::Group> group = new osg::Group;
osg::ref_ptr<osg::Node> node = new osg::Node;
node = terrainnode;
osg::StateSet* stateset = node->getOrCreateStateSet();
stateset->setMode(osg::StateAttribute::CULLFACE,osg::StateAttribute::ON);
osg::Camera *camera = new osg::Camera();
camera->setClearMask(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
const osg::BoundingSphere& bs = terrainnode->getBound();
if (!bs.valid())
{
std::cout<<"the box is not valid!"<<std::endl;
}
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->setViewport(0,0,tex_width,tex_height);
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,0.0f,0.5f)*bs.radius(),bs.center()+osg::Vec3(0.0f,0.0f,10000.0f),osg::Vec3(0.0f,0.0f,-1.0f));
camera->setRenderOrder(osg::Camera:RE_RENDER);
camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
osg::Shader* vertexShader = new osg::Shader( osg::Shader::VERTEX );
osg::Shader* fragmentShader = new osg::Shader( osg::Shader::FRAGMENT );
vertexShader->loadShaderSourceFromFile( "depth.vert" );
fragmentShader->loadShaderSourceFromFile( "depth.frag" );
osg::Program* program = new osg::Program;
program->addShader( vertexShader );
program->addShader( fragmentShader);
stateset->setAttributeAndModes( program, osg::StateAttribute::ON );
osg::Uniform* param1 = new osg::Uniform( "depthOffset", 0.01f);
stateset->addUniform( param1 );
osg::Uniform* param2 = new osg::Uniform( "pNear", znear);
stateset->addUniform( param2 );
osg::Uniform* param3 = new osg::Uniform( "pFar", zfar);
stateset->addUniform( param3 );
camera->attach(osg::Camera::COLOR_BUFFER,texture2D,0,0,false,0,0);
camera->addChild(node.get());
group->addChild(camera);
return texture2D;
} |
|