ImfEnvmap.h 10.9 KB
Newer Older
wester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// *       Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// *       Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// *       Neither the name of Industrial Light & Magic nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////


#ifndef INCLUDED_IMF_ENVMAP_H
#define INCLUDED_IMF_ENVMAP_H

//-----------------------------------------------------------------------------
//
//	Environment maps
//
//	Environment maps define a mapping from 3D directions to 2D
//	pixel space locations.  Environment maps are typically used
//	in 3D rendering, for effects such as quickly approximating
//	how shiny surfaces reflect their environment.
//
//	Environment maps can be stored in scanline-based or in tiled
//	OpenEXR files.  The fact that an image is an environment map
//	is indicated by the presence of an EnvmapAttribute whose name
//	is "envmap". (Convenience functions to access this attribute
//	are defined in header file ImfStandardAttributes.h.)
//	The attribute's value defines the mapping from 3D directions
//	to 2D pixel space locations.
//
//	This header file defines the set of possible EnvmapAttribute
//	values.
//
//	For each possible EnvmapAttribute value, this header file also
//	defines a set of convienience functions to convert between 3D
//	directions and 2D pixel locations.
//
//	Most of the convenience functions defined below require a
//	dataWindow parameter.  For scanline-based images, and for
//	tiled images with level mode ONE_LEVEL, the dataWindow
//	parameter should be set to the image's data window, as
//	defined in the image header.  For tiled images with level
//	mode MIPMAP_LEVELS or RIPMAP_LEVELS, the data window of the
//	image level that is being accessed should be used instead.
//	(See the dataWindowForLevel() methods in ImfTiledInputFile.h
//	and ImfTiledOutputFile.h.)
//
//-----------------------------------------------------------------------------

#include "ImathBox.h"

namespace Imf {

//--------------------------------
// Supported environment map types
//--------------------------------

enum Envmap
{
    ENVMAP_LATLONG = 0,		// Latitude-longitude environment map
    ENVMAP_CUBE = 1,		// Cube map

    NUM_ENVMAPTYPES		// Number of different environment map types
};


//-------------------------------------------------------------------------
// Latitude-Longitude Map:
//
// The environment is projected onto the image using polar coordinates
// (latitude and longitude).  A pixel's x coordinate corresponds to
// its longitude, and the y coordinate corresponds to its latitude.
// Pixel (dataWindow.min.x, dataWindow.min.y) has latitude +pi/2 and
// longitude +pi; pixel (dataWindow.max.x, dataWindow.max.y) has
// latitude -pi/2 and longitude -pi.
//
// In 3D space, latitudes -pi/2 and +pi/2 correspond to the negative and
// positive y direction.  Latitude 0, longitude 0 points into positive
// z direction; and latitude 0, longitude pi/2 points into positive x
// direction.
//
// The size of the data window should be 2*N by N pixels (width by height),
// where N can be any integer greater than 0.
//-------------------------------------------------------------------------

namespace LatLongMap
{
    //----------------------------------------------------
    // Convert a 3D direction to a 2D vector whose x and y
    // components represent the corresponding latitude
    // and longitude.
    //----------------------------------------------------

    Imath::V2f		latLong (const Imath::V3f &direction);


    //--------------------------------------------------------
    // Convert the position of a pixel to a 2D vector whose
    // x and y components represent the corresponding latitude
    // and longitude.
    //--------------------------------------------------------

    Imath::V2f		latLong (const Imath::Box2i &dataWindow,
                 const Imath::V2f &pixelPosition);


    //-------------------------------------------------------------
    // Convert a 2D vector, whose x and y components represent
    // longitude and latitude, into a corresponding pixel position.
    //-------------------------------------------------------------

    Imath::V2f		pixelPosition (const Imath::Box2i &dataWindow,
                       const Imath::V2f &latLong);


    //-----------------------------------------------------
    // Convert a 3D direction vector into a corresponding
    // pixel position.  pixelPosition(dw,dir) is equivalent
    // to pixelPosition(dw,latLong(dw,dir)).
    //-----------------------------------------------------

    Imath::V2f		pixelPosition (const Imath::Box2i &dataWindow,
                       const Imath::V3f &direction);


    //--------------------------------------------------------
    // Convert the position of a pixel in a latitude-longitude
    // map into a corresponding 3D direction.
    //--------------------------------------------------------

