查看: 1737|回复: 3

求解Texture2DMultisample的正确用法

[复制链接]

该用户从未签到

发表于 2013-7-28 19:23:26 | 显示全部楼层 |阅读模式
#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);
};

该用户从未签到

发表于 2013-8-4 18:17:37 | 显示全部楼层
按照原作者的说法,基本上这么做就可以了
  1. tex2DMS = new Texture2DMultisample(4,false);
  2. camera->attach(osg::Camera::COLOR_BUFFER, tex2DMS);
复制代码

该用户从未签到

 楼主| 发表于 2013-8-16 10:31:49 | 显示全部楼层
array 发表于 2013-8-4 18:17
按照原作者的说法,基本上这么做就可以了

不需要设置纹理大小和格式?

该用户从未签到

发表于 2013-8-20 09:59:15 | 显示全部楼层
其他该设置的项当然还是要设置的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表