osg fbo(一),生成颜色缓冲区图片

2023-10-30

由于工作需要,重新捡了下shader。很明显,fbo是重中之重。好记性不如烂笔头,先记录下

1,生成一个颜色纹理(为了省事,可以将纹理宽高=屏幕宽高)

osg::ref_ptr<osg::Texture2D> tex = createFloatRectangleTexture(texWidth, texHeight);

2,采样摄像机添加场景根,并把场景根的颜色缓冲区与纹理关联,

	sampleCamera->addChild(sceneRoot);
	sampleCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); //这句话使内容不渲染到屏幕上
	sampleCamera->attach(osg::Camera::COLOR_BUFFER0, tex); //关联颜色贴图

3,将纹理关联到面片上,以方便加载使用

osg::ref_ptr<osg::Geode> panelGeode = createTexturePanelGeode();
osg::ref_ptr<osg::StateSet> ss = panelGeode->getOrCreateStateSet();
ss->setTextureAttributeAndModes(0, tex);

4,这个面片关联的纹理,由于是在摄像机坐标系下,所以要规格化坐标系,位置【-1,1】
纹理坐标【0,1】

osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-1.0f, -1.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, -1.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));
vertices->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f));

osg::ref_ptr<osg::Vec2Array> texCoord = new osg::Vec2Array;
texCoord->push_back(osg::Vec2(0.0, 0.0));
texCoord->push_back(osg::Vec2(1.0, 0.0));
texCoord->push_back(osg::Vec2(1.0, 1.0));
texCoord->push_back(osg::Vec2(0.0, 1.0));

4,三维转二维,就是贴图片的过程,FBO中的图片一般还要进行一个图像处理,所以要设置一个passRoot,用于显示。这个passroot要同时包含采样摄像机和面片才行。

passRoot->addChild(sampleCamera); //将摄像机加入场景
passRoot->addChild(panelGeode);
viewer->setSceneData(passRoot);

运行结果如下:
在这里插入图片描述
代码如下:

#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osg/CoordinateSystemNode>

#include <osg/Switch>
#include <osg/Types>
#include <osgText/Text>

#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>

#include <osgGA/TrackballManipulator>
#include <osgGA/FlightManipulator>
#include <osgGA/DriveManipulator>
#include <osgGA/KeySwitchMatrixManipulator>
#include <osgGA/StateSetManipulator>
#include <osgGA/AnimationPathManipulator>
#include <osgGA/TerrainManipulator>
#include <osgGA/SphericalManipulator>

#include <osgGA/Device>
#include <osg/Shader>

osg::ref_ptrosg::Texture2D createFloatRectangleTexture(int width, int height)
{
osg::ref_ptrosg::Texture2D tex2D = new osg::Texture2D;
tex2D->setTextureSize(width, height);
tex2D->setInternalFormat(GL_RGBA16F_ARB);
tex2D->setSourceFormat(GL_RGBA);
tex2D->setSourceType(GL_FLOAT);
return tex2D.release();
}
osg::ref_ptrosg::Geode createTexturePanelGeode()
{
osg::ref_ptrosg::Vec3Array vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(-1.0f, -1.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, -1.0f, 0.0f));
vertices->push_back(osg::Vec3(1.0f, 1.0f, 0.0f));
vertices->push_back(osg::Vec3(-1.0f, 1.0f, 0.0f));

osg::ref_ptr<osg::Vec2Array> texCoord = new osg::Vec2Array;
texCoord->push_back(osg::Vec2(0.0, 0.0));
texCoord->push_back(osg::Vec2(1.0, 0.0));
texCoord->push_back(osg::Vec2(1.0, 1.0));
texCoord->push_back(osg::Vec2(0.0, 1.0));

osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
geom->setVertexArray(vertices);
geom->setTexCoordArray(0, texCoord);
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geom);
osg::ref_ptr<osg::StateSet> set1 = geode->getOrCreateStateSet();
set1->setMode(GL_LIGHTING, osg::StateAttribute::OFF); //设置不受光照影响,不然太暗了就看不清楚
return geode;

}

