ImfEnvmap.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_ENVMAP_H
  6. #define INCLUDED_IMF_ENVMAP_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // Environment maps
  10. //
  11. // Environment maps define a mapping from 3D directions to 2D
  12. // pixel space locations. Environment maps are typically used
  13. // in 3D rendering, for effects such as quickly approximating
  14. // how shiny surfaces reflect their environment.
  15. //
  16. // Environment maps can be stored in scanline-based or in tiled
  17. // OpenEXR files. The fact that an image is an environment map
  18. // is indicated by the presence of an EnvmapAttribute whose name
  19. // is "envmap". (Convenience functions to access this attribute
  20. // are defined in header file ImfStandardAttributes.h.)
  21. // The attribute's value defines the mapping from 3D directions
  22. // to 2D pixel space locations.
  23. //
  24. // This header file defines the set of possible EnvmapAttribute
  25. // values.
  26. //
  27. // For each possible EnvmapAttribute value, this header file also
  28. // defines a set of convenience functions to convert between 3D
  29. // directions and 2D pixel locations.
  30. //
  31. // Most of the convenience functions defined below require a
  32. // dataWindow parameter. For scanline-based images, and for
  33. // tiled images with level mode ONE_LEVEL, the dataWindow
  34. // parameter should be set to the image's data window, as
  35. // defined in the image header. For tiled images with level
  36. // mode MIPMAP_LEVELS or RIPMAP_LEVELS, the data window of the
  37. // image level that is being accessed should be used instead.
  38. // (See the dataWindowForLevel() methods in ImfTiledInputFile.h
  39. // and ImfTiledOutputFile.h.)
  40. //
  41. //-----------------------------------------------------------------------------
  42. #include "ImfExport.h"
  43. #include "ImfNamespace.h"
  44. #include <ImathBox.h>
  45. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  46. //--------------------------------
  47. // Supported environment map types
  48. //--------------------------------
  49. enum IMF_EXPORT_ENUM Envmap : int
  50. {
  51. ENVMAP_LATLONG = 0, // Latitude-longitude environment map
  52. ENVMAP_CUBE = 1, // Cube map
  53. NUM_ENVMAPTYPES // Number of different environment map types
  54. };
  55. //-------------------------------------------------------------------------
  56. // Latitude-Longitude Map:
  57. //
  58. // The environment is projected onto the image using polar coordinates
  59. // (latitude and longitude). A pixel's x coordinate corresponds to
  60. // its longitude, and the y coordinate corresponds to its latitude.
  61. // Pixel (dataWindow.min.x, dataWindow.min.y) has latitude +pi/2 and
  62. // longitude +pi; pixel (dataWindow.max.x, dataWindow.max.y) has
  63. // latitude -pi/2 and longitude -pi.
  64. //
  65. // In 3D space, latitudes -pi/2 and +pi/2 correspond to the negative and
  66. // positive y direction. Latitude 0, longitude 0 points into positive
  67. // z direction; and latitude 0, longitude pi/2 points into positive x
  68. // direction.
  69. //
  70. // The size of the data window should be 2*N by N pixels (width by height),
  71. // where N can be any integer greater than 0.
  72. //-------------------------------------------------------------------------
  73. namespace LatLongMap
  74. {
  75. //----------------------------------------------------
  76. // Convert a 3D direction to a 2D vector whose x and y
  77. // components represent the corresponding latitude
  78. // and longitude.
  79. //----------------------------------------------------
  80. IMF_EXPORT
  81. IMATH_NAMESPACE::V2f latLong (const IMATH_NAMESPACE::V3f &direction);
  82. //--------------------------------------------------------
  83. // Convert the position of a pixel to a 2D vector whose
  84. // x and y components represent the corresponding latitude
  85. // and longitude.
  86. //--------------------------------------------------------
  87. IMF_EXPORT
  88. IMATH_NAMESPACE::V2f latLong (const IMATH_NAMESPACE::Box2i &dataWindow,
  89. const IMATH_NAMESPACE::V2f &pixelPosition);
  90. //-------------------------------------------------------------
  91. // Convert a 2D vector, whose x and y components represent
  92. // longitude and latitude, into a corresponding pixel position.
  93. //-------------------------------------------------------------
  94. IMF_EXPORT
  95. IMATH_NAMESPACE::V2f pixelPosition (const IMATH_NAMESPACE::Box2i &dataWindow,
  96. const IMATH_NAMESPACE::V2f &latLong);
  97. //-----------------------------------------------------
  98. // Convert a 3D direction vector into a corresponding
  99. // pixel position. pixelPosition(dw,dir) is equivalent
  100. // to pixelPosition(dw,latLong(dw,dir)).
  101. //-----------------------------------------------------
  102. IMF_EXPORT
  103. IMATH_NAMESPACE::V2f pixelPosition (const IMATH_NAMESPACE::Box2i &dataWindow,
  104. const IMATH_NAMESPACE::V3f &direction);
  105. //--------------------------------------------------------
  106. // Convert the position of a pixel in a latitude-longitude
  107. // map into a corresponding 3D direction.
  108. //--------------------------------------------------------
  109. IMF_EXPORT
  110. IMATH_NAMESPACE::V3f direction (const IMATH_NAMESPACE::Box2i &dataWindow,
  111. const IMATH_NAMESPACE::V2f &pixelPosition);
  112. }
  113. //--------------------------------------------------------------
  114. // Cube Map:
  115. //
  116. // The environment is projected onto the six faces of an
  117. // axis-aligned cube. The cube's faces are then arranged
  118. // in a 2D image as shown below.
  119. //
  120. // 2-----------3
  121. // / /|
  122. // / / | Y
  123. // / / | |
  124. // 6-----------7 | |
  125. // | | | |
  126. // | | | |
  127. // | 0 | 1 *------- X
  128. // | | / /
  129. // | | / /
  130. // | |/ /
  131. // 4-----------5 Z
  132. //
  133. // dataWindow.min
  134. // /
  135. // /
  136. // +-----------+
  137. // |3 Y 7|
  138. // | | |
  139. // | | |
  140. // | ---+---Z | +X face
  141. // | | |
  142. // | | |
  143. // |1 5|
  144. // +-----------+
  145. // |6 Y 2|
  146. // | | |
  147. // | | |
  148. // | Z---+--- | -X face
  149. // | | |
  150. // | | |
  151. // |4 0|
  152. // +-----------+
  153. // |6 Z 7|
  154. // | | |
  155. // | | |
  156. // | ---+---X | +Y face
  157. // | | |
  158. // | | |
  159. // |2 3|
  160. // +-----------+
  161. // |0 1|
  162. // | | |
  163. // | | |
  164. // | ---+---X | -Y face
  165. // | | |
  166. // | | |
  167. // |4 Z 5|
  168. // +-----------+
  169. // |7 Y 6|
  170. // | | |
  171. // | | |
  172. // | X---+--- | +Z face
  173. // | | |
  174. // | | |
  175. // |5 4|
  176. // +-----------+
  177. // |2 Y 3|
  178. // | | |
  179. // | | |
  180. // | ---+---X | -Z face
  181. // | | |
  182. // | | |
  183. // |0 1|
  184. // +-----------+
  185. // /
  186. // /
  187. // dataWindow.max
  188. //
  189. // The size of the data window should be N by 6*N pixels
  190. // (width by height), where N can be any integer greater
  191. // than 0.
  192. //
  193. //--------------------------------------------------------------
  194. //------------------------------------
  195. // Names for the six faces of the cube
  196. //------------------------------------
  197. enum IMF_EXPORT_ENUM CubeMapFace
  198. {
  199. CUBEFACE_POS_X, // +X face
  200. CUBEFACE_NEG_X, // -X face
  201. CUBEFACE_POS_Y, // +Y face
  202. CUBEFACE_NEG_Y, // -Y face
  203. CUBEFACE_POS_Z, // +Z face
  204. CUBEFACE_NEG_Z // -Z face
  205. };
  206. namespace CubeMap
  207. {
  208. //---------------------------------------------
  209. // Width and height of a cube's face, in pixels
  210. //---------------------------------------------
  211. IMF_EXPORT
  212. int sizeOfFace (const IMATH_NAMESPACE::Box2i &dataWindow);
  213. //------------------------------------------
  214. // Compute the region in the environment map
  215. // that is covered by the specified face.
  216. //------------------------------------------
  217. IMF_EXPORT
  218. IMATH_NAMESPACE::Box2i dataWindowForFace (CubeMapFace face,
  219. const IMATH_NAMESPACE::Box2i &dataWindow);
  220. //----------------------------------------------------
  221. // Convert the coordinates of a pixel within a face
  222. // [in the range from (0,0) to (s-1,s-1), where
  223. // s == sizeOfFace(dataWindow)] to pixel coordinates
  224. // in the environment map.
  225. //----------------------------------------------------
  226. IMF_EXPORT
  227. IMATH_NAMESPACE::V2f pixelPosition (CubeMapFace face,
  228. const IMATH_NAMESPACE::Box2i &dataWindow,
  229. IMATH_NAMESPACE::V2f positionInFace);
  230. //--------------------------------------------------------------
  231. // Convert a 3D direction into a cube face, and a pixel position
  232. // within that face.
  233. //
  234. // If you have a 3D direction, dir, the following code fragment
  235. // finds the position, pos, of the corresponding pixel in an
  236. // environment map with data window dw:
  237. //
  238. // CubeMapFace f;
  239. // V2f pif, pos;
  240. //
  241. // faceAndPixelPosition (dir, dw, f, pif);
  242. // pos = pixelPosition (f, dw, pif);
  243. //
  244. //--------------------------------------------------------------
  245. IMF_EXPORT
  246. void faceAndPixelPosition (const IMATH_NAMESPACE::V3f &direction,
  247. const IMATH_NAMESPACE::Box2i &dataWindow,
  248. CubeMapFace &face,
  249. IMATH_NAMESPACE::V2f &positionInFace);
  250. // --------------------------------------------------------
  251. // Given a cube face and a pixel position within that face,
  252. // compute the corresponding 3D direction.
  253. // --------------------------------------------------------
  254. IMF_EXPORT
  255. IMATH_NAMESPACE::V3f direction (CubeMapFace face,
  256. const IMATH_NAMESPACE::Box2i &dataWindow,
  257. const IMATH_NAMESPACE::V2f &positionInFace);
  258. }
  259. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  260. #endif