| 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 |
| SoTextureCoordinateElement 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 <Inventor/elements/SoTextureCoordinateElement.h> |
| 56 |
#include <Inventor/errors/SoDebugError.h> |
| 57 |
#include <Inventor/elements/SoShapeStyleElement.h> |
| 58 |
|
| 59 |
SO_ELEMENT_SOURCE(SoTextureCoordinateElement); |
| 60 |
|
| 61 |
//////////////////////////////////////////////////////////////////////// |
| 62 |
// |
| 63 |
// Description: |
| 64 |
// Destructor. |
| 65 |
// |
| 66 |
// Use: private |
| 67 |
|
| 68 |
SoTextureCoordinateElement::~SoTextureCoordinateElement() |
| 69 |
// |
| 70 |
//////////////////////////////////////////////////////////////////////// |
| 71 |
{ |
| 72 |
} |
| 73 |
|
| 74 |
//////////////////////////////////////////////////////////////////////// |
| 75 |
// |
| 76 |
// Description: |
| 77 |
// Initializes element. |
| 78 |
// |
| 79 |
// Use: public |
| 80 |
|
| 81 |
void |
| 82 |
SoTextureCoordinateElement::init(SoState *state) |
| 83 |
// |
| 84 |
//////////////////////////////////////////////////////////////////////// |
| 85 |
{ |
| 86 |
SoReplacedElement::init(state); |
| 87 |
|
| 88 |
whatKind = EXPLICIT; |
| 89 |
funcCB = NULL; |
| 90 |
numCoords = 0; |
| 91 |
coords2 = NULL; |
| 92 |
coords4 = NULL; |
| 93 |
} |
| 94 |
|
| 95 |
//////////////////////////////////////////////////////////////////////// |
| 96 |
// |
| 97 |
// Description: |
| 98 |
// Tell shapes to generate their own texture coordinates. This is |
| 99 |
// the default. |
| 100 |
// |
| 101 |
// Use: public |
| 102 |
|
| 103 |
void |
| 104 |
SoTextureCoordinateElement::setDefault(SoState *state, SoNode *node) |
| 105 |
// |
| 106 |
//////////////////////////////////////////////////////////////////////// |
| 107 |
{ |
| 108 |
SoTextureCoordinateElement *elt; |
| 109 |
|
| 110 |
elt = (SoTextureCoordinateElement *) |
| 111 |
getElement(state, classStackIndex, node); |
| 112 |
|
| 113 |
if (elt != NULL) { |
| 114 |
elt->whatKind = EXPLICIT; |
| 115 |
elt->numCoords = 0; |
| 116 |
elt->coords2 = NULL; |
| 117 |
elt->coords4 = NULL; |
| 118 |
} |
| 119 |
//The shapeStyle element will track this value: |
| 120 |
SoShapeStyleElement::setTextureFunction(state, FALSE); |
| 121 |
} |
| 122 |
|
| 123 |
//////////////////////////////////////////////////////////////////////// |
| 124 |
// |
| 125 |
// Description: |
| 126 |
// Sets the current coords, given a function to compute them. |
| 127 |
// |
| 128 |
// Use: public |
| 129 |
|
| 130 |
void |
| 131 |
SoTextureCoordinateElement::setFunction(SoState *state, SoNode *node, |
| 132 |
SoTextureCoordinateFunctionCB *func, |
| 133 |
void *userData) |
| 134 |
// |
| 135 |
//////////////////////////////////////////////////////////////////////// |
| 136 |
{ |
| 137 |
SoTextureCoordinateElement *elt; |
| 138 |
|
| 139 |
elt = (SoTextureCoordinateElement *) getElement(state, |
| 140 |
classStackIndex, node); |
| 141 |
if (elt != NULL) { |
| 142 |
elt->whatKind = FUNCTION; |
| 143 |
elt->funcCB = func; |
| 144 |
elt->funcCBData = userData; |
| 145 |
} |
| 146 |
//The shapeStyle element will track this value: |
| 147 |
SoShapeStyleElement::setTextureFunction(state, TRUE); |
| 148 |
} |
| 149 |
|
| 150 |
//////////////////////////////////////////////////////////////////////// |
| 151 |
// |
| 152 |
// Description: |
| 153 |
// Sets the current coords, given a set of 2D texture coordinates. |
| 154 |
// |
| 155 |
// Use: public |
| 156 |
|
| 157 |
void |
| 158 |
SoTextureCoordinateElement::set2(SoState *state, SoNode *node, |
| 159 |
int32_t numCoords, const SbVec2f *coords) |
| 160 |
// |
| 161 |
//////////////////////////////////////////////////////////////////////// |
| 162 |
{ |
| 163 |
SoTextureCoordinateElement *elt; |
| 164 |
|
| 165 |
elt = (SoTextureCoordinateElement *) |
| 166 |
getElement(state, classStackIndex, node); |
| 167 |
|
| 168 |
if (elt != NULL) { |
| 169 |
elt->whatKind = EXPLICIT; |
| 170 |
elt->numCoords = numCoords; |
| 171 |
elt->coords2 = coords; |
| 172 |
elt->coordsAre2D = TRUE; |
| 173 |
} |
| 174 |
SoShapeStyleElement::setTextureFunction(state, FALSE); |
| 175 |
} |
| 176 |
|
| 177 |
//////////////////////////////////////////////////////////////////////// |
| 178 |
// |
| 179 |
// Description: |
| 180 |
// Sets the current coords, given a set of 4D texture coordinates. |
| 181 |
// |
| 182 |
// Use: public |
| 183 |
|
| 184 |
void |
| 185 |
SoTextureCoordinateElement::set4(SoState *state, SoNode *node, |
| 186 |
int32_t numCoords, const SbVec4f *coords) |
| 187 |
// |
| 188 |
//////////////////////////////////////////////////////////////////////// |
| 189 |
{ |
| 190 |
SoTextureCoordinateElement *elt; |
| 191 |
|
| 192 |
elt = (SoTextureCoordinateElement *) |
| 193 |
getElement(state, classStackIndex, node); |
| 194 |
|
| 195 |
if (elt != NULL) { |
| 196 |
elt->whatKind = EXPLICIT; |
| 197 |
elt->numCoords = numCoords; |
| 198 |
elt->coords4 = coords; |
| 199 |
elt->coordsAre2D = FALSE; |
| 200 |
} |
| 201 |
SoShapeStyleElement::setTextureFunction(state, FALSE); |
| 202 |
} |
| 203 |
|
| 204 |
//////////////////////////////////////////////////////////////////////// |
| 205 |
// |
| 206 |
// Description: |
| 207 |
// Returns code depending on which set routine was called. |
| 208 |
// |
| 209 |
// Use: public, static |
| 210 |
|
| 211 |
SoTextureCoordinateElement::CoordType |
| 212 |
SoTextureCoordinateElement::getType(SoState *state) |
| 213 |
// |
| 214 |
//////////////////////////////////////////////////////////////////////// |
| 215 |
{ |
| 216 |
return getInstance(state)->getType(); |
| 217 |
} |
| 218 |
|
| 219 |
//////////////////////////////////////////////////////////////////////// |
| 220 |
// |
| 221 |
// Description: |
| 222 |
// Non-static (virtual) version of above method. |
| 223 |
// |
| 224 |
// Use: public |
| 225 |
|
| 226 |
SoTextureCoordinateElement::CoordType |
| 227 |
SoTextureCoordinateElement::getType() const |
| 228 |
// |
| 229 |
//////////////////////////////////////////////////////////////////////// |
| 230 |
{ |
| 231 |
return whatKind; |
| 232 |
} |
| 233 |
|
| 234 |
//////////////////////////////////////////////////////////////////////// |
| 235 |
// |
| 236 |
// Description: |
| 237 |
// Returns the top (current) instance of the element in the state. |
| 238 |
// |
| 239 |
// Use: public |
| 240 |
|
| 241 |
const SoTextureCoordinateElement * |
| 242 |
SoTextureCoordinateElement::getInstance(SoState *state) |
| 243 |
// |
| 244 |
//////////////////////////////////////////////////////////////////////// |
| 245 |
{ |
| 246 |
return (const SoTextureCoordinateElement *) |
| 247 |
getConstElement(state, classStackIndex); |
| 248 |
} |
| 249 |
|
| 250 |
//////////////////////////////////////////////////////////////////////// |
| 251 |
// |
| 252 |
// Description: |
| 253 |
// Given a point and a normal, call the registered callback to get |
| 254 |
// corresponding texture coordinates: |
| 255 |
// |
| 256 |
// Use: public |
| 257 |
|
| 258 |
const SbVec4f & |
| 259 |
SoTextureCoordinateElement::get(const SbVec3f &point, |
| 260 |
const SbVec3f &normal) const |
| 261 |
// |
| 262 |
//////////////////////////////////////////////////////////////////////// |
| 263 |
{ |
| 264 |
#ifdef DEBUG |
| 265 |
if (whatKind != FUNCTION) |
| 266 |
SoDebugError::post("SoTextureCoordinateElement::get", |
| 267 |
"Functional texture coordinates were not set!"); |
| 268 |
#endif /* DEBUG */ |
| 269 |
return (*funcCB)(funcCBData, point, normal); |
| 270 |
} |
| 271 |
|
| 272 |
//////////////////////////////////////////////////////////////////////// |
| 273 |
// |
| 274 |
// Description: |
| 275 |
// Returns the indexed coordinate from an element as an SbVec2f. |
| 276 |
// |
| 277 |
// Use: public |
| 278 |
|
| 279 |
const SbVec2f & |
| 280 |
SoTextureCoordinateElement::get2(int index) const |
| 281 |
// |
| 282 |
//////////////////////////////////////////////////////////////////////// |
| 283 |
{ |
| 284 |
#ifdef DEBUG |
| 285 |
if (whatKind != EXPLICIT) |
| 286 |
SoDebugError::post("SoTextureCoordinateElement::get2", |
| 287 |
"Explicit texture coordinates were not set!"); |
| 288 |
|
| 289 |
if (index < 0 || index >= numCoords) |
| 290 |
SoDebugError::post("SoTextureCoordinateElement::get2", |
| 291 |
"Index (%d) is out of range 0 - %d", |
| 292 |
index, numCoords - 1); |
| 293 |
#endif /* DEBUG */ |
| 294 |
|
| 295 |
if (coordsAre2D) |
| 296 |
return coords2[index]; |
| 297 |
|
| 298 |
// Convert from 4-D if necessary |
| 299 |
else { |
| 300 |
// Cast the const away... |
| 301 |
SoTextureCoordinateElement *elt = (SoTextureCoordinateElement *) this; |
| 302 |
const SbVec4f &c4 = coords4[index]; |
| 303 |
|
| 304 |
// If we can't do the projection, or we don't have to |
| 305 |
if (c4[3] == 0.0 || c4[3] == 1.0) { |
| 306 |
elt->convert2[0] = c4[0]; |
| 307 |
elt->convert2[1] = c4[1]; |
| 308 |
} |
| 309 |
|
| 310 |
else { |
| 311 |
elt->convert2[0] = c4[0] / c4[3]; |
| 312 |
elt->convert2[1] = c4[1] / c4[3]; |
| 313 |
} |
| 314 |
|
| 315 |
return convert2; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
//////////////////////////////////////////////////////////////////////// |
| 320 |
// |
| 321 |
// Description: |
| 322 |
// Returns the indexed coordinate from an element as an SbVec4f. |
| 323 |
// |
| 324 |
// Use: public |
| 325 |
|
| 326 |
const SbVec4f & |
| 327 |
SoTextureCoordinateElement::get4(int index) const |
| 328 |
// |
| 329 |
//////////////////////////////////////////////////////////////////////// |
| 330 |
{ |
| 331 |
#ifdef DEBUG |
| 332 |
if (whatKind != EXPLICIT) |
| 333 |
SoDebugError::post("SoTextureCoordinateElement::get4", |
| 334 |
"Explicit texture coordinates were not set!"); |
| 335 |
|
| 336 |
if (index < 0 || index >= numCoords) |
| 337 |
SoDebugError::post("SoTextureCoordinateElement::get4", |
| 338 |
"Index (%d) is out of range 0 - %d", |
| 339 |
index, numCoords - 1); |
| 340 |
#endif /* DEBUG */ |
| 341 |
|
| 342 |
// Convert from 2-D if necessary |
| 343 |
if (coordsAre2D) { |
| 344 |
// Cast the const away... |
| 345 |
SoTextureCoordinateElement *elt = (SoTextureCoordinateElement *) this; |
| 346 |
const SbVec2f &c2 = coords2[index]; |
| 347 |
|
| 348 |
elt->convert4[0] = c2[0]; |
| 349 |
elt->convert4[1] = c2[1]; |
| 350 |
elt->convert4[2] = 0.0; |
| 351 |
elt->convert4[3] = 1.0; |
| 352 |
|
| 353 |
return convert4; |
| 354 |
} |
| 355 |
|
| 356 |
else |
| 357 |
return coords4[index]; |
| 358 |
} |
| 359 |
|
| 360 |
//////////////////////////////////////////////////////////////////////// |
| 361 |
// |
| 362 |
// Description: |
| 363 |
// Prints contents of element for debugging. |
| 364 |
// |
| 365 |
// Use: public |
| 366 |
// |
| 367 |
//////////////////////////////////////////////////////////////////////// |
| 368 |
|
| 369 |
#ifdef DEBUG |
| 370 |
void |
| 371 |
SoTextureCoordinateElement::print(FILE *fp) const |
| 372 |
{ |
| 373 |
SoReplacedElement::print(fp); |
| 374 |
} |
| 375 |
#else /* DEBUG */ |
| 376 |
void |
| 377 |
SoTextureCoordinateElement::print(FILE *) const |
| 378 |
{ |
| 379 |
} |
| 380 |
#endif /* DEBUG */ |
| 381 |
|