Ran svn propset -R svn:eol-style native . on the OpenSceneGraph

This commit is contained in:
Robert Osfield
2011-04-19 11:40:22 +00:00
parent c70e7bda86
commit 581e6fb630
11 changed files with 815 additions and 815 deletions

View File

@@ -1,74 +1,74 @@
/* OpenSceneGraph example, osgdirectinput.
*
* 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.
*/
#ifndef OSGDIRECTINPUT_H
#define OSGDIRECTINPUT_H
#define DIRECTINPUT_VERSION 0x0800
#include <windows.h>
#include <dinput.h>
#include <iostream>
class JoystickEvent : public osg::Referenced
{
public:
JoystickEvent() {}
virtual ~JoystickEvent() {}
DIJOYSTATE2 _js;
};
class DirectInputRegistry : public osg::Referenced
{
public:
static DirectInputRegistry* instance()
{
static osg::ref_ptr<DirectInputRegistry> s_registry = new DirectInputRegistry;
return s_registry.get();
}
LPDIRECTINPUT8& getDevice() { return _inputDevice; }
LPDIRECTINPUTDEVICE8& getKeyboard() { return _keyboard; }
LPDIRECTINPUTDEVICE8& getMouse() { return _mouse; }
LPDIRECTINPUTDEVICE8& getJoyStick() { return _joystick; }
bool valid() const { return _supportDirectInput; }
bool initKeyboard( HWND handle );
bool initMouse( HWND handle );
bool initJoystick( HWND handle );
void updateState( osgGA::EventQueue* eventQueue );
protected:
DirectInputRegistry();
virtual ~DirectInputRegistry();
bool initImplementation( HWND handle, LPDIRECTINPUTDEVICE8 device, LPCDIDATAFORMAT format );
void pollDevice( LPDIRECTINPUTDEVICE8 device );
void releaseDevice( LPDIRECTINPUTDEVICE8 device );
static BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* didInstance, VOID* );
LPDIRECTINPUT8 _inputDevice;
LPDIRECTINPUTDEVICE8 _keyboard;
LPDIRECTINPUTDEVICE8 _mouse;
LPDIRECTINPUTDEVICE8 _joystick;
bool _supportDirectInput;
};
#endif
/* OpenSceneGraph example, osgdirectinput.
*
* 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.
*/
#ifndef OSGDIRECTINPUT_H
#define OSGDIRECTINPUT_H
#define DIRECTINPUT_VERSION 0x0800
#include <windows.h>
#include <dinput.h>
#include <iostream>
class JoystickEvent : public osg::Referenced
{
public:
JoystickEvent() {}
virtual ~JoystickEvent() {}
DIJOYSTATE2 _js;
};
class DirectInputRegistry : public osg::Referenced
{
public:
static DirectInputRegistry* instance()
{
static osg::ref_ptr<DirectInputRegistry> s_registry = new DirectInputRegistry;
return s_registry.get();
}
LPDIRECTINPUT8& getDevice() { return _inputDevice; }
LPDIRECTINPUTDEVICE8& getKeyboard() { return _keyboard; }
LPDIRECTINPUTDEVICE8& getMouse() { return _mouse; }
LPDIRECTINPUTDEVICE8& getJoyStick() { return _joystick; }
bool valid() const { return _supportDirectInput; }
bool initKeyboard( HWND handle );
bool initMouse( HWND handle );
bool initJoystick( HWND handle );
void updateState( osgGA::EventQueue* eventQueue );
protected:
DirectInputRegistry();
virtual ~DirectInputRegistry();
bool initImplementation( HWND handle, LPDIRECTINPUTDEVICE8 device, LPCDIDATAFORMAT format );
void pollDevice( LPDIRECTINPUTDEVICE8 device );
void releaseDevice( LPDIRECTINPUTDEVICE8 device );
static BOOL CALLBACK EnumJoysticksCallback( const DIDEVICEINSTANCE* didInstance, VOID* );
LPDIRECTINPUT8 _inputDevice;
LPDIRECTINPUTDEVICE8 _keyboard;
LPDIRECTINPUTDEVICE8 _mouse;
LPDIRECTINPUTDEVICE8 _joystick;
bool _supportDirectInput;
};
#endif

View File

