Added bare bones osgGA::SimpleViewer class to help simplify OSG setup when embedding the OSG

into existing GUI applications, and for one one a single camera, single window is required.
This commit is contained in:
Robert Osfield
2006-09-25 16:25:53 +00:00
parent 78444878c3
commit 5c0eb0b013
17 changed files with 552 additions and 179 deletions

View File

@@ -77,9 +77,9 @@ bool EventQueue::copyEvents(Events& events) const
}
void EventQueue::windowResize(float Xmin, float Ymin, float Xmax, float Ymax)
void EventQueue::windowResize(int x, int y, unsigned int width, unsigned int height, bool updateMouseRange)
{
_accumulateEventState->setWindowSize(Xmin, Ymin, Xmax, Ymax);
_accumulateEventState->setWindowRectangle(x, y, width, height, updateMouseRange);
GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
event->setEventType(GUIEventAdapter::RESIZE);

View File

@@ -16,6 +16,7 @@ CXXFILES = \
KeySwitchMatrixManipulator.cpp\
SetSceneViewVisitor.cpp\
StateSetManipulator.cpp\
SimpleViewer.cpp\
TerrainManipulator.cpp\
NodeTrackerManipulator.cpp\
TrackballManipulator.cpp\

View File

@@ -18,6 +18,10 @@ using namespace osgGA;
GUIEventAdapter::GUIEventAdapter():
_eventType(NONE),
_time(0.0),
_windowX(0),
_windowY(0),
_windowWidth(1280),
_windowHeight(1024),
_key(0),
_button(0),
_Xmin(0.0),
@@ -40,6 +44,10 @@ GUIEventAdapter::GUIEventAdapter(const GUIEventAdapter& rhs):
osg::Referenced(),
_eventType(rhs._eventType),
_time(rhs._time),
_windowX(rhs._windowX),
_windowY(rhs._windowY),
_windowWidth(rhs._windowWidth),
_windowHeight(rhs._windowHeight),
_key(rhs._key),
_button(rhs._button),
_Xmin(rhs._Xmin),
@@ -62,7 +70,21 @@ GUIEventAdapter::~GUIEventAdapter()
{
}
void GUIEventAdapter::setWindowSize(float Xmin, float Ymin, float Xmax, float Ymax)
void GUIEventAdapter::setWindowRectangle(int x, int y, unsigned int width, unsigned int height, bool updateMouseRange)
{
_windowX = x;
_windowY = y;
_windowWidth = width;
_windowHeight = height;
if (updateMouseRange)
{
setInputRange(x, y, x+width, y+height);
}
}
void GUIEventAdapter::setInputRange(float Xmin, float Ymin, float Xmax, float Ymax)
{
_Xmin = Xmin;
_Ymin = Ymin;

235
src/osgGA/SimpleViewer.cpp Normal file
View File

@@ -0,0 +1,235 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 <osgGA/SimpleViewer>
#include <osgGA/TrackballManipulator>
using namespace osgGA;
SimpleViewer::SimpleViewer():
_firstFrame(true)
{
_sceneView = new osgUtil::SceneView;
_sceneView->setDefaults();
_sceneView->getState()->setContextID(osg::GraphicsContext::createNewContextID());
_startTick = osg::Timer::instance()->tick();
_frameStamp = new osg::FrameStamp;
_frameStamp->setFrameNumber(0);
_frameStamp->setReferenceTime(0);
_eventQueue = new osgGA::EventQueue;
_eventQueue->setStartTick(_startTick);
_eventVisitor = new osgGA::EventVisitor;
}
SimpleViewer::~SimpleViewer()
{
_sceneView->releaseAllGLObjects();
osg::GraphicsContext::decrementContextIDUsageCount(_sceneView->getState()->getContextID());
}
void SimpleViewer::setSceneData(osg::Node* node)
{
_sceneView->setSceneData(node);
if (_cameraManipulator.valid())
{
_cameraManipulator->setNode(node);
osg::ref_ptr<osgGA::GUIEventAdapter> dummyEvent = _eventQueue->createEvent();
_cameraManipulator->home(*dummyEvent, *this);
}
}
osg::Node* SimpleViewer::getSceneData()
{
return _sceneView->getSceneData();
}
const osg::Node* SimpleViewer::getSceneData() const
{
return _sceneView->getSceneData();
}
osg::CameraNode* SimpleViewer::getCamera()
{
return _sceneView->getCamera();
}
const osg::CameraNode* SimpleViewer::getCamera() const
{
return _sceneView->getCamera();
}
void SimpleViewer::setCameraManipulator(MatrixManipulator* manipulator)
{
if (_cameraManipulator == manipulator) return;
_cameraManipulator = manipulator;
if (_cameraManipulator.valid())
{
_cameraManipulator->setNode(_sceneView->getSceneData());
osg::ref_ptr<osgGA::GUIEventAdapter> dummyEvent = _eventQueue->createEvent();
_cameraManipulator->home(*dummyEvent, *this);
}
}
void SimpleViewer::addEventHandler(GUIEventHandler* eventHandler)
{
_eventHandlers.push_back(eventHandler);
}
void SimpleViewer::init()
{
osg::ref_ptr<osgGA::GUIEventAdapter> initEvent = _eventQueue->createEvent();
initEvent->setEventType(osgGA::GUIEventAdapter::FRAME);
_cameraManipulator->init(*initEvent, *this);
}
void SimpleViewer::frame()
{
if (_firstFrame)
{
init();
_firstFrame = false;
}
frameAdvance();
frameEventTraversal();
frameUpdateTraversal();
frameCullTraversal();
frameDrawTraversal();
}
void SimpleViewer::frameAdvance()
{
osg::Timer_t currentTick = osg::Timer::instance()->tick();
_frameStamp->setReferenceTime(osg::Timer::instance()->delta_s(_startTick,currentTick));
_frameStamp->setFrameNumber(_frameStamp->getFrameNumber()+1);
_sceneView->setFrameStamp(_frameStamp.get());
}
void SimpleViewer::frameEventTraversal()
{
_eventQueue->frame( _frameStamp->getReferenceTime() );
osgGA::EventQueue::Events events;
_eventQueue->takeEvents(events);
if (_eventVisitor.valid())
{
_eventVisitor->setTraversalNumber(_frameStamp->getFrameNumber());
}
for(osgGA::EventQueue::Events::iterator itr = events.begin();
itr != events.end();
++itr)
{
osgGA::GUIEventAdapter* event = itr->get();
bool handled = false;
if (_eventVisitor.valid())
{
_eventVisitor->reset();
_eventVisitor->addEvent( event );
getSceneData()->accept(*_eventVisitor);
if (_eventVisitor->getEventHandled()) handled = true;
}
if (_cameraManipulator.valid())
{
_cameraManipulator->handle( *event, *this );
}
for(EventHandlers::iterator hitr = _eventHandlers.begin();
hitr != _eventHandlers.end() && !handled;
++hitr)
{
handled = (*hitr)->handle( *event, *this, 0, 0);
}
}
}
void SimpleViewer::frameUpdateTraversal()
{
double previousAspectRatio = ( static_cast<double>(_sceneView->getViewport()->width())/
static_cast<double>(_sceneView->getViewport()->height()) );
// update the viewport
int width = _eventQueue->getCurrentEventState()->getWindowWidth();
int height = _eventQueue->getCurrentEventState()->getWindowHeight();
_sceneView->setViewport(0,0,width,height);
double newAspectRatio = ( static_cast<double>(_sceneView->getViewport()->width())/
static_cast<double>(_sceneView->getViewport()->height()) );
// if aspect ratio adjusted change the project matrix to suit.
if (previousAspectRatio != newAspectRatio)
{
osg::Matrixd& pm = _sceneView->getProjectionMatrix();
bool orthographicCamera = (pm(0,3)==0.0) && (pm(0,3)==0.0) && (pm(0,3)==0.0) && (pm(0,3)==1.0);
if (orthographicCamera)
{
double left, right, bottom, top, zNear, zFar;
_sceneView->getProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar);
double mid = (right+left)*0.5;
double halfWidth = (right-left)*0.5;
left = mid - halfWidth * (newAspectRatio/previousAspectRatio);
right = mid + halfWidth * (newAspectRatio/previousAspectRatio);
_sceneView->setProjectionMatrixAsOrtho(left, right, bottom, top, zNear, zFar);
}
else
{
double left, right, bottom, top, zNear, zFar;
_sceneView->getProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar);
double mid = (right+left)*0.5;
double halfWidth = (right-left)*0.5;
left = mid - halfWidth * (newAspectRatio/previousAspectRatio);
right = mid + halfWidth * (newAspectRatio/previousAspectRatio);
_sceneView->setProjectionMatrixAsFrustum(left, right, bottom, top, zNear, zFar);
}
}
if (_cameraManipulator.valid())
{
_sceneView->setViewMatrix(_cameraManipulator->getInverseMatrix());
}
_sceneView->update();
}
void SimpleViewer::frameCullTraversal()
{
_sceneView->cull();
}
void SimpleViewer::frameDrawTraversal()
{
_sceneView->draw();
}
void SimpleViewer::cleanup()
{
_sceneView->releaseAllGLObjects();
_sceneView->flushAllDeletedGLObjects();
}