int main()
{
osg::ref_ptrosgViewer::Viewer viewer = new osgViewer::Viewer;
std::string strFileName = “D:/OpenSceneGraph-master/OpenSceneGraph-Data-master/cow.osg”;
//pass1的根
osg::ref_ptrosg::Group passRoot = new osg::Group();
//场景根
osg::ref_ptrosg::Group sceneRoot = new osg::Group();
osg::ref_ptrosg::Node node = osgDB::readNodeFile(strFileName);
sceneRoot->addChild(node);

//获取系统分辨率
unsigned int screenWidth, screenHeight;
osg::GraphicsContext::WindowingSystemInterface * wsInterface = osg::GraphicsContext::getWindowingSystemInterface();
if (!wsInterface)
{
	return -1;
}
wsInterface->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(0), screenWidth, screenHeight);
int texWidth = screenWidth;
int texHeight = screenHeight;
osg::ref_ptr<osg::Texture2D> tex = createFloatRectangleTexture(texWidth, texHeight);
//绑定采样摄像机
osg::ref_ptr<osg::Camera> sampleCamera = new osg::Camera;
{
	sampleCamera->addChild(sceneRoot);
	sampleCamera->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
	sampleCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); //这句话使内容不渲染到屏幕上
	sampleCamera->attach(osg::Camera::COLOR_BUFFER0, tex); //关联颜色贴图
	//摄像机关联视口
	sampleCamera->setViewport(0, 0, screenWidth, screenHeight);

}
osg::ref_ptr<osg::Geode> panelGeode = createTexturePanelGeode();
osg::ref_ptr<osg::StateSet> ss = panelGeode->getOrCreateStateSet();
ss->setTextureAttributeAndModes(0, tex);

;
passRoot->addChild(sampleCamera); //将摄像机加入场景
passRoot->addChild(panelGeode);
viewer->setSceneData(passRoot);
viewer->run();
return 0;
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

