Code: Select all
#include "spacenavigator.h"
#define UnknownDevice 0
using namespace std;
CComPtr<IUnknown> _3DxDevice;
CComPtr<ISensor> g3DSensor;
CComPtr<IKeyboard> g3DKeyboard;
CComPtr<IAngleAxis> pRotation;
CComPtr<IVector3D> pTranslation;
SpaceNavigator::SpaceNavigator() {
translation.set(0.0,0.0,0.0);
rotation.set(0.0,0.0,0.0);
initDevice();
for(int i = 0; i < 6; i++) absoluteposition[i] = 0;
}
void SpaceNavigator::initDevice() {
long type;
HRESULT hr = :: CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (!SUCCEEDED(hr)) {
cout << "Fail on device connexion!" << endl;
return;
}
hr = _3DxDevice.CoCreateInstance(__uuidof(Device));
if (SUCCEEDED(hr)) {
CComPtr<ISimpleDevice> _3DxSimpleDevice;
hr = _3DxDevice.QueryInterface(&_3DxSimpleDevice);
hr = _3DxSimpleDevice -> get_Type(&type);
if (type == UnknownDevice) cout << "Space Navigator not found!";
if (SUCCEEDED(hr) && type != UnknownDevice) {
g3DSensor = _3DxSimpleDevice -> Sensor;
g3DKeyboard = _3DxSimpleDevice -> Keyboard;
_3DxSimpleDevice -> Connect();
cout << "SpaceNavigator found...";
}
}
}
void SpaceNavigator::updateDevice() {
if (g3DSensor) {
try {
pRotation = g3DSensor -> Rotation;
pTranslation = g3DSensor -> Translation;
absoluteposition[0] += pTranslation -> X; absoluteposition[1] += pTranslation -> Y;
absoluteposition[2] += pTranslation -> Z;
cout << "Translação: " << absoluteposition[0] << " " << absoluteposition[1] << " " << absoluteposition[2] <<endl> X; absoluteposition[4] += pRotation -> Y;
absoluteposition[5] += pRotation -> Z;
cout << "Rotação: " << absoluteposition[3] << " " << absoluteposition[4] << " " << absoluteposition[5] << endl;
}
catch (_com_error &e) {
cout << "Erro de comunicação..." << endl;
}
}
}