PBRT
/home/felix/UBC/projects/AdaptiveLightfieldSampling/pbrt_v2/src/3rdparty/ilmbase-1.0.2/ImathGL.h
00001 
00002 //
00003 // Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
00004 // Digital Ltd. LLC
00005 // 
00006 // All rights reserved.
00007 // 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are
00010 // met:
00011 // *       Redistributions of source code must retain the above copyright
00012 // notice, this list of conditions and the following disclaimer.
00013 // *       Redistributions in binary form must reproduce the above
00014 // copyright notice, this list of conditions and the following disclaimer
00015 // in the documentation and/or other materials provided with the
00016 // distribution.
00017 // *       Neither the name of Industrial Light & Magic nor the names of
00018 // its contributors may be used to endorse or promote products derived
00019 // from this software without specific prior written permission. 
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00024 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00025 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00027 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00028 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00029 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00030 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00031 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00034 
00035 
00036 #ifndef INCLUDED_IMATHGL_H
00037 #define INCLUDED_IMATHGL_H
00038 
00039 #include <GL/gl.h>
00040 
00041 #include "ImathVec.h"
00042 #include "ImathMatrix.h"
00043 #include "IexMathExc.h"
00044 #include "ImathFun.h"
00045 
00046 inline void glVertex    ( const Imath::V3f &v ) { glVertex3f(v.x,v.y,v.z);   }
00047 inline void glVertex    ( const Imath::V2f &v ) { glVertex2f(v.x,v.y);       }
00048 inline void glNormal    ( const Imath::V3f &n ) { glNormal3f(n.x,n.y,n.z);   }
00049 inline void glColor     ( const Imath::V3f &c ) { glColor3f(c.x,c.y,c.z);    }
00050 inline void glTranslate ( const Imath::V3f &t ) { glTranslatef(t.x,t.y,t.z); }
00051 
00052 inline void glTexCoord( const Imath::V2f &t )
00053 {
00054     glTexCoord2f(t.x,t.y);
00055 }
00056 
00057 inline void glDisableTexture()
00058 {
00059     glActiveTexture(GL_TEXTURE1);
00060     glBindTexture(GL_TEXTURE_2D, 0);
00061     glDisable(GL_TEXTURE_2D);
00062 
00063     glActiveTexture(GL_TEXTURE0);
00064 }
00065 
00066 namespace {
00067     
00068 const float GL_FLOAT_MAX = 1.8e+19; // sqrt (FLT_MAX)
00069 
00070 inline bool
00071 badFloat (float f)
00072 {
00073     return !Imath::finitef (f) || f < - GL_FLOAT_MAX || f > GL_FLOAT_MAX;
00074 }
00075 
00076 } // namespace
00077         
00078 inline void
00079 throwBadMatrix (const Imath::M44f& m)
00080 {
00081     if (badFloat (m[0][0]) ||
00082         badFloat (m[0][1]) ||
00083         badFloat (m[0][2]) ||
00084         badFloat (m[0][3]) || 
00085         badFloat (m[1][0]) ||
00086         badFloat (m[1][1]) ||
00087         badFloat (m[1][2]) ||
00088         badFloat (m[1][3]) || 
00089         badFloat (m[2][0]) ||
00090         badFloat (m[2][1]) ||
00091         badFloat (m[2][2]) ||
00092         badFloat (m[2][3]) || 
00093         badFloat (m[3][0]) ||
00094         badFloat (m[3][1]) ||
00095         badFloat (m[3][2]) ||
00096         badFloat (m[3][3]))
00097         throw Iex::OverflowExc ("GL matrix overflow");
00098 }
00099 
00100 inline void 
00101 glMultMatrix( const Imath::M44f& m ) 
00102 { 
00103     throwBadMatrix (m);
00104     glMultMatrixf( (GLfloat*)m[0] ); 
00105 }
00106 
00107 inline void 
00108 glMultMatrix( const Imath::M44f* m ) 
00109 { 
00110     throwBadMatrix (*m);
00111     glMultMatrixf( (GLfloat*)(*m)[0] ); 
00112 }
00113 
00114 inline void 
00115 glLoadMatrix( const Imath::M44f& m ) 
00116 { 
00117     throwBadMatrix (m);
00118     glLoadMatrixf( (GLfloat*)m[0] ); 
00119 }
00120 
00121 inline void 
00122 glLoadMatrix( const Imath::M44f* m ) 
00123 { 
00124     throwBadMatrix (*m);
00125     glLoadMatrixf( (GLfloat*)(*m)[0] ); 
00126 }
00127 
00128 
00129 namespace Imath {
00130 
00131 //
00132 // Class objects that push/pop the GL state. These objects assist with
00133 // proper cleanup of the state when exceptions are thrown.
00134 //
00135 
00136 class GLPushMatrix {
00137   public:
00138 
00139     GLPushMatrix ()                     { glPushMatrix(); }
00140     ~GLPushMatrix()                     { glPopMatrix(); }
00141 };
00142 
00143 class GLPushAttrib {
00144   public:
00145 
00146     GLPushAttrib (GLbitfield mask)      { glPushAttrib (mask); }
00147     ~GLPushAttrib()                     { glPopAttrib(); }
00148 };
00149 
00150 class GLBegin {
00151   public:
00152 
00153     GLBegin (GLenum mode)               { glBegin (mode); }
00154     ~GLBegin()                          { glEnd(); }
00155 };
00156 
00157 } // namespace Imath
00158 
00159 #endif