TextureNode.cc 19.3 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
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
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                       *
 *   $Author$                                                      *
 *   $Date$                   *
 *                                                                           *
\*===========================================================================*/




//=============================================================================
//
//  CLASS TextureNode - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================


#include "TextureNode.hh"
#include <QGLWidget>

//== NAMESPACES ===============================================================


namespace ACG {
namespace SceneGraph {


//== IMPLEMENTATION ==========================================================


TextureNode::TextureNode( BaseNode*            _parent,
                          const std::string&   _name,
                          bool                 _texture_repeat,
                          GLint                _texture_filter )
      : BaseNode( _parent, _name ),
        textures_(),
        alpha_( 0 ),
        texture_repeat_( _texture_repeat ),
        tex_mode_( GL_MODULATE ),
        texture_filter_( _texture_filter ),
        mipmapping_globally_active_(true),
        last_mipmapping_status_(true),
        mipmapping_(true),
        activeTexture_(-1),
        open_volume_mesh_texture_draw_modes_(DrawModes::getDrawMode("Faces (textured)") | DrawModes::getDrawMode("Faces (textured and shaded)") )
{
//    open_volume_mesh_texture_draw_modes_  = DrawModes::getDrawMode("Faces (textured)");
//    open_volume_mesh_texture_draw_modes_ |= DrawModes::getDrawMode("Faces (textured and shaded)");
}


//----------------------------------------------------------------------------


TextureNode::~TextureNode()
{
  for (std::vector<TextureInfo>::iterator texturesIt = textures_.begin(); texturesIt != textures_.end(); ++texturesIt) {
    delete texturesIt->tex;
  }
  textures_.clear();
}



//----------------------------------------------------------------------------


bool
TextureNode::read(const char* _filename)
{
   // load to image
   QImage image;
   if ( !image.load( _filename ) )
   {
      std::cerr << "Cannot load texture " << _filename << "\n";
      return false;
   }

   set_texture( image );

   return true;
}


//----------------------------------------------------------------------------


void
TextureNode::applyGLSettings(  )
{
  // GL settings
  glPixelStorei( GL_UNPACK_ROW_LENGTH,  0 );
  glPixelStorei( GL_UNPACK_SKIP_ROWS,   0 );
  glPixelStorei( GL_UNPACK_SKIP_PIXELS, 0 );
  glPixelStorei( GL_UNPACK_ALIGNMENT,   1 );
  glPixelStorei( GL_PACK_ROW_LENGTH,    0 );
  glPixelStorei( GL_PACK_SKIP_ROWS,     0 );
  glPixelStorei( GL_PACK_SKIP_PIXELS,   0 );
  glPixelStorei( GL_PACK_ALIGNMENT,     1 );
}

void
TextureNode::applyTextureParameters( int _id )
{
  if ( texture_repeat_ ) {
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
  } else {
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );

    // Set BorderColor for Clamping
    const float borderColor[4] = {1.0, 1.0, 1.0, 1.0};
    glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
  }
  
  if((mipmapping_globally_active_ && mipmapping_ && textures_[_id].mipmapAvailable) &&
     (texture_filter_ == GL_LINEAR_MIPMAP_NEAREST ||
      texture_filter_ == GL_LINEAR_MIPMAP_LINEAR  ||
      texture_filter_ == GL_NEAREST_MIPMAP_LINEAR ||
      texture_filter_ == GL_NEAREST_MIPMAP_NEAREST)) {
      
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texture_filter_ );
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
      
  } else if(texture_filter_ == GL_LINEAR) {
      
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  
  } else {
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
      glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  }
  
}

//----------------------------------------------------------------------------

void TextureNode::enable_mipmapping() {
    
    if(!mipmapping_) {
        mipmapping_ = true;
        updateMipmaps(mipmapping_globally_active_);
    }
}

//----------------------------------------------------------------------------

void TextureNode::disable_mipmapping() {
    
    if(mipmapping_) {
        mipmapping_ = false;
        updateMipmaps(mipmapping_);
    }
}

//----------------------------------------------------------------------------

