Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "tier2/camerautils.h"
  7. #include "tier0/dbg.h"
  8. #include "mathlib/vector.h"
  9. #include "mathlib/vmatrix.h"
  10. #include "tier2/tier2.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. //-----------------------------------------------------------------------------
  14. // accessors for generated matrices
  15. //-----------------------------------------------------------------------------
  16. void ComputeViewMatrix( matrix3x4_t *pWorldToCamera, const Camera_t &camera )
  17. {
  18. matrix3x4_t transform;
  19. AngleMatrix( camera.m_angles, camera.m_origin, transform );
  20. VMatrix matRotate( transform );
  21. VMatrix matRotateZ;
  22. MatrixBuildRotationAboutAxis( matRotateZ, Vector(0,0,1), -90 );
  23. MatrixMultiply( matRotate, matRotateZ, matRotate );
  24. VMatrix matRotateX;
  25. MatrixBuildRotationAboutAxis( matRotateX, Vector(1,0,0), 90 );
  26. MatrixMultiply( matRotate, matRotateX, matRotate );
  27. transform = matRotate.As3x4();
  28. MatrixInvert( transform, *pWorldToCamera );
  29. }
  30. void ComputeViewMatrix( VMatrix *pWorldToCamera, const Camera_t &camera )
  31. {
  32. matrix3x4_t transform, invTransform;
  33. AngleMatrix( camera.m_angles, camera.m_origin, transform );
  34. VMatrix matRotate( transform );
  35. VMatrix matRotateZ;
  36. MatrixBuildRotationAboutAxis( matRotateZ, Vector(0,0,1), -90 );
  37. MatrixMultiply( matRotate, matRotateZ, matRotate );
  38. VMatrix matRotateX;
  39. MatrixBuildRotationAboutAxis( matRotateX, Vector(1,0,0), 90 );
  40. MatrixMultiply( matRotate, matRotateX, matRotate );
  41. transform = matRotate.As3x4();
  42. MatrixInvert( transform, invTransform );
  43. *pWorldToCamera = invTransform;
  44. }
  45. void ComputeProjectionMatrix( VMatrix *pCameraToProjection, const Camera_t &camera, int width, int height )
  46. {
  47. float flFOV = camera.m_flFOV;
  48. float flZNear = camera.m_flZNear;
  49. float flZFar = camera.m_flZFar;
  50. float flApsectRatio = (float)width / (float)height;
  51. // MatrixBuildPerspective( proj, flFOV, flFOV * flApsectRatio, flZNear, flZFar );
  52. #if 1
  53. float halfWidth = tan( flFOV * M_PI / 360.0 );
  54. float halfHeight = halfWidth / flApsectRatio;
  55. #else
  56. float halfHeight = tan( flFOV * M_PI / 360.0 );
  57. float halfWidth = flApsectRatio * halfHeight;
  58. #endif
  59. memset( pCameraToProjection, 0, sizeof( VMatrix ) );
  60. pCameraToProjection->m[0][0] = 1.0f / halfWidth;
  61. pCameraToProjection->m[1][1] = 1.0f / halfHeight;
  62. pCameraToProjection->m[2][2] = flZFar / ( flZNear - flZFar );
  63. pCameraToProjection->m[3][2] = -1.0f;
  64. pCameraToProjection->m[2][3] = flZNear * flZFar / ( flZNear - flZFar );
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Computes the screen space position given a screen size
  68. //-----------------------------------------------------------------------------
  69. void ComputeScreenSpacePosition( Vector2D *pScreenPosition, const Vector &vecWorldPosition,
  70. const Camera_t &camera, int width, int height )
  71. {
  72. VMatrix view, proj, viewproj;
  73. ComputeViewMatrix( &view, camera );
  74. ComputeProjectionMatrix( &proj, camera, width, height );
  75. MatrixMultiply( proj, view, viewproj );
  76. Vector vecScreenPos;
  77. Vector3DMultiplyPositionProjective( viewproj, vecWorldPosition, vecScreenPos );
  78. pScreenPosition->x = ( vecScreenPos.x + 1.0f ) * width / 2.0f;
  79. pScreenPosition->y = ( -vecScreenPos.y + 1.0f ) * height / 2.0f;
  80. }