camera.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2017, 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. #include "types.h"
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. // ---------------------------------------------------------------------------
  45. /** Helper structure to describe a virtual camera.
  46. *
  47. * Cameras have a representation in the node graph and can be animated.
  48. * An important aspect is that the camera itself is also part of the
  49. * scenegraph. This means, any values such as the look-at vector are not
  50. * *absolute*, they're <b>relative</b> to the coordinate system defined
  51. * by the node which corresponds to the camera. This allows for camera
  52. * animations. For static cameras parameters like the 'look-at' or 'up' vectors
  53. * are usually specified directly in aiCamera, but beware, they could also
  54. * be encoded in the node transformation. The following (pseudo)code sample
  55. * shows how to do it: <br><br>
  56. * @code
  57. * // Get the camera matrix for a camera at a specific time
  58. * // if the node hierarchy for the camera does not contain
  59. * // at least one animated node this is a static computation
  60. * get-camera-matrix (node sceneRoot, camera cam) : matrix
  61. * {
  62. * node cnd = find-node-for-camera(cam)
  63. * matrix cmt = identity()
  64. *
  65. * // as usual - get the absolute camera transformation for this frame
  66. * for each node nd in hierarchy from sceneRoot to cnd
  67. * matrix cur
  68. * if (is-animated(nd))
  69. * cur = eval-animation(nd)
  70. * else cur = nd->mTransformation;
  71. * cmt = mult-matrices( cmt, cur )
  72. * end for
  73. *
  74. * // now multiply with the camera's own local transform
  75. * cam = mult-matrices (cam, get-camera-matrix(cmt) )
  76. * }
  77. * @endcode
  78. *
  79. * @note some file formats (such as 3DS, ASE) export a "target point" -
  80. * the point the camera is looking at (it can even be animated). Assimp
  81. * writes the target point as a subnode of the camera's main node,
  82. * called "<camName>.Target". However this is just additional information
  83. * then the transformation tracks of the camera main node make the
  84. * camera already look in the right direction.
  85. *
  86. */
  87. struct aiCamera
  88. {
  89. /** The name of the camera.
  90. *
  91. * There must be a node in the scenegraph with the same name.
  92. * This node specifies the position of the camera in the scene
  93. * hierarchy and can be animated.
  94. */
  95. C_STRUCT aiString mName;
  96. /** Position of the camera relative to the coordinate space
  97. * defined by the corresponding node.
  98. *
  99. * The default value is 0|0|0.
  100. */
  101. C_STRUCT aiVector3D mPosition;
  102. /** 'Up' - vector of the camera coordinate system relative to
  103. * the coordinate space defined by the corresponding node.
  104. *
  105. * The 'right' vector of the camera coordinate system is
  106. * the cross product of the up and lookAt vectors.
  107. * The default value is 0|1|0. The vector
  108. * may be normalized, but it needn't.
  109. */
  110. C_STRUCT aiVector3D mUp;
  111. /** 'LookAt' - vector of the camera coordinate system relative to
  112. * the coordinate space defined by the corresponding node.
  113. *
  114. * This is the viewing direction of the user.
  115. * The default value is 0|0|1. The vector
  116. * may be normalized, but it needn't.
  117. */
  118. C_STRUCT aiVector3D mLookAt;
  119. /** Half horizontal field of view angle, in radians.
  120. *
  121. * The field of view angle is the angle between the center
  122. * line of the screen and the left or right border.
  123. * The default value is 1/4PI.
  124. */
  125. float mHorizontalFOV;
  126. /** Distance of the near clipping plane from the camera.
  127. *
  128. * The value may not be 0.f (for arithmetic reasons to prevent
  129. * a division through zero). The default value is 0.1f.
  130. */
  131. float mClipPlaneNear;
  132. /** Distance of the far clipping plane from the camera.
  133. *
  134. * The far clipping plane must, of course, be further away than the
  135. * near clipping plane. The default value is 1000.f. The ratio
  136. * between the near and the far plane should not be too
  137. * large (between 1000-10000 should be ok) to avoid floating-point
  138. * inaccuracies which could lead to z-fighting.
  139. */
  140. float mClipPlaneFar;
  141. /** Screen aspect ratio.
  142. *
  143. * This is the ration between the width and the height of the
  144. * screen. Typical values are 4/3, 1/2 or 1/1. This value is
  145. * 0 if the aspect ratio is not defined in the source file.
  146. * 0 is also the default value.
  147. */
  148. float mAspect;
  149. #ifdef __cplusplus
  150. aiCamera()
  151. : mUp (0.f,1.f,0.f)
  152. , mLookAt (0.f,0.f,1.f)
  153. , mHorizontalFOV (0.25f * (float)AI_MATH_PI)
  154. , mClipPlaneNear (0.1f)
  155. , mClipPlaneFar (1000.f)
  156. , mAspect (0.f)
  157. {}
  158. /** @brief Get a *right-handed* camera matrix from me
  159. * @param out Camera matrix to be filled
  160. */
  161. void GetCameraMatrix (aiMatrix4x4& out) const
  162. {
  163. /** todo: test ... should work, but i'm not absolutely sure */
  164. /** We don't know whether these vectors are already normalized ...*/
  165. aiVector3D zaxis = mLookAt; zaxis.Normalize();
  166. aiVector3D yaxis = mUp; yaxis.Normalize();
  167. aiVector3D xaxis = mUp^mLookAt; xaxis.Normalize();
  168. out.a4 = -(xaxis * mPosition);
  169. out.b4 = -(yaxis * mPosition);
  170. out.c4 = -(zaxis * mPosition);
  171. out.a1 = xaxis.x;
  172. out.a2 = xaxis.y;
  173. out.a3 = xaxis.z;
  174. out.b1 = yaxis.x;
  175. out.b2 = yaxis.y;
  176. out.b3 = yaxis.z;
  177. out.c1 = zaxis.x;
  178. out.c2 = zaxis.y;
  179. out.c3 = zaxis.z;
  180. out.d1 = out.d2 = out.d3 = 0.f;
  181. out.d4 = 1.f;
  182. }
  183. #endif
  184. };
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. #endif // AI_CAMERA_H_INC