void TextureNode::setTextureDataGL (  GLuint _textureId,
                                      GLenum _target,
                                      GLint _width ,
                                      GLint _height,
                                      GLenum _format ,
                                      GLenum _type,
                                      const void * _data) {
  
  applyGLSettings();

  Texture2D* tex = textures_[_textureId].tex;

  tex->bind();

  if ( mipmapping_ )
    textures_[_textureId].mipmapAvailable = true;
  else
    textures_[_textureId].mipmapAvailable = false;

  applyTextureParameters(_textureId);

Kersten Schuster's avatar
Kersten Schuster committed
227
  bool mipmaps =  mipmapping_globally_active_ && mipmapping_;
Jan Möbius's avatar
Jan Möbius committed
228
  // Load the image
Kersten Schuster's avatar
Kersten Schuster committed
229
  if ( mipmaps )
Jan Möbius's avatar
Jan Möbius committed
230 231 232 233 234 235 236 237
    tex->autogenerateMipMaps();
  
  tex->setData( 0,                   // level
                GL_RGBA,             // internal format
                _width,              // width  (2^n)
                _height,             // height (2^m)
                _format,             // format
                _type,               // type
Kersten Schuster's avatar
Kersten Schuster committed
238 239
                _data,               // pointer to pixels
                mipmaps);            // mipmaps or not
Jan Möbius's avatar
Jan Möbius committed
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
  
  // Unbind until we use it
  ACG::GLState::bindTexture(GL_TEXTURE_2D,0);
}


//----------------------------------------------------------------------------

/** \brief Generate mipmaps for each texture that does not have one yet
*/
void TextureNode::updateMipmaps(bool _mipmap) {

  // Make sure we have at least on element in the textures list
  checkEmpty();

  for(unsigned int i = 1; i < textures_.size(); ++i) {

    Texture2D* tex = textures_[i].tex;

    // Bind texture
    tex->bind();

    // size in bytes of level 0 texture
    size_t bufferSize = textures_[i].tex->getWidth() * textures_[i].tex->getHeight()*4;

    if(_mipmap && bufferSize) {

      // Get pixel data out of texture memory
      GLubyte* buffer = new GLubyte[bufferSize];
//      glGetTexImage(textures_[i].target, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
      tex->getData(0, buffer);

      // Build mipmap
      tex->autogenerateMipMaps();
      tex->setData(0, tex->getInternalFormat(), tex->getWidth(), tex->getHeight(),
        tex->getFormat(), tex->getType(), buffer);

      delete [] buffer;
    }

    // Set mipmap available flag
    textures_[i].mipmapAvailable = _mipmap;

    // Update parameters such that changes apply during runtime
    applyTextureParameters(i);
  }
}

//----------------------------------------------------------------------------


void
TextureNode::set_texture(const unsigned char * _image, int _width, int _height)
{
  checkEmpty();

  // enough texture mem?
  if ( !Texture2D::checkTextureMem(GL_RGBA, _width, _height, GL_RGBA) ) {
    std::cerr << "Can't load texture";
    return;
  }

  setTextureDataGL(activeTexture_,GL_TEXTURE_2D,_width,_height,GL_RGBA,GL_UNSIGNED_BYTE,_image);

}


//----------------------------------------------------------------------------