    Imath::V3f		direction (const Imath::Box2i &dataWindow,
                   const Imath::V2f &pixelPosition);
}


//--------------------------------------------------------------
// Cube Map:
//
// The environment is projected onto the six faces of an
// axis-aligned cube.  The cube's faces are then arranged
// in a 2D image as shown below.
//
//          2-----------3
//         /           /|
//        /           / |       Y
//       /           /  |       |
//      6-----------7   |       |
//      |           |   |       |
//      |           |   |       |
//      |   0       |   1       *------- X
//      |           |  /       /
//      |           | /       /
//      |           |/       /
//      4-----------5       Z
//
//   dataWindow.min
//        /
//       /
//      +-----------+
//      |3    Y    7|
//      |     |     |
//      |     |     |
//      |  ---+---Z |  +X face
//      |     |     |
//      |     |     |
//      |1         5|
//      +-----------+
//      |6    Y    2|
//      |     |     |
//      |     |     |
//      | Z---+---  |  -X face
//      |     |     |
//      |     |     |
//      |4         0|
//      +-----------+
//      |6    Z    7|
//      |     |     |
//      |     |     |
//      |  ---+---X |  +Y face
//      |     |     |
//      |     |     |
//      |2         3|
//      +-----------+
//      |0         1|
//      |     |     |
//      |     |     |
//      |  ---+---X |  -Y face
//      |     |     |
//      |     |     |
//      |4    Z    5|
//      +-----------+
//      |7    Y    6|
//      |     |     |
//      |     |     |
//      | X---+---  |  +Z face
//      |     |     |
//      |     |     |
//      |5         4|
//      +-----------+
//      |2    Y    3|
//      |     |     |
//      |     |     |
//      |  ---+---X |  -Z face
//      |     |     |
//      |     |     |
//      |0         1|
//      +-----------+
//                 /
//                /
//          dataWindow.max
//
// The size of the data window should be N by 6*N pixels
// (width by height), where N can be any integer greater
// than 0.
//
//--------------------------------------------------------------

//------------------------------------
// Names for the six faces of the cube
//------------------------------------

enum CubeMapFace
{
    CUBEFACE_POS_X,	// +X face
    CUBEFACE_NEG_X,	// -X face
    CUBEFACE_POS_Y,	// +Y face
    CUBEFACE_NEG_Y,	// -Y face
    CUBEFACE_POS_Z,	// +Z face
    CUBEFACE_NEG_Z 	// -Z face
};

namespace CubeMap
{
    //---------------------------------------------
    // Width and height of a cube's face, in pixels
    //---------------------------------------------

    int			sizeOfFace (const Imath::Box2i &dataWindow);


    //------------------------------------------
    // Compute the region in the environment map
    // that is covered by the specified face.
    //------------------------------------------

    Imath::Box2i	dataWindowForFace (CubeMapFace face,
                       const Imath::Box2i &dataWindow);


    //----------------------------------------------------
    // Convert the coordinates of a pixel within a face
    // [in the range from (0,0) to (s-1,s-1), where
    // s == sizeOfFace(dataWindow)] to pixel coordinates
    // in the environment map.
    //----------------------------------------------------

    Imath::V2f		pixelPosition (CubeMapFace face,
                       const Imath::Box2i &dataWindow,
                       Imath::V2f positionInFace);


    //--------------------------------------------------------------
    // Convert a 3D direction into a cube face, and a pixel position
    // within that face.
    //
    // If you have a 3D direction, dir, the following code fragment
    // finds the position, pos, of the corresponding pixel in an
    // environment map with data window dw:
    //
    // CubeMapFace f;
    // V2f pif, pos;
    //
    // faceAndPixelPosition (dir, dw, f, pif);
    // pos = pixelPosition (f, dw, pif);
    //
    //--------------------------------------------------------------

    void		faceAndPixelPosition (const Imath::V3f &direction,
                          const Imath::Box2i &dataWindow,
                          CubeMapFace &face,
                          Imath::V2f &positionInFace);


    // --------------------------------------------------------
    // Given a cube face and a pixel position within that face,
    // compute the corresponding 3D direction.
    // --------------------------------------------------------

    Imath::V3f		direction (CubeMapFace face,
                   const Imath::Box2i &dataWindow,
                   const Imath::V2f &positionInFace);
}


} // namespace Imf

#endif