orthoAPI.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. 
  2. #ifndef DFPSR_ORTHO
  3. #define DFPSR_ORTHO
  4. #include <assert.h>
  5. #include "../../DFPSR/includeFramework.h"
  6. namespace dsr {
  7. // TODO: Give an ortho_ prefix
  8. using Direction = int32_t;
  9. static const Direction dir360 = 8;
  10. static const Direction dir315 = 7;
  11. static const Direction dir270 = 6;
  12. static const Direction dir225 = 5;
  13. static const Direction dir180 = 4;
  14. static const Direction dir135 = 3;
  15. static const Direction dir90 = 2;
  16. static const Direction dir45 = 1;
  17. static const Direction dir0 = 0;
  18. inline int correctDirection(Direction direction) {
  19. return (int32_t)((uint32_t)((int32_t)direction + (dir360 * 1024)) % dir360);
  20. }
  21. // World 3D units
  22. // Tile = Diameter from one side to another along a standard tile
  23. // Used for expressing exact tile indices in games so that information can be stored efficiently
  24. // Mini-Tile = Tile / miniUnitsPerTile
  25. // Used to express locations in 3D without relying too much on non-deterministic floats
  26. static constexpr int ortho_miniUnitsPerTile = 1024;
  27. static constexpr float ortho_tilesPerMiniUnit = 1.0f / (float)ortho_miniUnitsPerTile;
  28. int ortho_roundToTile(int miniCoordinate);
  29. IVector3D ortho_roundToTile(const IVector3D& miniPosition);
  30. float ortho_miniToFloatingTile(int miniCoordinate);
  31. FVector3D ortho_miniToFloatingTile(const IVector3D& miniPosition);
  32. int ortho_floatingTileToMini(float tileCoordinate);
  33. IVector3D ortho_floatingTileToMini(const FVector3D& tilePosition);
  34. // TODO: Make sure that every conversion is derived from a single pixel-rounded world-to-screen transform
  35. // Do this by letting it be the only argument for construction using integers
  36. // Everything else will simply be derived from it on construction
  37. struct OrthoView {
  38. public:
  39. // Unique integer for identifying the view
  40. int id = -1;
  41. // Direction for rotating sprites
  42. Direction worldDirection = dir0; // How are sprites in the world rotated relative to the camera's point of view
  43. // The rotating transform from normal-space to world-space.
  44. // Light-space is a superset of normal-space with the origin around the camera. (Almost like camera-space but with Y straight up)
  45. FMatrix3x3 normalToWorldSpace;
  46. // Pixel aligned space (To ensure that moving one tile has the same number of pixels each time)
  47. IVector2D pixelOffsetPerTileX; // How many pixels does a sprite move per tile in X.
  48. IVector2D pixelOffsetPerTileZ; // How many pixels does a sprite move per tile in Z.
  49. int yPixelsPerTile = 0;
  50. // How pixels in the depth buffer maps to world-space coordinates in whole floating tiles.
  51. FMatrix3x3 screenDepthToWorldSpace;
  52. FMatrix3x3 worldSpaceToScreenDepth;
  53. // How pixels in the depth buffer maps to light-space coordinates in whole floating tiles.
  54. // The origin is at the center of the image.
  55. // The X and Y axis gives tile offsets in light space along the screen without depth information.
  56. // The Z axis gives tile offset per mini-tile unit of height in the depth buffer.
  57. FMatrix3x3 screenDepthToLightSpace;
  58. FMatrix3x3 lightSpaceToScreenDepth;
  59. // Conversion systems between rounded pixels and XZ tiles along Y = 0
  60. FMatrix2x2 roundedScreenPixelsToWorldTiles; // TODO: Replace with a screenToTile sub-set
  61. public:
  62. OrthoView() {}
  63. OrthoView(int id, const IVector2D roundedXAxis, const IVector2D roundedZAxis, int yPixelsPerTile, const FMatrix3x3 &normalToWorldSpace, Direction worldDirection);
  64. public:
  65. IVector2D miniTileOffsetToScreenPixel(const IVector3D& miniTileOffset) const;
  66. // Position is expressed in world space using mini units
  67. IVector2D miniTilePositionToScreenPixel(const IVector3D& position, const IVector2D& worldCenter) const;
  68. // Returns the 3D mini-tile units moved along the ground for the pixel offset
  69. // Only rotation and scaling for pixel offsets
  70. FVector3D pixelToTileOffset(const IVector2D& pixelOffset) const;
  71. IVector3D pixelToMiniOffset(const IVector2D& pixelOffset) const;
  72. // Returns the 3D mini-tile location for a certain pixel on the screen intersecting with the ground
  73. // Full transform for pixel locations
  74. IVector3D pixelToMiniPosition(const IVector2D& pixelLocation, const IVector2D& worldCenter) const;
  75. };
  76. // How to use the orthogonal system
  77. // * Place tiles in whole tile integer units
  78. // Multiply directly with pixelOffsetPerTileX and pixelOffsetPerTileZ to get deterministic pixel offsets
  79. // * Define sprites in mini units (1 tile = ortho_miniUnitsPerTile mini units)
  80. // First multiply mini units with yPixelsPerTile, pixelOffsetPerTileX and pixelOffsetPerTileZ for each 3D coordinate
  81. // Then divide by ortho_miniUnitsPerTile, which most processors should have custom instructions for handling quickly
  82. // With enough bits in the integers, the result should be steady and not shake around randomly
  83. struct OrthoSystem {
  84. public:
  85. static constexpr int maxCameraAngles = 8;
  86. static constexpr float diag = 0.707106781f; // cos(45 degrees) = Sqrt(2) / 2
  87. // Persistent settings
  88. float cameraTilt; // Camera coefficient. (-inf is straight down, -1 is diagonal down, 0 is horizontal)
  89. int pixelsPerTile; // The sideway length of a tile in pixels when seen from straight ahead.
  90. // Generated views
  91. OrthoView view[maxCameraAngles];
  92. private:
  93. // Update generated settings from persistent settings
  94. // Enforces a valid orthogonal camera system
  95. void update();
  96. public:
  97. OrthoSystem();
  98. OrthoSystem(float cameraTilt, int pixelsPerTile);
  99. explicit OrthoSystem(const ReadableString& content);
  100. public:
  101. IVector2D miniTileOffsetToScreenPixel(const IVector3D& miniTileOffset, int cameraIndex) const {
  102. return this->view[cameraIndex].miniTileOffsetToScreenPixel(miniTileOffset);
  103. }
  104. // Position is expressed in world space using mini units
  105. IVector2D miniTilePositionToScreenPixel(const IVector3D& position, int cameraIndex, const IVector2D& worldCenter) const {
  106. return this->view[cameraIndex].miniTilePositionToScreenPixel(position, worldCenter);
  107. }
  108. public:
  109. // Returns the 3D mini-tile units moved along the ground for the pixel offset
  110. // Only rotation and scaling for pixel offsets
  111. FVector3D pixelToTileOffset(const IVector2D& pixelOffset, int cameraIndex) const {
  112. return this->view[cameraIndex].pixelToTileOffset(pixelOffset);
  113. }
  114. IVector3D pixelToMiniOffset(const IVector2D& pixelOffset, int cameraIndex) const {
  115. return this->view[cameraIndex].pixelToMiniOffset(pixelOffset);
  116. }
  117. // Returns the 3D mini-tile location for a certain pixel on the screen intersecting with the ground
  118. // Full transform for pixel locations
  119. IVector3D pixelToMiniPosition(const IVector2D& pixelLocation, int cameraIndex, const IVector2D& worldCenter) const {
  120. return this->view[cameraIndex].pixelToMiniPosition(pixelLocation, worldCenter);
  121. }
  122. };
  123. }
  124. #endif