@@ -1,210 +1,210 @@
/* OpenSceneGraph example, osgdirectinput.
*
* 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 <osgGA/EventQueue>
#include <iostream>
#include "DirectInputRegistry"
typedef std::pair<int, int> KeyValue;
typedef std::map<int, KeyValue> KeyMap;
KeyMap g_keyMap;
void buildKeyMap()
{
// TODO: finish the key map as you wish
g_keyMap[DIK_ESCAPE] = KeyValue(osgGA::GUIEventAdapter::KEY_Escape, 0);
g_keyMap[DIK_1] = KeyValue('1', 0);
g_keyMap[DIK_2] = KeyValue('2', 0);
g_keyMap[DIK_3] = KeyValue('3', 0);
g_keyMap[DIK_4] = KeyValue('4', 0);
g_keyMap[DIK_5] = KeyValue('5', 0);
g_keyMap[DIK_6] = KeyValue('6', 0);
g_keyMap[DIK_7] = KeyValue('7', 0);
g_keyMap[DIK_8] = KeyValue('8', 0);
g_keyMap[DIK_9] = KeyValue('9', 0);
g_keyMap[DIK_0] = KeyValue('0', 0);
g_keyMap[DIK_MINUS] = KeyValue('-', 0);
g_keyMap[DIK_EQUALS] = KeyValue('=', 0);
g_keyMap[DIK_BACK] = KeyValue(osgGA::GUIEventAdapter::KEY_BackSpace, 0);
g_keyMap[DIK_TAB] = KeyValue(osgGA::GUIEventAdapter::KEY_Tab, 0);
g_keyMap[DIK_SPACE] = KeyValue(osgGA::GUIEventAdapter::KEY_Space, 0);
}
bool DirectInputRegistry::initKeyboard( HWND handle )
{
if ( !_inputDevice ) return false;
HRESULT hr = _inputDevice->CreateDevice( GUID_SysKeyboard, &_keyboard, NULL );
if ( FAILED(hr) || _keyboard==NULL )
{
osg::notify(osg::WARN) << "Unable to create keyboard." << std::endl;
return false;
}
buildKeyMap();
return initImplementation( handle, _keyboard, &c_dfDIKeyboard );
}
bool DirectInputRegistry::initMouse( HWND handle )
{
if ( !_inputDevice ) return false;
HRESULT hr = _inputDevice->CreateDevice( GUID_SysMouse, &_mouse, NULL );
if ( FAILED(hr) || _mouse==NULL )
{
osg::notify(osg::WARN) << "Unable to create mouse." << std::endl;
return false;
}
return initImplementation( handle, _mouse, &c_dfDIMouse2 );
}
bool DirectInputRegistry::initJoystick( HWND handle )
{
if ( !_inputDevice ) return false;
HRESULT hr = _inputDevice->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback,
NULL, DIEDFL_ATTACHEDONLY );
if ( FAILED(hr) || _joystick==NULL )
{
osg::notify(osg::WARN) << "Unable to enumerate joysticks." << std::endl;
return false;
}
return initImplementation( handle, _joystick, &c_dfDIJoystick2 );
}
void DirectInputRegistry::updateState( osgGA::EventQueue* eventQueue )
{
HRESULT hr;
if ( !_supportDirectInput || !eventQueue ) return;
if ( _keyboard )
{
pollDevice( _keyboard );
char buffer[256] = {0};
hr = _keyboard->GetDeviceState( sizeof(buffer), &buffer );
if ( SUCCEEDED(hr) )
{
for ( KeyMap::iterator itr=g_keyMap.begin(); itr!=g_keyMap.end(); ++itr )
{
KeyValue& key = itr->second;
char value = buffer[itr->first];
if ( key.second==value ) continue;
key.second = value;
if ( value&0x80 )
eventQueue->keyPress( key.first );
else
eventQueue->keyRelease( key.first );
}
}
}
if ( _mouse )
{
pollDevice( _mouse );
DIMOUSESTATE2 mouseState;
hr = _mouse->GetDeviceState( sizeof(DIMOUSESTATE2), &mouseState );
// TODO: add mouse handlers
}
if ( _joystick )
{
pollDevice( _joystick );
osg::ref_ptr<JoystickEvent> event = new JoystickEvent;
hr = _joystick->GetDeviceState( sizeof(DIJOYSTATE2), &(event->_js) );
if ( SUCCEEDED(hr) ) eventQueue->userEvent( event.get() );
}
}
DirectInputRegistry::DirectInputRegistry()
: _keyboard(0), _mouse(0), _joystick(0),
_supportDirectInput(true)
{
HRESULT hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&_inputDevice, NULL );
if ( FAILED(hr) )
{
osg::notify(osg::WARN) << "Unable to create DirectInput object." << std::endl;
_supportDirectInput = false;
}
}
DirectInputRegistry::~DirectInputRegistry()
{
releaseDevice( _keyboard );
releaseDevice( _mouse );
releaseDevice( _joystick );
if ( _inputDevice ) _inputDevice->Release();
}
bool DirectInputRegistry::initImplementation( HWND handle, LPDIRECTINPUTDEVICE8 device, LPCDIDATAFORMAT format )
{
_supportDirectInput = true;
HRESULT hr = device->SetDataFormat( format );
if ( FAILED(hr) )
{
osg::notify(osg::WARN) << "Unable to set device data format." << std::endl;
_supportDirectInput = false;
}
hr = device->SetCooperativeLevel( handle, DISCL_EXCLUSIVE|DISCL_FOREGROUND );
if ( FAILED(hr) )
{
osg::notify(osg::WARN) << "Unable to attach device to window." << std::endl;
_supportDirectInput = false;
}
device->Acquire();
return _supportDirectInput;
}
void DirectInputRegistry::pollDevice( LPDIRECTINPUTDEVICE8 device )
{
HRESULT hr = device->Poll();
if ( FAILED(hr) )
{
device->Acquire();
if ( hr==DIERR_INPUTLOST )
osg::notify(osg::WARN) << "Device lost." << std::endl;
}
}
void DirectInputRegistry::releaseDevice( LPDIRECTINPUTDEVICE8 device )
{
if ( device )
{
device->Unacquire();
device->Release();
}
}
BOOL CALLBACK DirectInputRegistry::EnumJoysticksCallback( const DIDEVICEINSTANCE* didInstance, VOID* )
{
HRESULT hr;
LPDIRECTINPUT8 device = DirectInputRegistry::instance()->getDevice();
if ( device )
{
hr = device->CreateDevice( didInstance->guidInstance,
&(DirectInputRegistry::instance()->getJoyStick()), NULL );
}
if ( FAILED(hr) ) return DIENUM_CONTINUE;
return DIENUM_STOP;
}
/* OpenSceneGraph example, osgdirectinput.
*
* 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 <osgGA/EventQueue>
#include <iostream>
#include "DirectInputRegistry"
typedef std::pair<int, int> KeyValue;
typedef std::map<int, KeyValue> KeyMap;
KeyMap g_keyMap;
void buildKeyMap()
{
// TODO: finish the key map as you wish
g_keyMap[DIK_ESCAPE] = KeyValue(osgGA::GUIEventAdapter::KEY_Escape, 0);
g_keyMap[DIK_1] = KeyValue('1', 0);
g_keyMap[DIK_2] = KeyValue('2', 0);
g_keyMap[DIK_3] = KeyValue('3', 0);
g_keyMap[DIK_4] = KeyValue('4', 0);
g_keyMap[DIK_5] = KeyValue('5', 0);
g_keyMap[DIK_6] = KeyValue('6', 0);
g_keyMap[DIK_7] = KeyValue('7', 0);
g_keyMap[DIK_8] = KeyValue('8', 0);
g_keyMap[DIK_9] = KeyValue('9', 0);
g_keyMap[DIK_0] = KeyValue('0', 0);
g_keyMap[DIK_MINUS] = KeyValue('-', 0);
g_keyMap[DIK_EQUALS] = KeyValue('=', 0);
g_keyMap[DIK_BACK] = KeyValue(osgGA::GUIEventAdapter::KEY_BackSpace, 0);
g_keyMap[DIK_TAB] = KeyValue(osgGA::GUIEventAdapter::KEY_Tab, 0);
g_keyMap[DIK_SPACE] = KeyValue(osgGA::GUIEventAdapter::KEY_Space, 0);
}
bool DirectInputRegistry::initKeyboard( HWND handle )
{
if ( !_inputDevice ) return false;
HRESULT hr = _inputDevice->CreateDevice( GUID_SysKeyboard, &_keyboard, NULL );
if ( FAILED(hr) || _keyboard==NULL )
{
osg::notify(osg::WARN) << "Unable to create keyboard." << std::endl;
return false;
}
buildKeyMap();
return initImplementation( handle, _keyboard, &c_dfDIKeyboard );
}
bool DirectInputRegistry::initMouse( HWND handle )
{
if ( !_inputDevice ) return false;
HRESULT hr = _inputDevice->CreateDevice( GUID_SysMouse, &_mouse, NULL );
if ( FAILED(hr) || _mouse==NULL )
{
osg::notify(osg::WARN) << "Unable to create mouse." << std::endl;
return false;
}
return initImplementation( handle, _mouse, &c_dfDIMouse2 );
}
bool DirectInputRegistry::initJoystick( HWND handle )
{
if ( !_inputDevice ) return false;
HRESULT hr = _inputDevice->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback,
NULL, DIEDFL_ATTACHEDONLY );
if ( FAILED(hr) || _joystick==NULL )
{
osg::notify(osg::WARN) << "Unable to enumerate joysticks." << std::endl;
return false;
}
return initImplementation( handle, _joystick, &c_dfDIJoystick2 );
}
void DirectInputRegistry::updateState( osgGA::EventQueue* eventQueue )
{
HRESULT hr;
if ( !_supportDirectInput || !eventQueue ) return;
if ( _keyboard )
{
pollDevice( _keyboard );
char buffer[256] = {0};
hr = _keyboard->GetDeviceState( sizeof(buffer), &buffer );
if ( SUCCEEDED(hr) )
{
for ( KeyMap::iterator itr=g_keyMap.begin(); itr!=g_keyMap.end(); ++itr )
{
KeyValue& key = itr->second;
char value = buffer[itr->first];
if ( key.second==value ) continue;
key.second = value;
if ( value&0x80 )
eventQueue->keyPress( key.first );
else
eventQueue->keyRelease( key.first );
}
}
}
if ( _mouse )
{
pollDevice( _mouse );
DIMOUSESTATE2 mouseState;
hr = _mouse->GetDeviceState( sizeof(DIMOUSESTATE2), &mouseState );
// TODO: add mouse handlers
}
if ( _joystick )
{
pollDevice( _joystick );
osg::ref_ptr<JoystickEvent> event = new JoystickEvent;
hr = _joystick->GetDeviceState( sizeof(DIJOYSTATE2), &(event->_js) );
if ( SUCCEEDED(hr) ) eventQueue->userEvent( event.get() );
}
}
DirectInputRegistry::DirectInputRegistry()
: _keyboard(0), _mouse(0), _joystick(0),
_supportDirectInput(true)
{
HRESULT hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
IID_IDirectInput8, (VOID**)&_inputDevice, NULL );
if ( FAILED(hr) )
{
osg::notify(osg::WARN) << "Unable to create DirectInput object." << std::endl;
_supportDirectInput = false;
}
}
DirectInputRegistry::~DirectInputRegistry()
{
releaseDevice( _keyboard );
releaseDevice( _mouse );
releaseDevice( _joystick );
if ( _inputDevice ) _inputDevice->Release();
}
bool DirectInputRegistry::initImplementation( HWND handle, LPDIRECTINPUTDEVICE8 device, LPCDIDATAFORMAT format )
{
_supportDirectInput = true;
HRESULT hr = device->SetDataFormat( format );
if ( FAILED(hr) )
{
osg::notify(osg::WARN) << "Unable to set device data format." << std::endl;
_supportDirectInput = false;
}
hr = device->SetCooperativeLevel( handle, DISCL_EXCLUSIVE|DISCL_FOREGROUND );
if ( FAILED(hr) )
{
osg::notify(osg::WARN) << "Unable to attach device to window." << std::endl;
_supportDirectInput = false;
}
device->Acquire();
return _supportDirectInput;
}
void DirectInputRegistry::pollDevice( LPDIRECTINPUTDEVICE8 device )
{
HRESULT hr = device->Poll();
if ( FAILED(hr) )
{
device->Acquire();
if ( hr==DIERR_INPUTLOST )
osg::notify(osg::WARN) << "Device lost." << std::endl;
}
}
void DirectInputRegistry::releaseDevice( LPDIRECTINPUTDEVICE8 device )
{
if ( device )
{
device->Unacquire();
device->Release();
}
}
BOOL CALLBACK DirectInputRegistry::EnumJoysticksCallback( const DIDEVICEINSTANCE* didInstance, VOID* )
{
HRESULT hr;
LPDIRECTINPUT8 device = DirectInputRegistry::instance()->getDevice();
if ( device )
{
hr = device->CreateDevice( didInstance->guidInstance,
&(DirectInputRegistry::instance()->getJoyStick()), NULL );
}
if ( FAILED(hr) ) return DIENUM_CONTINUE;
return DIENUM_STOP;
}

View File

@@ -1,115 +1,115 @@
/* OpenSceneGraph example, osgdirectinput.
*
* 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 <osgDB/ReadFile>
#include <osgGA/StateSetManipulator>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <iostream>
#include "DirectInputRegistry"
class CustomViewer : public osgViewer::Viewer
{
public:
CustomViewer() : osgViewer::Viewer() {}
virtual ~CustomViewer() {}
virtual void eventTraversal()
{
DirectInputRegistry::instance()->updateState( _eventQueue.get() );
osgViewer::Viewer::eventTraversal();
}
protected:
virtual void viewerInit()
{
osgViewer::GraphicsWindowWin32* windowWin32 =
dynamic_cast<osgViewer::GraphicsWindowWin32*>( _camera->getGraphicsContext() );
if ( windowWin32 )
{
HWND hwnd = windowWin32->getHWND();
DirectInputRegistry::instance()->initKeyboard( hwnd );
//DirectInputRegistry::instance()->initMouse( hwnd );
DirectInputRegistry::instance()->initJoystick( hwnd );
}
osgViewer::Viewer::viewerInit();
}
};
class JoystickHandler : public osgGA::GUIEventHandler
{
public:
JoystickHandler() {}
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
switch ( ea.getEventType() )
{
case osgGA::GUIEventAdapter::KEYDOWN:
std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " down ***" << std::endl;
break;
case osgGA::GUIEventAdapter::KEYUP:
std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " up ***" << std::endl;
break;
case osgGA::GUIEventAdapter::USER:
{
const JoystickEvent* event = dynamic_cast<const JoystickEvent*>( ea.getUserData() );
if ( !event ) break;
const DIJOYSTATE2& js = event->_js;
for ( unsigned int i=0; i<128; ++i )
{
if ( js.rgbButtons[i] )
std::cout << "*** Joystick Btn" << i << " = " << (int)js.rgbButtons[i] << std::endl;
}
if ( js.lX==0x0000 ) std::cout << "*** Joystick X-" << std::endl;
else if ( js.lX==0xffff ) std::cout << "*** Joystick X+" << std::endl;
if ( js.lY==0 ) std::cout << "*** Joystick Y-" << std::endl;
else if ( js.lY==0xffff ) std::cout << "*** Joystick Y+" << std::endl;
}
return true;
default:
break;
}
return false;
}
};
int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );
osg::Node* model = osgDB::readNodeFiles( arguments );
if ( !model ) model = osgDB::readNodeFile( "cow.osg" );
if ( !model )
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
CustomViewer viewer;
viewer.addEventHandler( new JoystickHandler );
viewer.addEventHandler( new osgViewer::StatsHandler );
viewer.addEventHandler( new osgViewer::WindowSizeHandler );
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
viewer.setSceneData( model );
viewer.setUpViewInWindow( 250, 50, 800, 600 );
return viewer.run();
}
/* OpenSceneGraph example, osgdirectinput.
*
* 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 <osgDB/ReadFile>
#include <osgGA/StateSetManipulator>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <iostream>
#include "DirectInputRegistry"
class CustomViewer : public osgViewer::Viewer
{
public:
CustomViewer() : osgViewer::Viewer() {}
virtual ~CustomViewer() {}
virtual void eventTraversal()
{
DirectInputRegistry::instance()->updateState( _eventQueue.get() );
osgViewer::Viewer::eventTraversal();
}
protected:
virtual void viewerInit()
{
osgViewer::GraphicsWindowWin32* windowWin32 =
dynamic_cast<osgViewer::GraphicsWindowWin32*>( _camera->getGraphicsContext() );
if ( windowWin32 )
{
HWND hwnd = windowWin32->getHWND();
DirectInputRegistry::instance()->initKeyboard( hwnd );
//DirectInputRegistry::instance()->initMouse( hwnd );
DirectInputRegistry::instance()->initJoystick( hwnd );
}
osgViewer::Viewer::viewerInit();
}
};
class JoystickHandler : public osgGA::GUIEventHandler
{
public:
JoystickHandler() {}
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
switch ( ea.getEventType() )
{
case osgGA::GUIEventAdapter::KEYDOWN:
std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " down ***" << std::endl;
break;
case osgGA::GUIEventAdapter::KEYUP:
std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " up ***" << std::endl;
break;
case osgGA::GUIEventAdapter::USER:
{
const JoystickEvent* event = dynamic_cast<const JoystickEvent*>( ea.getUserData() );
if ( !event ) break;
const DIJOYSTATE2& js = event->_js;
for ( unsigned int i=0; i<128; ++i )
{
if ( js.rgbButtons[i] )
std::cout << "*** Joystick Btn" << i << " = " << (int)js.rgbButtons[i] << std::endl;
}
if ( js.lX==0x0000 ) std::cout << "*** Joystick X-" << std::endl;
else if ( js.lX==0xffff ) std::cout << "*** Joystick X+" << std::endl;
if ( js.lY==0 ) std::cout << "*** Joystick Y-" << std::endl;
else if ( js.lY==0xffff ) std::cout << "*** Joystick Y+" << std::endl;
}
return true;
default:
break;
}
return false;
}
};
int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );
osg::Node* model = osgDB::readNodeFiles( arguments );
if ( !model ) model = osgDB::readNodeFile( "cow.osg" );
if ( !model )
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
CustomViewer viewer;
viewer.addEventHandler( new JoystickHandler );
viewer.addEventHandler( new osgViewer::StatsHandler );
viewer.addEventHandler( new osgViewer::WindowSizeHandler );
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
viewer.setSceneData( model );
viewer.setUpViewInWindow( 250, 50, 800, 600 );
return viewer.run();
}

View File

@@ -1,150 +1,150 @@
/* OpenSceneGraph example, osgdrawinstanced.
*
* 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.
*/
//
// This code is copyright (c) 2008 Skew Matrix Software LLC. You may use
// the code under the licensing terms described above.
//
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osgDB/WriteFile>
#include <iostream>
void
createDAIGeometry( osg::Geometry& geom, int nInstances=1 )
{
const float halfDimX( .5 );
const float halfDimZ( .5 );
osg::Vec3Array* v = new osg::Vec3Array;
v->resize( 4 );
geom.setVertexArray( v );
// Geometry for a single quad.
(*v)[ 0 ] = osg::Vec3( -halfDimX, 0., -halfDimZ );
(*v)[ 1 ] = osg::Vec3( halfDimX, 0., -halfDimZ );
(*v)[ 2 ] = osg::Vec3( halfDimX, 0., halfDimZ );
(*v)[ 3 ] = osg::Vec3( -halfDimX, 0., halfDimZ );
// Use the DrawArraysInstanced PrimitiveSet and tell it to draw 1024 instances.
geom.addPrimitiveSet( new osg::DrawArrays( GL_QUADS, 0, 4, nInstances ) );
}
osg::StateSet*
createStateSet()
{
osg::ref_ptr< osg::StateSet > ss = new osg::StateSet;
// Create a vertex program that references the gl_InstanceID to
// render each instance uniquely. gl_InstanceID will be in the range
// 0 to numInstances-1 (1023 in our case).
std::string vertexSource =
"uniform sampler2D osgLogo; \n"
"uniform float osg_SimulationTime; \n"
"void main() \n"
"{ \n"
// Using the instance ID, generate "texture coords" for this instance.
"vec2 tC; \n"
"float r = float(gl_InstanceID) / 32.; \n"
"tC.s = fract( r ); tC.t = floor( r ) / 32.; \n"
// Get the color from the OSG logo.
"gl_FrontColor = texture2D( osgLogo, tC ); \n"
// Use the (scaled) tex coord to translate the position of the vertices.
"vec4 pos = vec4( tC.s * 48., 0., tC.t * 48., 1. ); \n"
// Compute a rotation angle from the instanceID and elapsed time.
"float timeOffset = gl_InstanceID / (32. * 32.); \n"
"float angle = ( osg_SimulationTime - timeOffset ) * 6.283; \n"
"float sa = sin( angle ); \n"
"float ca = cos( angle ); \n"
// New orientation, rotate around z axis.
"vec4 newX = vec4( ca, sa, 0., 0. ); \n"
"vec4 newY = vec4( sa, ca, 0., 0. ); \n"
"vec4 newZ = vec4( 0., 0., 1., 0. ); \n"
"mat4 mV = mat4( newX, newY, newZ, pos ); \n"
"gl_Position = ( gl_ModelViewProjectionMatrix * mV * gl_Vertex ); \n"
"} \n";
osg::ref_ptr< osg::Shader > vertexShader = new osg::Shader();
vertexShader->setType( osg::Shader::VERTEX );
vertexShader->setShaderSource( vertexSource );
osg::ref_ptr< osg::Program > program = new osg::Program();
program->addShader( vertexShader.get() );
ss->setAttribute( program.get(),
osg::StateAttribute::ON | osg::StateAttribute::PROTECTED );
osg::ref_ptr< osg::Image> iLogo = osgDB::readImageFile( "Images/osg128.png" );
if( !iLogo.valid() )
{
osg::notify( osg::ALWAYS ) << "Can't open image file osg128.png" << std::endl;
return( NULL );
}
osg::Texture2D* texLogo = new osg::Texture2D( iLogo.get() );
texLogo->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );
texLogo->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );
ss->setTextureAttribute( 0, texLogo );
osg::ref_ptr< osg::Uniform > texLogoUniform =
new osg::Uniform( "osgLogo", 0 );
ss->addUniform( texLogoUniform.get() );
return( ss.release() );
}
int main( int argc, char **argv )
{
osg::ArgumentParser arguments(&argc, argv);
// Make a scene graph consisting of a single Geode, containing
// a single Geometry, and a single PrimitiveSet.
osg::ref_ptr< osg::Geode > geode = new osg::Geode;
osg::ref_ptr< osg::Geometry > geom = new osg::Geometry;
// Configure the Geometry for use with EXT_draw_arrays:
// DL off and buffer objects on.
geom->setUseDisplayList( false );
geom->setUseVertexBufferObjects( true );
// OSG has no clue where out vertex shader will place the geometric data,
// so specify an initial bound to allow proper culling and near/far computation.
osg::BoundingBox bb( -1., -.1, -1., 49., 1., 49. );
geom->setInitialBound( bb );
// Add geometric data and the PrimitiveSet. Specify numInstances as 32*32 or 1024.
createDAIGeometry( *geom, 32*32 );
geode->addDrawable( geom.get() );
// Create a StateSet to render the instanced Geometry.
osg::ref_ptr< osg::StateSet > ss = createStateSet();
geode->setStateSet( ss.get() );
// osgDB::writeNodeFile(*geode, "instanced.osg");
osgViewer::Viewer viewer(arguments);
viewer.setSceneData( geode.get() );
return viewer.run();
}
/* OpenSceneGraph example, osgdrawinstanced.
*
* 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.
*/
//
// This code is copyright (c) 2008 Skew Matrix Software LLC. You may use
// the code under the licensing terms described above.
//
#include <osgDB/ReadFile>
#include <osgViewer/Viewer>
#include <osg/Geometry>
#include <osg/Texture2D>
#include <osgDB/WriteFile>
#include <iostream>
void
createDAIGeometry( osg::Geometry& geom, int nInstances=1 )
{
const float halfDimX( .5 );
const float halfDimZ( .5 );
osg::Vec3Array* v = new osg::Vec3Array;
v->resize( 4 );
geom.setVertexArray( v );
// Geometry for a single quad.
(*v)[ 0 ] = osg::Vec3( -halfDimX, 0., -halfDimZ );
(*v)[ 1 ] = osg::Vec3( halfDimX, 0., -halfDimZ );
(*v)[ 2 ] = osg::Vec3( halfDimX, 0., halfDimZ );
(*v)[ 3 ] = osg::Vec3( -halfDimX, 0., halfDimZ );
// Use the DrawArraysInstanced PrimitiveSet and tell it to draw 1024 instances.
geom.addPrimitiveSet( new osg::DrawArrays( GL_QUADS, 0, 4, nInstances ) );
}
osg::StateSet*
createStateSet()
{
osg::ref_ptr< osg::StateSet > ss = new osg::StateSet;
// Create a vertex program that references the gl_InstanceID to
// render each instance uniquely. gl_InstanceID will be in the range
// 0 to numInstances-1 (1023 in our case).
std::string vertexSource =
"uniform sampler2D osgLogo; \n"
"uniform float osg_SimulationTime; \n"
"void main() \n"
"{ \n"
// Using the instance ID, generate "texture coords" for this instance.
"vec2 tC; \n"
"float r = float(gl_InstanceID) / 32.; \n"
"tC.s = fract( r ); tC.t = floor( r ) / 32.; \n"
// Get the color from the OSG logo.
"gl_FrontColor = texture2D( osgLogo, tC ); \n"
// Use the (scaled) tex coord to translate the position of the vertices.
"vec4 pos = vec4( tC.s * 48., 0., tC.t * 48., 1. ); \n"
// Compute a rotation angle from the instanceID and elapsed time.
"float timeOffset = gl_InstanceID / (32. * 32.); \n"
"float angle = ( osg_SimulationTime - timeOffset ) * 6.283; \n"
"float sa = sin( angle ); \n"
"float ca = cos( angle ); \n"
// New orientation, rotate around z axis.
"vec4 newX = vec4( ca, sa, 0., 0. ); \n"
"vec4 newY = vec4( sa, ca, 0., 0. ); \n"
"vec4 newZ = vec4( 0., 0., 1., 0. ); \n"
"mat4 mV = mat4( newX, newY, newZ, pos ); \n"
"gl_Position = ( gl_ModelViewProjectionMatrix * mV * gl_Vertex ); \n"
"} \n";
osg::ref_ptr< osg::Shader > vertexShader = new osg::Shader();
vertexShader->setType( osg::Shader::VERTEX );
vertexShader->setShaderSource( vertexSource );
osg::ref_ptr< osg::Program > program = new osg::Program();
program->addShader( vertexShader.get() );
ss->setAttribute( program.get(),
osg::StateAttribute::ON | osg::StateAttribute::PROTECTED );
osg::ref_ptr< osg::Image> iLogo = osgDB::readImageFile( "Images/osg128.png" );
if( !iLogo.valid() )
{
osg::notify( osg::ALWAYS ) << "Can't open image file osg128.png" << std::endl;
return( NULL );
}
osg::Texture2D* texLogo = new osg::Texture2D( iLogo.get() );
texLogo->setFilter( osg::Texture2D::MIN_FILTER, osg::Texture2D::LINEAR );
texLogo->setFilter( osg::Texture2D::MAG_FILTER, osg::Texture2D::LINEAR );
ss->setTextureAttribute( 0, texLogo );
osg::ref_ptr< osg::Uniform > texLogoUniform =
new osg::Uniform( "osgLogo", 0 );
ss->addUniform( texLogoUniform.get() );
return( ss.release() );
}
int main( int argc, char **argv )
{
osg::ArgumentParser arguments(&argc, argv);
// Make a scene graph consisting of a single Geode, containing
// a single Geometry, and a single PrimitiveSet.
osg::ref_ptr< osg::Geode > geode = new osg::Geode;
osg::ref_ptr< osg::Geometry > geom = new osg::Geometry;
// Configure the Geometry for use with EXT_draw_arrays:
// DL off and buffer objects on.
geom->setUseDisplayList( false );
geom->setUseVertexBufferObjects( true );
// OSG has no clue where out vertex shader will place the geometric data,
// so specify an initial bound to allow proper culling and near/far computation.
osg::BoundingBox bb( -1., -.1, -1., 49., 1., 49. );
geom->setInitialBound( bb );
// Add geometric data and the PrimitiveSet. Specify numInstances as 32*32 or 1024.
createDAIGeometry( *geom, 32*32 );
geode->addDrawable( geom.get() );
// Create a StateSet to render the instanced Geometry.
osg::ref_ptr< osg::StateSet > ss = createStateSet();
geode->setStateSet( ss.get() );
// osgDB::writeNodeFile(*geode, "instanced.osg");
osgViewer::Viewer viewer(arguments);
viewer.setSceneData( geode.get() );
return viewer.run();
}

