ImathGL.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright Contributors to the OpenEXR Project.
  4. //
  5. //
  6. // Convenience functions that call GL with Imath types
  7. //
  8. #ifndef INCLUDED_IMATHGL_H
  9. #define INCLUDED_IMATHGL_H
  10. #include <GL/gl.h>
  11. #include "ImathFun.h"
  12. #include "ImathMatrix.h"
  13. #include "ImathNamespace.h"
  14. #include "ImathVec.h"
  15. /// Call glVertex3f
  16. inline void
  17. glVertex (const IMATH_INTERNAL_NAMESPACE::V3f& v)
  18. {
  19. glVertex3f (v.x, v.y, v.z);
  20. }
  21. /// Call glVertex2f
  22. inline void
  23. glVertex (const IMATH_INTERNAL_NAMESPACE::V2f& v)
  24. {
  25. glVertex2f (v.x, v.y);
  26. }
  27. /// Call glNormal3f
  28. inline void
  29. glNormal (const IMATH_INTERNAL_NAMESPACE::V3f& n)
  30. {
  31. glNormal3f (n.x, n.y, n.z);
  32. }
  33. /// Call glColor3f
  34. inline void
  35. glColor (const IMATH_INTERNAL_NAMESPACE::V3f& c)
  36. {
  37. glColor3f (c.x, c.y, c.z);
  38. }
  39. /// Call glTranslatef
  40. inline void
  41. glTranslate (const IMATH_INTERNAL_NAMESPACE::V3f& t)
  42. {
  43. glTranslatef (t.x, t.y, t.z);
  44. }
  45. /// Call glTexCoord2f
  46. inline void
  47. glTexCoord (const IMATH_INTERNAL_NAMESPACE::V2f& t)
  48. {
  49. glTexCoord2f (t.x, t.y);
  50. }
  51. /// Disable GL textures
  52. inline void
  53. glDisableTexture()
  54. {
  55. glActiveTexture (GL_TEXTURE1);
  56. glBindTexture (GL_TEXTURE_2D, 0);
  57. glDisable (GL_TEXTURE_2D);
  58. glActiveTexture (GL_TEXTURE0);
  59. }
  60. namespace
  61. {
  62. const float GL_FLOAT_MAX = 1.8e+19; // sqrt (FLT_MAX)
  63. inline bool
  64. badFloat (float f)
  65. {
  66. return !IMATH_INTERNAL_NAMESPACE::finitef (f) || f < -GL_FLOAT_MAX || f > GL_FLOAT_MAX;
  67. }
  68. } // namespace
  69. /// Throw an exception if m is not a valid matrix for GL
  70. inline void
  71. throwBadMatrix (const IMATH_INTERNAL_NAMESPACE::M44f& m)
  72. {
  73. if (badFloat (m[0][0]) || badFloat (m[0][1]) || badFloat (m[0][2]) || badFloat (m[0][3]) ||
  74. badFloat (m[1][0]) || badFloat (m[1][1]) || badFloat (m[1][2]) || badFloat (m[1][3]) ||
  75. badFloat (m[2][0]) || badFloat (m[2][1]) || badFloat (m[2][2]) || badFloat (m[2][3]) ||
  76. badFloat (m[3][0]) || badFloat (m[3][1]) || badFloat (m[3][2]) || badFloat (m[3][3]))
  77. throw std::invalid_argument ("GL matrix overflow");
  78. }
  79. /// Call glMultmatrixf. Throw an exception if m is not a valid matrix for GL.
  80. inline void
  81. glMultMatrix (const IMATH_INTERNAL_NAMESPACE::M44f& m)
  82. {
  83. throwBadMatrix (m);
  84. glMultMatrixf ((GLfloat*) m[0]);
  85. }
  86. /// Call glMultmatrixf. Throw an exception if m is not a valid matrix for GL.
  87. inline void
  88. glMultMatrix (const IMATH_INTERNAL_NAMESPACE::M44f* m)
  89. {
  90. throwBadMatrix (*m);
  91. glMultMatrixf ((GLfloat*) (*m)[0]);
  92. }
  93. /// Call glLoadmatrixf. Throw an exception if m is not a valid matrix for GL.
  94. inline void
  95. glLoadMatrix (const IMATH_INTERNAL_NAMESPACE::M44f& m)
  96. {
  97. throwBadMatrix (m);
  98. glLoadMatrixf ((GLfloat*) m[0]);
  99. }
  100. /// Call glLoadmatrixf. Throw an exception if m is not a valid matrix for GL.
  101. inline void
  102. glLoadMatrix (const IMATH_INTERNAL_NAMESPACE::M44f* m)
  103. {
  104. throwBadMatrix (*m);
  105. glLoadMatrixf ((GLfloat*) (*m)[0]);
  106. }
  107. IMATH_INTERNAL_NAMESPACE_HEADER_ENTER
  108. ///
  109. /// A class object that pushes/pops the GL matrix. This object assists with
  110. /// proper cleanup of the state when exceptions are thrown.
  111. ///
  112. class GLPushMatrix
  113. {
  114. public:
  115. GLPushMatrix() { glPushMatrix(); }
  116. ~GLPushMatrix() { glPopMatrix(); }
  117. };
  118. ///
  119. /// A class object that pushes/pops the current GL attribute state. This object assists with
  120. /// proper cleanup of the state when exceptions are thrown.
  121. ///
  122. class GLPushAttrib
  123. {
  124. public:
  125. /// call glPushAttrib()
  126. GLPushAttrib (GLbitfield mask) { glPushAttrib (mask); }
  127. /// call glPopAttrib()
  128. ~GLPushAttrib() { glPopAttrib(); }
  129. };
  130. ///
  131. /// A class object that wraps glBegin/glEnd. The constructor calls
  132. /// glBegin(). The destructor calls glEnd().
  133. ///
  134. class GLBegin
  135. {
  136. public:
  137. /// Call glBegin()
  138. GLBegin (GLenum mode) { glBegin (mode); }
  139. /// Call glEnd()
  140. ~GLBegin() { glEnd(); }
  141. };
  142. IMATH_INTERNAL_NAMESPACE_HEADER_EXIT
  143. #endif