View File

@@ -158,9 +158,7 @@ void KeyboardMouseCallback::updateWindowSize()
maxY = osg::maximum(maxY,ir.bottom()+ir.height());
}
// osg::notify(osg::NOTICE)<<"IA ea->setWindowSize("<<minX<<","<<minY<<","<<maxX<<","<<maxY<<")"<<std::endl;
ea->setWindowSize(minX,minY,maxX,maxY);
ea->setInputRange(minX,minY,maxX,maxY);
}
else if (rs)
{
@@ -175,7 +173,12 @@ void KeyboardMouseCallback::updateWindowSize()
// osg::notify(osg::NOTICE)<<"RS ea->setWindowSize("<<minX<<","<<minY<<","<<maxX<<","<<maxY<<")"<<std::endl;
ea->setWindowSize(minX,minY,maxX,maxY);
ea->setInputRange(minX,minY,maxX,maxY);
}
if (rs)
{
ea->setWindowRectangle(rs->getWindowOriginX(), rs->getWindowOriginY(), rs->getWindowWidth(),rs->getWindowHeight());
}
}

View File

@@ -32,7 +32,7 @@ BEGIN_OBJECT_REFLECTOR(osgGA::EventQueue)
I_Method1(bool, copyEvents, IN, osgGA::EventQueue::Events &, events);
I_Method1(void, appendEvents, IN, osgGA::EventQueue::Events &, events);
I_Method1(void, addEvent, IN, osgGA::GUIEventAdapter *, event);
I_Method4(void, windowResize, IN, float, Xmin, IN, float, Ymin, IN, float, Xmax, IN, float, Ymax);
I_MethodWithDefaults5(void, windowResize, IN, int, x, , IN, int, y, , IN, unsigned int, width, , IN, unsigned int, height, , IN, bool, updateMouseRange, true);
I_Method1(void, mouseScroll, IN, osgGA::GUIEventAdapter::ScrollingMotion, sm);
I_Method2(void, mouseScroll2D, IN, float, x, IN, float, y);
I_Method1(void, penPressure, IN, float, pressure);

