Debian Codegraph @

lib/database/src/so/nodes/SoEnvironment.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 | SoEnvironment
47 |
48 | Author(s) : Paul S. Strauss
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 <GL/gl.h>
55 #include <Inventor/actions/SoCallbackAction.h>
56 #include <Inventor/actions/SoGLRenderAction.h>
57 #include <Inventor/elements/SoLightAttenuationElement.h>
58 #include <Inventor/elements/SoViewVolumeElement.h>
59 #include <Inventor/nodes/SoEnvironment.h>
60
61 SO_NODE_SOURCE(SoEnvironment);
62
63 ////////////////////////////////////////////////////////////////////////
64 //
65 // Description:
66 // Constructor
67 //
68 // Use: public
69
70 SoEnvironment::SoEnvironment()
71 //
72 ////////////////////////////////////////////////////////////////////////
73 {
74 SO_NODE_CONSTRUCTOR(SoEnvironment);
75 SO_NODE_ADD_FIELD(ambientIntensity, (0.2));
76 SO_NODE_ADD_FIELD(ambientColor, (1.0, 1.0, 1.0));
77 SO_NODE_ADD_FIELD(attenuation, (0.0, 0.0, 1.0));
78 SO_NODE_ADD_FIELD(fogType, (NONE));
79 SO_NODE_ADD_FIELD(fogColor, (1.0, 1.0, 1.0));
80 SO_NODE_ADD_FIELD(fogVisibility, (0.0));
81
82 // Set up static info for enumerated type field
83 SO_NODE_DEFINE_ENUM_VALUE(FogType, NONE);
84 SO_NODE_DEFINE_ENUM_VALUE(FogType, HAZE);
85 SO_NODE_DEFINE_ENUM_VALUE(FogType, FOG);
86 SO_NODE_DEFINE_ENUM_VALUE(FogType, SMOKE);
87
88 // Set up info in enumerated type fields
89 SO_NODE_SET_SF_ENUM_TYPE(fogType, FogType);
90
91 isBuiltIn = TRUE;
92 }
93
94 ////////////////////////////////////////////////////////////////////////
95 //
96 // Description:
97 // Destructor
98 //
99 // Use: private
100
101 SoEnvironment::~SoEnvironment()
102 //
103 ////////////////////////////////////////////////////////////////////////
104 {
105 }
106
107 ////////////////////////////////////////////////////////////////////////
108 //
109 // Description:
110 // Sets up environment parameters during rendering.
111 //
112 // Use: extender
113
114 void
115 SoEnvironment::GLRender(SoGLRenderAction *action)
116 //
117 ////////////////////////////////////////////////////////////////////////
118 {
119 SbVec3f v3;
120 SbVec4f v4;
121
122 //////////////////////
123 //
124 // Set up ambient lighting.
125 //
126
127 // RGBA ambient intensity is the product of the color and
128 // intensity, with 1.0 alpha
129 v3 = ambientIntensity.getValue() * ambientColor.getValue();
130 v4.setValue(v3[0], v3[1], v3[2], 1.0);
131 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, v4.getValue());
132
133 //////////////////////
134 //
135 // Set up light attenuation. This is stored in the
136 // SoLightAttenuationElement, which is then accessed by subsequent
137 // light sources.
138 //
139
140 SoLightAttenuationElement::set(action->getState(), this,
141 attenuation.getValue());
142
143 //////////////////////
144 //
145 // Set up fog.
146 //
147 FogType type = (FogType) fogType.getValue();
148
149 if (type == NONE)
150 glDisable(GL_FOG);
151
152 else {
153 float visibility = fogVisibility.getValue();
154
155 // Check for visibility of 0, which is the default value - this
156 // means that we should use the far plane of the current view
157 // volume as the visibility
158 if (visibility == 0.0) {
159 const SbViewVolume &vol =
160 SoViewVolumeElement::get(action->getState());
161 visibility = vol.getNearDist() + vol.getDepth();
162 }
163
164 glEnable(GL_FOG);
165 glFogfv(GL_FOG_COLOR, fogColor.getValue().getValue());
166
167 switch (type) {
168
169 case NONE:
170 // Can't get here!
171 break;
172
173 case HAZE:
174 // Set up linear ramp based on visibility
175 glFogf(GL_FOG_MODE, GL_LINEAR);
176 glFogf(GL_FOG_START, 0.0);
177 glFogf(GL_FOG_END, visibility);
178 break;
179
180 case FOG:
181 glEnable(GL_FOG);
182 glFogf(GL_FOG_MODE, GL_EXP);
183 glFogf(GL_FOG_DENSITY, computeDensity(visibility, FALSE));
184 break;
185
186 case SMOKE:
187 glFogf(GL_FOG_MODE, GL_EXP2);
188 glFogf(GL_FOG_DENSITY, computeDensity(visibility, TRUE));
189 break;
190 }
191 }
192 }
193
194 ////////////////////////////////////////////////////////////////////////
195 //
196 // Description:
197 // Does callback action thing.
198 //
199 // Use: extender
200 void
201 SoEnvironment::callback(SoCallbackAction *action)
202 //
203 ////////////////////////////////////////////////////////////////////////
204 {
205 SoLightAttenuationElement::set(action->getState(), this,
206 attenuation.getValue());
207 }
208
209 ////////////////////////////////////////////////////////////////////////
210 //
211 // Description:
212 // Computes fog density based on visibility.
213 //
214 // Use: private
215
216 float
217 SoEnvironment::computeDensity(float visibility, SbBool squared)
218 //
219 ////////////////////////////////////////////////////////////////////////
220 {
221 //
222 // We want nearly total opacity at a distance of "visibility" from
223 // the eye. The exponential GL fog function is:
224 //
225 // f = e ** (-density * distance)
226 //
227 // (the exponent is squared in the SMOKE case)
228 //
229 // Since this function approaches 0 asymptotically, we have to
230 // choose a reasonable cutoff point that approximates total
231 // opacity. e ** -4 is about 0.018, so all we have to do is make
232 // the exponent equal to -4 at a distance of "visibility".
233 //
234
235 if (squared)
236 return 2.0 / visibility;
237
238 else
239 return 4.0 / visibility;
240 }