#include #include #include #include "XPLMDisplay.h" #include "XPLMGraphics.h" #include "XPLMCamera.h" #include "XPLMPlanes.h" #include "XPLMUtilities.h" #include "XPLMDataAccess.h" #include "XPLMProcessing.h" #include const double kFtToMeters = 0.3048; /* This is 3 miles expressed in meters, 5280 is 1 mile expressed in feet. */ /* Reduce this to * 0.25 to see the aircraft drawing reduced to lights only */ const double kFullPlaneDist = 5280.0 * kFtToMeters * 3.0; char *pAircraft[9]; static inline float sqr(float a) { return a * a; } static inline float CalcDist3D(float x1, float y1, float z1, float x2, float y2, float z2) { return sqrt(sqr(x2-x1) + sqr(y2-y1) + sqr(z2-z1)); } int AircraftDrawCallback( XPLMDrawingPhase inPhase, int inIsBefore, void * inRefcon); PLUGIN_API int XPluginStart( char * outName, char * outSig, char * outDesc) { strcpy(outName, "DrawOneAircraft"); strcpy(outSig, "xplanesdk.sandyb.drawoneaircraft"); strcpy(outDesc, "A plugin that draws one aircraft."); /* Next register the drawing callback. We want to be drawn * after X-Plane draws its 3-d objects. */ XPLMRegisterDrawCallback( AircraftDrawCallback, xplm_Phase_Airplanes, /* Draw when sim is doing objects */ 0, /* After objects */ NULL); /* No refcon needed */ char AircraftPath[256]; long index = 1; // strcpy(AircraftPath, "D:\\X-System 7\\Aircraft\\Heavy Metal\\B747-400 United\\United-Air.acf"); strcpy(AircraftPath, "D:\\X-System 7\\Aircraft\\General Aviation\\King-Air B200\\King Air B200.acf"); /* Only interested in the first aircraft */ pAircraft[0] = (char *)AircraftPath; /* So make sure the rest zeroed */ for (index = 1; index < 8; index++) pAircraft[index] = 0; if (XPLMAcquirePlanes((char **)&pAircraft, NULL, NULL)) { /* If we have acquired then we can set our aircraft type */ index = 1; XPLMSetAircraftModel(index, AircraftPath); } return 1; } PLUGIN_API void XPluginStop(void) { } PLUGIN_API void XPluginDisable(void) { } PLUGIN_API int XPluginEnable(void) { return 1; } PLUGIN_API void XPluginReceiveMessage( XPLMPluginID inFromWho, long inMessage, void * inParam) { } /* * AircraftDrawCallback * * This is the actual drawing callback that does the work; for us it will * be called after X-Plane has drawn its 3-d objects. The coordinate system * is 'normal' for 3-d drawing, meaning 0,0,0 is at the earth's surface at the * lat/lon reference point, with +Y = up, +Z = South, +X = East. (Note that * these relationships are only true at 0,0,0 due to the Earth's curvature!!) * * Drawing hooks that draw before X-Plane return 1 to let X-Plane draw or 0 * to inhibit drawing. For drawing hooks that run after X-Plane, this return * value is ignored but we will return 1 anyway. * */ int AircraftDrawCallback( XPLMDrawingPhase inPhase, int inIsBefore, void * inRefcon) { int planeCount; double x,y,z; double distMeters, Lat = 34.091, Lon = -117.25, Alt = 1162; float Heading = 65, Pitch = 0, Roll = 0; int drawFullPlane; int Index = 1; DWORD dwResult; XPLMCountAircraft(&planeCount, 0, 0); /* No aircraft set in Xplane so no point in going on */ if (planeCount == 0) return 0; XPLMCameraPosition_t cameraPos; /* Get camera position, need this in case a zoom is applied */ XPLMReadCameraPosition(&cameraPos); /* Need the x, y, z coords for these */ XPLMWorldToLocal(Lat, Lon, Alt * kFtToMeters, &x, &y, &z); /* Need to work out the viewing distance, if too far away then don't draw full plane */ distMeters = CalcDist3D(x,y,z,cameraPos.x, cameraPos.y, cameraPos.z); if (cameraPos.zoom != 0.0) distMeters /= cameraPos.zoom; drawFullPlane = distMeters < kFullPlaneDist; /* Set up the default state of the aircraft */ XPLMPlaneDrawState_t DrawState; DrawState.structSize = sizeof(XPLMPlaneDrawState_t); DrawState.gearPosition = 1; DrawState.flapRatio = 0; DrawState.spoilerRatio = 0; DrawState.speedBrakeRatio = 0; DrawState.slatRatio = 0; DrawState.wingSweep = 0; DrawState.thrust = 0; DrawState.yolkPitch = 0; DrawState.yolkHeading = 0; DrawState.yolkRoll = 0; /* Need to get our orientation correct before drawing aircraft */ glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(x, y, z); glRotatef(Heading, 0.0, -1.0, 0.0); glRotatef(Roll, 0.0, 0.0, -1.0); glRotatef(Pitch, 1.0, 0.0, 0.0); XPLMDrawAircraft(Index, (float) x, (float) y, (float) z, Pitch, Roll, Heading, drawFullPlane ? 1 : 0, &DrawState); glPopMatrix(); return 1; }