View File

@@ -15,6 +15,7 @@ CXXFILES =\
MatrixManipulator.cpp\
NodeTrackerManipulator.cpp\
SetSceneViewVisitor.cpp\
SimpleViewer.cpp\
StateSetManipulator.cpp\
TerrainManipulator.cpp\
TrackballManipulator.cpp\

View File

@@ -215,18 +215,19 @@ BEGIN_OBJECT_REFLECTOR(osgGA::GUIEventAdapter)
I_Method1(void, setTime, IN, double, time);
I_Method0(double, getTime);
I_Method0(double, time);
I_MethodWithDefaults5(void, setWindowRectangle, IN, int, x, , IN, int, y, , IN, unsigned int, width, , IN, unsigned int, height, , IN, bool, updateMouseRange, true);
I_Method0(int, getWindowX);
I_Method0(int, getWindowY);
I_Method0(unsigned int, getWindowWidth);
I_Method0(unsigned int, getWindowHeight);
I_Method1(void, setKey, IN, int, key);
I_Method0(int, getKey);
I_Method1(void, setButton, IN, int, button);
I_Method0(int, getButton);
I_Method4(void, setWindowSize, IN, float, Xmin, IN, float, Ymin, IN, float, Xmax, IN, float, Ymax);
I_Method1(void, setXmin, IN, float, x);
I_Method4(void, setInputRange, IN, float, Xmin, IN, float, Ymin, IN, float, Xmax, IN, float, Ymax);
I_Method0(float, getXmin);
I_Method1(void, setXmax, IN, float, x);
I_Method0(float, getXmax);
I_Method1(void, setYmin, IN, float, y);
I_Method0(float, getYmin);
I_Method1(void, setYmax, IN, float, y);
I_Method0(float, getYmax);
I_Method1(void, setX, IN, float, x);
I_Method0(float, getX);
@@ -261,13 +262,17 @@ BEGIN_OBJECT_REFLECTOR(osgGA::GUIEventAdapter)
I_Property(osgGA::GUIEventAdapter::ScrollingMotion, ScrollingMotion);
I_Property(osgGA::GUIEventAdapter::TabletPointerType, TabletPointerType);
I_Property(double, Time);
I_ReadOnlyProperty(unsigned int, WindowHeight);
I_ReadOnlyProperty(unsigned int, WindowWidth);
I_ReadOnlyProperty(int, WindowX);
I_ReadOnlyProperty(int, WindowY);
I_Property(float, X);
I_Property(float, Xmax);
I_Property(float, Xmin);
I_ReadOnlyProperty(float, Xmax);
I_ReadOnlyProperty(float, Xmin);
I_ReadOnlyProperty(float, Xnormalized);
I_Property(float, Y);
I_Property(float, Ymax);
I_Property(float, Ymin);
I_ReadOnlyProperty(float, Ymax);
I_ReadOnlyProperty(float, Ymin);
I_ReadOnlyProperty(float, Ynormalized);
END_REFLECTOR

