Advertisement
Aleks11

3D coords to 2D screen coords

Aug 13th, 2012
1,851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.20 KB | None | 0 0
  1.  
  2. stock Float:MatrixMultiply(Float:mat1[4][4], Float:mat2[4][4])
  3. {
  4.     new Float:matRet[4][4];
  5.     for (new i = 0; i < 4; i++)
  6.         for (new j = 0; j < 4; j++)
  7.             for (new k = 0; k < 4; k++)
  8.                 matRet[i][j] += mat1[i][k] * mat2[k][j];
  9.     return matRet;
  10. }
  11.  
  12. stock Float:MatrixMultiplyVector(Float:mat[4][4], Float:vec[4])
  13. {
  14.     new Float:vTmp[4];
  15.     for (new i = 0; i < 4; i++)
  16.         for (new k = 0; k < 4; k++)
  17.             vTmp[i] += vec[k] * mat[k][i];
  18.     return vTmp;
  19. }
  20.  
  21. stock calcPosInScreen(playerid, nScreenW, nScreenH, Float:fObjectPos[3])
  22. {
  23.     new str[256];
  24.    
  25.     new Float:matProj[4][4],
  26.         Float:matView[4][4],
  27.         Float:matWorld[4][4],
  28.         Float:matRet[4][4];
  29.        
  30.     for (new i = 0; i < 4; i++)
  31.         for (new j = 0; j < 4; j++)
  32.             matProj[i][j] = 0.0;
  33.            
  34.     for (new i = 0; i < 4; i++)
  35.         for (new j = 0; j < 4; j++)
  36.             matView[i][j] = 0.0;
  37.  
  38.     for (new i = 0; i < 4; i++)
  39.         for (new j = 0; j < 4; j++)
  40.             matWorld[i][j] = 0.0;
  41.            
  42.     for (new i = 0; i < 4; i++)
  43.         for (new j = 0; j < 4; j++)
  44.             matRet[i][j] = 0.0;
  45.            
  46.     new Float:fYScale = 1.0 / floattan(27.0, degrees),
  47.         Float:fZFar = 10.0,
  48.         Float:fZNear = 0.5;
  49.    
  50.     new bWideScreen = 0;
  51.    
  52.     matProj[0][0] = fYScale * nScreenH / nScreenW * 1.2;
  53.     matProj[1][1] = fYScale;
  54.    
  55.     if (bWideScreen)
  56.         matProj[1][1] *= 1.33;
  57.  
  58.     matProj[2][2] = fZFar / (fZFar - fZNear);
  59.     matProj[3][2] = -fZNear * fZFar / (fZFar - fZNear);
  60.     matProj[2][3] = 1;
  61.    
  62.    
  63.     new Float:fCamPos[3],
  64.         Float:fCamLookAt[3];
  65.     GetPlayerCameraPos(playerid, fCamPos[0], fCamPos[1], fCamPos[2]);
  66.     GetPlayerCameraFrontVector(playerid, fCamLookAt[0], fCamLookAt[1], fCamLookAt[2]);
  67.  
  68.     new Float:fYaw = atan2(fCamLookAt[0], fCamLookAt[1]),
  69.         Float:fPitch = acos(fCamLookAt[2]) + 270.0,
  70.         Float:fRoll = 0.0;
  71.  
  72.     if (fYaw < 0.0)
  73.         fYaw += 360.0;
  74.        
  75.     fYaw = 90.0;
  76.     fPitch = 0.0;
  77.    
  78.     fCamPos[0] = -0.1;
  79.     fCamPos[1] = 0.0;
  80.     fCamPos[2] = fHeight;
  81.  
  82.     new Float:fSinYaw = floatsin(fYaw, degrees),
  83.         Float:fCosYaw = floatcos(fYaw, degrees),
  84.         Float:fSinPitch = floatsin(fPitch, degrees),
  85.         Float:fCosPitch = floatcos(fPitch, degrees),
  86.         Float:fSinRoll = floatsin(fRoll, degrees),
  87.         Float:fCosRoll = floatcos(fRoll, degrees);
  88.        
  89.     matView[0][0] = fCosYaw * fCosRoll + fSinYaw * fSinPitch * fSinRoll;
  90.     matView[0][1] = -fCosYaw * fSinRoll + fSinYaw * fSinPitch * fCosRoll;
  91.     matView[0][2] = fSinYaw * fCosPitch;
  92.    
  93.     matView[1][0] = fSinRoll * fCosPitch;
  94.     matView[1][1] = fCosRoll * fCosPitch;
  95.     matView[1][2] = -fSinPitch;
  96.    
  97.     matView[2][0] = -fSinYaw * fCosRoll + fCosYaw * fSinPitch * fSinRoll;
  98.     matView[2][1] = fSinRoll * fSinYaw + fCosYaw * fSinPitch * fCosRoll;
  99.     matView[2][2] = fCosYaw * fCosPitch;
  100.    
  101.     matView[3][3] = 1.0;
  102.     matView[3][0] = -(fCamPos[0] * matView[0][0] + fCamPos[2] * matView[1][0] + fCamPos[1] * matView[2][0]);
  103.     matView[3][1] = -(fCamPos[0] * matView[0][1] + fCamPos[2] * matView[1][1] + fCamPos[1] * matView[2][1]);
  104.     matView[3][2] = -(fCamPos[0] * matView[0][2] + fCamPos[2] * matView[1][2] + fCamPos[1] * matView[2][2]);
  105.    
  106.  
  107.     matWorld[0][0] = 1.0;
  108.     matWorld[1][1] = 1.0;
  109.     matWorld[2][2] = 1.0;
  110.     matWorld[3][3] = 1.0;
  111.     //matWorld[3][0] = fObjectPos[0];
  112.     //matWorld[3][1] = fObjectPos[2];
  113.     //matWorld[3][2] = fObjectPos[1];
  114.  
  115.     matRet = MatrixMultiply(matView, matProj);
  116.    
  117.     matRet = MatrixMultiply(matWorld, matRet);
  118.    
  119.     new Float:fNewVec[4];
  120.     fNewVec[0] = fObjectPos[0];
  121.     fNewVec[1] = fObjectPos[2];
  122.     fNewVec[2] = fObjectPos[1];
  123.     fNewVec[3] = 1.0;
  124.     fNewVec = MatrixMultiplyVector(matRet, fNewVec);
  125.    
  126.     fNewVec[0] /= fNewVec[3];
  127.     fNewVec[1] /= fNewVec[3];
  128.     fNewVec[2] /= fNewVec[3];
  129.  
  130.     if (fNewVec[0] < -1.0 || fNewVec[0] > 1.0 || fNewVec[1] < -1.0 || fNewVec[1] > 1.0 || fNewVec[2] < fZNear || fNewVec[2] > fZFar)
  131.         format(str, 256, "Object out of screen");
  132.     else
  133.     {
  134.         fNewVec[0] = (fNewVec[0] + 1) * nScreenW / 2;
  135.         fNewVec[1] = nScreenH / 2 - fNewVec[1] * nScreenH / 2;
  136.         format(str, 256, "%0.5f %0.5f %0.5f", fNewVec[0], fNewVec[1], fNewVec[2]);
  137.     }
  138.     SendClientMessage(playerid, -1, str);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement