Camera.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <stdint.h>
  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. class ViewFrustum {
  37. private:
  38. FPlane3D planes[6];
  39. int planeCount;
  40. public:
  41. ViewFrustum() : planeCount(0) {}
  42. // Orthogonal view frustum in camera space
  43. ViewFrustum(float halfWidth, float halfHeight)
  44. : planeCount(4) {
  45. // Sides
  46. planes[0] = FPlane3D(FVector3D(1.0f, 0.0f, 0.0f), halfWidth);
  47. planes[1] = FPlane3D(FVector3D(-1.0f, 0.0f, 0.0f), halfWidth);
  48. planes[2] = FPlane3D(FVector3D(0.0f, 1.0f, 0.0f), halfHeight);
  49. planes[3] = FPlane3D(FVector3D(0.0f, -1.0f, 0.0f), halfHeight);
  50. }
  51. // Perspective view frustum in camera space
  52. ViewFrustum(float nearClip, float farClip, float widthSlope, float heightSlope)
  53. : planeCount(farClip == std::numeric_limits<float>::infinity() ? 5 : 6) { // Skip the far clip plane if its distance is infinite.
  54. // Sides
  55. planes[0] = FPlane3D(FVector3D(1.0f, 0.0f, -widthSlope), 0.0f);
  56. planes[1] = FPlane3D(FVector3D(-1.0f, 0.0f, -widthSlope), 0.0f);
  57. planes[2] = FPlane3D(FVector3D(0.0f, 1.0f, -heightSlope), 0.0f);
  58. planes[3] = FPlane3D(FVector3D(0.0f, -1.0f, -heightSlope), 0.0f);
  59. // Near and far clip planes
  60. planes[4] = FPlane3D(FVector3D(0.0f, 0.0f, -1.0f), -nearClip);
  61. planes[5] = FPlane3D(FVector3D(0.0f, 0.0f, 1.0f), farClip);
  62. }
  63. int getPlaneCount() const {
  64. return this->planeCount;
  65. }
  66. FPlane3D getPlane(int sideIndex) const {
  67. assert(sideIndex >= 0 && sideIndex < this->planeCount);
  68. return planes[sideIndex];
  69. }
  70. };
  71. // How much is the image region magnified for skipping entire triangles.
  72. // A small margin is needed to prevent missing pixels from rounding errors along the borders in high image resolutions.
  73. static const float cullRatio = 1.0001f;
  74. // How much is the image region magnified for clipping triangles.
  75. // The larger you make the clip region, the less triangles you have to apply clipping to.
  76. // The triangle rasterization can handle clipping triangles in integer coordinates,
  77. // but there are limits to how large those integers can become before overflowing.
  78. static const float clipRatio = 2.0f;
  79. // To prevent division by zero, a near clipping distance is slightly above zero to
  80. // clip triangles in 3D camera space before projecting the coordinates to the target image.
  81. static const float defaultNearClip = 0.01f;
  82. static const float defaultFarClip = 1000.0f;
  83. // Just create a new camera on stack memory every time you need to render something
  84. class Camera {
  85. public: // Do not modify individual settings without assigning whole new cameras
  86. bool perspective; // When off, widthSlope and heightSlope will be used as halfWidth and halfHeight.
  87. Transform3D location; // Only translation and rotation allowed. Scaling and tilting will obviously not work for cameras.
  88. float widthSlope, heightSlope, invWidthSlope, invHeightSlope, imageWidth, imageHeight, nearClip, farClip;
  89. ViewFrustum cullFrustum, clipFrustum;
  90. Camera() :
  91. perspective(true), location(Transform3D()), widthSlope(0.0f), heightSlope(0.0f),
  92. invWidthSlope(0.0f), invHeightSlope(0.0f), imageWidth(0), imageHeight(0),
  93. nearClip(0.0f), farClip(0.0f), cullFrustum(ViewFrustum()), clipFrustum(ViewFrustum()) {}
  94. Camera(bool perspective, const Transform3D &location, float imageWidth, float imageHeight, float widthSlope, float heightSlope, float nearClip, float farClip, const ViewFrustum &cullFrustum, const ViewFrustum &clipFrustum) :
  95. perspective(perspective), location(location), widthSlope(widthSlope), heightSlope(heightSlope),
  96. invWidthSlope(0.5f / widthSlope), invHeightSlope(0.5f / heightSlope), imageWidth(imageWidth), imageHeight(imageHeight),
  97. nearClip(nearClip), farClip(farClip), cullFrustum(cullFrustum), clipFrustum(clipFrustum) {}
  98. public:
  99. static Camera createPerspective(const Transform3D &location, float imageWidth, float imageHeight, float widthSlope = 1.0f, float nearClip = defaultNearClip, float farClip = defaultFarClip) {
  100. float heightSlope = widthSlope * imageHeight / imageWidth;
  101. return Camera(true, location, imageWidth, imageHeight, widthSlope, heightSlope, nearClip, farClip,
  102. ViewFrustum(nearClip, farClip, widthSlope * cullRatio, heightSlope * cullRatio),
  103. ViewFrustum(nearClip, farClip, widthSlope * clipRatio, heightSlope * clipRatio));
  104. }
  105. // Orthogonal cameras doesn't have any near or far clip planes
  106. static Camera createOrthogonal(const Transform3D &location, float imageWidth, float imageHeight, float halfWidth) {
  107. float halfHeight = halfWidth * imageHeight / imageWidth;
  108. return Camera(false, location, imageWidth, imageHeight, halfWidth, halfHeight, -std::numeric_limits<float>::max(), std::numeric_limits<float>::max(),
  109. ViewFrustum(halfWidth * cullRatio, halfHeight * cullRatio),
  110. ViewFrustum(halfWidth * clipRatio, halfHeight * clipRatio));
  111. }
  112. FVector3D worldToCamera(const FVector3D &worldSpace) const {
  113. return this->location.transformPointTransposedInverse(worldSpace);
  114. }
  115. ProjectedPoint cameraToScreen(const FVector3D &cameraSpace) const {
  116. // Camera to image space
  117. if (this->perspective) {
  118. float invDepth;
  119. if (cameraSpace.z > 0.0f) {
  120. invDepth = 1.0f / cameraSpace.z;
  121. } else {
  122. invDepth = 0.0f;
  123. }
  124. float centerShear = cameraSpace.z * 0.5f;
  125. FVector2D preProjection = FVector2D(
  126. ( cameraSpace.x * this->invWidthSlope + centerShear) * this->imageWidth,
  127. (-cameraSpace.y * this->invHeightSlope + centerShear) * this->imageHeight
  128. );
  129. FVector2D projectedFloat = preProjection * invDepth;
  130. FVector2D subPixel = projectedFloat * constants::unitsPerPixel;
  131. LVector2D rounded = LVector2D(safeRoundInt64(subPixel.x), safeRoundInt64(subPixel.y));
  132. return ProjectedPoint(cameraSpace, projectedFloat, rounded);
  133. } else {
  134. FVector2D projectedFloat = FVector2D(
  135. ( cameraSpace.x * this->invWidthSlope + 0.5f) * this->imageWidth,
  136. (-cameraSpace.y * this->invHeightSlope + 0.5f) * this->imageHeight
  137. );
  138. FVector2D subPixel = projectedFloat * constants::unitsPerPixel;
  139. LVector2D rounded = LVector2D(safeRoundInt64(subPixel.x), safeRoundInt64(subPixel.y));
  140. return ProjectedPoint(cameraSpace, projectedFloat, rounded);
  141. }
  142. }
  143. ProjectedPoint worldToScreen(const FVector3D &worldSpace) const {
  144. return this->cameraToScreen(this->worldToCamera(worldSpace));
  145. }
  146. int getFrustumPlaneCount(bool clipping) const {
  147. return clipping ? this->clipFrustum.getPlaneCount() : this->cullFrustum.getPlaneCount();
  148. }
  149. FPlane3D getFrustumPlane(int sideIndex, bool clipping) const {
  150. return clipping ? this->clipFrustum.getPlane(sideIndex) : this->cullFrustum.getPlane(sideIndex);
  151. }
  152. // Returns false iff all 6 points from the box of minBound and maxBound multiplied by transform are outside of the same plane of cullFrustum
  153. // This is a quick indication to if something within that bound would be rendered
  154. bool isBoxSeen(const FVector3D& minBound, const FVector3D& maxBound, const Transform3D &modelToWorld) const {
  155. FVector3D corner[8] = {
  156. FVector3D(minBound.x, minBound.y, minBound.z),
  157. FVector3D(maxBound.x, minBound.y, minBound.z),
  158. FVector3D(minBound.x, maxBound.y, minBound.z),
  159. FVector3D(maxBound.x, maxBound.y, minBound.z),
  160. FVector3D(minBound.x, minBound.y, maxBound.z),
  161. FVector3D(maxBound.x, minBound.y, maxBound.z),
  162. FVector3D(minBound.x, maxBound.y, maxBound.z),
  163. FVector3D(maxBound.x, maxBound.y, maxBound.z)
  164. };
  165. for (int c = 0; c < 8; c++) {
  166. corner[c] = worldToCamera(modelToWorld.transformPoint(corner[c]));
  167. }
  168. for (int s = 0; s < this->cullFrustum.getPlaneCount(); s++) {
  169. FPlane3D plane = this->cullFrustum.getPlane(s);
  170. if (!(plane.inside(corner[0])
  171. || plane.inside(corner[1])
  172. || plane.inside(corner[2])
  173. || plane.inside(corner[3])
  174. || plane.inside(corner[4])
  175. || plane.inside(corner[5])
  176. || plane.inside(corner[6])
  177. || plane.inside(corner[7]))) {
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. };
  184. }
  185. #endif