Debian Codegraph @

lib/database/src/so/nodes/SoTextureCoordinateEnvironment.c++

raw
1 /*
2 *
3 * Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * Further, this software is distributed without any warranty that it is
16 * free of the rightful claim of any third person regarding infringement
17 * or the like. Any license provided herein, whether implied or
18 * otherwise, applies only to this software file. Patent licenses, if
19 * any, provided herein do not apply to combinations of this program with
20 * other software, or any other product whatsoever.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
27 * Mountain View, CA 94043, or:
28 *
29 * http://www.sgi.com
30 *
31 * For further information regarding this notice, see:
32 *
33 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
34 *
35 */
36
37 /*
38 * Copyright (C) 1990,91 Silicon Graphics, Inc.
39 *
40 _______________________________________________________________________
41 ______________ S I L I C O N G R A P H I C S I N C . ____________
42 |
43 | $Revision: 1.1.1.1 $
44 |
45 | Classes:
46 | SoTextureCoordinateEnvironment
47 |
48 | Author(s) : Thad Beier, Gavin Bell
49 |
50 ______________ S I L I C O N G R A P H I C S I N C . ____________
51 _______________________________________________________________________
52 */
53
54 #include <Inventor/actions/SoCallbackAction.h>
55 #include <Inventor/actions/SoGLRenderAction.h>
56 #include <Inventor/actions/SoPickAction.h>
57 #include <Inventor/elements/SoGLTextureCoordinateElement.h>
58 #include <Inventor/elements/SoModelMatrixElement.h>
59 #include <Inventor/elements/SoTextureQualityElement.h>
60 #include <Inventor/elements/SoViewingMatrixElement.h>
61 #include <Inventor/nodes/SoTextureCoordinateEnvironment.h>
62
63 #include <GL/gl.h>
64
65 SO_NODE_SOURCE(SoTextureCoordinateEnvironment);
66
67 ////////////////////////////////////////////////////////////////////////
68 //
69 // Description:
70 // Constructor
71 //
72 // Use: public
73
74 SoTextureCoordinateEnvironment::SoTextureCoordinateEnvironment()
75 //
76 ////////////////////////////////////////////////////////////////////////
77 {
78 SO_NODE_CONSTRUCTOR(SoTextureCoordinateEnvironment);
79 isBuiltIn = TRUE;
80 }
81
82 ////////////////////////////////////////////////////////////////////////
83 //
84 // Description:
85 // Destructor
86 //
87 // Use: public
88
89 SoTextureCoordinateEnvironment::~SoTextureCoordinateEnvironment()
90 //
91 ////////////////////////////////////////////////////////////////////////
92 {
93 }
94
95 ////////////////////////////////////////////////////////////////////////
96 //
97 // Description:
98 // Calculates texture coordinate
99 //
100 // Use: public
101
102 const SbVec4f &
103 SoTextureCoordinateEnvironment::valueCallback(void *action,
104 const SbVec3f &point,
105 const SbVec3f &normal)
106 //
107 ////////////////////////////////////////////////////////////////////////
108 {
109 SoAction *a = (SoAction *)action;
110
111 //
112 // See the glTexGen() man page for the math here.
113 //
114
115 // First, map normal and point into eye space:
116 const SbMatrix &mm = SoModelMatrixElement::get(a->getState());
117 const SbMatrix &vm = SoViewingMatrixElement::get(a->getState());
118
119 // Compute the matrix that transforms normals from object-space to
120 // eye-space; use the inverse transpose to scale correctly
121 SbVec3f normalE;
122 SbMatrix nm = (vm * mm).inverse().transpose();
123 nm.multDirMatrix(normal, normalE);
124
125 SbVec3f pointE;
126 mm.multVecMatrix(point, pointE); // Gives world-space point
127 vm.multVecMatrix(pointE, pointE); // ... to eye-space.
128
129 // Get the normalized vector from the eye (which is conveniently
130 // at 0,0,0 in eye space) to the point.
131 pointE.normalize();
132
133 // Now, figure out reflection vector, from formula:
134 // R = P - 2 (N . N) pointE
135 SbVec3f reflection = pointE - 2.0 * normalE.dot(normalE) * pointE;
136
137 // Finally, compute s/t coordinates...
138 reflection[2] += 1.0;
139 float magnitude = reflection.length();
140
141 // This is static so we can return a reference to it
142 static SbVec4f result;
143 result.setValue(reflection[0] / magnitude + 0.5,
144 reflection[1] / magnitude + 0.5,
145 0.0, 1.0);
146
147 return result;
148 }
149
150 ////////////////////////////////////////////////////////////////////////
151 //
152 // Description:
153 // Does GL render action.
154 //
155 // Use: protected
156
157 void
158 SoTextureCoordinateEnvironment::GLRender(SoGLRenderAction *action)
159 //
160 ////////////////////////////////////////////////////////////////////////
161 {
162 SoState *state = action->getState();
163
164 // Special case to workaround OpenGL on Indigo/IndigoII bug:
165 if (SoTextureOverrideElement::getQualityOverride(state) &&
166 SoTextureQualityElement::get(state) == 0.0) return;
167
168 // Let the state know that the GL is generating texture
169 // coordinates.
170 SoGLTextureCoordinateElement::setTexGen(state, this,
171 doTexgen, this,
172 valueCallback, action);
173 }
174
175 ////////////////////////////////////////////////////////////////////////
176 //
177 // Description:
178 // Callback registered with the TextureCoordinateElement that keep
179 // the GL up-to-date as the state is pushed/popped.
180 //
181 // Use: private, static
182
183 void
184 SoTextureCoordinateEnvironment::doTexgen(void *)
185 //
186 ////////////////////////////////////////////////////////////////////////
187 {
188 glTexGenf(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
189 glTexGenf(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
190 }
191
192 ////////////////////////////////////////////////////////////////////////
193 //
194 // Description:
195 // Does callback action thing.
196 //
197 // Use: protected
198
199 void
200 SoTextureCoordinateEnvironment::callback(SoCallbackAction *action)
201 //
202 ////////////////////////////////////////////////////////////////////////
203 {
204 SoTextureCoordinateEnvironment::doAction(action);
205 }
206
207 ////////////////////////////////////////////////////////////////////////
208 //
209 // Description:
210 // Does pick action...
211 //
212 // Use: protected
213
214 void
215 SoTextureCoordinateEnvironment::pick(SoPickAction *action)
216 //
217 ////////////////////////////////////////////////////////////////////////
218 {
219 SoTextureCoordinateEnvironment::doAction(action);
220 }
221
222 ////////////////////////////////////////////////////////////////////////
223 //
224 // Description:
225 // doAction. Add this node to the state
226 //
227 // Use: Extender public
228
229 void
230 SoTextureCoordinateEnvironment::doAction(SoAction *action)
231 //
232 ////////////////////////////////////////////////////////////////////////
233 {
234 SoState *state = action->getState();
235
236 SoTextureCoordinateElement::setFunction(state, this,
237 valueCallback, action);
238 }
239