1 /////////////////////////////////////////////////////////////////////////// 2 // 3 // Copyright (c) 2002, 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 35 36 #ifndef INCLUDED_IMATHGL_H 37 #define INCLUDED_IMATHGL_H 38 39 #include <GL/gl.h> 40 41 #include "ImathVec.h" 42 #include "ImathMatrix.h" 43 #include "IexMathExc.h" 44 #include "ImathFun.h" 45 46 inline void glVertex ( const Imath::V3f &v ) { glVertex3f(v.x,v.y,v.z); } 47 inline void glVertex ( const Imath::V2f &v ) { glVertex2f(v.x,v.y); } 48 inline void glNormal ( const Imath::V3f &n ) { glNormal3f(n.x,n.y,n.z); } 49 inline void glColor ( const Imath::V3f &c ) { glColor3f(c.x,c.y,c.z); } 50 inline void glTranslate ( const Imath::V3f &t ) { glTranslatef(t.x,t.y,t.z); } 51 52 inline void glTexCoord( const Imath::V2f &t ) 53 { 54 glTexCoord2f(t.x,t.y); 55 } 56 57 inline void glDisableTexture() 58 { 59 glActiveTexture(GL_TEXTURE1); 60 glBindTexture(GL_TEXTURE_2D, 0); 61 glDisable(GL_TEXTURE_2D); 62 63 glActiveTexture(GL_TEXTURE0); 64 } 65 66 namespace { 67 68 const float GL_FLOAT_MAX = 1.8e+19; // sqrt (FLT_MAX) 69 70 inline bool 71 badFloat (float f) 72 { 73 return !Imath::finitef (f) || f < - GL_FLOAT_MAX || f > GL_FLOAT_MAX; 74 } 75 76 } // namespace 77 78 inline void 79 throwBadMatrix (const Imath::M44f& m) 80 { 81 if (badFloat (m[0][0]) || 82 badFloat (m[0][1]) || 83 badFloat (m[0][2]) || 84 badFloat (m[0][3]) || 85 badFloat (m[1][0]) || 86 badFloat (m[1][1]) || 87 badFloat (m[1][2]) || 88 badFloat (m[1][3]) || 89 badFloat (m[2][0]) || 90 badFloat (m[2][1]) || 91 badFloat (m[2][2]) || 92 badFloat (m[2][3]) || 93 badFloat (m[3][0]) || 94 badFloat (m[3][1]) || 95 badFloat (m[3][2]) || 96 badFloat (m[3][3])) 97 throw Iex::OverflowExc ("GL matrix overflow"); 98 } 99 100 inline void 101 glMultMatrix( const Imath::M44f& m ) 102 { 103 throwBadMatrix (m); 104 glMultMatrixf( (GLfloat*)m[0] ); 105 } 106 107 inline void 108 glMultMatrix( const Imath::M44f* m ) 109 { 110 throwBadMatrix (*m); 111 glMultMatrixf( (GLfloat*)(*m)[0] ); 112 } 113 114 inline void 115 glLoadMatrix( const Imath::M44f& m ) 116 { 117 throwBadMatrix (m); 118 glLoadMatrixf( (GLfloat*)m[0] ); 119 } 120 121 inline void 122 glLoadMatrix( const Imath::M44f* m ) 123 { 124 throwBadMatrix (*m); 125 glLoadMatrixf( (GLfloat*)(*m)[0] ); 126 } 127 128 129 namespace Imath { 130 131 // 132 // Class objects that push/pop the GL state. These objects assist with 133 // proper cleanup of the state when exceptions are thrown. 134 // 135 136 class GLPushMatrix { 137 public: 138 139 GLPushMatrix () { glPushMatrix(); } 140 ~GLPushMatrix() { glPopMatrix(); } 141 }; 142 143 class GLPushAttrib { 144 public: 145 146 GLPushAttrib (GLbitfield mask) { glPushAttrib (mask); } 147 ~GLPushAttrib() { glPopAttrib(); } 148 }; 149 150 class GLBegin { 151 public: 152 153 GLBegin (GLenum mode) { glBegin (mode); } 154 ~GLBegin() { glEnd(); } 155 }; 156 157 } // namespace Imath 158 159 #endif 160