|
#include <osgviewer/viewer>
#include <osg/Group>
#include <osgDB/ReadFile>
#include <osg/Camera>
#include <osg/Texture2D>
#include <osg/Projection>
#include <osg/MatrixTransform>
#include <osgGA/StateSetManipulator>
#include <osg/Texture2DMultisample>
#include <osg/Program>
#include <osg/Shader>
#include <iostream>
using std::string;
void assginShder(osg::Node* node ,string path){
osg:rogram* prog = new osg::Program();
osg::Shader* frag = new osg::Shader(osg::Shader::FRAGMENT);
frag->loadShaderSourceFromFile(path);
prog->addShader(frag);
node->getOrCreateStateSet()->setAttributeAndModes(prog);
node->getOrCreateStateSet()->addUniform(new osg::Uniform("ColorTex",0));
};
osg::Node* createRTTQuad( )
{
osg::ref_ptr<osg::Geode> quad_geode = new osg::Geode;
osg::ref_ptr<osg::Vec3Array> quad_coords = new osg::Vec3Array; // vertex coords
// counter-clockwise
quad_coords->push_back(osg::Vec3d(0, 0, -1));
quad_coords->push_back(osg::Vec3d(1, 0, -1));
quad_coords->push_back(osg::Vec3d(1, 1, -1));
quad_coords->push_back(osg::Vec3d(0, 1, -1));
osg::ref_ptr<osg::Vec2Array> quad_tcoords = new osg::Vec2Array; // texture coords
quad_tcoords->push_back(osg::Vec2(0, 0));
quad_tcoords->push_back(osg::Vec2(1, 0));
quad_tcoords->push_back(osg::Vec2(1, 1));
quad_tcoords->push_back(osg::Vec2(0, 1));
osg::ref_ptr<osg::Geometry> quad_geom = new osg::Geometry;
osg::ref_ptr<osg:rawArrays> quad_da = new osg::DrawArrays(osg::PrimitiveSet:UADS,0,4);
osg::ref_ptr<osg::Vec4Array> quad_colors = new osg::Vec4Array;
quad_colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f));
quad_geom->setVertexArray(quad_coords.get());
quad_geom->setTexCoordArray(0, quad_tcoords.get());
quad_geom->addPrimitiveSet(quad_da.get());
quad_geom->setColorArray(quad_colors.get(), osg::Array::BIND_OVERALL);
osg::StateSet *stateset = quad_geom->getOrCreateStateSet();
stateset->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
quad_geode->addDrawable(quad_geom.get());
osg::Projection* projnode = new osg::Projection;
projnode->setMatrix(osg::Matrix:rtho2D(0,1,0,1));
osg::MatrixTransform* mt= new osg::MatrixTransform();
mt->setMatrix(osg::Matrix::identity());
mt->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
mt->addChild(projnode);
projnode->addChild(quad_geode);
return mt;
}
int main(){
osgViewer::Viewer view ;
osg::Group* root = new osg::Group();
osg::Camera* fbocam = new osg::Camera();
root->addChild(fbocam);
osg::Camera* hudcam = new osg::Camera();
osg::Node* hud = createRTTQuad();
hudcam->addChild(hud);
root->addChild(hudcam);
osg::Node* scenenode = osgDB::readNodeFile("D:/worksdk/data/glider.osg");
fbocam->addChild(scenenode);
osg::Texture2D* tx2d = new osg::Texture2D;
tx2d->setTextureSize(1366,768);
tx2d->setInternalFormat(GL_RGB16F_ARB);
tx2d->setSourceFormat(GL_RGBA);
tx2d->setSourceType(GL_FLOAT);
fbocam->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
// fbocam->attach(osg::Camera::COLOR_BUFFER,tx2d);
// hud->getOrCreateStateSet()->setTextureAttributeAndModes(0,tx2d);
osg::Texture2DMultisample* tx2dms = new osg::Texture2DMultisample();
tx2dms->setTextureSize(1366,768);
tx2dms->setInternalFormat(GL_RGB16F_ARB);
fbocam->attach(osg::Camera::COLOR_BUFFER0,tx2dms);
hud->getOrCreateStateSet()->setTextureAttributeAndModes(0,tx2dms);
assginShder(hud,"D:/data/worksdk.txt");
view.setSceneData(root);
view.addEventHandler(new osgGA::StateSetManipulator);
view.run();
};
以上是测试的代码,使用texture2d关联FBO是没问题的,但是显卡不支持FBO多重采样,支持纹理多重采样纹理,所以想尝试使用Texture2dMultisample.但是怎么测试都会刷invalid frambuffer operation。
着色:sampler2DMS ColorTex;
void main(void){
gl_FragColor = texelFetch(ColorTex,gl_FragCoord.xy,0);
}; |
|