View File

@@ -1,6 +1,6 @@
#this file is automatically generated
SET(TARGET_SRC osgshadergen.cpp )
#### end var setup ###
SETUP_EXAMPLE(osgshadergen)
#this file is automatically generated
SET(TARGET_SRC osgshadergen.cpp )
#### end var setup ###
SETUP_EXAMPLE(osgshadergen)

View File

@@ -1,13 +1,13 @@
//
// MFC_OSG_MDI.RC2 - resources Microsoft Visual C++ does not edit directly
//
#ifdef APSTUDIO_INVOKED
#error this file is not editable by Microsoft Visual C++
#endif //APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
// Add manually edited resources here...
/////////////////////////////////////////////////////////////////////////////
//
// MFC_OSG_MDI.RC2 - resources Microsoft Visual C++ does not edit directly
//
#ifdef APSTUDIO_INVOKED
#error this file is not editable by Microsoft Visual C++
#endif //APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
// Add manually edited resources here...
/////////////////////////////////////////////////////////////////////////////

View File

@@ -1,97 +1,97 @@
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QGridLayout>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <osgQt/GraphicsWindowQt>
#include <iostream>
class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
ViewerWidget() : QWidget()
{
setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("cow.osg") );
QWidget* widget2 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("glider.osg") );
QWidget* widget3 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("axes.osg") );
QWidget* widget4 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("fountain.osg") );
QWidget* popupWidget = addViewWidget( createCamera(900,100,320,240,"Popup window",true),
osgDB::readNodeFile("dumptruck.osg") );
popupWidget->show();
QGridLayout* grid = new QGridLayout;
grid->addWidget( widget1, 0, 0 );
grid->addWidget( widget2, 0, 1 );
grid->addWidget( widget3, 1, 0 );
grid->addWidget( widget4, 1, 1 );
setLayout( grid );
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}
QWidget* addViewWidget( osg::Camera* camera, osg::Node* scene )
{
osgViewer::View* view = new osgViewer::View;
view->setCamera( camera );
addView( view );
view->setSceneData( scene );
view->addEventHandler( new osgViewer::StatsHandler );
view->setCameraManipulator( new osgGA::TrackballManipulator );
osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
return gw ? gw->getGraphWidget() : NULL;
}
osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
camera->setProjectionMatrixAsPerspective(
30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
return camera.release();
}
virtual void paintEvent( QPaintEvent* event )
{ frame(); }
protected:
QTimer _timer;
};
int main( int argc, char** argv )
{
QApplication app(argc, argv);
ViewerWidget* viewWidget = new ViewerWidget;
viewWidget->setGeometry( 100, 100, 800, 600 );
viewWidget->show();
return app.exec();
}
#include <QtCore/QTimer>
#include <QtGui/QApplication>
#include <QtGui/QGridLayout>
#include <osgViewer/CompositeViewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/TrackballManipulator>
#include <osgDB/ReadFile>
#include <osgQt/GraphicsWindowQt>
#include <iostream>
class ViewerWidget : public QWidget, public osgViewer::CompositeViewer
{
public:
ViewerWidget() : QWidget()
{
setThreadingModel(osgViewer::CompositeViewer::SingleThreaded);
QWidget* widget1 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("cow.osg") );
QWidget* widget2 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("glider.osg") );
QWidget* widget3 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("axes.osg") );
QWidget* widget4 = addViewWidget( createCamera(0,0,100,100), osgDB::readNodeFile("fountain.osg") );
QWidget* popupWidget = addViewWidget( createCamera(900,100,320,240,"Popup window",true),
osgDB::readNodeFile("dumptruck.osg") );
popupWidget->show();
QGridLayout* grid = new QGridLayout;
grid->addWidget( widget1, 0, 0 );
grid->addWidget( widget2, 0, 1 );
grid->addWidget( widget3, 1, 0 );
grid->addWidget( widget4, 1, 1 );
setLayout( grid );
connect( &_timer, SIGNAL(timeout()), this, SLOT(update()) );
_timer.start( 10 );
}
QWidget* addViewWidget( osg::Camera* camera, osg::Node* scene )
{
osgViewer::View* view = new osgViewer::View;
view->setCamera( camera );
addView( view );
view->setSceneData( scene );
view->addEventHandler( new osgViewer::StatsHandler );
view->setCameraManipulator( new osgGA::TrackballManipulator );
osgQt::GraphicsWindowQt* gw = dynamic_cast<osgQt::GraphicsWindowQt*>( camera->getGraphicsContext() );
return gw ? gw->getGraphWidget() : NULL;
}
osg::Camera* createCamera( int x, int y, int w, int h, const std::string& name="", bool windowDecoration=false )
{
osg::DisplaySettings* ds = osg::DisplaySettings::instance().get();
osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
traits->windowName = name;
traits->windowDecoration = windowDecoration;
traits->x = x;
traits->y = y;
traits->width = w;
traits->height = h;
traits->doubleBuffer = true;
traits->alpha = ds->getMinimumNumAlphaBits();
traits->stencil = ds->getMinimumNumStencilBits();
traits->sampleBuffers = ds->getMultiSamples();
traits->samples = ds->getNumMultiSamples();
osg::ref_ptr<osg::Camera> camera = new osg::Camera;
camera->setGraphicsContext( new osgQt::GraphicsWindowQt(traits.get()) );
camera->setClearColor( osg::Vec4(0.2, 0.2, 0.6, 1.0) );
camera->setViewport( new osg::Viewport(0, 0, traits->width, traits->height) );
camera->setProjectionMatrixAsPerspective(
30.0f, static_cast<double>(traits->width)/static_cast<double>(traits->height), 1.0f, 10000.0f );
return camera.release();
}
virtual void paintEvent( QPaintEvent* event )
{ frame(); }
protected:
QTimer _timer;
};
int main( int argc, char** argv )
{
QApplication app(argc, argv);
ViewerWidget* viewWidget = new ViewerWidget;
viewWidget->setGeometry( 100, 100, 800, 600 );
viewWidget->show();
return app.exec();
}