osg fbo(一),生成颜色缓冲区图片 的相关文章

  • osg--读写

    文件I O 命名规则 osgdb xxx 比如 osgdb osg osgdb jpeg 关联文件后缀和加载器 osgDB Registry instance gt addFileExtensionAlias jpeg jpeg osgDB
  • 3ds max文件导出osg或者ive格式

    osg osgEarth系列文章目录 文章目录 osg osgEarth系列文章目录 前言 参考 前言 首先下载插件osgexp Osgexp的下载地址 安装上之后 如果3ds max导出里面已经可以选择导出ive或者osg 恭喜你 如果没
  • Array王锐大神力作:osg与PhysX结合系列内容——第3节 地形碰撞体

    Array王锐大神力作 osg与PhysX结合系列内容 地形碰撞体 烘焙 物理碰撞体 HeightField与TriangleMesh 物理材质的概念与使用 直接读取高度图数据 与osg HeightField结合使用 Pvd调试环境 构建
  • osg传递数组到shader,(以qedl为例)

    一 对float类型的数组 osg ref ptrosg FloatArray m dampingPixelDist 赋值 void ccBilateralFilter updateDampingTable m dampingPixelDi
  • osgFBO(十)多pass-3,pass3,shader将背景从绿色变为蓝色

    pass3和pass2类似 只是再熟悉下 这个Pass设定为最后一步 可以不再输出纹理 即 1 pass3摄像机输入tex2 osg ref ptr
  • qedl中的fixDepth()简化

    如果将PerspectiveMode的设置为1 则会传递zNear和Zfar 在fixDepth 中 而将perspectiveMode 0 则大大简化fixDepth float fixDepth float depth return c
  • osg fbo(一),生成颜色缓冲区图片

    由于工作需要 重新捡了下shader 很明显 fbo是重中之重 好记性不如烂笔头 先记录下 1 生成一个颜色纹理 为了省事 可以将纹理宽高 屏幕宽高 osg ref ptr
  • 调试最长的一帧(第七天)

    先看看总体进展 eventTraversal函数的任务 在每帧仿真过程中 取出已经发生的所有事件 摒弃哪些对场景不会有助益的 比如 在视口外的鼠标事件 依次交付给各个事件处理器 最后清空现有的事件队列 等待下一帧的到来 在View的几个成员
  • osg学习(七十四)Type mismatch in arithmetic operation between ‘vec2‘ and ‘int‘

    可能是手机端语法检查更严格 glsl语句是这样的 再桌面端执行没有问题 在手机端执行会提示上述错误 vec3 tmpNormal osg NormalMatrix osg Normal tmpNormal normalize tmpNorm
  • osgcuda

    osgcuda 转 原文 http blog sina com cn s blog df1b276a0101inbi html osgCompute是对代码的并行流处理器执行的抽象基库 库连接到OSG的 OSG 因此它可以被包括在场景图 它
  • 调试最长的一帧(第12天)

    先看看总体 流程走到了更新分页数据库 分页数据库的数据流图 先找上图的4个成员变量 上图中 左侧的图框表示数据的检索和输入 中间的白色图框表示用于数据存储的内存空间 而右边的图框表示存储数据的输出 此外 蓝绿色图框表示可以在DataBase
  • osgFBO(九)多pass---2,pass2,shader将背景从红色变为绿色

    二 pass2是比较完整的 同时有输入纹理和输出纹理 与pass1类似 这里只列出不同的地方 1 pass2摄像机输入tex1 osg ref ptr
  • 调试最长的一帧(第八天)

    先看看总体进度 先获取所有的图形上下文 然后进行checkEvents 请求分发消息并通过takeEvents 获取交互事件 再交由GUIEventHandler处理交互事件 中间的步骤 在checkeEvents里面 消息分发函数 消息处
  • OpenSceneGraph-OpenSceneGraph-3.6.5源码编译

    前言 准备 git 不是必须 使用git得到的源码是3 6 5版本的 CMake vs2019 VS017可以 我这里用的vs2019 osg主页 源码下载 Cmake编译源码 编译报错 CMake Warning dev at F Pro
  • osg报错:错误(活动) E0757 变量 “GLenum“ 不是类型名

    前言 osg报错 错误 活动 E0757 变量 GLenum 不是类型名 原因 osg中封装了openGL的库 感觉vs2019无法识别openGL相关的部分 解决 vs2019中配置 预处理器
  • osgEarth的Rex引擎原理分析(四十八)osgEarth::Drivers::RexTerrainEngine::DrawState的作用

    目标 四十五 中的110 每帧都会创建一个DrawState 创建在TerrainRenderData setup 这里存在内存泄漏的问题 因为只有new没有delete 不存在的 drawState是智能指针 超出范围时会自动销毁 也就是
  • osgEarth的Rex引擎原理分析(十五)分页瓦片加载器在更新遍历时对请求处理过程

    目标 十四 中的34 osgEarthDrivers engine rex Loader cpp void PagerLoader traverse osg NodeVisitor nv for count 0 count lt merge
  • osg漫游器

    在三维中常见的就是漫游整个场景 所谓漫游就是 观察者的视线从一个位置移动到另外一个位置或者在希望的方向上移动 在OSG中漫游可以通过改变观察者 相机 的位置和姿态来实现 三维世界中的模型的相对位置和形态不会发生变化 只是观察者的角度和位置发
  • osg orbitManipulator拖拽位置不精确的问题解决。

    实际上 就是个焦距和视距的问题 摄像机在不同的位置 远近切面会改变 但是fovy和aspectRatio不改变 这样 也会导致近切面的宽高发生改变 就类似于一个无限延伸的视锥体滑梯 远近切面就在这滑梯上滑动 由于最终物体要投影到近切面 而近
  • OSG中几何体的绘制(二)

    5 几何体操作 在本章的前言中就讲到 场景都是由基本的绘图基元构成的 基本的绘图基元构成简单的几何体 简单的几何体构成复杂的几何体 复杂的几何体最终构造成复杂的场景 当多个几何体组合时 可能存在多种降低场景渲染效率的原因 在很多3D引擎中

随机推荐