void
TextureNode::set_texture(const QImage& _image)
{
  checkEmpty();

  // adjust texture size: 2^k * 2^l
  int tex_w, w( _image.width()  );
  int tex_h, h( _image.height() );

  for (tex_w=1; tex_w < w; tex_w <<= 1) {};
  for (tex_h=1; tex_h < h; tex_h <<= 1) {};
  if (5 * tex_w > 7 *w)   // tex_w longer than sqrt(2)*w means too much waste of storage space (7/5 = sqrt(2)-1%)
    tex_w >>= 1;
  if (5 * tex_h > 7 *h)   // tex_h longer than sqrt(2)*h means too much waste of storage space (7/5 = sqrt(2)-1%)
    tex_h >>= 1;

  // is size of input image a power of 2?
  bool isNPOT = ( tex_w != w ) || ( tex_h != h );

  // image has to be converted to GL_RGBA8 to avoid crash
  QImage textureGL;

  // eventually scale to pot image if no hw support  / core in GL 2.0
  if (!openGLVersion(2,0) && isNPOT)
  {
    // because texture will only be accessed proportionally by texture coordinates, aspect ratio is of no concern
    textureGL = QGLWidget::convertToGLFormat ( _image.scaled( tex_w, tex_h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ) );
  }
  else
  {
    // use npot texture
    tex_w = w;
    tex_h = h;
    textureGL = QGLWidget::convertToGLFormat ( _image );
  }

  // enough texture mem?
  if ( !Texture2D::checkTextureMem(GL_RGBA, tex_w, tex_h, GL_RGBA) ) {
    std::cerr << "Can't load texture TextureNode::set_texture" << std::endl;
    return;
  }

  // Set the image
  setTextureDataGL(activeTexture_ ,GL_TEXTURE_2D,tex_w,tex_h,GL_RGBA,GL_UNSIGNED_BYTE,textureGL.bits());
}

//----------------------------------------------------------------------------


void
TextureNode::set_texture(const float * _image, int _width, int _height )
{
  checkEmpty();

  // enough texture mem?
  if ( !Texture2D::checkTextureMem(GL_RGBA, _width, _height, GL_RGBA) ) {
    std::cerr << "Can't load texture TextureNode::set_texture" << std::endl;
    return;
  }

  // Set the image
  setTextureDataGL(activeTexture_,GL_TEXTURE_2D,_width,_height,GL_RGBA,GL_FLOAT,_image);
}

//----------------------------------------------------------------------------

void TextureNode::checkEmpty() {

  // No texture generated yet!
  if ( textures_.empty() ) {
    textures_.resize(1);
    activeTexture_ = 0;
//     textures_[activeTexture_].id = 0;
    Texture2D* t = new Texture2D();
    t->gen();
    textures_[activeTexture_].tex = t;
  }

}

//----------------------------------------------------------------------------

int TextureNode::available( GLuint _id  ) {
  // If the texture is found in the array return its id otherwise -1
  for ( uint i = 0 ; i < textures_.size(); ++i )
    if ( textures_[i].tex->id() == _id )
      return i;

  return -1;
}

//----------------------------------------------------------------------------

bool TextureNode::read(const char* _filename, GLuint _id ) {
  if ( available(_id) != -1 ) {
    activeTexture_ = available(_id);
    return read(_filename);
  } else
    std::cerr << "Texture with id " << _id << " not handled by this Node!!!" << std::endl;

  return false;

}


//----------------------------------------------------------------------------



void TextureNode::set_texture(const QImage& _image, GLuint _id) {

  checkEmpty();

  if ( available(_id) != -1 ) {
    activeTexture_ = available(_id);
    set_texture(_image);
  } else
    std::cerr << "Texture with id " << _id << " not handled by this Node!!!" << std::endl;

}


//----------------------------------------------------------------------------



void TextureNode::set_texture(const float * _image, int _width, int _height, GLuint _id) {

  checkEmpty();

  if ( available(_id) != -1 ) {
    activeTexture_ = available(_id);
    set_texture(_image,_width,_height);
  } else
    std::cerr << "Texture with id " << _id << " not handled by this Node!!!" << std::endl;
}



//----------------------------------------------------------------------------


void TextureNode::set_texture(const unsigned char * _image, int _width, int _height, GLuint _id) {

  checkEmpty();

  if ( available(_id) != -1 ) {
    activeTexture_ = available(_id);
    set_texture(_image,_width,_height);

  } else
    std::cerr << "Texture with id " << _id << " not handled by this Node!!!" << std::endl;

}


//----------------------------------------------------------------------------


// add QImage _image as additional texture, using face_texture_index
GLuint
TextureNode::add_texture(const QImage& _image)
{
  checkEmpty();

  textures_.resize(textures_.size()+1);  // can't push_back, because glGenTextures needs a pointer

  // Generate new texture
  textures_.back().tex = new Texture2D();

  activeTexture_ = int(textures_.size() - 1);

  // Set the image
  set_texture(_image);

  // return the id of the new texture
  return textures_.back().tex->id();
}


