camera.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file camera.h
  35. * @brief Defines the aiCamera data structure
  36. */
  37. #pragma once
  38. #ifndef AI_CAMERA_H_INC
  39. #define AI_CAMERA_H_INC
  40. #ifdef __GNUC__
  41. #pragma GCC system_header
  42. #endif
  43. #include "types.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. // ---------------------------------------------------------------------------
  48. /** Helper structure to describe a virtual camera.
  49. *
  50. * Cameras have a representation in the node graph and can be animated.
  51. * An important aspect is that the camera itself is also part of the
  52. * scene-graph. This means, any values such as the look-at vector are not
  53. * *absolute*, they're <b>relative</b> to the coordinate system defined
  54. * by the node which corresponds to the camera. This allows for camera
  55. * animations. For static cameras parameters like the 'look-at' or 'up' vectors
  56. * are usually specified directly in aiCamera, but beware, they could also
  57. * be encoded in the node transformation. The following (pseudo)code sample
  58. * shows how to do it: <br><br>
  59. * @code
  60. * // Get the camera matrix for a camera at a specific time
  61. * // if the node hierarchy for the camera does not contain
  62. * // at least one animated node this is a static computation
  63. * get-camera-matrix (node sceneRoot, camera cam) : matrix
  64. * {
  65. * node cnd = find-node-for-camera(cam)
  66. * matrix cmt = identity()
  67. *
  68. * // as usual - get the absolute camera transformation for this frame
  69. * for each node nd in hierarchy from sceneRoot to cnd
  70. * matrix cur
  71. * if (is-animated(nd))
  72. * cur = eval-animation(nd)
  73. * else cur = nd->mTransformation;
  74. * cmt = mult-matrices( cmt, cur )
  75. * end for
  76. *
  77. * // now multiply with the camera's own local transform
  78. * cam = mult-matrices (cam, get-camera-matrix(cmt) )
  79. * }
  80. * @endcode
  81. *
  82. * @note some file formats (such as 3DS, ASE) export a "target point" -
  83. * the point the camera is looking at (it can even be animated). Assimp
  84. * writes the target point as a subnode of the camera's main node,
  85. * called "<camName>.Target". However this is just additional information
  86. * then the transformation tracks of the camera main node make the
  87. * camera already look in the right direction.
  88. *
  89. */
  90. struct aiCamera {
  91. /** The name of the camera.
  92. *
  93. * There must be a node in the scenegraph with the same name.
  94. * This node specifies the position of the camera in the scene
  95. * hierarchy and can be animated.
  96. */
  97. C_STRUCT aiString mName;
  98. /** Position of the camera relative to the coordinate space
  99. * defined by the corresponding node.
  100. *
  101. * The default value is 0|0|0.
  102. */
  103. C_STRUCT aiVector3D mPosition;
  104. /** 'Up' - vector of the camera coordinate system relative to
  105. * the coordinate space defined by the corresponding node.
  106. *
  107. * The 'right' vector of the camera coordinate system is
  108. * the cross product of the up and lookAt vectors.
  109. * The default value is 0|1|0. The vector
  110. * may be normalized, but it needn't.
  111. */
  112. C_STRUCT aiVector3D mUp;
  113. /** 'LookAt' - vector of the camera coordinate system relative to
  114. * the coordinate space defined by the corresponding node.
  115. *
  116. * This is the viewing direction of the user.
  117. * The default value is 0|0|1. The vector
  118. * may be normalized, but it needn't.
  119. */
  120. C_STRUCT aiVector3D mLookAt;
  121. /** Horizontal field of view angle, in radians.
  122. *
  123. * The field of view angle is the angle between the center
  124. * line of the screen and the left or right border.
  125. * The default value is 1/4PI.
  126. */
  127. float mHorizontalFOV;
  128. /** Distance of the near clipping plane from the camera.
  129. *
  130. * The value may not be 0.f (for arithmetic reasons to prevent
  131. * a division through zero). The default value is 0.1f.
  132. */
  133. float mClipPlaneNear;
  134. /** Distance of the far clipping plane from the camera.
  135. *
  136. * The far clipping plane must, of course, be further away than the
  137. * near clipping plane. The default value is 1000.f. The ratio
  138. * between the near and the far plane should not be too
  139. * large (between 1000-10000 should be ok) to avoid floating-point
  140. * inaccuracies which could lead to z-fighting.
  141. */
  142. float mClipPlaneFar;
  143. /** Screen aspect ratio.
  144. *
  145. * This is the ration between the width and the height of the
  146. * screen. Typical values are 4/3, 1/2 or 1/1. This value is
  147. * 0 if the aspect ratio is not defined in the source file.
  148. * 0 is also the default value.
  149. */
  150. float mAspect;
  151. /** Half horizontal orthographic width, in scene units.
  152. *
  153. * The orthographic width specifies the half width of the
  154. * orthographic view box. If non-zero the camera is
  155. * orthographic and the mAspect should define to the
  156. * ratio between the orthographic width and height
  157. * and mHorizontalFOV should be set to 0.
  158. * The default value is 0 (not orthographic).
  159. */
  160. float mOrthographicWidth;
  161. #ifdef __cplusplus
  162. aiCamera() AI_NO_EXCEPT
  163. : mUp(0.f, 1.f, 0.f),
  164. mLookAt(0.f, 0.f, 1.f),
  165. mHorizontalFOV(0.25f * (float)AI_MATH_PI),
  166. mClipPlaneNear(0.1f),
  167. mClipPlaneFar(1000.f),
  168. mAspect(0.f),
  169. mOrthographicWidth(0.f) {}
  170. /** @brief Get a *right-handed* camera matrix from me
  171. * @param out Camera matrix to be filled
  172. */
  173. void GetCameraMatrix(aiMatrix4x4 &out) const {
  174. /** todo: test ... should work, but i'm not absolutely sure */
  175. /** We don't know whether these vectors are already normalized ...*/
  176. aiVector3D zaxis = mLookAt;
  177. zaxis.Normalize();
  178. aiVector3D yaxis = mUp;
  179. yaxis.Normalize();
  180. aiVector3D xaxis = mUp ^ mLookAt;
  181. xaxis.Normalize();
  182. out.a4 = -(xaxis * mPosition);
  183. out.b4 = -(yaxis * mPosition);
  184. out.c4 = -(zaxis * mPosition);
  185. out.a1 = xaxis.x;
  186. out.a2 = xaxis.y;
  187. out.a3 = xaxis.z;
  188. out.b1 = yaxis.x;
  189. out.b2 = yaxis.y;
  190. out.b3 = yaxis.z;
  191. out.c1 = zaxis.x;
  192. out.c2 = zaxis.y;
  193. out.c3 = zaxis.z;
  194. out.d1 = out.d2 = out.d3 = 0.f;
  195. out.d4 = 1.f;
  196. }
  197. #endif
  198. };
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif // AI_CAMERA_H_INC