/src/FreeImage/Source/OpenEXR/Imath/ImathGL.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 159 lines · 94 code · 28 blank · 37 comment · 6 complexity · e9ce2b90ab28921507629cafda1b7ac4 MD5 · raw file

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