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 | #ifndef PLATFORM_IRIX |
---|
56 | glMultiTexCoord2f(GL_TEXTURE1, t.x, t.y); |
---|
57 | #endif |
---|
58 | } |
---|
59 | |
---|
60 | inline void glDisableTexture() |
---|
61 | { |
---|
62 | #ifndef PLATFORM_IRIX |
---|
63 | glActiveTexture(GL_TEXTURE1); |
---|
64 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
65 | glDisable(GL_TEXTURE_2D); |
---|
66 | |
---|
67 | glActiveTexture(GL_TEXTURE0); |
---|
68 | #endif |
---|
69 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
70 | glDisable(GL_TEXTURE_2D); |
---|
71 | } |
---|
72 | |
---|
73 | namespace { |
---|
74 | |
---|
75 | const float GL_FLOAT_MAX = 1.8e+19; // sqrt (FLT_MAX) |
---|
76 | |
---|
77 | inline bool |
---|
78 | badFloat (float f) |
---|
79 | { |
---|
80 | return !Imath::finitef (f) || f < - GL_FLOAT_MAX || f > GL_FLOAT_MAX; |
---|
81 | } |
---|
82 | |
---|
83 | } // namespace |
---|
84 | |
---|
85 | inline void |
---|
86 | throwBadMatrix (const Imath::M44f& m) |
---|
87 | { |
---|
88 | if (badFloat (m[0][0]) || |
---|
89 | badFloat (m[0][1]) || |
---|
90 | badFloat (m[0][2]) || |
---|
91 | badFloat (m[0][3]) || |
---|
92 | badFloat (m[1][0]) || |
---|
93 | badFloat (m[1][1]) || |
---|
94 | badFloat (m[1][2]) || |
---|
95 | badFloat (m[1][3]) || |
---|
96 | badFloat (m[2][0]) || |
---|
97 | badFloat (m[2][1]) || |
---|
98 | badFloat (m[2][2]) || |
---|
99 | badFloat (m[2][3]) || |
---|
100 | badFloat (m[3][0]) || |
---|
101 | badFloat (m[3][1]) || |
---|
102 | badFloat (m[3][2]) || |
---|
103 | badFloat (m[3][3])) |
---|
104 | throw Iex::OverflowExc ("GL matrix overflow"); |
---|
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 | glMultMatrix( const Imath::M44f* m ) |
---|
116 | { |
---|
117 | throwBadMatrix (*m); |
---|
118 | glMultMatrixf( (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 | inline void |
---|
129 | glLoadMatrix( const Imath::M44f* m ) |
---|
130 | { |
---|
131 | throwBadMatrix (*m); |
---|
132 | glLoadMatrixf( (GLfloat*)(*m)[0] ); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | namespace Imath { |
---|
137 | |
---|
138 | // |
---|
139 | // Class objects that push/pop the GL state. These objects assist with |
---|
140 | // proper cleanup of the state when exceptions are thrown. |
---|
141 | // |
---|
142 | |
---|
143 | class GLPushMatrix { |
---|
144 | public: |
---|
145 | |
---|
146 | GLPushMatrix () { glPushMatrix(); } |
---|
147 | ~GLPushMatrix() { glPopMatrix(); } |
---|
148 | }; |
---|
149 | |
---|
150 | class GLPushAttrib { |
---|
151 | public: |
---|
152 | |
---|
153 | GLPushAttrib (GLbitfield mask) { glPushAttrib (mask); } |
---|
154 | ~GLPushAttrib() { glPopAttrib(); } |
---|
155 | }; |
---|
156 | |
---|
157 | class GLBegin { |
---|
158 | public: |
---|
159 | |
---|
160 | GLBegin (GLenum mode) { glBegin (mode); } |
---|
161 | ~GLBegin() { glEnd(); } |
---|
162 | }; |
---|
163 | |
---|
164 | } // namespace Imath |
---|
165 | |
---|
166 | #endif |
---|