From 9b34bc5c86c205d2d3968873d2a9046a4712d1f1 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 21 Jul 2005 19:37:44 +0000 Subject: [PATCH] Added GraphicsContextImplementation --- VisualStudio/osgProducer/osgProducer.dsp | 8 ++ .../osgProducer/GraphicsContextImplementation | 73 ++++++++++ .../GraphicsContextImplementation.cpp | 129 ++++++++++++++++++ 3 files changed, 210 insertions(+) create mode 100644 include/osgProducer/GraphicsContextImplementation create mode 100644 src/osgProducer/GraphicsContextImplementation.cpp diff --git a/VisualStudio/osgProducer/osgProducer.dsp b/VisualStudio/osgProducer/osgProducer.dsp index 48f179d83..43a95fcd6 100755 --- a/VisualStudio/osgProducer/osgProducer.dsp +++ b/VisualStudio/osgProducer/osgProducer.dsp @@ -103,6 +103,10 @@ SOURCE=..\..\src\osgProducer\KeyboardMouseCallback.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osgProducer\GraphicsContextImplementation.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osgProducer\OsgCameraGroup.cpp # End Source File # Begin Source File @@ -139,6 +143,10 @@ SOURCE=..\..\Include\osgProducer\KeyboardMouseCallback # End Source File # Begin Source File +SOURCE=..\..\Include\osgProducer\GraphicsContextImplementation +# End Source File +# Begin Source File + SOURCE=..\..\include\osgProducer\OsgCameraGroup # End Source File # Begin Source File diff --git a/include/osgProducer/GraphicsContextImplementation b/include/osgProducer/GraphicsContextImplementation new file mode 100644 index 000000000..00668955f --- /dev/null +++ b/include/osgProducer/GraphicsContextImplementation @@ -0,0 +1,73 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGPRODUCER_GRAPHICSCONTEXTIMPLEMENTATION +#define OSGPRODUCER_GRAPHICSCONTEXTIMPLEMENTATION 1 + + +#include + +#include + +#include + + + +namespace osgProducer { + +class OSGPRODUCER_EXPORT GraphicsContextImplementation : public osg::GraphicsContext +{ + public : + + /** Construct a graphics context to specified traits.*/ + GraphicsContextImplementation(Traits* traits); + + /** Construct a graphics context with specified RenderSurface.*/ + GraphicsContextImplementation(Producer::RenderSurface* rs); + + /** Return true of graphics context is realized.*/ + bool isRealized() { return _rs.valid() && _rs->isRealized(); } + + /** Return the RenderSurface that implements the graphics context.*/ + Producer::RenderSurface* getRenderSurface() { return _rs.get(); } + + /** Return the const RenderSurface that implements the graphics context.*/ + const Producer::RenderSurface* getRenderSurface() const { return _rs.get(); } + + + /** Release the graphics context.*/ + virtual void release(); + + /** Make this graphics context current.*/ + virtual void makeCurrent(); + + /** Make this graphics context current with specified read context.*/ + virtual void makeContextCurrent(GraphicsContext* readContext); + + /** Bind the graphics context to associated texture.*/ + virtual void bindPBufferToTexture(GLenum buffer); + + /** swap the front and back buffers.*/ + virtual void swapBuffers(); + + + protected: + + virtual ~GraphicsContextImplementation(); + + osg::ref_ptr _rs; +}; + +} + +#endif diff --git a/src/osgProducer/GraphicsContextImplementation.cpp b/src/osgProducer/GraphicsContextImplementation.cpp new file mode 100644 index 000000000..aad93ff71 --- /dev/null +++ b/src/osgProducer/GraphicsContextImplementation.cpp @@ -0,0 +1,129 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include +#include + +using namespace osgProducer; + +namespace osgProducer +{ + struct MyCreateGraphicContexCallback : public osg::GraphicsContext::CreateGraphicContexCallback + { + virtual osg::GraphicsContext* createGraphicsContext(osg::GraphicsContext::Traits* traits) + { + return new GraphicsContextImplementation(traits); + } + }; + + struct RegisterCreateGraphicsContextCallbackProxy + { + RegisterCreateGraphicsContextCallbackProxy() + { + osg::GraphicsContext::setCreateGraphicsContextCallback(new MyCreateGraphicContexCallback); + } + }; + + RegisterCreateGraphicsContextCallbackProxy createGraphicsContextCallbackProxy; +}; + + + +GraphicsContextImplementation::GraphicsContextImplementation(Traits* traits) +{ + _rs = new Producer::RenderSurface; + _rs->setWindowName(traits->_windowName); + _rs->setWindowRectangle(traits->_x, traits->_y, traits->_width, traits->_height); + _rs->useBorder(traits->_windowDecoration); + + if (traits->_pbuffer) + { + _rs->setDrawableType( Producer::RenderSurface::DrawableType_PBuffer ); + + if (_trais->_alpha>0) + { + _rs->setRenderToTextureMode(Producer::RenderSurface::RenderToRGBATexture); + } + else + { + _rs->setRenderToTextureMode(Producer::RenderSurface::RenderToRGBTexture); + } + } + + setState(new osg::State); + getState()->setContextID(1); + + _rs->realize(); + + _traits = traits; +} + +GraphicsContextImplementation::GraphicsContextImplementation(Producer::RenderSurface* rs) +{ + _rs = rs; +} + +GraphicsContextImplementation::~GraphicsContextImplementation() +{ + release(); +} + +void GraphicsContextImplementation::makeCurrent() +{ + if (!_rs) return; + + _rs->setReadDrawable( 0 ); + + _rs->makeCurrent(); +} + +void GraphicsContextImplementation::makeContextCurrent(GraphicsContext* readContext) +{ + if (!_rs) return; + + GraphicsContextImplementation* readContextImplemention = dynamic_cast(readContext); + + if (readContextImplemention) + { + _rs->setReadDrawable( readContextImplemention->getRenderSurface() ); + } + + _rs->makeCurrent(); +} + +void GraphicsContextImplementation::release() +{ + if (!_rs) return; + + // need to close render surface... +} + +void GraphicsContextImplementation::bindPBufferToTexture(GLenum buffer) +{ + if (!_rs) return; + + Producer::RenderSurface::BufferType bufferType = Producer::RenderSurface::FrontBuffer; + switch(buffer) + { + case(GL_BACK): bufferType = Producer::RenderSurface::BackBuffer; break; + case(GL_FRONT): bufferType = Producer::RenderSurface::FrontBuffer; break; + default: bufferType = Producer::RenderSurface::FrontBuffer; break; + } + + _rs->bindPBufferToTexture(bufferType); +} + +void GraphicsContextImplementation::swapBuffers() +{ + _rs->swapBuffers(); +}