From Jaromir Vitek, "In attachment are another fixes for using packed depth+stencil (PDS).
* When used PDS RenderStage::runCameraSetUp sets flag that FBO has already stencil,depth buffer attached. Prevents adding next depth buffer. * Sets correct traits for p-buffer if used PDS and something goes wrong with FBO setup or p-buffer is used directly. * Adds warning to camera if user add depth/stencil already attached through PDS. * Sets blitMask when use blit to resolve buffer. There is also new example with using multisampled FBO."
This commit is contained in:
@@ -1,3 +1,21 @@
|
||||
/* OpenSceneGraph example, osgpackeddepthstencil.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <osg/GLExtensions>
|
||||
#include <osg/Node>
|
||||
#include <osg/Geometry>
|
||||
@@ -6,10 +24,12 @@
|
||||
#include <osg/Stencil>
|
||||
#include <osg/ColorMask>
|
||||
#include <osg/Geode>
|
||||
#include <osg/FrameBufferObject>
|
||||
|
||||
#include <osgViewer/Viewer>
|
||||
#include <osgViewer/ViewerEventHandlers>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
osg::Geode* createMask()
|
||||
{
|
||||
@@ -97,12 +117,36 @@ int main( int argc, char **argv )
|
||||
// use an ArgumentParser object to manage the program arguments.
|
||||
osg::ArgumentParser arguments(&argc,argv);
|
||||
|
||||
arguments.getApplicationUsage()->addCommandLineOption("--fbo","Use Frame Buffer Object for render to texture, where supported.");
|
||||
arguments.getApplicationUsage()->addCommandLineOption("--pbuffer-rtt","Use Pixel Buffer for render to texture, where supported.");
|
||||
arguments.getApplicationUsage()->addCommandLineOption("--nopds", "Don't use packed depth stencil.");
|
||||
arguments.getApplicationUsage()->addCommandLineOption("--fbo-samples","");
|
||||
arguments.getApplicationUsage()->addCommandLineOption("--color-samples", "");
|
||||
|
||||
// construct the viewer.
|
||||
osgViewer::Viewer viewer(arguments);
|
||||
|
||||
// add stats
|
||||
viewer.addEventHandler( new osgViewer::StatsHandler() );
|
||||
|
||||
// if user request help write it out to cout.
|
||||
if (arguments.read("-h") || arguments.read("--help"))
|
||||
{
|
||||
arguments.getApplicationUsage()->write(std::cout);
|
||||
return 1;
|
||||
}
|
||||
|
||||
osg::Camera::RenderTargetImplementation renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT;
|
||||
int colorSamples = 0, samples = 0;
|
||||
bool usePDS = true;
|
||||
|
||||
while (arguments.read("--fbo")) { renderImplementation = osg::Camera::FRAME_BUFFER_OBJECT; }
|
||||
while (arguments.read("--pbuffer-rtt")) { renderImplementation = osg::Camera::PIXEL_BUFFER_RTT; }
|
||||
while (arguments.read("--nopds")) { usePDS = false; }
|
||||
while (arguments.read("--fbo-samples", samples)) {}
|
||||
while (arguments.read("--color-samples", colorSamples)) {}
|
||||
|
||||
|
||||
osg::Group* rootNode = new osg::Group;
|
||||
rootNode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
|
||||
|
||||
@@ -125,19 +169,21 @@ int main( int argc, char **argv )
|
||||
rttCamera->setViewMatrix(osg::Matrixd::identity());
|
||||
rttCamera->setViewport(0, 0, 1024, 1024);
|
||||
rttCamera->setRenderOrder(osg::Camera::PRE_RENDER);
|
||||
rttCamera->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
|
||||
|
||||
#define USE_PACKED_DEPTH_STENCIL
|
||||
rttCamera->setRenderTargetImplementation(renderImplementation);
|
||||
|
||||
#ifdef USE_PACKED_DEPTH_STENCIL
|
||||
rttCamera->attach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, GL_DEPTH_STENCIL_EXT);
|
||||
#else
|
||||
// this doesn't work on NVIDIA/Vista 64bit
|
||||
// FBO status = 0x8cd6 (FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT)
|
||||
rttCamera->attach(osg::Camera::DEPTH_BUFFER, GL_DEPTH_COMPONENT);
|
||||
rttCamera->attach(osg::Camera::STENCIL_BUFFER, GL_STENCIL_INDEX_EXT);
|
||||
#endif
|
||||
rttCamera->attach(osg::Camera::COLOR_BUFFER, texture);
|
||||
if(usePDS)
|
||||
{
|
||||
rttCamera->attach(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER, GL_DEPTH_STENCIL_EXT);
|
||||
}
|
||||
else
|
||||
{
|
||||
// this doesn't work on NVIDIA/Vista 64bit
|
||||
// FBO status = 0x8cd6 (FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT)
|
||||
rttCamera->attach(osg::Camera::DEPTH_BUFFER, GL_DEPTH_COMPONENT);
|
||||
rttCamera->attach(osg::Camera::STENCIL_BUFFER, GL_STENCIL_INDEX8_EXT);
|
||||
}
|
||||
|
||||
rttCamera->attach(osg::Camera::COLOR_BUFFER, texture, 0, 0, false, samples, colorSamples);
|
||||
rttCamera->setCullingMode(osg::Camera::VIEW_FRUSTUM_SIDES_CULLING);
|
||||
|
||||
// creates rtt subtree
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
#define GL_EXT_framebuffer_object 1
|
||||
#define GL_FRAMEBUFFER_EXT 0x8D40
|
||||
#define GL_RENDERBUFFER_EXT 0x8D41
|
||||
#define GL_STENCIL_INDEX_EXT 0x8D45
|
||||
// #define GL_STENCIL_INDEX_EXT 0x8D45 // removed in rev. #114 of the spec
|
||||
#define GL_STENCIL_INDEX1_EXT 0x8D46
|
||||
#define GL_STENCIL_INDEX4_EXT 0x8D47
|
||||
#define GL_STENCIL_INDEX8_EXT 0x8D48
|
||||
|
||||
@@ -255,6 +255,37 @@ void Camera::getViewMatrixAsLookAt(Vec3f& eye,Vec3f& center,Vec3f& up,float look
|
||||
|
||||
void Camera::attach(BufferComponent buffer, GLenum internalFormat)
|
||||
{
|
||||
switch(buffer)
|
||||
{
|
||||
case DEPTH_BUFFER:
|
||||
if(_bufferAttachmentMap.find(PACKED_DEPTH_STENCIL_BUFFER) != _bufferAttachmentMap.end())
|
||||
{
|
||||
notify(WARN)
|
||||
<< "Camera: DEPTH_BUFFER already attached as PACKED_DEPTH_STENCIL_BUFFER !"
|
||||
<< std::endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case STENCIL_BUFFER:
|
||||
if(_bufferAttachmentMap.find(PACKED_DEPTH_STENCIL_BUFFER) != _bufferAttachmentMap.end())
|
||||
{
|
||||
notify(WARN)
|
||||
<< "Camera: STENCIL_BUFFER already attached as PACKED_DEPTH_STENCIL_BUFFER !"
|
||||
<< std::endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case PACKED_DEPTH_STENCIL_BUFFER:
|
||||
if(_bufferAttachmentMap.find(DEPTH_BUFFER) != _bufferAttachmentMap.end())
|
||||
{
|
||||
notify(WARN) << "Camera: DEPTH_BUFFER already attached !" << std::endl;
|
||||
}
|
||||
if(_bufferAttachmentMap.find(STENCIL_BUFFER) != _bufferAttachmentMap.end())
|
||||
{
|
||||
notify(WARN) << "Camera: STENCIL_BUFFER already attached !" << std::endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
_bufferAttachmentMap[buffer]._internalFormat = internalFormat;
|
||||
}
|
||||
|
||||
|
||||
@@ -388,6 +388,9 @@ void RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo)
|
||||
case Camera::STENCIL_BUFFER:
|
||||
internalFormat = GL_STENCIL_INDEX8_EXT;
|
||||
break;
|
||||
case Camera::PACKED_DEPTH_STENCIL_BUFFER:
|
||||
internalFormat = GL_DEPTH_STENCIL_EXT;
|
||||
break;
|
||||
default:
|
||||
internalFormat = GL_RGBA;
|
||||
break;
|
||||
@@ -401,7 +404,13 @@ void RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo)
|
||||
|
||||
if (buffer==osg::Camera::DEPTH_BUFFER) depthAttached = true;
|
||||
else if (buffer==osg::Camera::STENCIL_BUFFER) stencilAttached = true;
|
||||
else if (buffer==osg::Camera::PACKED_DEPTH_STENCIL_BUFFER)
|
||||
{
|
||||
depthAttached = true;
|
||||
stencilAttached = true;
|
||||
}
|
||||
else if (buffer>=osg::Camera::COLOR_BUFFER) colorAttached = true;
|
||||
|
||||
}
|
||||
|
||||
if (!depthAttached)
|
||||
@@ -559,6 +568,13 @@ void RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo)
|
||||
stencilAttached = true;
|
||||
break;
|
||||
}
|
||||
case(osg::Camera::PACKED_DEPTH_STENCIL_BUFFER):
|
||||
{
|
||||
traits->depth = 24;
|
||||
depthAttached = true;
|
||||
traits->stencil = 8;
|
||||
stencilAttached = true;
|
||||
}
|
||||
case(osg::Camera::COLOR_BUFFER):
|
||||
{
|
||||
if (attachment._internalFormat!=GL_NONE)
|
||||
@@ -862,6 +878,8 @@ void RenderStage::drawInner(osg::RenderInfo& renderInfo,RenderLeaf*& previous, b
|
||||
case Camera::STENCIL_BUFFER:
|
||||
blitMask |= GL_STENCIL_BUFFER_BIT;
|
||||
break;
|
||||
case Camera::PACKED_DEPTH_STENCIL_BUFFER:
|
||||
blitMask |= GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
|
||||
default:
|
||||
blitMask |= GL_COLOR_BUFFER_BIT;
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user