Debian Codegraph @

lib/database/src/so/elements/SoGLLightIdElement.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 | SoGLLightIdElement
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/elements/SoGLLightIdElement.h>
56
57 SO_ELEMENT_SOURCE(SoGLLightIdElement);
58
59 // This holds the maximum number of concurrent GL sources
60 int SoGLLightIdElement::maxGLSources = -1;
61
62 ////////////////////////////////////////////////////////////////////////
63 //
64 // Description:
65 // Destructor.
66 //
67 // Use: private
68
69 SoGLLightIdElement::~SoGLLightIdElement()
70 //
71 ////////////////////////////////////////////////////////////////////////
72 {
73 }
74
75 ////////////////////////////////////////////////////////////////////////
76 //
77 // Description:
78 // Initializes element
79 //
80 // Use: public
81
82 void
83 SoGLLightIdElement::init(SoState *)
84 //
85 ////////////////////////////////////////////////////////////////////////
86 {
87 data = getDefault();
88 }
89
90 ////////////////////////////////////////////////////////////////////////
91 //
92 // Description:
93 // Increments the current light id. This should be called when a
94 // light source becomes active. This returns the new id, or -1 if
95 // the element is being overridden or if the maximum number of GL
96 // light sources has been exceeded. Otherwise, enables light source.
97 //
98 // Use: public, static
99
100 int
101 SoGLLightIdElement::increment(SoState *state)
102 //
103 ////////////////////////////////////////////////////////////////////////
104 {
105 SoGLLightIdElement *elt;
106
107 // Get an instance we can change (pushing if necessary)
108 elt = (SoGLLightIdElement *) getElement(state, classStackIndex);
109
110 // Being overridden?
111 if (elt == NULL)
112 return -1;
113
114 // Increment the current data in the element. Since we initialize
115 // this to -1 in init() and copy it in push(), the data field is
116 // always up to date. This makes it possible to increment() twice
117 // without a push() in between.
118 elt->data++;
119
120 // Too many sources?
121 if (elt->data >= getMaxGLSources())
122 return -1;
123
124 // It's a valid source, so enable it
125 glEnable((GLenum)(GL_LIGHT0 + elt->data));
126
127 return elt->data;
128 }
129
130 ////////////////////////////////////////////////////////////////////////
131 //
132 // Description:
133 // Returns the maximum number of concurrent light sources supported
134 // by GL implementation.
135 //
136 // Use: protected, static
137
138 int
139 SoGLLightIdElement::getMaxGLSources()
140 //
141 ////////////////////////////////////////////////////////////////////////
142 {
143 // Inquire GL if not already done
144 if (maxGLSources < 0) {
145 GLint max;
146 glGetIntegerv(GL_MAX_LIGHTS, &max);
147 maxGLSources = max;
148 }
149
150 return maxGLSources;
151 }
152
153 ////////////////////////////////////////////////////////////////////////
154 //
155 // Description:
156 // Pushes element, copying current id from previous top instance.
157 //
158 // Use: public
159
160 void
161 SoGLLightIdElement::push(SoState *)
162 //
163 ////////////////////////////////////////////////////////////////////////
164 {
165 data = ((SoGLLightIdElement *) getNextInStack())->data;
166 }
167
168 ////////////////////////////////////////////////////////////////////////
169 //
170 // Description:
171 // Pops element, disabling light in GL
172 //
173 // Use: public
174
175 void
176 SoGLLightIdElement::pop(SoState *state, const SoElement *prevTopElement)
177 //
178 ////////////////////////////////////////////////////////////////////////
179 {
180 // Since popping this element has GL side effects, make sure any
181 // open caches capture it
182 capture(state);
183
184 const SoGLLightIdElement *prevElt =
185 (const SoGLLightIdElement *) prevTopElement;
186 int i, max;
187
188 // Disable previous light(s), if valid. All lights between the
189 // previous element and this one should be turned off.
190 max = getMaxGLSources();
191 for (i = (int) prevElt->data; i > data; i--)
192 if (i < max)
193 glDisable((GLenum)(GL_LIGHT0 + i));
194 }
195
196 ////////////////////////////////////////////////////////////////////////
197 //
198 // Description:
199 // Prints element for debugging.
200 //
201 // Use: public
202 //
203 ////////////////////////////////////////////////////////////////////////
204
205 #ifdef DEBUG
206 void
207 SoGLLightIdElement::print(FILE *fp) const
208 {
209 SoElement::print(fp);
210
211 fprintf(fp, "\tLight id = %" PRId32 "\n", data);
212 }
213 #else /* DEBUG */
214 void
215 SoGLLightIdElement::print(FILE *) const
216 {
217 }
218 #endif /* DEBUG */