随笔 - 833  文章 - 1  评论 - 106  阅读 - 200万

osgMulitiplerendertargets sample 中fbo使用【HTC VIVE开发中应用】

osgmultiplerendertargets.cpp  

  ......................................

  // now create the camera to do the multiple render to texture
    {
        osg::Camera* camera = new osg::Camera;

        // set up the background color and clear mask.
        camera->setClearColor(osg::Vec4(0.1f,0.1f,0.3f,1.0f));
        camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // the camera is going to look at our input quad
        camera->setProjectionMatrix(osg::Matrix::ortho2D(0,1,0,1));
        camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
        camera->setViewMatrix(osg::Matrix::identity());

        // set viewport
        camera->setViewport(0, 0, tex_width, tex_height);

        // set the camera to render before the main camera.
        camera->setRenderOrder(osg::Camera::PRE_RENDER);

        // tell the camera to use OpenGL frame buffer objects
        camera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);

        // attach the textures to use
        for (int i=0; i<NUM_TEXTURES; i++) {
            if (useMultiSample)// 使用多重渲染方式消除锯齿现象
                camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+i), textureRect[i], 0, 0, false, 4, 4);
            else
                camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0+i), textureRect[i]);
        }

        // we can also read back any of the targets as an image, modify this image and push it back

    // 绑定一张image获取fbo渲染结果
        if (useImage) {
            // which texture to get the image from
            const int tex_to_get = 0;

            osg::Image* image = new osg::Image;
            if (useHDR) {
                image->allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_FLOAT);
            } else {
                image->allocateImage(tex_width, tex_height, 1, GL_RGBA, GL_UNSIGNED_BYTE);
            }

            // attach the image so its copied on each frame.
            camera->attach(osg::Camera::BufferComponent(osg::Camera::COLOR_BUFFER0 + tex_to_get), image);

            camera->setPostDrawCallback(new MyCameraPostDrawCallback(image));

            // push back the image to the texture
            textureRect[tex_to_get]->setImage(0, image);
        }

        // add the subgraph to render
        camera->addChild(cam_subgraph);

        parent->addChild(camera);
    }

posted on   3D入魔  阅读(745)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示