1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
2 | * |
---|
3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
5 | * (at your option) any later version. The full license is in LICENSE file |
---|
6 | * included with this distribution, and on the openscenegraph.org website. |
---|
7 | * |
---|
8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
9 | * You have to aquire licenses for all used proprietary modules. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * OpenSceneGraph Public License for more details. |
---|
15 | */ |
---|
16 | |
---|
17 | #include <manip_spaceMouse.h> |
---|
18 | |
---|
19 | #define _ATL_APARTMENT_THREADED |
---|
20 | #define _ATL_NO_AUTOMATIC_NAMESPACE |
---|
21 | |
---|
22 | #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit |
---|
23 | |
---|
24 | #define _WIN32_DCOM |
---|
25 | |
---|
26 | #include <objbase.h> |
---|
27 | #include <atlbase.h> |
---|
28 | #include <atlcom.h> |
---|
29 | #include <atlwin.h> |
---|
30 | #include <atltypes.h> |
---|
31 | #include <atlctl.h> |
---|
32 | #include <atlhost.h> |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | using namespace ATL; |
---|
37 | using namespace osgVisual; |
---|
38 | |
---|
39 | |
---|
40 | #import "progid:TDxInput.Device" no_namespace |
---|
41 | |
---|
42 | class SpaceMouse::SpaceMouseImpl |
---|
43 | { |
---|
44 | public: |
---|
45 | enum SupportedStatus |
---|
46 | { |
---|
47 | DETECTION_PENDING, |
---|
48 | SUPPORTED, |
---|
49 | NOT_SUPPORTED |
---|
50 | }; |
---|
51 | |
---|
52 | SpaceMouseImpl() : _supported(DETECTION_PENDING) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | SupportedStatus isSupported() const |
---|
57 | { |
---|
58 | return(_supported); |
---|
59 | } |
---|
60 | |
---|
61 | int initialize() |
---|
62 | { |
---|
63 | HRESULT hr = ::CoInitializeEx(NULL, COINIT_MULTITHREADED); |
---|
64 | |
---|
65 | // Create the device object |
---|
66 | hr = _3DxDevice.CoCreateInstance(__uuidof(Device)); |
---|
67 | if (SUCCEEDED(hr)) |
---|
68 | { |
---|
69 | CComPtr<ISimpleDevice> _3DxSimpleDevice; |
---|
70 | |
---|
71 | hr = _3DxDevice.QueryInterface(&_3DxSimpleDevice); |
---|
72 | if (SUCCEEDED(hr)) |
---|
73 | { |
---|
74 | // Get the interfaces to the sensor and the keyboard; |
---|
75 | _3DSensor = _3DxSimpleDevice->Sensor; |
---|
76 | _3DKeyboard = _3DxSimpleDevice->Keyboard; |
---|
77 | |
---|
78 | // Associate a configuration with this device |
---|
79 | hr = _3DxSimpleDevice->LoadPreferences(_T("RBAnimation")); |
---|
80 | // Connect to the driver |
---|
81 | hr = _3DxSimpleDevice->Connect(); |
---|
82 | |
---|
83 | _supported = SUPPORTED; |
---|
84 | |
---|
85 | return(1); //Base::SUCCESS |
---|
86 | } |
---|
87 | } |
---|
88 | |
---|
89 | _supported = NOT_SUPPORTED; |
---|
90 | |
---|
91 | return(0); //Base::FAILURE |
---|
92 | } |
---|
93 | |
---|
94 | void shutdown() |
---|
95 | { |
---|
96 | if (_3DSensor) |
---|
97 | _3DSensor.Release(); |
---|
98 | |
---|
99 | if (_3DKeyboard) |
---|
100 | _3DKeyboard.Release(); |
---|
101 | |
---|
102 | if (_3DxDevice) |
---|
103 | { |
---|
104 | _3DxDevice->Disconnect(); |
---|
105 | _3DxDevice.Release(); |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | int getTranslations(double& dTX, double& dTY, double& dTZ) |
---|
111 | { |
---|
112 | try { |
---|
113 | CComPtr<IVector3D> pTranslation = _3DSensor->Translation; |
---|
114 | |
---|
115 | double dTransFactor = pTranslation->Length; |
---|
116 | dTransFactor /= 5.0; |
---|
117 | |
---|
118 | dTX = pTranslation->X * dTransFactor; |
---|
119 | dTY = pTranslation->Y * dTransFactor; |
---|
120 | dTZ = pTranslation->Z * dTransFactor; |
---|
121 | |
---|
122 | pTranslation.Release(); |
---|
123 | } |
---|
124 | catch (...) |
---|
125 | { |
---|
126 | return 0; //Base::FAILURE |
---|
127 | } |
---|
128 | |
---|
129 | return 1; //Base::SUCCESS |
---|
130 | } |
---|
131 | |
---|
132 | int getRotations(double& dRX, double& dRY, double& dRZ) |
---|
133 | { |
---|
134 | try { |
---|
135 | CComPtr<IAngleAxis> pRotation = _3DSensor->Rotation; |
---|
136 | |
---|
137 | double dRotAngle = pRotation->Angle; |
---|
138 | dRotAngle /= 4000.0; |
---|
139 | |
---|
140 | dRX = pRotation->X * dRotAngle; |
---|
141 | dRY = pRotation->Y * dRotAngle; |
---|
142 | dRZ = pRotation->Z * dRotAngle; |
---|
143 | |
---|
144 | pRotation.Release(); |
---|
145 | } |
---|
146 | catch (...) |
---|
147 | { |
---|
148 | return 0; //Base::FAILURE |
---|
149 | } |
---|
150 | |
---|
151 | return 1; //Base::SUCCESS |
---|
152 | } |
---|
153 | |
---|
154 | private: |
---|
155 | SupportedStatus _supported; |
---|
156 | |
---|
157 | CComPtr<ISimpleDevice> _3DxDevice; |
---|
158 | CComPtr<ISensor> _3DSensor; |
---|
159 | CComPtr<IKeyboard> _3DKeyboard; |
---|
160 | }; |
---|
161 | |
---|
162 | |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | SpaceMouse::SpaceMouse() : _spaceMouseImpl(new SpaceMouseImpl) |
---|
170 | { |
---|
171 | } |
---|
172 | |
---|
173 | SpaceMouse::~SpaceMouse() |
---|
174 | { |
---|
175 | delete _spaceMouseImpl; |
---|
176 | } |
---|
177 | |
---|
178 | int SpaceMouse::initialize() |
---|
179 | { |
---|
180 | return(_spaceMouseImpl->initialize()); |
---|
181 | } |
---|
182 | |
---|
183 | void SpaceMouse::shutdown() |
---|
184 | { |
---|
185 | _spaceMouseImpl->shutdown(); |
---|
186 | } |
---|
187 | |
---|
188 | int SpaceMouse::getTranslations(double& dX, double& dY, double& dZ) |
---|
189 | { |
---|
190 | dX = 0; |
---|
191 | dY = 0; |
---|
192 | dZ = 0; |
---|
193 | return(_spaceMouseImpl->getTranslations(dX, dY, dZ)); |
---|
194 | } |
---|
195 | |
---|
196 | int SpaceMouse::getRotations(double& dX, double& dY, double& dZ) |
---|
197 | { |
---|
198 | dX = 0; |
---|
199 | dY = 0; |
---|
200 | dZ = 0; |
---|
201 | return(_spaceMouseImpl->getRotations(dX, dY, dZ)); |
---|
202 | } |
---|