Advertisement
qdwdqw

world to screen

Jul 27th, 2021
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. bool WorldToScreen(const Vector3 pos,Vector3& screen, float matrix[16], int windowWidth,int windowHeight) {
  2.     Vector4 clipCoords;
  3.     clipCoords.x = matrix[0] * pos.x + matrix[4] * pos.y + matrix[8] * pos.z + matrix[12];
  4.     clipCoords.y = matrix[1] * pos.x + matrix[5] * pos.y + matrix[9] * pos.z + matrix[13];
  5.     clipCoords.z = matrix[2] * pos.x + matrix[6] * pos.y + matrix[10] * pos.z + matrix[14];
  6.     clipCoords.w = matrix[3] * pos.x + matrix[7] * pos.y + matrix[11] * pos.z + matrix[15];
  7.    
  8.     if (clipCoords.w < 1.f)
  9.     {
  10.         return false;
  11.     }
  12.     Vector3 NDC;
  13.    
  14.     NDC.x = clipCoords.x / clipCoords.w;
  15.     NDC.y = clipCoords.y / clipCoords.w;
  16.     NDC.z = clipCoords.z / clipCoords.w;
  17.  
  18.  
  19.  
  20.     screen.x = (windowWidth / 2 * NDC.x) + (NDC.x + windowWidth / 2);
  21.     screen.y = -(windowHeight / 2 * NDC.y) + (NDC.y + windowHeight / 2);
  22.  
  23.     return true;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement