|
本帖最后由 smtKafka 于 2024-5-23 16:42 编辑
国内外论坛搜了一下没有相关的例子,尝试写了一个记录一下。重点说明:颜色和深度纹理都要写,且OSG_GL_TEXTURE_STORAGE设置一致。
- #include <osg/Group>
- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osg/Camera>
- #include <osgViewer/Viewer>
- #include <osgGA/StateSetManipulator>
- #include <osg/Texture2D>
- #include <osg/Texture2DMultisample>
- #include <Windows.h>
- int main()
- {
- putenv("OSG_GL_TEXTURE_STORAGE=DISABLE");
- const double w = 800;
- const double h = 600;
- // 创建场景图
- osg::ref_ptr<osg::Group> root = new osg::Group;
- // 创建采样相机
- osg::ref_ptr<osg::Camera> renderToTextureCamera = new osg::Camera;
- renderToTextureCamera->setRenderOrder(osg::Camera::PRE_RENDER);
- renderToTextureCamera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- renderToTextureCamera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 0.0));
- renderToTextureCamera->setViewport(0, 0, w, h);
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- // 创建红色矩形的顶点数组
- osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
- //vertices->push_back(osg::Vec3(-0.5, -0.5, 0.0));
- //vertices->push_back(osg::Vec3(0.5, -0.5, 0.0));
- //vertices->push_back(osg::Vec3(0.5, 0.5, 0.0));
- //vertices->push_back(osg::Vec3(-0.5, 0.5, 0.0));
- vertices->push_back(osg::Vec3(-0.5, 0.0, -0.5));
- vertices->push_back(osg::Vec3(0.5, 0.0, -0.5));
- vertices->push_back(osg::Vec3(0.5, 0.0, 0.5));
- vertices->push_back(osg::Vec3(-0.5, 0.0, 0.5));
- // 创建红色矩形的颜色数组
- osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
- colors->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0)); // 红色
- // 创建Geometry并设置顶点和颜色数组
- osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;
- geometry->setVertexArray(vertices);
- geometry->setColorArray(colors, osg::Array::BIND_OVERALL);
- geometry->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
- geode->addDrawable(geometry);
- // 添加红色球到采样相机
- renderToTextureCamera->addChild(geode);
- root->addChild(renderToTextureCamera);
- // 创建一个纹理用于存储渲染结果
- osg::ref_ptr<osg::Texture2D> textureColor = new osg::Texture2D;
- textureColor->setTextureSize(512, 512);
- textureColor->setInternalFormat(GL_RGBA);
- osg::ref_ptr<osg::Texture2D> textureDepth = new osg::Texture2D;
- textureDepth->setTextureSize(512, 512);
- textureDepth->setInternalFormat(GL_DEPTH_COMPONENT24);
- osg::ref_ptr<osg::Texture2DMultisample> textureMSColor = new osg::Texture2DMultisample(4, true);
- textureMSColor->setTextureSize(512, 512);
- textureMSColor->setInternalFormat(GL_RGBA);
- osg::ref_ptr<osg::Texture2DMultisample> textureMSDepth = new osg::Texture2DMultisample(4, true);
- textureMSDepth->setTextureSize(512, 512);
- textureMSDepth->setInternalFormat(GL_DEPTH_COMPONENT24);
- // 设置渲染到纹理的相机的纹理
- renderToTextureCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
- renderToTextureCamera->setRenderOrder(osg::Camera::POST_RENDER);
- bool isMultisample = true;
- if (isMultisample)
- {
- renderToTextureCamera->attach(osg::Camera::COLOR_BUFFER, textureMSColor);
- renderToTextureCamera->attach(osg::Camera::DEPTH_BUFFER, textureMSDepth);
- }
- else
- {
- renderToTextureCamera->attach(osg::Camera::COLOR_BUFFER, textureColor);
- //renderToTextureCamera->attach(osg::Camera::DEPTH_BUFFER, textureDepth);
- }
- // 创建HUD相机
- osg::ref_ptr<osg::Camera> hudCamera = new osg::Camera;
- hudCamera->setClearMask(GL_DEPTH_BUFFER_BIT);
- hudCamera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
- hudCamera->setViewMatrix(osg::Matrix::identity());
- hudCamera->setProjectionMatrix(osg::Matrix::ortho2D(0, w, 0, h));
- hudCamera->setRenderOrder(osg::Camera::POST_RENDER);
- hudCamera->setViewport(0, 0, w, h);
- hudCamera->setAllowEventFocus(false);
- hudCamera->setCullingActive(false);
- // 创建顶点着色器
- osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX, R"(
- #version 430
- layout(location = 0) in vec3 VertexPosition;
- out vec2 TexCoord;
- void main()
- {
- TexCoord = (VertexPosition.xy + vec2(1.0)) * 0.5; // 转换到纹理坐标范围 [0, 1]
- gl_Position = vec4(VertexPosition, 1);
- }
- )");
- // 创建带有多重采样支持的片段着色器
- osg::ref_ptr<osg::Shader> fragShaderMS = new osg::Shader(osg::Shader::FRAGMENT, R"(
- #version 430
- #extension GL_ARB_texture_multisample : enable
- in vec2 TexCoord;
- out vec4 FragColor;
- uniform sampler2DMS tex; // 多重采样纹理
- void main()
- {
- ivec2 texSize = textureSize(tex);
- ivec2 coord = ivec2(TexCoord * vec2(texSize));
- vec4 a1 = texelFetch(tex, coord, 0);
- vec4 a2 = texelFetch(tex, coord, 1);
- vec4 a3 = texelFetch(tex, coord, 2);
- vec4 a4 = texelFetch(tex, coord, 3);
- //FragColor = (a1+a2+a3+a4)/4.0;
- FragColor = a1;
- }
- )");
- // 创建不带多重采样的片段着色器
- osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT, R"(
- #version 430
- in vec2 TexCoord;
- out vec4 FragColor;
- uniform sampler2D tex;
- void main()
- {
- FragColor = texture(tex, TexCoord);
- }
- )");
- // 创建Program,并将Shader附加到其中
- osg::ref_ptr<osg::Program> program = new osg::Program;
- program->addShader(vertShader);
- if (isMultisample)
- {
- program->addShader(fragShaderMS);
- }
- else
- {
- program->addShader(fragShader);
- }
- // 将Program设置到hudGeometry的StateSet中
- hudCamera->getOrCreateStateSet()->setAttributeAndModes(program, osg::StateAttribute::ON);
- // 创建HUD相机中显示纹理的节点
- osg::ref_ptr<osg::Geode> hudGeode = new osg::Geode;
- osg::ref_ptr<osg::Geometry> hudGeometry = osg::createTexturedQuadGeometry(osg::Vec3(-1.0f, -1.0f, 0.0f), osg::Vec3(2.0f, 0.0f, 0.0f), osg::Vec3(0.0f, 2.0f, 0.0f));
- hudGeode->addDrawable(hudGeometry);
- // 设置HUD相机的纹理
- if (isMultisample)
- {
- hudCamera->getOrCreateStateSet()->setTextureAttributeAndModes(0, textureMSColor, osg::StateAttribute::ON);
- }
- else
- {
- hudCamera->getOrCreateStateSet()->setTextureAttributeAndModes(0, textureColor, osg::StateAttribute::ON);
- }
- hudCamera->getOrCreateStateSet()->addUniform(new osg::Uniform("tex", 0));
- hudGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
- hudCamera->addChild(hudGeode);
- root->addChild(hudCamera);
- // 创建查看器
- osgViewer::Viewer viewer;
- viewer.setSceneData(root);
- // 添加事件处理器
- viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
- viewer.setUpViewInWindow(100, 100, w, h);
- viewer.run();
- }
复制代码
|
-
|