//----------------------------------------------------------------------------


void TextureNode::enter(GLState& _state , const DrawModes::DrawMode& _drawmode)
{
   if ( _drawmode & ( DrawModes::SOLID_TEXTURED |
                      DrawModes::SOLID_TEXTURED_SHADED |
                      DrawModes::SOLID_ENV_MAPPED |
                      DrawModes::SOLID_2DTEXTURED_FACE |
                      DrawModes::SOLID_2DTEXTURED_FACE_SHADED |
                      DrawModes::SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED |
                      DrawModes::SOLID_SHADER |
                      open_volume_mesh_texture_draw_modes_ ))
   {
504
     if(_state.compatibilityProfile())
Jan Möbius's avatar
Jan Möbius committed
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
      ACG::GLState::enable( GL_TEXTURE_2D );
      
      mipmapping_globally_active_ = _state.mipmapping_allowed();
      
      // Check if mipmapping status has changed
      if(_state.mipmapping_allowed() != last_mipmapping_status_) {
          
              // Update mipmaps
              updateMipmaps(mipmapping_globally_active_ && mipmapping_);
              
              // Keep track of changes
              last_mipmapping_status_ = _state.mipmapping_allowed();
          
      }
      
      if ( !textures_.empty() ) {
        textures_[activeTexture_].tex->bind();
      }
523
      if(_state.compatibilityProfile())
Jan Möbius's avatar
Jan Möbius committed
524 525 526 527 528 529 530 531 532 533 534
      glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, tex_mode_ );

//       if (_drawmode & DrawModes::SOLID_FACES_COLORED_2DTEXTURED_FACE_SMOOTH_SHADED)
//         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   }
}


//----------------------------------------------------------------------------


535
void TextureNode::leave(GLState&  _state  , const DrawModes::DrawMode& _drawmode)
Jan Möbius's avatar
Jan Möbius committed
536 537 538 539 540 541 542 543 544 545
{
   if ( _drawmode & ( DrawModes::SOLID_TEXTURED |
                      DrawModes::SOLID_TEXTURED_SHADED |
                      DrawModes::SOLID_ENV_MAPPED |
                      DrawModes::SOLID_2DTEXTURED_FACE |
                      DrawModes::SOLID_2DTEXTURED_FACE_SHADED |
                      DrawModes::SOLID_SHADER |
                      open_volume_mesh_texture_draw_modes_ ))
   {
      ACG::GLState::bindTexture( GL_TEXTURE_2D, 0 );
546 547
      if(_state.compatibilityProfile())
        ACG::GLState::disable( GL_TEXTURE_2D );
Jan Möbius's avatar
Jan Möbius committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
   }
}

void TextureNode::enterPick(GLState& /*_state*/ , PickTarget /*_target*/, const DrawModes::DrawMode& /*_drawMode*/ ) {

}

void TextureNode::leavePick(GLState& /*_state*/, PickTarget /*_target*/, const DrawModes::DrawMode& /*_drawMode*/ ) {
}

//----------------------------------------------------------------------------


GLuint TextureNode::activeTexture()
{
  if (0 <= activeTexture_  && activeTexture_ < int(textures_.size()))
    return textures_[activeTexture_].tex->id();

  return 0;
}

//----------------------------------------------------------------------------

bool TextureNode::activateTexture(GLuint _id)
{
  int search = available(_id);

  //==========================================================================
  // If zero is given, unbind all textures
  //==========================================================================
  if ( _id == 0 ) {
    search = 0;
  }

  //==========================================================================
  // Index has not been found ... No corresponding Texture in this node
  //==========================================================================
  if ( search == -1 ) {
    std::cerr << "Texture to activate not found!" << std::endl;
    return false;
  }

  activeTexture_ = search;

  return true;
}

//=============================================================================
} // namespace SceneGraph
} // namespace ACG
//=============================================================================