osgFBO(十)多pass-3,pass3,shader将背景从绿色变为蓝色
Posted directx3d_beginner
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了osgFBO(十)多pass-3,pass3,shader将背景从绿色变为蓝色相关的知识,希望对你有一定的参考价值。
pass3和pass2类似,只是再熟悉下,这个Pass设定为最后一步,可以不再输出纹理,
即
1,pass3摄像机输入tex2
osg::ref_ptr<osg::StateSet> stateset = pass3Camera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex2);
2,shader将背景从绿色变为蓝色
static const char* psShader3 =
“varying vec2 outTexCoord;”
“uniform sampler2D tex1;”
“void main(void)\\n”
“\\n”
“gl_FragColor = texture2D(tex1,outTexCoord);”
“if(gl_FragColor.xyz == vec3(0.0,1.0,0))”
“”
“gl_FragColor = vec4(0.0,0.0,1.0,1.0);”
""
"\\n"
;
3,pass3Root按顺序加上以前的所有摄像机,查看Pass3Root
osg::ref_ptr<osg::Group> pass3Root = new osg::Group();
pass3Root->addChild(sampleCamera0); //将摄像机加入场景
pass3Root->addChild(pass1Camera); //将摄像机加入场景
pass3Root->addChild(pass2Camera); //将摄像机加入场景
pass3Root->addChild(pass3Camera);
viewer->setSceneData(pass3Root);
运行结果
说明是正确的。完整代码如下:
#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>
#include <osg/Geometry>
#include <osg/Array>
#include <osg/primitiveset>
#include<osg/Geode>
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->setVertexAttribArray(1, texCoord, osg::Array::BIND_PER_VERTEX);
//geom->setTexCoordArray(0, texCoord);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS, 0, 4));
osg::ref_ptr<osg::Geode> geode = new osg::Geode;
geode->addDrawable(geom);
return geode;
static const char* vertexShader =
“in vec2 texCoord;\\n”
“varying vec2 outTexCoord;”
“void main(void)\\n”
“\\n”
“outTexCoord = texCoord;\\n”
" gl_Position = ftransform();\\n"
“\\n”
;
static const char* psShader =
“varying vec2 outTexCoord;”
“uniform sampler2D tex0;”
“void main(void)\\n”
“\\n”
“gl_FragColor = texture2D(tex0,outTexCoord);”
“if(gl_FragColor.xyz == vec3(0,0,0))”
“”
“gl_FragColor = vec4(1.0,0.0,0.0,1.0);”
""
"\\n"
;
static const char* psShader2 =
“varying vec2 outTexCoord;”
“uniform sampler2D tex1;”
“void main(void)\\n”
“\\n”
“gl_FragColor = texture2D(tex1,outTexCoord);”
“if(gl_FragColor.xyz == vec3(1.0,0,0))”
“”
“gl_FragColor = vec4(0.0,1.0,0.0,1.0);”
""
"\\n"
;
static const char* psShader3 =
“varying vec2 outTexCoord;”
“uniform sampler2D tex1;”
“void main(void)\\n”
“\\n”
“gl_FragColor = texture2D(tex1,outTexCoord);”
“if(gl_FragColor.xyz == vec3(0.0,1.0,0))”
“”
“gl_FragColor = vec4(0.0,0.0,1.0,1.0);”
""
"\\n"
;
int main()
osg::ref_ptrosgViewer::Viewer viewer = new osgViewer::Viewer;
//场景根
osg::ref_ptrosg::Group sceneRoot = new osg::Group();
std::string strFileName = “D:/tutorial/OpenSceneGraph-Data-3.0.0/cow.osg”;
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_ptrosg::Texture2D tex0 = createFloatRectangleTexture(texWidth, texHeight);
//绑定采样摄像机1
osg::ref_ptrosg::Camera sampleCamera0 = new osg::Camera;
//pass1Camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
//sampleCamera0->setRenderOrder(osg::Camera::RenderOrder::PRE_RENDER);
sampleCamera0->addChild(sceneRoot);
sampleCamera0->setClearColor(osg::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
sampleCamera0->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT); //这句话使内容不渲染到屏幕上
sampleCamera0->attach(osg::Camera::COLOR_BUFFER0, tex0); //关联采样贴图
sampleCamera0->setViewport(0, 0, texWidth, texHeight);//摄像机视口投影到纹理大小
//pass1摄像机
osg::ref_ptr<osg::Texture2D> tex1 = createFloatRectangleTexture(texWidth, texHeight);
osg::ref_ptr<osg::Camera> pass1Camera = new osg::Camera;
pass1Camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
//pass1Camera->setRenderOrder(osg::Camera::RenderOrder::PRE_RENDER);
pass1Camera->attach(osg::Camera::COLOR_BUFFER0, tex1);
osg::ref_ptr<osg::Geode> panelGeode_pass1 = createTexturePanelGeode();
pass1Camera->addChild(panelGeode_pass1);
osg::ref_ptr<osg::StateSet> stateset = pass1Camera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex0);
//对场景进行处理
osg::ref_ptr<osg::Shader> vs1 = new osg::Shader(osg::Shader::VERTEX, vertexShader);
osg::ref_ptr<osg::Shader> ps1 = new osg::Shader(osg::Shader::FRAGMENT, psShader);
osg::ref_ptr<osg::Program> program1 = new osg::Program;
program1->addShader(vs1);
program1->addShader(ps1);
program1->addBindAttribLocation("texCoord", 1);
osg::ref_ptr<osg::Uniform> tex0Uniform = new osg::Uniform("tex0", 0);
stateset->addUniform(tex0Uniform);
stateset->setAttribute(program1, osg::StateAttribute::ON);
osg::ref_ptr<osg::Group> pass1Root = new osg::Group;
pass1Root->addChild(sampleCamera0);
pass1Root->addChild(pass1Camera);
///
//pass2
osg::ref_ptr<osg::Texture2D> tex2 = createFloatRectangleTexture(texWidth, texHeight);
osg::ref_ptr<osg::Camera> pass2Camera = new osg::Camera;
pass2Camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
pass2Camera->attach(osg::Camera::COLOR_BUFFER0, tex2);
//pass2Camera->setRenderOrder(osg::Camera::RenderOrder::PRE_RENDER,1);
// pass2Camera->addChild(pass1Root);
osg::ref_ptr<osg::Geode> panelGeode = createTexturePanelGeode();
pass2Camera->addChild(panelGeode);
osg::ref_ptr<osg::StateSet> stateset = pass2Camera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex1);
#if 1
//对场景进行处理
osg::ref_ptrosg::Shader vs1 = new osg::Shader(osg::Shader::VERTEX, vertexShader);
osg::ref_ptrosg::Shader ps1 = new osg::Shader(osg::Shader::FRAGMENT, psShader2);
osg::ref_ptrosg::Program program1 = new osg::Program;
program1->addShader(vs1);
program1->addShader(ps1);
program1->addBindAttribLocation(“texCoord”, 1);
osg::ref_ptrosg::Uniform tex1Uniform = new osg::Uniform(“tex1”, 0);
stateset->addUniform(tex1Uniform);
stateset->setAttribute(program1, osg::StateAttribute::ON);
#endif
osg::ref_ptr<osg::Group> pass2Root = new osg::Group;
pass2Root->addChild(sampleCamera0);
pass2Root->addChild(pass1Camera);
pass2Root->addChild(pass2Camera);
//pass3
osg::ref_ptr<osg::Camera> pass3Camera = new osg::Camera;
pass3Camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
osg::ref_ptr<osg::Geode> panelGeode = createTexturePanelGeode();
pass3Camera->addChild(panelGeode);
osg::ref_ptr<osg::StateSet> stateset = pass3Camera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex2);
#if 1
//对场景进行处理
osg::ref_ptrosg::Shader vs1 = new osg::Shader(osg::Shader::VERTEX, vertexShader);
osg::ref_ptrosg::Shader ps1 = new osg::Shader(osg::Shader::FRAGMENT, psShader3);
osg::ref_ptrosg::Program program1 = new osg::Program;
program1->addShader(vs1);
program1->addShader(ps1);
program1->addBindAttribLocation(“texCoord”, 1);
osg::ref_ptrosg::Uniform tex1Uniform = new osg::Uniform(“tex1”, 0);
stateset->addUniform(tex1Uniform);
stateset->setAttribute(program1, osg::StateAttribute::ON);
#endif
osg::ref_ptr<osg::Group> pass3Root = new osg::Group();
pass3Root->addChild(sampleCamera0); //将摄像机加入场景
pass3Root->addChild(pass1Camera); //将摄像机加入场景
pass3Root->addChild(pass2Camera); //将摄像机加入场景
pass3Root->addChild(pass3Camera);
viewer->setSceneData(pass3Root);
viewer->run();
return 0;
osgFBO(九)多pass---2,pass2,shader将背景从红色变为绿色
二,pass2是比较完整的,同时有输入纹理和输出纹理
与pass1类似,这里只列出不同的地方
1,pass2摄像机输入tex1
osg::ref_ptr<osg::StateSet> stateset = pass2Camera->getOrCreateStateSet();
stateset->setTextureAttributeAndModes(0, tex1);
2,输出tex2
pass2Camera->attach(osg::Camera::COLOR_BUFFER0, tex2);
3,shader中,将红色背景改为绿色
static const char* psShader2 =
“varying vec2 outTexCoord;”
“uniform sampler2D tex1;”
“void main(void)\\n”
“\\n”
“gl_FragColor = texture2D(tex1,outTexCoord);”
“if(gl_FragColor.xyz == vec3(1.0,0,0))”
“”
“gl_FragColor = vec4(0.0,1.0,0.0,1.0);”
""
"\\n"
;
4,pass2Root按顺序加上以前的所有摄像机,查看Pass2Root
osg::ref_ptr<osg::Group> pass2Root = new osg::Group;
pass2Root->addChild(sampleCamera0);
pass2Root->addChild(pass1Camera);
pass2Root->addChild(pass2Camera);
viewer->setSceneData(pass2Root);
运行如下![在这里插入图片描述](https://img-blog.csdnimg.cn/aec15dcf4f0f47e88ea63fdb6e1b0a22.png)
以上是关于osgFBO(十)多pass-3,pass3,shader将背景从绿色变为蓝色的主要内容,如果未能解决你的问题,请参考以下文章