| 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 $ |
| 44 |
| |
| 45 |
| Classes: |
| 46 |
| SoGLTextureCoordinateElement class. |
| 47 |
| |
| 48 |
| Author(s) : Paul S. Strauss, Nick Thompson, Thad Beier, |
| 49 |
| Gavin Bell |
| 50 |
| |
| 51 |
______________ S I L I C O N G R A P H I C S I N C . ____________ |
| 52 |
_______________________________________________________________________ |
| 53 |
*/ |
| 54 |
|
| 55 |
#include <GL/gl.h> |
| 56 |
#include <Inventor/elements/SoGLTextureCoordinateElement.h> |
| 57 |
#include <Inventor/errors/SoDebugError.h> |
| 58 |
|
| 59 |
SO_ELEMENT_SOURCE(SoGLTextureCoordinateElement); |
| 60 |
|
| 61 |
//////////////////////////////////////////////////////////////////////// |
| 62 |
// |
| 63 |
// Description: |
| 64 |
// Destructor. |
| 65 |
// |
| 66 |
// Use: private |
| 67 |
|
| 68 |
SoGLTextureCoordinateElement::~SoGLTextureCoordinateElement() |
| 69 |
// |
| 70 |
//////////////////////////////////////////////////////////////////////// |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
//////////////////////////////////////////////////////////////////////// |
| 75 |
// |
| 76 |
// Description: |
| 77 |
// Initializes element. |
| 78 |
// |
| 79 |
// Use: public |
| 80 |
|
| 81 |
void |
| 82 |
SoGLTextureCoordinateElement::init(SoState *s) |
| 83 |
// |
| 84 |
//////////////////////////////////////////////////////////////////////// |
| 85 |
{ |
| 86 |
SoTextureCoordinateElement::init(s); |
| 87 |
|
| 88 |
texgenCB = NULL; |
| 89 |
texgenCBData = NULL; |
| 90 |
copiedFromParent = NULL; |
| 91 |
} |
| 92 |
|
| 93 |
//////////////////////////////////////////////////////////////////////// |
| 94 |
// |
| 95 |
// Description: |
| 96 |
// TexGen is being used |
| 97 |
// |
| 98 |
// Use: public |
| 99 |
|
| 100 |
void |
| 101 |
SoGLTextureCoordinateElement::setTexGen(SoState *state, SoNode *node, |
| 102 |
SoTexCoordTexgenCB *texGenFunc, void *texGenData, |
| 103 |
SoTextureCoordinateFunctionCB *func, void *funcData) |
| 104 |
// |
| 105 |
//////////////////////////////////////////////////////////////////////// |
| 106 |
{ |
| 107 |
// Do base-class stuff |
| 108 |
SoTextureCoordinateElement::setFunction(state, node, func, |
| 109 |
funcData); |
| 110 |
|
| 111 |
// Get an element we can modify: |
| 112 |
SoGLTextureCoordinateElement *elt; |
| 113 |
elt = (SoGLTextureCoordinateElement *) |
| 114 |
getElement(state, classStackIndex, node); |
| 115 |
|
| 116 |
if (elt != NULL) { |
| 117 |
|
| 118 |
elt->setElt(texGenFunc, texGenData); |
| 119 |
|
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
//////////////////////////////////////////////////////////////////////// |
| 124 |
// |
| 125 |
// Description: |
| 126 |
// TexGen is being used |
| 127 |
// |
| 128 |
// Use: protected |
| 129 |
|
| 130 |
void |
| 131 |
SoGLTextureCoordinateElement::setElt(SoTexCoordTexgenCB *func, |
| 132 |
void *userData) |
| 133 |
// |
| 134 |
//////////////////////////////////////////////////////////////////////// |
| 135 |
{ |
| 136 |
// Enable or disable texgen as appropriate |
| 137 |
if (func != NULL) { |
| 138 |
// Only call Enable if not already enabled: |
| 139 |
if (texgenCB == NULL) { |
| 140 |
glEnable(GL_TEXTURE_GEN_S); |
| 141 |
glEnable(GL_TEXTURE_GEN_T); |
| 142 |
copiedFromParent = NULL; |
| 143 |
} |
| 144 |
else if (copiedFromParent) { |
| 145 |
SoGLTextureCoordinateElement *parent = |
| 146 |
(SoGLTextureCoordinateElement *) getNextInStack(); |
| 147 |
parent->capture(copiedFromParent); |
| 148 |
} |
| 149 |
|
| 150 |
// Call function to set up texgen parameters |
| 151 |
(*func)(userData); |
| 152 |
|
| 153 |
whatKind = FUNCTION; |
| 154 |
} |
| 155 |
else { |
| 156 |
// Only call Disable if parent element was enabled: |
| 157 |
if (texgenCB != NULL) { |
| 158 |
glDisable(GL_TEXTURE_GEN_S); |
| 159 |
glDisable(GL_TEXTURE_GEN_T); |
| 160 |
copiedFromParent = NULL; |
| 161 |
} |
| 162 |
else if (copiedFromParent) { |
| 163 |
SoGLTextureCoordinateElement *parent = |
| 164 |
(SoGLTextureCoordinateElement *) getNextInStack(); |
| 165 |
parent->capture(copiedFromParent); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
texgenCB = func; |
| 170 |
texgenCBData = userData; |
| 171 |
} |
| 172 |
|
| 173 |
//////////////////////////////////////////////////////////////////////// |
| 174 |
// |
| 175 |
// Description: |
| 176 |
// Returns code depending on which set routine was called. |
| 177 |
// |
| 178 |
// Use: public, virtual |
| 179 |
|
| 180 |
SoGLTextureCoordinateElement::CoordType |
| 181 |
SoGLTextureCoordinateElement::getType() const |
| 182 |
// |
| 183 |
//////////////////////////////////////////////////////////////////////// |
| 184 |
{ |
| 185 |
return whatKind; |
| 186 |
} |
| 187 |
|
| 188 |
//////////////////////////////////////////////////////////////////////// |
| 189 |
// |
| 190 |
// Description: |
| 191 |
// Returns the top (current) instance of the element in the state. |
| 192 |
// |
| 193 |
// Use: public |
| 194 |
|
| 195 |
const SoGLTextureCoordinateElement * |
| 196 |
SoGLTextureCoordinateElement::getInstance(SoState *state) |
| 197 |
// |
| 198 |
//////////////////////////////////////////////////////////////////////// |
| 199 |
{ |
| 200 |
return (const SoGLTextureCoordinateElement *) |
| 201 |
getConstElement(state, classStackIndex); |
| 202 |
} |
| 203 |
|
| 204 |
//////////////////////////////////////////////////////////////////////// |
| 205 |
// |
| 206 |
// Description: |
| 207 |
// Given an index, send the appropriate stuff to GL. |
| 208 |
// |
| 209 |
// Use: public |
| 210 |
|
| 211 |
void |
| 212 |
SoGLTextureCoordinateElement::send(int index) const |
| 213 |
// |
| 214 |
//////////////////////////////////////////////////////////////////////// |
| 215 |
{ |
| 216 |
#ifdef DEBUG |
| 217 |
if (whatKind != EXPLICIT) |
| 218 |
SoDebugError::post("SoGLTextureCoordinateElement::send", |
| 219 |
"explicit texture coordinates were not set!"); |
| 220 |
|
| 221 |
if (index < 0 || index >= numCoords) |
| 222 |
SoDebugError::post("SoGLTextureCoordinateElement::send", |
| 223 |
"Index (%d) out of range 0 - %d", |
| 224 |
index, numCoords - 1); |
| 225 |
#endif /* DEBUG */ |
| 226 |
|
| 227 |
if (coordsAre2D) |
| 228 |
glTexCoord2fv(coords2[index].getValue()); |
| 229 |
else |
| 230 |
glTexCoord4fv(coords4[index].getValue()); |
| 231 |
} |
| 232 |
|
| 233 |
//////////////////////////////////////////////////////////////////////// |
| 234 |
// |
| 235 |
// Description: |
| 236 |
// Pushes element, initializing new instance. |
| 237 |
// |
| 238 |
// Use: public |
| 239 |
|
| 240 |
void |
| 241 |
SoGLTextureCoordinateElement::push(SoState *state) |
| 242 |
// |
| 243 |
//////////////////////////////////////////////////////////////////////// |
| 244 |
{ |
| 245 |
// Copy texgen function and data from previous instance |
| 246 |
const SoGLTextureCoordinateElement *prevElt = |
| 247 |
(const SoGLTextureCoordinateElement *) getNextInStack(); |
| 248 |
texgenCB = prevElt->texgenCB; |
| 249 |
texgenCBData = prevElt->texgenCBData; |
| 250 |
|
| 251 |
copiedFromParent = state; |
| 252 |
} |
| 253 |
|
| 254 |
//////////////////////////////////////////////////////////////////////// |
| 255 |
// |
| 256 |
// Description: |
| 257 |
// Pops element, causing side effects in GL if necessary. This |
| 258 |
// code is a little convoluted-- we're trying pretty hard not to |
| 259 |
// send GL commands if we don't have to. |
| 260 |
// |
| 261 |
// Use: public |
| 262 |
|
| 263 |
void |
| 264 |
SoGLTextureCoordinateElement::pop(SoState *state, const SoElement *childElt) |
| 265 |
// |
| 266 |
//////////////////////////////////////////////////////////////////////// |
| 267 |
{ |
| 268 |
// If the previous element didn't have the same value... |
| 269 |
const SoGLTextureCoordinateElement *child = |
| 270 |
(const SoGLTextureCoordinateElement *)childElt; |
| 271 |
|
| 272 |
// Since popping this element may have GL side effects, we must |
| 273 |
// capture the state. We may not send any GL commands, but |
| 274 |
// the cache dependency must exist even if we don't send any GL |
| 275 |
// commands, because if the element changes, the _lack_ of GL |
| 276 |
// commands here is a bug (remember, GL commands issued here are |
| 277 |
// put inside the cache). |
| 278 |
capture(state); |
| 279 |
copiedFromParent = NULL; |
| 280 |
|
| 281 |
// Different callbacks, must either disable texgen or resend: |
| 282 |
if (texgenCB != NULL) { |
| 283 |
|
| 284 |
// Enable if it wasn't enabled before: |
| 285 |
if (child->texgenCB == NULL) { |
| 286 |
glEnable(GL_TEXTURE_GEN_S); |
| 287 |
glEnable(GL_TEXTURE_GEN_T); |
| 288 |
} |
| 289 |
|
| 290 |
(*texgenCB)(texgenCBData); |
| 291 |
} |
| 292 |
else if (child->texgenCB != NULL) { |
| 293 |
glDisable(GL_TEXTURE_GEN_S); |
| 294 |
glDisable(GL_TEXTURE_GEN_T); |
| 295 |
} |
| 296 |
} |