Added GraphicsContextImplementation

This commit is contained in:
Robert Osfield 2005-07-21 19:37:44 +00:00
parent 302c58fc93
commit 9b34bc5c86
3 changed files with 210 additions and 0 deletions

View File

@ -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

View File

@ -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 <osg/GraphicsContext>
#include <Producer/RenderSurface>
#include <osgProducer/Export>
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<Producer::RenderSurface> _rs;
};
}
#endif

View File

@ -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 <osgProducer/GraphicsContextImplementation>
#include <osg/Notify>
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<GraphicsContextImplementation*>(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();
}