Advertisement
pycache

world2screen

Dec 6th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. bool ScreenTransform(const Vector &point, Vector &screen)
  2.     float w;
  3.     const VMatrix &worldToScreen = I::EngineClient->WorldToScreenMatrix();
  4.  
  5.     screen.x = worldToScreen[0][0] * point[0] + worldToScreen[0][1] * point[1] + worldToScreen[0][2] * point[2] + worldToScreen[0][3];
  6.     screen.y = worldToScreen[1][0] * point[0] + worldToScreen[1][1] * point[1] + worldToScreen[1][2] * point[2] + worldToScreen[1][3];
  7.     w = worldToScreen[3][0] * point[0] + worldToScreen[3][1] * point[1] + worldToScreen[3][2] * point[2] + worldToScreen[3][3];
  8.     screen.z = 0.0f;
  9.  
  10.     bool behind = false;
  11.  
  12.     if (w < 0.001f) {
  13.         behind = true;
  14.         screen.x *= 100000;
  15.         screen.y *= 100000;
  16.     }
  17.     else {
  18.         behind = false;
  19.         float invw = 1.0f / w;
  20.         screen.x *= invw;
  21.         screen.y *= invw;
  22.     }
  23.  
  24.     return behind;
  25. }
  26.  
  27. bool WorldToScreen(const Vector &origin, Vector &screen) {
  28.     if (!ScreenTransform(origin, screen)) {
  29.         int ScreenWidth, ScreenHeight;
  30.         I::EngineClient->GetScreenSize(ScreenWidth, ScreenHeight);
  31.         float x = ScreenWidth / 2;
  32.         float y = ScreenHeight / 2;
  33.         x += 0.5 * screen.x * ScreenWidth + 0.5;
  34.         y -= 0.5 * screen.y * ScreenHeight + 0.5;
  35.         screen.x = x;
  36.         screen.y = y;
  37.         return true;
  38.     }
  39.  
  40.     return false;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement