orthoAPI.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. // TODO: Find a way to avoid the default constructor
  63. OrthoView() {}
  64. OrthoView(int id, const IVector2D roundedXAxis, const IVector2D roundedZAxis, int yPixelsPerTile, const FMatrix3x3 &normalToWorldSpace, Direction worldDirection)
  65. : id(id), worldDirection(worldDirection), normalToWorldSpace(normalToWorldSpace),
  66. pixelOffsetPerTileX(roundedXAxis), pixelOffsetPerTileZ(roundedZAxis), yPixelsPerTile(yPixelsPerTile) {
  67. // Pixel aligned 3D transformation matrix from tile (x, y, z) to screen (x, y, h)
  68. FMatrix3x3 tileToScreen = FMatrix3x3(
  69. FVector3D(roundedXAxis.x, roundedXAxis.y, 0),
  70. FVector3D(0, -this->yPixelsPerTile, 1.0f),
  71. FVector3D(roundedZAxis.x, roundedZAxis.y, 0)
  72. );
  73. // Back from deep screen pixels to world tile coordinates
  74. FMatrix3x3 screenToTile = inverse(tileToScreen);
  75. // TODO: Obsolete
  76. this->roundedScreenPixelsToWorldTiles = inverse(FMatrix2x2(FVector2D(roundedXAxis.x, roundedXAxis.y), FVector2D(roundedZAxis.x, roundedZAxis.y)));
  77. // Save the conversion from screen-space to world-space in tile units
  78. this->screenDepthToWorldSpace = screenToTile;
  79. this->worldSpaceToScreenDepth = tileToScreen;
  80. // Save the conversion from screen-space to light-space in tile units
  81. this->screenDepthToLightSpace = FMatrix3x3(
  82. this->normalToWorldSpace.transformTransposed(screenToTile.xAxis),
  83. this->normalToWorldSpace.transformTransposed(screenToTile.yAxis),
  84. this->normalToWorldSpace.transformTransposed(screenToTile.zAxis)
  85. );
  86. this->lightSpaceToScreenDepth = inverse(this->screenDepthToLightSpace);
  87. }
  88. public:
  89. IVector2D miniTileOffsetToScreenPixel(const IVector3D& miniTileOffset) const {
  90. IVector2D centeredPixelLocation = this->pixelOffsetPerTileX * miniTileOffset.x + this->pixelOffsetPerTileZ * miniTileOffset.z;
  91. centeredPixelLocation.y -= miniTileOffset.y * this->yPixelsPerTile;
  92. return centeredPixelLocation / ortho_miniUnitsPerTile;
  93. }
  94. // Position is expressed in world space using mini units
  95. IVector2D miniTilePositionToScreenPixel(const IVector3D& position, const IVector2D& worldCenter) const {
  96. return this->miniTileOffsetToScreenPixel(position) + worldCenter;
  97. }
  98. // Returns the 3D mini-tile units moved along the ground for the pixel offset
  99. // Only rotation and scaling for pixel offsets
  100. FVector3D pixelToTileOffset(const IVector2D& pixelOffset) const {
  101. FVector2D xzTiles = this->roundedScreenPixelsToWorldTiles.transform(FVector2D(pixelOffset.x, pixelOffset.y));
  102. return FVector3D(xzTiles.x, 0.0f, xzTiles.y);
  103. }
  104. IVector3D pixelToMiniOffset(const IVector2D& pixelOffset) const {
  105. FVector3D tiles = this->pixelToTileOffset(pixelOffset);
  106. return IVector3D(ortho_floatingTileToMini(tiles.x), 0, ortho_floatingTileToMini(tiles.z));
  107. }
  108. // Returns the 3D mini-tile location for a certain pixel on the screen intersecting with the ground
  109. // Full transform for pixel locations
  110. IVector3D pixelToMiniPosition(const IVector2D& pixelLocation, const IVector2D& worldCenter) const {
  111. return this->pixelToMiniOffset(pixelLocation - worldCenter);
  112. }
  113. };
  114. // How to use the orthogonal system
  115. // * Place tiles in whole tile integer units
  116. // Multiply directly with pixelOffsetPerTileX and pixelOffsetPerTileZ to get deterministic pixel offsets
  117. // * Define sprites in mini units (1 tile = ortho_miniUnitsPerTile mini units)
  118. // First multiply mini units with yPixelsPerTile, pixelOffsetPerTileX and pixelOffsetPerTileZ for each 3D coordinate
  119. // Then divide by ortho_miniUnitsPerTile, which most processors should have custom instructions for handling quickly
  120. // With enough bits in the integers, the result should be steady and not shake around randomly
  121. struct OrthoSystem {
  122. public:
  123. static constexpr int maxCameraAngles = 8;
  124. static constexpr float diag = 0.707106781f; // cos(45 degrees) = Sqrt(2) / 2
  125. // Persistent settings
  126. float cameraTilt; // Camera coefficient. (-inf is straight down, -1 is diagonal down, 0 is horizontal)
  127. int pixelsPerTile; // The sideway length of a tile in pixels when seen from straight ahead.
  128. // Generated views
  129. OrthoView view[maxCameraAngles];
  130. private:
  131. // Update generated settings from persistent settings
  132. // Enforces a valid orthogonal camera system
  133. void update() {
  134. // Calculate y offset rounded to whole tiles to prevent random gaps in grids
  135. int yPixelsPerTile = (float)this->pixelsPerTile / sqrt(this->cameraTilt * this->cameraTilt + 1);
  136. // Define sprite directions
  137. FVector3D upAxis = FVector3D(0.0f, 1.0f, 0.0f);
  138. Direction worldDirections[8] = {dir315, dir45, dir135, dir225, dir0, dir90, dir180, dir270};
  139. // Define approximate camera systems just to get something axis aligned
  140. FMatrix3x3 cameraSystems[8];
  141. cameraSystems[0] = FMatrix3x3::makeAxisSystem(FVector3D(diag, this->cameraTilt, diag), upAxis);
  142. cameraSystems[1] = FMatrix3x3::makeAxisSystem(FVector3D(-diag, this->cameraTilt, diag), upAxis);
  143. cameraSystems[2] = FMatrix3x3::makeAxisSystem(FVector3D(-diag, this->cameraTilt, -diag), upAxis);
  144. cameraSystems[3] = FMatrix3x3::makeAxisSystem(FVector3D(diag, this->cameraTilt, -diag), upAxis);
  145. cameraSystems[4] = FMatrix3x3::makeAxisSystem(FVector3D( 0, this->cameraTilt, 1), upAxis);
  146. cameraSystems[5] = FMatrix3x3::makeAxisSystem(FVector3D(-1, this->cameraTilt, 0), upAxis);
  147. cameraSystems[6] = FMatrix3x3::makeAxisSystem(FVector3D( 0, this->cameraTilt,-1), upAxis);
  148. cameraSystems[7] = FMatrix3x3::makeAxisSystem(FVector3D( 1, this->cameraTilt, 0), upAxis);
  149. for (int a = 0; a < maxCameraAngles; a++) {
  150. // Define the coordinate system for normals
  151. FVector3D normalSystemDirection = cameraSystems[a].zAxis;
  152. normalSystemDirection.y = 0.0f;
  153. FMatrix3x3 normalToWorldSpace = FMatrix3x3::makeAxisSystem(normalSystemDirection, FVector3D(0.0f, 1.0f, 0.0f));
  154. // Create an axis system truncated inwards to whole pixels to prevent creating empty seams between tile aligned sprites
  155. Camera approximateCamera = Camera::createOrthogonal(Transform3D(FVector3D(), cameraSystems[a]), this->pixelsPerTile, this->pixelsPerTile, 0.5f);
  156. float halfTile = (float)this->pixelsPerTile * 0.5f;
  157. FVector2D XAxis = approximateCamera.worldToScreen(FVector3D(1.0f, 0.0f, 0.0f)).is - halfTile;
  158. FVector2D ZAxis = approximateCamera.worldToScreen(FVector3D(0.0f, 0.0f, 1.0f)).is - halfTile;
  159. this->view[a] = OrthoView(
  160. a,
  161. IVector2D((int)XAxis.x, (int)XAxis.y),
  162. IVector2D((int)ZAxis.x, (int)ZAxis.y),
  163. yPixelsPerTile,
  164. normalToWorldSpace,
  165. worldDirections[a]
  166. );
  167. }
  168. }
  169. public:
  170. OrthoSystem() : cameraTilt(0), pixelsPerTile(0) {}
  171. OrthoSystem(float cameraTilt, int pixelsPerTile) : cameraTilt(cameraTilt), pixelsPerTile(pixelsPerTile) {
  172. this->update();
  173. }
  174. explicit OrthoSystem(const ReadableString& content) {
  175. config_parse_ini(content, [this](const ReadableString& block, const ReadableString& key, const ReadableString& value) {
  176. if (string_length(block) == 0) {
  177. if (string_caseInsensitiveMatch(key, U"DownTiltPerThousand")) {
  178. this->cameraTilt = (float)string_toInteger(value) * -0.001f;
  179. } else if (string_caseInsensitiveMatch(key, U"PixelsPerTile")) {
  180. this->pixelsPerTile = string_toInteger(value);
  181. } else {
  182. printText("Unrecognized key \"", key, "\" in orthogonal camera configuration file.\n");
  183. }
  184. } else {
  185. printText("Unrecognized block \"", block, "\" in orthogonal camera configuration file.\n");
  186. }
  187. });
  188. this->update();
  189. }
  190. public:
  191. IVector2D miniTileOffsetToScreenPixel(const IVector3D& miniTileOffset, int cameraIndex) const {
  192. return this->view[cameraIndex].miniTileOffsetToScreenPixel(miniTileOffset);
  193. }
  194. // Position is expressed in world space using mini units
  195. IVector2D miniTilePositionToScreenPixel(const IVector3D& position, int cameraIndex, const IVector2D& worldCenter) const {
  196. return this->view[cameraIndex].miniTilePositionToScreenPixel(position, worldCenter);
  197. }
  198. public:
  199. // Returns the 3D mini-tile units moved along the ground for the pixel offset
  200. // Only rotation and scaling for pixel offsets
  201. FVector3D pixelToTileOffset(const IVector2D& pixelOffset, int cameraIndex) const {
  202. return this->view[cameraIndex].pixelToTileOffset(pixelOffset);
  203. }
  204. IVector3D pixelToMiniOffset(const IVector2D& pixelOffset, int cameraIndex) const {
  205. return this->view[cameraIndex].pixelToMiniOffset(pixelOffset);
  206. }
  207. // Returns the 3D mini-tile location for a certain pixel on the screen intersecting with the ground
  208. // Full transform for pixel locations
  209. IVector3D pixelToMiniPosition(const IVector2D& pixelLocation, int cameraIndex, const IVector2D& worldCenter) const {
  210. return this->view[cameraIndex].pixelToMiniPosition(pixelLocation, worldCenter);
  211. }
  212. };
  213. }
  214. #endif