|
楼主 |
发表于 2012-3-6 11:14:32
|
显示全部楼层
array 发表于 2012-3-6 09:18
rttCamera本身可能已经设置了一些渲染状态,setStateSet()等于把这些都抹去并赋予全新的状态,我不知道您是 ...
我也认为这样,但是我在rttCamera感觉并没有设置渲染属性啊。代码如下(是从您的书上直接copy然后改的)
#include <osg/Camera>
#include <osg/Texture2D>
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/PositionAttitudeTransform>
#include <iostream>
#include <osg/Viewport>
#include <osgGA/TrackballManipulator>
const int tex_width = 512;
const int tex_height = 512;
osg::Texture* createRttTexture( int texWidth, int texHeight )
{
osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setInternalFormat( GL_RGBA );
texture->setFilter( osg::Texture::MIN_FILTER, osg::Texture:INEAR );
texture->setFilter( osg::Texture::MAG_FILTER, osg::Texture::LINEAR );
texture->setTextureSize( texWidth, texHeight );
return texture.release();
}
osg::Camera* createRttCamera( int texWidth, int texHeight, const osg::BoundingSphere& bs )
{
osg::ref_ptr<osg::Camera> rttCamera = new osg::Camera;
rttCamera->setClearMask( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
rttCamera->setViewport( 0, 0, texWidth, texHeight );
rttCamera->setRenderOrder( osg::Camera:RE_RENDER );
rttCamera->setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT );
const char* simpleVert = {
"//uniform mat4 uCamPMV;\n"
"void main\n"
"{\n"
"gl_Position = ftransform();\n"
"}"
};
const char* simpleFrag =
{
"void main\n"
"{\n"
" gl_FragColor = vec4(0.5, 0.5 ,0.1, 1.0);"
"}\n"
};
osg::ref_ptr<osg::Program> prog = new osg::Program;
osg::ref_ptr<osg::Shader> vert = new osg::Shader(osg::Shader::VERTEX);
osg::ref_ptr<osg::Shader> frag = new osg::Shader(osg::Shader::FRAGMENT);
vert->loadShaderSourceFromFile("simple.vert");
frag->loadShaderSourceFromFile("simple.frag");
prog->addShader(vert.get());
prog->addShader(frag.get());
osg::ref_ptr<osg::StateSet> stas = new osg::StateSet;//rttCamera->getOrCreateStateSet();
stas->setAttributeAndModes(prog.get());
rttCamera->setStateSet(stas.get());
return rttCamera.release();
}
int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );
osg::Node* model = osgDB::readNodeFiles( arguments );
if ( !model ) model = osgDB::readNodeFile( "rocky.ive" );
osg::Camera* rttCamera = createRttCamera( tex_width, tex_height, model->getBound() );
osg::Texture* rttTexture = createRttTexture( tex_width, tex_height);
rttCamera->addChild( model );
rttCamera->attach( osg::Camera::COLOR_BUFFER, rttTexture );
osg::ref_ptr<osg::Geode> quad = new osg::Geode;
quad->addDrawable( osg::createTexturedQuadGeometry(
osg::Vec3(0.0,0.0,0.0), osg::Vec3(1.0,0.0,0.0), osg::Vec3(0.0, 1.0, 0.0)));
quad->getOrCreateStateSet()->setTextureAttributeAndModes(0, rttTexture, osg::StateAttribute::ON);
quad->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
osg::Camera* hudCamera = new osg::Camera;
hudCamera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
hudCamera->setViewport(0, 0, 300, 300);
hudCamera->setClearColor(osg::Vec4(0.5, 0.5, 0.5, 1.0));
hudCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
hudCamera->setProjectionMatrixAsOrtho2D(0.0, 1.0, 0.0, 1.0);
hudCamera->setViewMatrix(osg::Matrix::identity());
hudCamera->addChild(quad.get());
osg::ref_ptr<osg::PositionAttitudeTransform> pat = new osg::PositionAttitudeTransform;
pat->addChild(model);
osg::ref_ptr<osg::Group> root = new osg::Group;
root->addChild(hudCamera);
root->addChild(rttCamera);
root->addChild(pat.get());
osgViewer::Viewer viewer;
viewer.setSceneData( root.get() );
return viewer.run();
}
|
|