Added osgGA::Device class for integration of both physical and virtual devices.

Added template readFile(..) function to make it more convinient to cast to a specific object type.

Added support for osgGA::Device to osgViewer.

Added sdl plugin to provides very basic joystick osgGA::Device integration.
This commit is contained in:
Robert Osfield
2012-10-23 16:15:03 +00:00
parent f9ad1e5673
commit 1591fe09f3
14 changed files with 491 additions and 2 deletions

View File

@@ -43,9 +43,25 @@ extern OSGDB_EXPORT osg::Object* readObjectFile(const std::string& filename,cons
* to read the specified file.*/
inline osg::Object* readObjectFile(const std::string& filename)
{
return readObjectFile(filename,Registry::instance()->getOptions());
return readObjectFile(filename, Registry::instance()->getOptions());
}
template<typename T>
inline T* readFile(const std::string& filename, const Options* options)
{
osg::ref_ptr<osg::Object> object = readObjectFile(filename, options);
osg::ref_ptr<T> t = dynamic_cast<T*>(object.get());
object = 0;
return t.release();
}
template<typename T>
inline T* readFile(const std::string& filename)
{
return readFile<T>(filename, Registry::instance()->getOptions());
}
/** Read an osg::Image from file.
* Return valid osg::Image on success,
* return NULL on failure.

52
include/osgGA/Device Normal file
View File

@@ -0,0 +1,52 @@
/* -*-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.
*/
#ifndef OSGGA_EVENTSOURCE
#define OSGGA_EVENTSOURCE 1
#include <osgGA/EventQueue>
namespace osgGA {
/**
* Device base class from abstracting away from devices/windows that can generate events.
*/
class OSGGA_EXPORT Device : public osg::Object
{
public:
Device();
Device(const Device& es, const osg::CopyOp& copyop);
META_Object(osgGA,Device);
virtual void checkEvents() {};
void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
osgGA::EventQueue* getEventQueue() { return _eventQueue.get(); }
const osgGA::EventQueue* getEventQueue() const { return _eventQueue.get(); }
protected:
virtual ~Device();
/** Prevent unwanted copy operator.*/
Device& operator = (const Device&) { return *this; }
osg::ref_ptr<osgGA::EventQueue> _eventQueue;
};
}
#endif

View File

@@ -24,6 +24,7 @@
#include <osgGA/CameraManipulator>
#include <osgGA/EventVisitor>
#include <osgGA/EventQueue>
#include <osgGA/Device>
#include <osgViewer/Scene>
#include <osgViewer/ViewerBase>
@@ -108,6 +109,19 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter
/** Get the const View's image pager.*/
const osgDB::ImagePager* getImagePager() const;
/** Add a Device.
* The Device is polled on each new frame via it's Device::checkEvents() method and any events generated then collected via Device::getEventQueue()*/
void addDevice(osgGA::Device* eventSource);
/** Remove a Device. /*/
void removeDevice(osgGA::Device* eventSource);
typedef std::vector< osg::ref_ptr<osgGA::Device> > Devices;
Devices& getDevices() { return _eventSources; }
const Devices& getDevices() const { return _eventSources; }
/* Set the EventQueue that the View uses to integrate external non window related events.*/
void setEventQueue(osgGA::EventQueue* eventQueue) { _eventQueue = eventQueue; }
@@ -250,6 +264,8 @@ class OSGVIEWER_EXPORT View : public osg::View, public osgGA::GUIActionAdapter
osg::Timer_t _startTick;
Devices _eventSources;
osg::ref_ptr<osgViewer::Scene> _scene;
osg::ref_ptr<osgGA::EventQueue> _eventQueue;
osg::ref_ptr<osgGA::CameraManipulator> _cameraManipulator;

View File

@@ -117,7 +117,6 @@ class OSGVIEWER_EXPORT ViewerBase : public virtual osg::Object
osg::BarrierOperation::PreBlockOp getEndBarrierOperation() const { return _endBarrierOperation; }
/** Set the done flag to signal the viewer's work is done and should exit the frame loop.*/
void setDone(bool done) { _done = done; }

View File

@@ -10,6 +10,7 @@ SET(HEADER_PATH ${OpenSceneGraph_SOURCE_DIR}/include/${LIB_NAME})
SET(TARGET_H
${HEADER_PATH}/AnimationPathManipulator
${HEADER_PATH}/DriveManipulator
${HEADER_PATH}/Device
${HEADER_PATH}/EventQueue
${HEADER_PATH}/EventVisitor
${HEADER_PATH}/Export
@@ -36,6 +37,7 @@ SET(TARGET_H
SET(TARGET_SRC
AnimationPathManipulator.cpp
DriveManipulator.cpp
Device.cpp
EventQueue.cpp
EventVisitor.cpp
FirstPersonManipulator.cpp

31
src/osgGA/Device.cpp Normal file
View File

@@ -0,0 +1,31 @@
/* -*-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/Device>
using namespace osgGA;
Device::Device()
{
setEventQueue(new EventQueue);
}
Device::Device(const Device& es, const osg::CopyOp& copyop):
osg::Object(es,copyop)
{
setEventQueue(new EventQueue);
}
Device::~Device()
{
}

View File

@@ -256,6 +256,15 @@ ENDIF()
ADD_SUBDIRECTORY(pvr)
####################################################
#
# Device integration plugins
#
IF (SDL_FOUND)
ADD_SUBDIRECTORY(sdl)
ENDIF(SDL_FOUND)
##########to get all the variables of Cmake
#GET_CMAKE_PROPERTY(MYVARS VARIABLES)
#FOREACH(myvar ${MYVARS})

View File

@@ -0,0 +1,16 @@
SET(TARGET_EXTERNAL_LIBRARIES ${SDL_LIBRARY} )
INCLUDE_DIRECTORIES(${SDL_INCLUDE_DIR} )
SET(TARGET_SRC
JoystickDevice.cpp
ReaderWriterSDL.cpp
)
SET(TARGET_H
JoystickDevice.h
)
SET(TARGET_ADDED_LIBRARIES osgGA)
#### end var setup ###
SETUP_PLUGIN(sdl sdl)

View File

@@ -0,0 +1,185 @@
/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
*
* This software is open source and may be redistributed and/or modified under
* the terms of the GNU General Public License (GPL) version 2.0.
* The full license is in LICENSE.txt file included with this distribution,.
*
* This software 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
* include LICENSE.txt for more details.
*/
#include <osgViewer/Viewer>
#include "JoystickDevice.h"
#include <SDL.h>
#include <SDL_events.h>
#include <SDL_joystick.h>
#include <iostream>
JoystickDevice::JoystickDevice()
{
_verbose = false;
// init SDL
if ( SDL_Init(SDL_INIT_JOYSTICK) < 0 )
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
int numJoysticks = SDL_NumJoysticks();
if (_verbose)
{
std::cout<<"number of joysticks "<<numJoysticks<<std::endl;
for(int i=0; i<numJoysticks; ++i)
{
std::cout<<"Joystick name '"<<SDL_JoystickName(i)<<"'"<<std::endl;
}
}
_joystick = numJoysticks>0 ? SDL_JoystickOpen(0) : 0;
_numAxes = _joystick ? SDL_JoystickNumAxes(_joystick) : 0;
_numBalls = _joystick ? SDL_JoystickNumBalls(_joystick) : 0;
_numHats = _joystick ? SDL_JoystickNumHats(_joystick) : 0;
_numButtons = _joystick ? SDL_JoystickNumButtons(_joystick) : 0;
if (_verbose)
{
std::cout<<"numAxes = "<<_numAxes<<std::endl;
std::cout<<"numBalls = "<<_numBalls<<std::endl;
std::cout<<"numHats = "<<_numHats<<std::endl;
std::cout<<"numButtons = "<<_numButtons<<std::endl;
}
addMouseButtonMapping(4, 1); // left
addMouseButtonMapping(5, 3); // right
addMouseButtonMapping(6, 2); // middle
addKeyMapping(10, ' '); // R2
addKeyMapping(0, '1'); // 1
addKeyMapping(1, '2'); // 2
addKeyMapping(2, '3'); // 3
addKeyMapping(4, '4'); // 4
addKeyMapping(7, ' '); // home
addKeyMapping(8, osgGA::GUIEventAdapter::KEY_Page_Up); // Start
addKeyMapping(9, osgGA::GUIEventAdapter::KEY_Page_Down); // Start
addKeyMapping(10, osgGA::GUIEventAdapter::KEY_Home); // Start
capture(_axisValues, _buttonValues);
}
JoystickDevice::~JoystickDevice()
{
}
void JoystickDevice::capture(ValueList& axisValues, ValueList& buttonValues) const
{
if (_joystick)
{
SDL_JoystickUpdate();
axisValues.resize(_numAxes);
for(int ai=0; ai<_numAxes; ++ai)
{
axisValues[ai] = SDL_JoystickGetAxis(_joystick, ai);
}
buttonValues.resize(_numButtons);
for(int bi=0; bi<_numButtons; ++bi)
{
buttonValues[bi] = SDL_JoystickGetButton(_joystick, bi);
}
}
}
void JoystickDevice::checkEvents()
{
if (_joystick)
{
OSG_NOTICE<<"JoystickDevice::checkEvents()"<<std::endl;
ValueList newAxisValues;
ValueList newButtonValues;
capture(newAxisValues, newButtonValues);
unsigned int mouseXaxis = 0;
unsigned int mouseYaxis = 1;
float prev_mx = (float)_axisValues[mouseXaxis]/32767.0f;
float prev_my = -(float)_axisValues[mouseYaxis]/32767.0f;
float mx = (float)newAxisValues[mouseXaxis]/32767.0f;
float my = -(float)newAxisValues[mouseYaxis]/32767.0f;
osgGA::EventQueue* eq = getEventQueue();
double time = eq ? eq->getTime() : 0.0;
osgGA::GUIEventAdapter* previous_event = eq->getCurrentEventState();
float projected_mx = previous_event->getXmin() + (mx+1.0)*0.5*(previous_event->getXmax()-previous_event->getXmin());
float projected_my = previous_event->getYmin() + (my+1.0)*0.5*(previous_event->getYmax()-previous_event->getYmin());
if (mx!=prev_mx || my!=prev_my)
{
eq->mouseMotion(projected_mx, projected_my, time);
}
OSG_NOTICE<<"mx="<<mx<<", my="<<my<<", projected_mx="<<projected_mx<<", projected_my="<<projected_my<<std::endl;
if (_verbose)
{
for(int ai=0; ai<_numAxes; ++ai)
{
if (newAxisValues[ai]!=_axisValues[ai])
{
std::cout<<"axis "<<ai<<" moved to "<<newAxisValues[ai]<<std::endl;
}
}
}
for(int bi=0; bi<_numButtons; ++bi)
{
if (newButtonValues[bi]!=_buttonValues[bi])
{
if (_verbose)
{
std::cout<<"button "<<bi<<" changed to "<<newButtonValues[bi]<<std::endl;
}
int key = getKeyMapping(bi);
int mouseButton = getMouseButtonMapping(bi);
if (mouseButton>0)
{
if (newButtonValues[bi]==0) eq->mouseButtonRelease(projected_mx,projected_my,mouseButton,time);
else eq->mouseButtonPress(projected_mx,projected_my,mouseButton,time);
}
else if (key>0)
{
if (newButtonValues[bi]==0) eq->keyRelease(key,time);
else eq->keyPress(key,time);
}
}
}
_axisValues.swap(newAxisValues);
_buttonValues.swap(newButtonValues);
}
}

View File

@@ -0,0 +1,79 @@
/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
*
* This software is open source and may be redistributed and/or modified under
* the terms of the GNU General Public License (GPL) version 2.0.
* The full license is in LICENSE.txt file included with this distribution,.
*
* This software 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
* include LICENSE.txt for more details.
*/
#ifndef SDLINTEGRATION
#define SDLINTEGRATION
#include <osgGA/Device>
#include <SDL.h>
#include <SDL_events.h>
#include <SDL_joystick.h>
#include <vector>
#include <map>
class JoystickDevice : public osgGA::Device
{
public:
JoystickDevice();
typedef std::vector<int> ValueList;
typedef std::map<int, int> ButtonMap;
virtual void checkEvents();
void addMouseButtonMapping(int joystickButton, int mouseButton)
{
_mouseButtonMap[joystickButton] = mouseButton;
}
int getMouseButtonMapping(int joystickButton)
{
ButtonMap::const_iterator itr = _mouseButtonMap.find(joystickButton);
if (itr != _mouseButtonMap.end()) return itr->second;
else return -1;
}
void addKeyMapping(int joystickButton, int key)
{
_keyMap[joystickButton] = key;
}
int getKeyMapping(int joystickButton)
{
ButtonMap::const_iterator itr = _keyMap.find(joystickButton);
if (itr != _keyMap.end()) return itr->second;
else return -1;
}
protected:
virtual ~JoystickDevice();
void capture(ValueList& axisValues, ValueList& buttonValues) const;
SDL_Joystick* _joystick;
int _numAxes;
int _numBalls;
int _numHats;
int _numButtons;
bool _verbose;
ValueList _axisValues;
ValueList _buttonValues;
ButtonMap _mouseButtonMap;
ButtonMap _keyMap;
};
#endif

View File

@@ -0,0 +1,38 @@
// Released under the OSGPL license, as part of the OpenSceneGraph distribution.
//
// ReaderWriter for sgi's .rgb format.
// specification can be found at http://local.wasp.uwa.edu.au/~pbourke/dataformats/sgirgb/sgiversion.html
#include <osg/Notify>
#include <osgDB/FileNameUtils>
#include <osgDB/Registry>
#include "JoystickDevice.h"
class ReaderWriterSDL : public osgDB::ReaderWriter
{
public:
ReaderWriterSDL()
{
supportsExtension("sdl","SDL Device Integration");
}
virtual const char* className() const { return "SDL Device Integration plugin"; }
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
{
if (file=="joystick.sdl")
{
return new JoystickDevice;
}
return ReadResult::FILE_NOT_HANDLED;
}
};
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(sdl, ReaderWriterSDL)

View File

@@ -735,6 +735,7 @@ void CompositeViewer::eventTraversal()
masterCameraVPW *= viewport->computeWindowMatrix();
}
// get events from all windows attached to Viewer.
for(Contexts::iterator citr = contexts.begin();
citr != contexts.end();
++citr)
@@ -925,6 +926,20 @@ void CompositeViewer::eventTraversal()
++vitr)
{
View* view = vitr->get();
// get events from user Devices attached to Viewer.
for(osgViewer::View::Devices::iterator eitr = view->getDevices().begin();
eitr != view->getDevices().end();
++eitr)
{
osgGA::Device* es = eitr->get();
es->checkEvents();
// open question, will we need to reproject mouse coordinates into current view's coordinate frame as is down for GraphicsWindow provided events?
// for now assume now and just get the events directly without any reprojection.
es->getEventQueue()->takeEvents(viewEventsMap[view], cutOffTime);
}
view->getEventQueue()->takeEvents(viewEventsMap[view], cutOffTime);
}

View File

@@ -2217,3 +2217,20 @@ bool View::computeIntersections(float x,float y, const osg::NodePath& nodePath,
}
void View::addDevice(osgGA::Device* eventSource)
{
Devices::iterator itr = std::find( _eventSources.begin(), _eventSources.end(), eventSource );
if (itr==_eventSources.end())
{
_eventSources.push_back(eventSource);
}
}
void View::removeDevice(osgGA::Device* eventSource)
{
Devices::iterator itr = std::find( _eventSources.begin(), _eventSources.end(), eventSource );
if (itr!=_eventSources.end())
{
_eventSources.erase(itr);
}
}

View File

@@ -645,6 +645,20 @@ void Viewer::eventTraversal()
}
// get events from user Devices attached to Viewer.
for(Devices::iterator eitr = _eventSources.begin();
eitr != _eventSources.end();
++eitr)
{
osgGA::Device* es = eitr->get();
es->checkEvents();
// open question, will we need to reproject mouse coordinates into current view's coordinate frame as is down for GraphicsWindow provided events?
// for now assume now and just get the events directly without any reprojection.
es->getEventQueue()->takeEvents(events, cutOffTime);
}
// get events from all windows attached to Viewer.
for(Contexts::iterator citr = contexts.begin();
citr != contexts.end();
++citr)