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