Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1.  
  2. #define Pi 3.14159265358979323846264338327950288
  3.  
  4. bool WorldToScreen (vector ObjectPoint, vector CameraPoint, vector CameraAngle, vector AOV, vector Resolution, vector Screen)
  5. {
  6.     vector Location, AngleA, Angle, ViewAngle, FOV;
  7.  
  8.     Location.x = ObjectPoint.x - CameraPoint.x;
  9.     Location.y = ObjectPoint.y - CameraPoint.y;
  10.  
  11.     float Distance = sqrt((Location.x * Location.x) + (Location.y * Location.y));
  12.  
  13.     if (ObjectPoint.x > CameraPoint.x && ObjectPoint.y > CameraPoint.y)
  14.     {
  15.         Location.x = ObjectPoint.x - CameraPoint.x;
  16.         Location.y = ObjectPoint.y - CameraPoint.y;
  17.         AngleA.x = atan(Location.y / Location.x) * (Pi / 180);
  18.         Angle.x = AngleA.x - AngleB.x;
  19.         ViewAngle.x = cos(Angle.x) * Distance;
  20.         FOV.x = tan(AOV.x / 2) * ViewAngle.x;
  21.         Screen.x = (Resolution.x / 2) + (ViewAngle.x / FOV.x) * (Resolution.x / 2);
  22.     }
  23.     else if (ObjectPoint.x < CameraPoint.x && ObjectPoint.y > CameraPoint.y)
  24.     {
  25.         Location.x = CameraPoint.x - ObjectPoint.x;
  26.         Location.y = ObjectPoint.y - CameraPoint.y;
  27.         AngleA.x = 180 - (atan(Location.y / Location.x) * (Pi / 180));
  28.         Angle.x = AngleB.x - AngleA.x;
  29.         ViewAngle.x = cos(Angle.x) * Distance;
  30.         FOV.x = tan(AOV.x / 2) * ViewAngle.x;
  31.         Screen.x = (Resolution.x / 2) - (ViewAngle.x / FOV.x) * (Resolution.x / 2);
  32.     }
  33.     else if (ObjectPoint.x < CameraPoint.x && ObjectPoint.y < CameraPoint.y)
  34.     {
  35.         Location.x = CameraPoint.x - ObjectPoint.x;
  36.         Location.y = CameraPoint.y - ObjectPoint.y;
  37.         AngleA.x = 180 + (atan(Location.y / Location.x) * (Pi / 180);
  38.         Angle.x = AngleB.x - AngleA.x;
  39.         ViewAngle.x = cos(Angle.x) * Distance;
  40.         FOV.x = tan(AOV.x / 2) * ViewAngle.x;
  41.         Screen.x = (Resolution.x / 2) - (ViewAngle.x / FOV.x) * (Resolution.x / 2);
  42.     }
  43.     else if (ObjectPoint.x > CameraPoint.x && ObjectPoint.y < CameraPoint.y)
  44.     {
  45.         Location.x = ObjectPoint.x - CameraPoint.x;
  46.         Location.y = CameraPoint.y - ObjectPoint.y;
  47.         AngleA.x = 360 - (atan(Location.y / Location.x) * (Pi / 180);
  48.         Angle.x = AngleA.x - AngleB.x;
  49.         ViewAngle.x = cos(Angle.x) * Distance;
  50.         FOV.x = tan(AOV.x / 2) * ViewAngle.x;
  51.         Screen.x = (Resolution.x / 2) + (ViewAngle.x / FOV.x) * (Resolution.x / 2);
  52.     }
  53.  
  54.     Location.x = ObjectPoint.x - CameraPoint.x;
  55.     Location.y = ObjectPoint.y - CameraPoint.y;
  56.  
  57.     Distance = sqrt((Location.x * Location.x) + (Location.y * Location.y));
  58.  
  59.     if (ObjectPoint.z > CameraPoint.z)
  60.     {
  61.         Location.z = ObjectPoint.z - CameraPoint.z;
  62.         AngleA.y = atan(Location.z / Distance) * (Pi / 180);
  63.         Angle.y = AngleA.y - AngleB.y;
  64.         ViewAngle.y = sin(Angle.y) * Distance;
  65.         FOV.y = tan(AOV.y / 2) * ViewAngle.y;
  66.         Screen.y = (Resolution.y / 2) + (ViewAngle.y / FOV.y) * (Resolution.y / 2);
  67.     }
  68.     else if (ObjectPoint.z < CameraPoint.z)
  69.     {
  70.         Location.z = CameraPoint.z - ObjectPoint.z;
  71.         AngleA.y = atan(Location.z / Distance) * (Pi / 180);
  72.         Angle.y = AngleB.y - AngleA.y;
  73.         ViewAngle.y = sin(Angle.y) * Distance;
  74.         FOV.y = tan(AOV.y / 2) * ViewAngle.y;
  75.         Screen.y = (Resolution.y / 2) - (ViewAngle.y / FOV.y) * (Resolution.y / 2);
  76.     }
  77.     else if (ObjectPoint.z == CameraPoint.z)
  78.     {
  79.         AngleA.y = 0.0;
  80.         Screen.y = (Resolution.y / 2);
  81.     }
  82.  
  83.     return true;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement