相关文章推荐
灰常酷的口罩  ·  C# ...·  1 月前    · 
追风的牛肉面  ·  python pandas ...·  2 月前    · 
大鼻子的椅子  ·  javascript - js ...·  1 年前    · 
首发于 科研利器

OSG RTT 离屏渲染

目录

[toc]


以前都是在osg的格式里打滚,终于接触到osg的渲染了。主要结合 《最长的一帧》 对个人的理解进行整理。

实例

    //图形上下文
    int width = 512, height = 512;
    osg::ref_ptr<osg::GraphicsContext::Traits>traits = new osg::GraphicsContext::Traits;
    traits->x = 0;
    traits->y = 0;
    traits->width = width;
    traits->height = height;
    traits->pbuffer = true;
    osg::ref_ptr<osg::GraphicsContext>gc = osg::GraphicsContext::createGraphicsContext(traits);
    //设置窗口属性,就会显示viewer
    //viewer->setUpViewInWindow(traits->x, traits->y, width, height);
    //场景模型
    osg::ref_ptr<osg::Node>model = osgDB::readNodeFile("E:/osgb/Tile_+005_+004_L18_000t3.osgb");
    osg::ComputeBoundsVisitor boundVisitor;
    model->accept(boundVisitor);
    osg::BoundingBox bs = boundVisitor.getBoundingBox();
    double r = bs.radius();
    double cenX = bs.center().x();
    double cenY = bs.center().y();
    double cenZ = bs.center().z();
    osg::Vec3d center = bs.center();
    width = 400; height = 400;
    osg::Vec3d eye0 = center + osg::Vec3d(0, 0, 1) * r;//上
    osg::ref_ptr<osg::Image> image0 = new osg::Image();
    image0->allocateImage(width, height, 1, GL_RGB, GL_UNSIGNED_BYTE);
    osg::ref_ptr<osg::Camera>rttCamera0 = new osg::Camera;
    rttCamera0->setViewport(0, 0, width, height);
    rttCamera0->setGraphicsContext(gc);
    rttCamera0->addChild(model);
    rttCamera0->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 0.0f));
    rttCamera0->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    rttCamera0->setRenderOrder(osg::Camera::PRE_RENDER);//最先渲染
    rttCamera0->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
    rttCamera0->attach(osg::Camera::COLOR_BUFFER, image0.get());
    rttCamera0->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
    rttCamera0->setViewMatrixAsLookAt(eye0, center, osg::Vec3d(0, 1, 0));
    rttCamera0->setProjectionMatrixAsOrtho(bs.xMin() - cenX, bs.xMax() - cenX, bs.yMin() - cenY, bs.yMax() - cenY, 0.01, 9999);
    //添加相机为从属相机
    viewer->addSlave(rttCamera0, false);
    //绘制一帧
    viewer->frame();
    osgDB::writeImageFile(*image0, "xxx.jpg");