1 /**************************************************************************\
2 * Copyright (c) Kongsberg Oil & Gas Technologies AS
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32
33 /*!
34 \class SoGLUpdateAreaElement Inventor/elements/SoGLUpdateAreaElement.h
35 \brief The SoGLUpdateAreaElement class is yet to be documented.
36
37 \ingroup elements
38
39 FIXME: write doc.
40 */
41
42 #include <Inventor/elements/SoGLUpdateAreaElement.h>
43 #include <Inventor/elements/SoViewportRegionElement.h>
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif // HAVE_CONFIG_H
48
49 #include <Inventor/system/gl.h>
50
51 /*!
52 \fn SoGLUpdateAreaElement::origin
53
54 FIXME: write doc
55 */
56
57 /*!
58 \fn SoGLUpdateAreaElement::size
59
60 FIXME: write doc
61 */
62
63 SO_ELEMENT_SOURCE(SoGLUpdateAreaElement);
64
65 /*!
66 This static method initializes static data for the
67 SoGLUpdateAreaElement class.
68 */
69
70 void
initClass(void)71 SoGLUpdateAreaElement::initClass(void)
72 {
73 SO_ELEMENT_INIT_CLASS(SoGLUpdateAreaElement, inherited);
74 }
75
76 /*!
77 The destructor.
78 */
79
~SoGLUpdateAreaElement()80 SoGLUpdateAreaElement::~SoGLUpdateAreaElement()
81 {
82 }
83
84 // doc from parent
85 void
init(SoState * state)86 SoGLUpdateAreaElement::init(SoState * state)
87 {
88 inherited::init(state);
89 this->origin = getDefaultOrigin();
90 this->size = getDefaultSize();
91
92 // set these to dummy values. scissor test will be disabled
93 this->screensize.setValue(0, 0);
94 this->screenorigin.setValue(0, 0);
95
96 // scissorstate is used to keep track of current scissor
97 // test state.
98 this->scissorstate = FALSE;
99
100 // disabled by default
101 glDisable(GL_SCISSOR_TEST);
102 }
103
104 // doc from parent
105 void
push(SoState * state)106 SoGLUpdateAreaElement::push(SoState * state)
107 {
108 inherited::push(state);
109 SoGLUpdateAreaElement * prev = (SoGLUpdateAreaElement*)
110 this->getNextInStack();
111 this->scissorstate = prev->scissorstate;
112 }
113
114 // doc from parent
115 void
pop(SoState * state,const SoElement * prevTopElement)116 SoGLUpdateAreaElement::pop(SoState * state,
117 const SoElement * prevTopElement)
118 {
119 this->scissorstate = ((SoGLUpdateAreaElement*)prevTopElement)->scissorstate;
120 this->updategl();
121 inherited::pop(state, prevTopElement);
122 }
123
124 // doc from parent
125 SbBool
matches(const SoElement * element) const126 SoGLUpdateAreaElement::matches(const SoElement * element) const
127 {
128 const SoGLUpdateAreaElement * elem = (SoGLUpdateAreaElement*) element;
129 return
130 this->origin == elem->origin &&
131 this->size == elem->size;
132 }
133
134 // doc from parent
135 SoElement *
copyMatchInfo() const136 SoGLUpdateAreaElement::copyMatchInfo() const
137 {
138 SoGLUpdateAreaElement * elem = (SoGLUpdateAreaElement*) this->getTypeId().createInstance();
139 elem->origin = this->origin;
140 elem->size = this->size;
141 return elem;
142 }
143
144
145 /*!
146 Sets the update area. This can, for instance, be used when
147 rendering in the front buffer, to only render parts of the scene
148 during a window expose event. \a origin and \a size is in
149 normalized window coordinates.
150 */
151 void
set(SoState * const state,const SbVec2f & origin,const SbVec2f & size)152 SoGLUpdateAreaElement::set(SoState * const state,
153 const SbVec2f & origin,
154 const SbVec2f & size)
155 {
156 SoGLUpdateAreaElement * e = (SoGLUpdateAreaElement *)
157 inherited::getElement(state, SoGLUpdateAreaElement::classStackIndex);
158 if (e) {
159 e->origin = origin;
160 e->size = size;
161 SbVec2s winsize = SoViewportRegionElement::get(state).getWindowSize();
162 e->screenorigin[0] = (short) (origin[0]*float(winsize[0]));
163 e->screenorigin[1] = (short) (origin[1]*float(winsize[1]));
164 e->screensize[0] = (short) (size[0]*float(winsize[0]));
165 e->screensize[1] = (short) (size[1]*float(winsize[1]));
166
167 e->updategl();
168 }
169 }
170
171
172 /*!
173 Returns the current update area.
174
175 \sa set()
176 */
177 SbBool
get(SoState * const state,SbVec2f & origin,SbVec2f & size)178 SoGLUpdateAreaElement::get(SoState * const state,
179 SbVec2f & origin,
180 SbVec2f & size)
181 {
182 const SoGLUpdateAreaElement * e = (const SoGLUpdateAreaElement *)
183 inherited::getConstElement(state, SoGLUpdateAreaElement::classStackIndex);
184 origin = e->origin;
185 size = e->size;
186
187 return e->isDefault();
188 }
189
190 /*!
191 Returns the default update area origin, (0,0).
192 */
193 SbVec2f
getDefaultOrigin(void)194 SoGLUpdateAreaElement::getDefaultOrigin(void)
195 {
196 return SbVec2f(0.0f, 0.0f);
197 }
198
199 /*!
200 Returns the default update area size, (1,1).
201 */
202 SbVec2f
getDefaultSize(void)203 SoGLUpdateAreaElement::getDefaultSize(void)
204 {
205 return SbVec2f(1.0f, 1.0f);
206 }
207
208 // return TRUE if element contains the default values
209 SbBool
isDefault(void) const210 SoGLUpdateAreaElement::isDefault(void) const
211 {
212 return
213 this->origin == getDefaultOrigin() &&
214 this->size == getDefaultSize();
215 }
216
217 //
218 // keeps GL in sync
219 //
220 void
updategl(void)221 SoGLUpdateAreaElement::updategl(void)
222 {
223 if (this->isDefault()) {
224 if (this->scissorstate) {
225 glDisable(GL_SCISSOR_TEST);
226 this->scissorstate = FALSE;
227 }
228 }
229 else {
230 if (!this->scissorstate) {
231 glEnable(GL_SCISSOR_TEST);
232 this->scissorstate = TRUE;
233 }
234 glScissor((GLint) this->screenorigin[0],
235 (GLint) this->screenorigin[1],
236 (GLint) this->screensize[0],
237 (GLint) this->screensize[1]);
238 }
239 }
240