Thanks for the cut-pasted code  :) ...

I have attached how i actually did it ..


On Dec 12, 2007 12:03 AM, Paul Speed <[EMAIL PROTECTED]> wrote:

>
>
> maruti borker wrote:
> >
> > Yes , even i wanted to have quake-type controls ... can u please copy
> > paste some example code so  that i can have an idea.
> >
>
> Ok, our code is part of a Java peer since we actually grab the mouse
> values from Java.  So I'll cut and paste the relevant stuff but don't be
> surprised if it it a little disjointed... oh, and ignore my funky
> bracing style...  (and errorf() and warnf() are our own calls into a
> logging API on the Java side.)
>
> In the header, we include:
>   #define DIRECTINPUT_VERSION 0x0800
>   #include <dinput.h>
>
> And add a field to the class:
>   LPDIRECTINPUTDEVICE8 g_pMouse;
>
>
> In the class implementation we have an initialize method that acquires
> access to the mouse device:
>
>     HRESULT hr;
>     LPDIRECTINPUT8       g_pDI    = NULL;
>     g_pMouse = NULL;
>
>     // Create a DInput object
>     hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION,
>                              IID_IDirectInput8, (VOID**)&g_pDI, NULL );
>     if( FAILED(hr) )
>         {
>         errorf( "Failed to initialize DirectInput: %i", hr );
>         return( -1 );
>         }
>
>     // Obtain an interface to the system mouse device.
>     hr = g_pDI->CreateDevice( GUID_SysMouse, &g_pMouse, NULL );
>     if( FAILED(hr) )
>         {
>         errorf( "Failed to create mouse device: %i", hr );
>         return( -1 );
>         }
>
>     // Set the data format to "mouse format" - a predefined data format
>     //
>     // A data format specifies which controls on a device we
>     // are interested in, and how they should be reported.
>     //
>     // This tells DirectInput that we will be passing a
>     // DIMOUSESTATE2 structure to IDirectInputDevice::GetDeviceState.
>     hr = g_pMouse->SetDataFormat( &c_dfDIMouse2 );
>     if( FAILED(hr) )
>         {
>         errorf( "Failed to set data format: %i", hr );
>         return( -1 );
>         }
>
>     // By default we'll set the device to be in non-exclusive
>     // mode until acquired.  Which actually is still pretty
>     // much what we want other than the fact that regular events
>     // still work.
>     DWORD dwCoopFlags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
>
>     HWND hWnd = GetForegroundWindow();
>
>     // Set the cooperativity level to let DirectInput know how
>     // this device should interact with the system and with other
>     // DirectInput applications.
>     hr = g_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
>     if( FAILED(hr) )
>         {
>         errorf( "Failed to set mouse cooperation mode: %i", hr );
>         return( -1 );
>         }
>
>     // Acquire the non-exclusive device
>     hr = g_pMouse->Acquire();
>     if( FAILED(hr) )
>         {
>         errorf( "Mouse acquisition failed:%i", hr );
>         return( -1 );
>         }
>
>     return( (int)0 );
>
>
> Then to read the mouse state we have a method for reading the device
> values that looks like:
>
>     HRESULT       hr;
>     DIMOUSESTATE2 dims2;      // DirectInput mouse state structure
>
>     // Get the input's device state, and put the state in dims
>     ZeroMemory( &dims2, sizeof(dims2) );
>     hr = g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 );
>     if( FAILED(hr) )
>         {
>         // We may have lost acquisition of the device because another
>         // app went to the foreground.  Since we don't busy-wait for
>         // it to come back we can't rely on the return code to tell us...
>         // so we'll keep trying to reacquire for every error.
>         if( hr == DIERR_INPUTLOST )
>             warnf( "Mouse access lost." );
>
>         // Try to reacquire it once
>         hr = g_pMouse->Acquire();
>         if( FAILED(hr) )
>             {
>             // Just return an empty state
>             return( createMouseState( 0, 0, 0, 0, 0, 0 ) );
>             }
>         else
>             {
>             warnf( "Mouse access reacquired." );
>             }
>         }
>
>
>     return createMouseState( dims2.lX, dims2.lY, dims2.lZ,
>                              dims2.rgbButtons[0],
>                              dims2.rgbButtons[1],
>                              dims2.rgbButtons[2] );
>
> The create mouse state method call above is creating our own Java holder
> for the data.  I left it in because it takes all of the data members
> that are interesting.
>
> We have another method for acquiring exclusive and non-exclusive access
> to the mouse:
>
> void MousePeer::setAcquire(bool flag)
> {
>     // Unacquire the mouse if we've grabbed it previously
>     if( g_pMouse )
>         {
>         g_pMouse->Unacquire();
>         }
>
>     if( flag )
>         {
>         // Acquire exclusively
>
>         // Right now we always do exclusive foreground mode
>         // when we acquire.  This means that our app should have
>         // full access to mouse deltas as long as they are the
>         // foreground app
>         DWORD dwCoopFlags = DISCL_EXCLUSIVE | DISCL_FOREGROUND;
>
>         HRESULT hr;
>         HWND hWnd = GetForegroundWindow();
>
>         // Set the cooperativity level to let DirectInput know how
>         // this device should interact with the system and with other
>         // DirectInput applications.
>         hr = g_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
>         if( FAILED(hr) )
>             {
>             errorf( "Failed to set exclusive mouse mode: %i", hr );
>             return;
>             }
>
>         // Acquire the device
>         hr = g_pMouse->Acquire();
>         if( FAILED(hr) )
>             {
>             errorf( "Mouse acquisition failed: %i", hr );
>             return;
>             }
>         }
>     else
>         {
>         // Acquire non-exclusively
>
>         // By default we'll set the device to be in non-exclusive
>         // mode until acquired.  Which actually is still pretty
>         // much what we want other than the fact that regular events
>         // still work.
>         DWORD dwCoopFlags = DISCL_NONEXCLUSIVE | DISCL_FOREGROUND;
>
>         HRESULT hr;
>         HWND hWnd = GetForegroundWindow();
>
>         // Set the cooperativity level to let DirectInput know how
>         // this device should interact with the system and with other
>         // DirectInput applications.
>         hr = g_pMouse->SetCooperativeLevel( hWnd, dwCoopFlags );
>         if( FAILED(hr) )
>             {
>             errorf( "Failed to set mouse cooperation mode: %i", hr );
>             return;
>             }
>
>         // Acquire the non-exclusive device
>         hr = g_pMouse->Acquire();
>         if( FAILED(hr) )
>             {
>             errorf( "Mouse acquisition failed: %i", hr );
>             return;
>             }
>         }
> }
>
> In our case, I'm not sure we ever call that method since in our
> particular application it is ok for the mouse pointer to still be
> visible (even if it's a little confusing).
>
> Hopefully the above code is helpful to you.
> -Paul
> _______________________________________________
> osg-users mailing list
> osg-users@lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>



-- 
----------------------------------------------
Maruti Borker
IIIT Hyderabad
Website:- http://students.iiit.ac.in/~maruti
Blog:- http://marutiborker.wordpress.com
/*
   Main Viewer Program
   Authors:- G Deepti,Maruti Borker,Sashidhar
*/
#include <osg/PositionAttitudeTransform>
#include <osg/Group>
#include <osg/Node>
#include <osgDB/ReadFile> 
#include <osgViewer/Viewer>
#include <osgGA/TrackballManipulator>
#include <osgGA/DriveManipulator>
#include<iostream>
#include "FadeOperation.h"
// Main Global Variables needed
osgGA::TrackballManipulator *Mn;
osgViewer::GraphicsWindow* gw;
int flag_warp=0;
int MVMENT=100;
class KeyboardHandler: public osgGA::GUIEventHandler 
{
	public:
		KeyboardHandler() {}
		bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &)
		{
			if (ea.getEventType() == osgGA::GUIEventAdapter::KEYDOWN) 
			{
				if (ea.getKey() == 'w') { // To move forward 
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(0.0,0.0,-MVMENT));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;	
				}
				if (ea.getKey() == 'a') { //To move left
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(-MVMENT,0.0,0.0));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;
				}
				if (ea.getKey() == 's') { //To move backward
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(0.0,0.0,MVMENT));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;
				}
				if (ea.getKey() == 'd') { //To move right
					osg::Matrixd tempMatrix;
					tempMatrix.makeTranslate(osg::Vec3f(MVMENT,0.0,0.0));
					tempMatrix=tempMatrix*Mn->getMatrix();
					Mn->setByMatrix(tempMatrix);
					return true;
				}
			}
			if( ea.getEventType() == osgGA::GUIEventAdapter::MOVE )
			{
				static float px = ea.getX();
				static float py = ea.getY();
				px = ea.getX();
				py = ea.getY();
				if(px==(ea.getXmax()+ea.getXmin())/2 && py==(ea.getYmax()+ea.getYmin())/2) return true;
				double nrm1=ea.getXmax()-ea.getXmin();
				double nrm2=ea.getYmax()-ea.getYmin();
				float dx = -(px-(ea.getXmax()+ea.getXmin())/2)/nrm1;
				float dy = (py-(ea.getYmax()+ea.getYmin())/2)/nrm2;
				dx*=10.0;
				dy*=10.0;
				osg::Matrixd tempMatrix;
				tempMatrix.makeRotate(
						osg::DegreesToRadians(dx), osg::Vec3(0,1,0), // roll
						osg::DegreesToRadians(dy), osg::Vec3(1,0,0) , // pitch
						osg::DegreesToRadians(0.0), osg::Vec3(0,0,1) );
				tempMatrix=tempMatrix*Mn->getMatrix();
				Mn->setByMatrix(tempMatrix);
				return true;
			}
			if( ea.getEventType()==osgGA::GUIEventAdapter::FRAME)
			{
				flag_warp++;
				if(flag_warp==10)
				{
					gw->requestWarpPointer((ea.getXmax()+ea.getXmin())/2,(ea.getYmax()+ea.getYmin())/2);
					flag_warp=0;
				}
			}
			return false;
		}
};
int main()
{
	osg::Node* fileNode = NULL;
	osg::Group* root = NULL;
	osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits;
	traits->width = 1024;
	traits->height = 768;
	traits->windowDecoration = false;
	traits->doubleBuffer = true;
	traits->sharedContext = 0;
	osg::ref_ptr<osg::GraphicsContext> gc = osg::GraphicsContext::createGraphicsContext(traits.get());
	gw = dynamic_cast<osgViewer::GraphicsWindow*>(gc.get());
	gw->useCursor(false);
	gw->requestWarpPointer(512,384);
	osgViewer::Viewer viewer;
	viewer.getCamera()->setGraphicsContext(gc.get());
	viewer.getCamera()->setViewport(0,0,1024,768);
	fileNode = osgDB::readNodeFile("cow.osg");
	root = new osg::Group();
	root->addChild(fileNode);
	Mn= new osgGA::TrackballManipulator();
	viewer.setCameraManipulator(Mn);
	KeyboardHandler *KH=new KeyboardHandler;
	viewer.addEventHandler(KH);
	viewer.setSceneData( root );
	viewer.realize();
	return viewer.run();
}
_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to