ImfEnvmap.h 11 KB

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