232 lines
5.7 KiB
C++
232 lines
5.7 KiB
C++
//------------------------------------------------------------------------------
|
|
//
|
|
// SimConnect Cockpit Camera Sample
|
|
//
|
|
// Description:
|
|
// Press the < and > keys (actually the , and . keys, as there is
|
|
// no use of the shift key) to rotate the pilot's view
|
|
//------------------------------------------------------------------------------
|
|
#include <windows.h>
|
|
#include <tchar.h>
|
|
#include <stdio.h>
|
|
#include <strsafe.h>
|
|
|
|
#include "SCENE.h"
|
|
|
|
#ifndef SIMUP
|
|
|
|
|
|
bool isNew = true;
|
|
int quit = 0;
|
|
HANDLE hSimConnect = NULL;
|
|
void FactorSmoothMatrix(int index, class MATRIX *MatrixPtr, float rotationSmooth);
|
|
void FactorSmoothMatrixQuat(int index, class MATRIX *MatrixPtr, float quatSmooth, float quatHold);
|
|
void MatrixStretchAngle(MATRIX *MatrixPtr, float factor, float limit);
|
|
void testCockpitCamera(bool close, bool newDelta, float xFactor, float yFactor, float zFactor, float rFactor, float tSmoothFactor, float rSmoothFactor){}
|
|
|
|
|
|
#else
|
|
|
|
#include "SimConnect.h"
|
|
|
|
extern WORLD *TheScene;
|
|
bool isNew = true;
|
|
|
|
|
|
static MATRIX lastMatrix;
|
|
static float stXFactor;
|
|
static float stYFactor;
|
|
static float stZFactor;
|
|
static float stRFactor;
|
|
static float stTSmoothFactor;
|
|
static float stRSmoothFactor;
|
|
static int quatMod;
|
|
|
|
static float Move[6];
|
|
|
|
int quit = 0;
|
|
HANDLE hSimConnect = NULL;
|
|
|
|
|
|
static enum GROUP_ID {
|
|
GROUP0,
|
|
};
|
|
|
|
static enum EVENT_ID {
|
|
EVENT_COUCOU,
|
|
EVENT_INIT,
|
|
};
|
|
|
|
static float cameraBank = 0.0f;
|
|
|
|
float normalize180 (float v)
|
|
{
|
|
while (v < -180.0f) v += 360.0f;
|
|
while (v > 180.0f) v -= 360.0f;
|
|
return v;
|
|
}
|
|
void FactorSmoothMatrix(int index, MATRIX *MatrixPtr, float rotationSmooth);
|
|
void FactorSmoothMatrixQuat(int index, MATRIX *MatrixPtr, float quatSmooth, float quatHold);
|
|
void MatrixStretchAngle(MATRIX *MatrixPtr, float factor, float limit);
|
|
|
|
void CALLBACK MyDispatchProcCC(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
|
|
{
|
|
HRESULT hr;
|
|
MATRIX *currentPtr = (MATRIX *)pContext;
|
|
MATRIX currentM;
|
|
|
|
switch(pData->dwID)
|
|
{
|
|
|
|
case SIMCONNECT_RECV_ID_EVENT:
|
|
{
|
|
SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
|
|
|
|
switch(evt->uEventID)
|
|
{
|
|
case EVENT_INIT:
|
|
isNew = true;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SIMCONNECT_RECV_ID_EVENT_FRAME:
|
|
if (stRFactor != 0 && stRFactor != 1)
|
|
{
|
|
MatrixStretchAngle(currentPtr, stRFactor, -1);
|
|
}
|
|
|
|
if (quatMod)
|
|
FactorSmoothMatrixQuat(55, currentPtr, stRSmoothFactor, 0);
|
|
else
|
|
FactorSmoothMatrix(55, currentPtr, stRSmoothFactor);
|
|
|
|
currentM = *currentPtr;
|
|
|
|
if (isNew || memcmp(&lastMatrix, ¤tM, sizeof(MATRIX)))
|
|
{
|
|
if (isNew)
|
|
{
|
|
lastMatrix = *currentPtr;
|
|
memset(Move, 0, sizeof(Move));
|
|
}
|
|
|
|
|
|
Move[0] = Move[0]*(1.0-stTSmoothFactor) + (currentM.T.x - lastMatrix.T.x)*stXFactor*stTSmoothFactor;
|
|
Move[1] = Move[1]*(1.0-stTSmoothFactor) + (currentM.T.y - lastMatrix.T.y)*stYFactor*stTSmoothFactor;
|
|
Move[2] = Move[2]*(1.0-stTSmoothFactor) + (currentM.T.z - lastMatrix.T.z)*stZFactor*stTSmoothFactor;
|
|
|
|
Vector currEuler;
|
|
Vector lastEuler;
|
|
|
|
currentM.ToEuler(currEuler);
|
|
lastMatrix.ToEuler(lastEuler);
|
|
|
|
Move[3] = normalize180((currEuler.x - lastEuler.x)*45/atan(1.0));
|
|
Move[4] = normalize180((currEuler.y - lastEuler.y)*45/atan(1.0));
|
|
Move[5] = normalize180((currEuler.z - lastEuler.z)*45/atan(1.0));
|
|
}
|
|
|
|
SimConnect_CameraSetRelative6DOF(hSimConnect, Move[0], Move[1], Move[2],
|
|
-Move[3], Move[5], Move[4]);
|
|
isNew = false;
|
|
break;
|
|
|
|
case SIMCONNECT_RECV_ID_EXCEPTION:
|
|
{
|
|
SIMCONNECT_RECV_EXCEPTION *except = (SIMCONNECT_RECV_EXCEPTION*)pData;
|
|
|
|
switch (except->dwException)
|
|
{
|
|
case SIMCONNECT_EXCEPTION_ERROR:
|
|
printf("\nCamera error");
|
|
break;
|
|
|
|
default:
|
|
printf("\nException");
|
|
break;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case SIMCONNECT_RECV_ID_QUIT:
|
|
{
|
|
quit = 1;
|
|
break;
|
|
}
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
static enum INPUT_ID {
|
|
INPUT0,
|
|
};
|
|
|
|
|
|
static bool connected = false;
|
|
|
|
void testCockpitCamera(bool close, bool newDelta, float xFactor, float yFactor, float zFactor, float rFactor, float tSmoothFactor, float rSmoothFactor)
|
|
{
|
|
HRESULT hr;
|
|
MATRIX smoothMatrix;
|
|
|
|
stXFactor = xFactor;
|
|
stYFactor = yFactor;
|
|
stZFactor = zFactor;
|
|
stRFactor = rFactor;
|
|
|
|
if (tSmoothFactor == 0)
|
|
stTSmoothFactor = 0.05;
|
|
else
|
|
stTSmoothFactor = tSmoothFactor;
|
|
|
|
if (rSmoothFactor >= 0)
|
|
{
|
|
quatMod = 0;
|
|
stRSmoothFactor = rSmoothFactor;
|
|
} else
|
|
{
|
|
quatMod = 1;
|
|
stRSmoothFactor = -rSmoothFactor;
|
|
}
|
|
|
|
if (newDelta)
|
|
isNew = true;
|
|
|
|
if (close)
|
|
{
|
|
SimConnect_Close(hSimConnect);
|
|
connected = false;
|
|
return;
|
|
}
|
|
|
|
|
|
if (!connected && SUCCEEDED(SimConnect_Open(&hSimConnect, "Cockpit Camera", NULL, 0, 0, 0)))
|
|
{
|
|
connected = true;
|
|
SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_COUCOU, "Frame");
|
|
|
|
hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_INIT);
|
|
hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_INIT);
|
|
hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP0, SIMCONNECT_GROUP_PRIORITY_HIGHEST);
|
|
hr = SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "VK_COMMA", EVENT_INIT);
|
|
// Map the keys , and . keys to the private events
|
|
//SimConnect_SubscribeToSystemEvent(hSimConnect, EVENT_COUCOU, "6Hz");
|
|
hr = SimConnect_SetInputGroupState(hSimConnect, INPUT0, SIMCONNECT_STATE_ON);
|
|
}
|
|
|
|
smoothMatrix = TheScene->ID_TABLE[0]->CurrentMatrix_U;
|
|
if (connected)
|
|
SimConnect_CallDispatch(hSimConnect, MyDispatchProcCC, &smoothMatrix);
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|