Camera.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 to 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_RENDER_CAMERA
  24. #define DFPSR_RENDER_CAMERA
  25. #include <cstdint>
  26. #include <cassert>
  27. #include "../../math/FVector.h"
  28. #include "../../math/LVector.h"
  29. #include "../../math/FPlane3D.h"
  30. #include "../../math/Transform3D.h"
  31. #include "../math/scalar.h"
  32. #include "constants.h"
  33. #include "ProjectedPoint.h"
  34. #include <limits>
  35. namespace dsr {
  36. // A special rounding used for vertex projection
  37. inline int64_t safeRoundInt64(float value) {
  38. int64_t result = (int64_t)value;
  39. if (value <= -1048576.0f || value >= 1048576.0f) { result = 0; }
  40. return result;
  41. }
  42. class ViewFrustum {
  43. private:
  44. FPlane3D planes[6];
  45. int planeCount;
  46. public:
  47. ViewFrustum() : planeCount(0) {}
  48. // Orthogonal view frustum in camera space
  49. ViewFrustum(float halfWidth, float halfHeight)
  50. : planeCount(4) {
  51. // Sides
  52. planes[0] = FPlane3D(FVector3D(1.0f, 0.0f, 0.0f), halfWidth);
  53. planes[1] = FPlane3D(FVector3D(-1.0f, 0.0f, 0.0f), halfWidth);
  54. planes[2] = FPlane3D(FVector3D(0.0f, 1.0f, 0.0f), halfHeight);
  55. planes[3] = FPlane3D(FVector3D(0.0f, -1.0f, 0.0f), halfHeight);
  56. }
  57. // Perspective view frustum in camera space
  58. ViewFrustum(float nearClip, float farClip, float widthSlope, float heightSlope)
  59. : planeCount(farClip == std::numeric_limits<float>::infinity() ? 5 : 6) { // Skip the far clip plane if its distance is infinite.
  60. // Sides
  61. planes[0] = FPlane3D(FVector3D(1.0f, 0.0f, -widthSlope), 0.0f);
  62. planes[1] = FPlane3D(FVector3D(-1.0f, 0.0f, -widthSlope), 0.0f);
  63. planes[2] = FPlane3D(FVector3D(0.0f, 1.0f, -heightSlope), 0.0f);
  64. planes[3] = FPlane3D(FVector3D(0.0f, -1.0f, -heightSlope), 0.0f);
  65. // Near and far clip planes
  66. planes[4] = FPlane3D(FVector3D(0.0f, 0.0f, -1.0f), -nearClip);
  67. planes[5] = FPlane3D(FVector3D(0.0f, 0.0f, 1.0f), farClip);
  68. }
  69. int getPlaneCount() const {
  70. return this->planeCount;
  71. }
  72. FPlane3D getPlane(int sideIndex) const {
  73. assert(sideIndex >= 0 && sideIndex < this->planeCount);
  74. return planes[sideIndex];
  75. }
  76. };
  77. // How much is the image region magnified for skipping entire triangles.
  78. // A small margin is needed to prevent missing pixels from rounding errors along the borders in high image resolutions.
  79. static const float cullRatio = 1.0001f;
  80. // How much is the image region magnified for clipping triangles.
  81. // The larger you make the clip region, the less triangles you have to apply clipping to.
  82. // The triangle rasterization can handle clipping triangles in integer coordinates,
  83. // but there are limits to how large those integers can become before overflowing.
  84. static const float clipRatio = 2.0f;
  85. // To prevent division by zero, a near clipping distance is slightly above zero to
  86. // clip triangles in 3D camera space before projecting the coordinates to the target image.
  87. static const float defaultNearClip = 0.01f;
  88. static const float defaultFarClip = 1000.0f;
  89. // Just create a new camera on stack memory every time you need to render something
  90. class Camera {
  91. public: // Do not modify individual settings without assigning whole new cameras
  92. bool perspective; // When off, widthSlope and heightSlope will be used as halfWidth and halfHeight.
  93. Transform3D location; // Only translation and rotation allowed. Scaling and tilting will obviously not work for cameras.
  94. float widthSlope, heightSlope, invWidthSlope, invHeightSlope, imageWidth, imageHeight, nearClip, farClip;
  95. ViewFrustum cullFrustum, clipFrustum;
  96. Camera() :
  97. perspective(true), location(Transform3D()), widthSlope(0.0f), heightSlope(0.0f),
  98. invWidthSlope(0.0f), invHeightSlope(0.0f), imageWidth(0), imageHeight(0),
  99. nearClip(0.0f), farClip(0.0f), cullFrustum(ViewFrustum()), clipFrustum(ViewFrustum()) {}
  100. Camera(bool perspective, const Transform3D &location, float imageWidth, float imageHeight, float widthSlope, float heightSlope, float nearClip, float farClip, const ViewFrustum &cullFrustum, const ViewFrustum &clipFrustum) :
  101. perspective(perspective), location(location), widthSlope(widthSlope), heightSlope(heightSlope),
  102. invWidthSlope(0.5f / widthSlope), invHeightSlope(0.5f / heightSlope), imageWidth(imageWidth), imageHeight(imageHeight),
  103. nearClip(nearClip), farClip(farClip), cullFrustum(cullFrustum), clipFrustum(clipFrustum) {}
  104. public:
  105. static Camera createPerspective(const Transform3D &location, float imageWidth, float imageHeight, float widthSlope = 1.0f, float nearClip = defaultNearClip, float farClip = defaultFarClip) {
  106. float heightSlope = widthSlope * imageHeight / imageWidth;
  107. return Camera(true, location, imageWidth, imageHeight, widthSlope, heightSlope, nearClip, farClip,
  108. ViewFrustum(nearClip, farClip, widthSlope * cullRatio, heightSlope * cullRatio),
  109. ViewFrustum(nearClip, farClip, widthSlope * clipRatio, heightSlope * clipRatio));
  110. }
  111. // Orthogonal cameras doesn't have any near or far clip planes
  112. static Camera createOrthogonal(const Transform3D &location, float imageWidth, float imageHeight, float halfWidth) {
  113. float halfHeight = halfWidth * imageHeight / imageWidth;
  114. return Camera(false, location, imageWidth, imageHeight, halfWidth, halfHeight, -std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),
  115. ViewFrustum(halfWidth * cullRatio, halfHeight * cullRatio),
  116. ViewFrustum(halfWidth * clipRatio, halfHeight * clipRatio));
  117. }
  118. FVector3D worldToCamera(const FVector3D &worldSpace) const {
  119. return this->location.transformPointTransposedInverse(worldSpace);
  120. }
  121. ProjectedPoint cameraToScreen(const FVector3D &cameraSpace) const {
  122. // Camera to image space
  123. if (this->perspective) {
  124. float invDepth;
  125. if (cameraSpace.z > 0.0f) {
  126. invDepth = 1.0f / cameraSpace.z;
  127. } else {
  128. invDepth = 0.0f;
  129. }
  130. float centerShear = cameraSpace.z * 0.5f;
  131. FVector2D preProjection = FVector2D(
  132. ( cameraSpace.x * this->invWidthSlope + centerShear) * this->imageWidth,
  133. (-cameraSpace.y * this->invHeightSlope + centerShear) * this->imageHeight
  134. );
  135. FVector2D projectedFloat = preProjection * invDepth;
  136. FVector2D subPixel = projectedFloat * constants::unitsPerPixel;
  137. LVector2D rounded = LVector2D(safeRoundInt64(subPixel.x), safeRoundInt64(subPixel.y));
  138. return ProjectedPoint(cameraSpace, projectedFloat, rounded);
  139. } else {
  140. FVector2D projectedFloat = FVector2D(
  141. ( cameraSpace.x * this->invWidthSlope + 0.5f) * this->imageWidth,
  142. (-cameraSpace.y * this->invHeightSlope + 0.5f) * this->imageHeight
  143. );
  144. FVector2D subPixel = projectedFloat * constants::unitsPerPixel;
  145. LVector2D rounded = LVector2D(safeRoundInt64(subPixel.x), safeRoundInt64(subPixel.y));
  146. return ProjectedPoint(cameraSpace, projectedFloat, rounded);
  147. }
  148. }
  149. ProjectedPoint worldToScreen(const FVector3D &worldSpace) const {
  150. return this->cameraToScreen(this->worldToCamera(worldSpace));
  151. }
  152. int getFrustumPlaneCount(bool clipping) const {
  153. return clipping ? this->clipFrustum.getPlaneCount() : this->cullFrustum.getPlaneCount();
  154. }
  155. FPlane3D getFrustumPlane(int sideIndex, bool clipping) const {
  156. return clipping ? this->clipFrustum.getPlane(sideIndex) : this->cullFrustum.getPlane(sideIndex);
  157. }
  158. // Returns false iff all 6 points from the box of minBound and maxBound multiplied by transform are outside of the same plane of cullFrustum
  159. // This is a quick indication to if something within that bound would be rendered
  160. bool isBoxSeen(const FVector3D& minBound, const FVector3D& maxBound, const Transform3D &modelToWorld) const {
  161. FVector3D corner[8] = {
  162. FVector3D(minBound.x, minBound.y, minBound.z),
  163. FVector3D(maxBound.x, minBound.y, minBound.z),
  164. FVector3D(minBound.x, maxBound.y, minBound.z),
  165. FVector3D(maxBound.x, maxBound.y, minBound.z),
  166. FVector3D(minBound.x, minBound.y, maxBound.z),
  167. FVector3D(maxBound.x, minBound.y, maxBound.z),
  168. FVector3D(minBound.x, maxBound.y, maxBound.z),
  169. FVector3D(maxBound.x, maxBound.y, maxBound.z)
  170. };
  171. for (int c = 0; c < 8; c++) {
  172. corner[c] = worldToCamera(modelToWorld.transformPoint(corner[c]));
  173. }
  174. for (int s = 0; s < this->cullFrustum.getPlaneCount(); s++) {
  175. FPlane3D plane = this->cullFrustum.getPlane(s);
  176. if (!(plane.inside(corner[0])
  177. || plane.inside(corner[1])
  178. || plane.inside(corner[2])
  179. || plane.inside(corner[3])
  180. || plane.inside(corner[4])
  181. || plane.inside(corner[5])
  182. || plane.inside(corner[6])
  183. || plane.inside(corner[7]))) {
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. };
  190. }
  191. #endif