1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
|
/**************************************************************************\
*
* This file is part of the Coin 3D visualization library.
* Copyright (C) by Kongsberg Oil & Gas Technologies.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* ("GPL") version 2 as published by the Free Software Foundation.
* See the file LICENSE.GPL at the root directory of this source
* distribution for additional information about the GNU GPL.
*
* For using Coin with software that can not be combined with the GNU
* GPL, and for taking advantage of the additional benefits of our
* support services, please contact Kongsberg Oil & Gas Technologies
* about acquiring a Coin Professional Edition License.
*
* See http://www.coin3d.org/ for more information.
*
* Kongsberg Oil & Gas Technologies, Bygdoy Alle 5, 0257 Oslo, NORWAY.
* http://www.sim.no/ sales@sim.no coin-support@coin3d.org
*
\**************************************************************************/
/*!
\class SoTexture3 SoTexture3.h Inventor/nodes/SoTexture3.h
\brief The SoTexture3 class is used to map a 3D texture onto geometry.
\ingroup nodes
Shape nodes within the scope of SoTexture3 nodes in the scenegraph
(ie below the same SoSeparator and to the righthand side of the
SoTexture3) will have the texture applied according to each shape
type's individual characteristics. See the documentation of the
various shape types (SoFaceSet, SoCube, SoSphere, etc etc) for
information about the specifics of how the textures will be applied.
An SoTexture3 node will override any previous encountered SoTexture2 nodes
and vice versa. Mixing of SoTexture3 and SoTextureCoordinate2 (or the other
way around) is legal, but the third texture coordinate component will
be ignored (set to 0.0).
<center>
<img src="http://doc.coin3d.org/images/SoGuiExamples/nodes/texture3.png">
</center>
\COIN_CLASS_EXTENSION
<b>FILE FORMAT/DEFAULTS:</b>
\code
Texture3 {
filenames ""
images 0 0 0 0
wrapR REPEAT
wrapS REPEAT
wrapT REPEAT
model MODULATE
blendColor 0 0 0
enableCompressedTexture FALSE
}
\endcode
\since Coin 2.0
\since TGS Inventor 2.6
*/
// *************************************************************************
#include <Inventor/nodes/SoTexture3.h>
#include <assert.h>
#include <string.h>
#include <Inventor/SoInput.h>
#include <Inventor/actions/SoCallbackAction.h>
#include <Inventor/actions/SoGLRenderAction.h>
#include <Inventor/elements/SoGLTexture3EnabledElement.h>
#include <Inventor/elements/SoGLCacheContextElement.h>
#include <Inventor/elements/SoGLTextureImageElement.h>
#include <Inventor/elements/SoTextureQualityElement.h>
#include <Inventor/elements/SoTextureOverrideElement.h>
#include <Inventor/errors/SoReadError.h>
#include <Inventor/misc/SoGLBigImage.h>
#include <Inventor/misc/SoGLDriverDatabase.h>
#include <Inventor/sensors/SoFieldSensor.h>
#include <Inventor/lists/SbStringList.h>
#include <Inventor/errors/SoDebugError.h>
#include <Inventor/SbImage.h>
#include <Inventor/C/glue/gl.h>
#include "nodes/SoSubNodeP.h"
#include "elements/SoTextureScalePolicyElement.h"
// *************************************************************************
/*!
\enum SoTexture3::Model
Texture mapping model.
*/
/*!
\var SoTexture3::Model SoTexture3::MODULATE
Texture image is modulated with polygon.
*/
/*!
\var SoTexture3::Model SoTexture3::DECAL
Texture image overwrites polygon color.
*/
/*!
\var SoTexture3::Model SoTexture3::BLEND
Blend image using blendColor.
*/
/*!
\enum SoTexture3::Wrap
Enum used to specify wrapping strategy.
*/
/*!
\var SoTexture3::Wrap SoTexture3::REPEAT
Repeat texture when coordinate is not between 0 and 1.
*/
/*!
\var SoTexture3::Wrap SoTexture3::CLAMP
Clamp coordinate between 0 and 1.
*/
/*!
\var SoMFString SoTexture3::filenames
Texture filename(s). Specify either this or use SoTexture3::images, not both.
The depth of the volume is specifies by the number of filenames specified.
All images must have the same dimensions and number of components.
NB! A field sensor is attached to this field internally and reloads all
images when this field changes. You must therefore be careful when
setting this field and either use startEditing()/finishEditing() or set
all values with one function call; setValues().
*/
/*!
\var SoSFImage3 SoTexture3::images
Inline image data.
*/
/*!
\var SoSFEnum SoTexture3::wrapR
Wrapping strategy for the R coordinate (depth).
*/
/*!
\var SoSFEnum SoTexture3::wrapS
Wrapping strategy for the S coordinate.
*/
/*!
\var SoSFEnum SoTexture3::wrapT
Wrapping strategy for the T coordinate.
*/
/*!
\var SoSFEnum SoTexture3::model
Texture model.
*/
/*!
\var SoSFColor SoTexture3::blendColor
Blend color. Used when SoTexture3::model is SoTexture3::BLEND.
*/
/*!
\var SoSFBool SoTexture3::enableCompressedTexture
Hint to Coin that compressed textures should be used if this
is supported by the graphics hardware and OpenGL drivers.
Using compressed textures usually reduces texture memory usage
for a texture by 4-6 times.
\since Coin 2.4.2
\since TGS Inventor 4.0
*/
// *************************************************************************
SO_NODE_SOURCE(SoTexture3);
/*!
Constructor.
*/
SoTexture3::SoTexture3(void)
{
SO_NODE_INTERNAL_CONSTRUCTOR(SoTexture3);
SO_NODE_ADD_FIELD(filenames, (""));
SO_NODE_ADD_FIELD(images, (SbVec3s(0, 0, 0), 0, NULL));
SO_NODE_ADD_FIELD(wrapR, (REPEAT));
SO_NODE_ADD_FIELD(wrapS, (REPEAT));
SO_NODE_ADD_FIELD(wrapT, (REPEAT));
SO_NODE_ADD_FIELD(model, (MODULATE));
SO_NODE_ADD_FIELD(blendColor, (0.0f, 0.0f, 0.0f));
SO_NODE_ADD_FIELD(enableCompressedTexture, (FALSE));
SO_NODE_DEFINE_ENUM_VALUE(Wrap, REPEAT);
SO_NODE_DEFINE_ENUM_VALUE(Wrap, CLAMP);
SO_NODE_SET_SF_ENUM_TYPE(wrapS, Wrap);
SO_NODE_SET_SF_ENUM_TYPE(wrapT, Wrap);
SO_NODE_SET_SF_ENUM_TYPE(wrapR, Wrap);
SO_NODE_DEFINE_ENUM_VALUE(Model, MODULATE);
SO_NODE_DEFINE_ENUM_VALUE(Model, DECAL);
SO_NODE_DEFINE_ENUM_VALUE(Model, BLEND);
SO_NODE_SET_SF_ENUM_TYPE(model, Model);
this->glimage = NULL;
this->glimagevalid = FALSE;
this->readstatus = 1;
// use field sensor for filename since we will load an image if
// filename changes. This is a time-consuming task which should
// not be done in notify().
this->filenamesensor = new SoFieldSensor(filenameSensorCB, this);
this->filenamesensor->setPriority(0);
this->filenamesensor->attach(&this->filenames);
}
/*!
Destructor.
*/
SoTexture3::~SoTexture3()
{
if (this->glimage) this->glimage->unref(NULL);
delete this->filenamesensor;
}
// doc from parent
void
SoTexture3::initClass(void)
{
SO_NODE_INTERNAL_INIT_CLASS(SoTexture3, SO_FROM_INVENTOR_2_6|SO_FROM_COIN_2_0);
SO_ENABLE(SoGLRenderAction, SoGLTextureImageElement);
SO_ENABLE(SoGLRenderAction, SoGLTexture3EnabledElement);
SO_ENABLE(SoCallbackAction, SoTextureImageElement);
}
// Documented in superclass.
SbBool
SoTexture3::readInstance(SoInput * in, unsigned short flags)
{
// Overridden to check if texture files (if any) can be found and
// loaded.
this->filenamesensor->detach();
SbBool readOK = inherited::readInstance(in, flags);
this->setReadStatus((int) readOK);
if (readOK && !filenames.isDefault() && filenames.getNum()>0) {
if (!this->loadFilenames(in)) {
this->setReadStatus(FALSE);
}
}
this->filenamesensor->attach(&this->filenames);
return readOK;
}
static SoGLImage::Wrap
translateWrap(const SoTexture3::Wrap wrap)
{
if (wrap == SoTexture3::REPEAT) return SoGLImage::REPEAT;
return SoGLImage::CLAMP;
}
// doc from parent
void
SoTexture3::GLRender(SoGLRenderAction * action)
{
SoState * state = action->getState();
const cc_glglue * glue = cc_glglue_instance((uint32_t) SoGLCacheContextElement::get(state));
if (!SoGLDriverDatabase::isSupported(glue, SO_GL_3D_TEXTURES)) {
static SbBool first = TRUE;
if (first) {
SoDebugError::postWarning("SoTexture3::GLRender",
"The current OpenGL context does not support 3D textures "
"(This warning message is only shown once, but "
"there could be more cases of this in the "
"scene graph.).");
first = FALSE;
}
return;
}
if (SoTextureOverrideElement::getImageOverride(state))
return;
float quality = SoTextureQualityElement::get(state);
if (!this->glimagevalid) {
int nc;
SbVec3s size;
const unsigned char *bytes = this->images.getValue(size, nc);
//FIXME: 3D support in SoGLBigImage (kintel 20011113)
// SbBool needbig =
// SoTextureScalePolicyElement::get(state) ==
// SoTextureScalePolicyElement::DONT_SCALE;
// if (needbig &&
// (this->glimage == NULL ||
// this->glimage->getTypeId() != SoGLBigImage::getClassTypeId())) {
// if (this->glimage) this->glimage->unref(state);
// this->glimage = new SoGLBigImage();
// }
// else if (!needbig &&
// (this->glimage == NULL ||
// this->glimage->getTypeId() != SoGLImage::getClassTypeId())) {
// if (this->glimage) this->glimage->unref(state);
// this->glimage = new SoGLImage();
// }
if (this->glimage) this->glimage->unref(state);
this->glimage = new SoGLImage();
if (this->enableCompressedTexture.getValue()) {
this->glimage->setFlags(this->glimage->getFlags()|
SoGLImage::COMPRESSED);
}
if (bytes && size != SbVec3s(0,0,0)) {
this->glimage->setData(bytes, size, nc,
translateWrap((Wrap)this->wrapS.getValue()),
translateWrap((Wrap)this->wrapT.getValue()),
translateWrap((Wrap)this->wrapR.getValue()),
quality);
this->glimagevalid = TRUE;
}
}
SoGLTexture3EnabledElement::set(state,
this, this->glimagevalid &&
quality > 0.0f);
SoGLTextureImageElement::set(state, this,
this->glimagevalid ? this->glimage : NULL,
(SoTextureImageElement::Model) model.getValue(),
this->blendColor.getValue());
if (this->isOverride()) {
SoTextureOverrideElement::setImageOverride(state, TRUE);
}
}
// doc from parent
void
SoTexture3::doAction(SoAction *action)
{
SoState *state = action->getState();
if (SoTextureOverrideElement::getImageOverride(state))
return;
int nc;
SbVec3s size;
const unsigned char *bytes = this->images.getValue(size, nc);
if (size != SbVec3s(0,0,0)) {
SoTextureImageElement::set(state, this,
size, nc, bytes,
(int)this->wrapT.getValue(),
(int)this->wrapS.getValue(),
(int)this->wrapR.getValue(),
(SoTextureImageElement::Model) model.getValue(),
this->blendColor.getValue());
}
// if a filename has been set, but the file has not been loaded, supply
// a dummy texture image to make sure texture coordinates are generated.
else if (this->images.isDefault() &&
this->filenames.getNum()>0 &&
this->filenames[0].getLength()) {
static const unsigned char dummytex[] = {0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff};
SoTextureImageElement::set(state, this,
SbVec3s(2,2,2), 1, dummytex,
(int)this->wrapT.getValue(),
(int)this->wrapS.getValue(),
(int)this->wrapR.getValue(),
(SoTextureImageElement::Model) model.getValue(),
this->blendColor.getValue());
}
else {
SoTextureImageElement::setDefault(state, this);
}
if (this->isOverride()) {
SoTextureOverrideElement::setImageOverride(state, TRUE);
}
}
// doc from parent
void
SoTexture3::callback(SoCallbackAction * action)
{
SoTexture3::doAction(action);
}
/*!
Returns read status. 1 for success, 0 for failure.
*/
int
SoTexture3::getReadStatus(void)
{
return this->readstatus;
}
/*!
Sets read status.
\sa getReadStatus()
*/
void
SoTexture3::setReadStatus(int s)
{
this->readstatus = s;
}
// Documented in superclass.
void
SoTexture3::notify(SoNotList * l)
{
// Overridden to detect when fields change.
SoField *f = l->getLastField();
if (f == &this->images) {
this->glimagevalid = FALSE;
this->filenames.setDefault(TRUE); // write image, not filename
}
else if (f == &this->wrapS || f == &this->wrapT || f == &this->wrapR) {
this->glimagevalid = FALSE;
}
inherited::notify(l);
}
//
// Called from readInstance() or when user changes the
// filenames field. \e in is set if this function is called
// while reading a scene graph.
//
//FIXME: Recalc so all images have same w, h and nc (kintel 20011201)
//FIXME: Rescale depth to be n^2 ? This might not work very well though
// if someone decides to add one layer at the time (kintel 20011201)
SbBool
SoTexture3::loadFilenames(SoInput * in)
{
SbBool retval = FALSE;
SbVec3s volumeSize(0,0,0);
int volumenc;
int numImages = this->filenames.getNum();
SbBool sizeError = FALSE;
int i;
// Fail on empty filenames
for (i=0;i<numImages;i++) if (this->filenames[i].getLength()==0) break;
if (i==numImages) { // All filenames valid
for (int n=0 ; n<numImages && !sizeError ; n++) {
SbString filename = this->filenames[n];
SbImage tmpimage;
const SbStringList &sl = SoInput::getDirectories();
if (tmpimage.readFile(filename, sl.getArrayPtr(), sl.getLength())) {
int nc;
SbVec3s size;
unsigned char *imgbytes = tmpimage.getValue(size, nc);
if (size[2]==0) size[2]=1;
if (this->images.isDefault()) { // First time => allocate memory
volumeSize.setValue(size[0],
size[1],
size[2]*numImages);
volumenc = nc;
this->images.setValue(volumeSize, nc, NULL);
}
else { // Verify size & components
if (size[0] != volumeSize[0] ||
size[1] != volumeSize[1] ||
//FIXME: always 1 or what? (kintel 20020110)
size[2] != (volumeSize[2]/numImages) ||
nc != volumenc) {
sizeError = TRUE;
retval = FALSE;
SbString errstr;
errstr.sprintf("Texture file #%d (%s) has wrong size:"
"Expected (%d,%d,%d,%d) got (%d,%d,%d,%d)\n",
n, filename.getString(),
volumeSize[0],volumeSize[1],volumeSize[2],
volumenc,
size[0],size[1],size[2],nc);
if (in) SoReadError::post(in, errstr.getString());
else SoDebugError::postWarning("SoTexture3::loadFilenames()",
errstr.getString());
}
}
if (!sizeError) {
// disable notification on images while setting data from the
// filenames as a notify will cause a filenames.setDefault(TRUE).
SbBool oldnotify = this->images.enableNotify(FALSE);
unsigned char *volbytes = this->images.startEditing(volumeSize,
volumenc);
memcpy(volbytes+int(size[0])*int(size[1])*int(size[2])*nc*n,
imgbytes, int(size[0])*int(size[1])*int(size[2])*nc);
this->images.finishEditing();
this->images.enableNotify(oldnotify);
this->glimagevalid = FALSE; // recreate GL images in next GLRender()
retval = TRUE;
}
}
else {
SbString errstr;
errstr.sprintf("Could not read texture file #%d: %s",
n, filename.getString());
if (in) SoReadError::post(in, errstr.getString());
else SoDebugError::postWarning("SoTexture3::loadFilenames()",
errstr.getString());
retval = FALSE;
}
}
}
//FIXME: If sizeError, invalidate texture? (kintel 20011113)
this->images.setDefault(TRUE); // write filenames, not images
return retval;
}
//
// called when \e filenames changes
//
void
SoTexture3::filenameSensorCB(void * data, SoSensor *)
{
SoTexture3 *thisp = (SoTexture3 *)data;
thisp->setReadStatus(TRUE);
if (thisp->filenames.getNum()<0 ||
thisp->filenames[0].getLength() &&
!thisp->loadFilenames()) {
thisp->setReadStatus(FALSE);
}
}
|