View File

@@ -0,0 +1,74 @@
// ***************************************************************************
//
// Generated automatically by genwrapper.
// Please DO NOT EDIT this file!
//
// ***************************************************************************
#include <osgIntrospection/ReflectionMacros>
#include <osgIntrospection/TypedMethodInfo>
#include <osgIntrospection/StaticMethodInfo>
#include <osgIntrospection/Attributes>
#include <osg/CameraNode>
#include <osg/Node>
#include <osgDB/DatabasePager>
#include <osgGA/EventQueue>
#include <osgGA/GUIEventHandler>
#include <osgGA/MatrixManipulator>
#include <osgGA/SimpleViewer>
#include <osgUtil/SceneView>
// Must undefine IN and OUT macros defined in Windows headers
#ifdef IN
#undef IN
#endif
#ifdef OUT
#undef OUT
#endif
TYPE_NAME_ALIAS(std::list< osg::ref_ptr< osgGA::GUIEventHandler > >, osgGA::SimpleViewer::EventHandlers);
BEGIN_OBJECT_REFLECTOR(osgGA::SimpleViewer)
I_BaseType(osgGA::GUIActionAdapter);
I_Constructor0();
I_Method1(void, setSceneData, IN, osg::Node *, node);
I_Method0(osg::Node *, getSceneData);
I_Method0(const osg::Node *, getSceneData);
I_Method0(osg::CameraNode *, getCamera);
I_Method0(const osg::CameraNode *, getCamera);
I_Method1(void, setCameraManipulator, IN, osgGA::MatrixManipulator *, manipulator);
I_Method0(osgGA::MatrixManipulator *, getCameraManipulator);
I_Method0(const osgGA::MatrixManipulator *, getCameraManipulator);
I_Method1(void, addEventHandler, IN, osgGA::GUIEventHandler *, eventHandler);
I_Method0(osgGA::SimpleViewer::EventHandlers &, getEventHandlers);
I_Method0(const osgGA::SimpleViewer::EventHandlers &, getEventHandlers);
I_Method0(osgGA::EventQueue *, getEventQueue);
I_Method0(const osgGA::EventQueue *, getEventQueue);
I_Method1(void, setDatabasePager, IN, osgDB::DatabasePager *, dp);
I_Method0(osgDB::DatabasePager *, getDatabasePager);
I_Method0(const osgDB::DatabasePager *, getDatabasePager);
I_Method0(void, frame);
I_Method0(void, frameAdvance);
I_Method0(void, frameEventTraversal);
I_Method0(void, frameUpdateTraversal);
I_Method0(void, frameCullTraversal);
I_Method0(void, frameDrawTraversal);
I_Method0(void, cleanup);
I_Method0(void, requestRedraw);
I_MethodWithDefaults1(void, requestContinuousUpdate, IN, bool, x, true);
I_Method2(void, requestWarpPointer, IN, float, x, IN, float, x);
I_Method0(osgUtil::SceneView *, getSceneView);
I_Method0(const osgUtil::SceneView *, getSceneView);
I_Method0(void, init);
I_ReadOnlyProperty(osg::CameraNode *, Camera);
I_Property(osgGA::MatrixManipulator *, CameraManipulator);
I_Property(osgDB::DatabasePager *, DatabasePager);
I_ReadOnlyProperty(osgGA::SimpleViewer::EventHandlers &, EventHandlers);
I_ReadOnlyProperty(osgGA::EventQueue *, EventQueue);
I_Property(osg::Node *, SceneData);
I_ReadOnlyProperty(osgUtil::SceneView *, SceneView);
END_REFLECTOR
STD_LIST_REFLECTOR(std::list< osg::ref_ptr< osgGA::GUIEventHandler > >);