TextureControl.cc 49.1 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
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
15
//
Jan Möbius's avatar
 
Jan Möbius committed
16 17 18 19
//  OpenFlipper is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
20
//
Jan Möbius's avatar
 
Jan Möbius committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




35

Jan Möbius's avatar
 
Jan Möbius committed
36 37 38 39 40
#include <QtGui>

#include "TextureControl.hh"

#include <iostream>
41
#include <ACG/GL/GLState.hh>
Jan Möbius's avatar
 
Jan Möbius committed
42 43

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
Jan Möbius's avatar
Jan Möbius committed
44
#include "OpenFlipper/BasePlugin/PluginFunctionsViewControls.hh"
Jan Möbius's avatar
 
Jan Möbius committed
45 46
#include "OpenFlipper/common/GlobalOptions.hh"

47 48
#include <math.h>

49 50 51 52
#define TEXTUREDATA "TextureData"



Dirk Wilden's avatar
Dirk Wilden committed
53 54 55 56 57 58
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension , int _id)
{
  // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
59
    return;
Dirk Wilden's avatar
Dirk Wilden committed
60 61 62 63 64 65 66 67 68
  }

  // Get Texture data for this object or create one if it does not exist
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0){
    texData = new TextureData();
    obj->setObjectData(TEXTUREDATA, texData);
  }

69
  if ( texData->textureExists(_textureName) ) {
Dirk Wilden's avatar
Dirk Wilden committed
70 71 72
    emit log(LOGERR,"Trying to add already existing texture " + _textureName + " for object " + QString::number(_id) );
    return;
  }
73 74 75 76 77 78

  // ================================================================================
  // Get the image file
  // ================================================================================

  QImage textureImage;
Dirk Wilden's avatar
 
Dirk Wilden committed
79 80 81 82

  if ( !getImage(_filename,textureImage) )
    emit log(LOGERR, "Cannot load texture '" + _textureName + "'. File not found '" + _filename + "'");

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

  // ================================================================================
  // Add the texture to the texture node and get the corresponding id
  // ================================================================================
  GLuint glName = 0;

  //inform textureNode about the new texture
  if( obj->dataType( DATA_TRIANGLE_MESH ) )
    glName = PluginFunctions::triMeshObject(obj)->textureNode()->add_texture(textureImage);

  if ( obj->dataType( DATA_POLY_MESH ) )
    glName = PluginFunctions::polyMeshObject(obj)->textureNode()->add_texture(textureImage);

  // ================================================================================
  // Store texture information in objects metadata
  // ================================================================================

  if (glName == 0) {
    emit log(LOGERR,"Unable to bind texture!");
    return;
  }

  texData->addTexture(_textureName,_filename,_dimension,glName);
106
  texData->texture(_textureName).disable();
Dirk Wilden's avatar
Dirk Wilden committed
107 108
}

Jan Möbius's avatar
 
Jan Möbius committed
109 110
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension)
{
111 112 113
  // Add this texture to the list of global textures
  if ( ! globalTextures_.textureExists(_textureName) ) {
    globalTextures_.addTexture(_textureName,_filename,_dimension,0);
114
    globalTextures_.texture(_textureName).disable();
115 116

    QImage textureImage;
Dirk Wilden's avatar
 
Dirk Wilden committed
117 118 119 120

    if ( !getImage(_filename,textureImage) )
      emit log(LOGERR, "Cannot load texture '" + _textureName + "'. File not found '" + _filename + "'");

121 122
    globalTextures_.texture(_textureName).textureImage = textureImage;

123 124 125 126
  } else {
    emit log(LOGERR,"Trying to add already existing global texture " + _textureName );
    return;
  }
127

128
  // Add a new entry to the global Texture menu
Jan Möbius's avatar
 
Jan Möbius committed
129
  QAction* new_texture = new QAction(_textureName, this);
130
  new_texture->setStatusTip(tr("Switch all objects to this Texture ( if available )"));
Jan Möbius's avatar
 
Jan Möbius committed
131 132 133 134 135
  new_texture->setCheckable(true);
  actionGroup_->addAction(new_texture);
  textureMenu_->addAction(new_texture);
  new_texture->setChecked(true);
  textureActions_.push_back(new_texture);
136

137
}
138

139
void TextureControlPlugin::slotMultiTextureAdded( QString _textureGroup , QString _name , QString _filename , int _id , int& _textureId ) {
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
   // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
  }

  // Check if we support this kind of data
  if ( !obj->dataType(DATA_TRIANGLE_MESH) && !obj->dataType(DATA_POLY_MESH) ) {
      emit log(LOGERR,"Trying to add textures to object failed because of unsupported object type");
      return;
  }

  // Get Texture data for this object or create one if it does not exist
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0){
    texData = new TextureData();
    obj->setObjectData(TEXTUREDATA, texData);
  }

  if ( !texData->textureExists( _textureGroup ) )
    texData->addMultiTexture( _textureGroup );

  // Add the texture
  slotTextureAdded( _name , _filename , 2 , _id);

  // Get the id of the new texture
  _textureId = -1;
167
  _textureId = texData->texture(_name).id();
168

169 170 171
  //hide the texture (its accessible through the multiTexture)
  texData->texture(_name).hidden( true );

172
  QImage textureImage;
Dirk Wilden's avatar
 
Dirk Wilden committed
173 174 175 176

  if ( !getImage(_filename,textureImage) )
    emit log(LOGERR, "Cannot load multiTexture '" + _textureGroup + "'. File not found '" + _filename + "'");

177 178 179 180 181 182 183 184 185 186 187
  texData->texture(_name).textureImage = textureImage;

  // Store the new texture in the list of this textureGroup
  if ( _textureId != -1 ) {
    texData->texture(_textureGroup).multiTextureList << _name ;
  } else {
    emit log(LOGERR,"Error when getting internal id of new multitexture!");
  }

}

Dirk Wilden's avatar
 
Dirk Wilden committed
188
bool TextureControlPlugin::getImage( QString _fileName, QImage& _image ) {
189 190
  QString loadFilename;

191
  if ( _fileName.startsWith("/") || _fileName.startsWith(".") )
192 193 194 195 196
    loadFilename = _fileName;
  else
    loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;

  if ( !_image.load( loadFilename ) ){
Dirk Wilden's avatar
 
Dirk Wilden committed
197 198
    _image.load(OpenFlipper::Options::textureDirStr() + QDir::separator() + "unknown.png");
    return false;
199 200
  }

Dirk Wilden's avatar
 
Dirk Wilden committed
201
  return true;
202 203 204 205
}

void TextureControlPlugin::addedEmptyObject( int _id ) {
  fileOpened(_id);
Jan Möbius's avatar
 
Jan Möbius committed
206 207
}

208
template< typename MeshT >
Jan Möbius's avatar
Jan Möbius committed
209
void TextureControlPlugin::handleFileOpenTextures( MeshT*& _mesh , int _objectId ) {
210

211 212 213
  // ================================================================================
  // Create a backup of the original per Vertex texture Coordinates
  // ================================================================================
214 215 216 217 218
  OpenMesh::VPropHandleT< typename MeshT::TexCoord2D > oldVertexCoords;
  _mesh->add_property(oldVertexCoords,"Original Per Vertex Texture Coords");
  for ( TriMesh::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it)
    _mesh->property(oldVertexCoords, v_it ) =  _mesh->texcoord2D( v_it );

219
  slotTextureAdded("Original Per Vertex Texture Coords","unknown.png",_objectId);
220

221 222 223
  // ================================================================================
  // Create a backup of the original per Face texture Coordinates
  // ================================================================================
224 225 226 227 228
  OpenMesh::HPropHandleT< typename MeshT::TexCoord2D > oldHalfedgeCoords;
  _mesh->add_property(oldHalfedgeCoords,"Original Per Face Texture Coords");
  for ( TriMesh::HalfedgeIter he_it = _mesh->halfedges_begin(); he_it != _mesh->halfedges_end(); ++he_it)
    _mesh->property(oldHalfedgeCoords, he_it ) =  _mesh->texcoord2D( he_it );

229
  slotTextureAdded("Original Per Face Texture Coords","unknown.png",_objectId);
230
  slotSetTextureMode("Original Per Face Texture Coords","type=halfedgebased",_objectId);
231

232
  // ================================================================================
233
  // If this property is available we have a mapping between face_index_property and
234 235
  // available textures stored in the map. So we have to enable multi texturing
  // ================================================================================
236 237 238
  OpenMesh::MPropHandleT< std::map< int, std::string > > property;
  if ( _mesh->get_property_handle(property,"TextureMapping") ) {

Jan Möbius's avatar
Jan Möbius committed
239 240
    // ================================================================================
    // As internal texture indices might differ from the available ones loaded from files,
241
    // We have to remap them after loading the textures!
Jan Möbius's avatar
Jan Möbius committed
242
    // ================================================================================
243
    std::map< int,int > newMapping;
244 245 246
    // zero ( no texture ) always maps to to zero
    newMapping[0]=0;

247 248 249 250 251 252 253 254 255 256 257 258 259

    // TODO : If only one Texture, use single Texturing mode
    if ( true ) {
      // Assume multiTexture Mode now and load the Textures
      for ( std::map< int, std::string >::iterator texture  = _mesh->property(property).begin();
                                                   texture != _mesh->property(property).end(); texture++ ) {
        int textureId = -1;
        slotMultiTextureAdded("OBJ Data",QString(texture->second.c_str()) , QString(texture->second.c_str()), _objectId, textureId );
        newMapping[texture->first] = textureId;
      }

      // Convert the indices stored in the mesh to the actual ones used for rendering
      OpenMesh::FPropHandleT< int > newIndexProperty;
Jan Möbius's avatar
Jan Möbius committed
260
      if (! _mesh->get_property_handle(newIndexProperty,"TextureControl: OriginalFileIndexMapping") )
261 262 263 264 265
        _mesh->add_property(newIndexProperty,"TextureControl: OriginalFileIndexMapping");

      for ( TriMesh::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it)
        _mesh->property(newIndexProperty, f_it ) =  newMapping[_mesh->texture_index( f_it )];

Jan Möbius's avatar
Jan Möbius committed
266 267 268
      // =================================================================================================
      // We use a different property for storing the IndexProperty to prevent overwriting the original one
      // =================================================================================================
269
      slotSetTextureMode("OBJ Data","indexProperty=TextureControl: OriginalFileIndexMapping", _objectId);
270 271

      doSwitchTexture("OBJ Data",_objectId);
272
    }
273 274


275 276 277 278
  }

}

279
void TextureControlPlugin::fileOpened( int _id ) {
280
  // TODO:: Store original texture coords in a new property!
Jan Möbius's avatar
Jan Möbius committed
281

282 283 284 285
  // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
286
    return;
287 288 289 290 291
  }

  // Check if we support this kind of data
  if ( !obj->dataType(DATA_TRIANGLE_MESH) && !obj->dataType(DATA_POLY_MESH) ) {
      return;
292 293
  }

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  // Get Texture data for this object or create one if it does not exist
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0){
    texData = new TextureData();
    obj->setObjectData(TEXTUREDATA, texData);
  }

  // Iterate over all available global textures and add them to the object
  for ( uint i = 0 ; i < globalTextures_.textures().size() ; ++i) {

    // ================================================================================
    // Get the image file
    // ================================================================================

    QImage textureImage;
Dirk Wilden's avatar
 
Dirk Wilden committed
309 310 311 312

    if ( !getImage(globalTextures_.textures()[i].filename(),textureImage) )
    emit log(LOGERR, "Cannot load global texture '" + globalTextures_.textures()[i].name() +
                     "'. File not found '" + globalTextures_.textures()[i].filename() + "'");
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

    // ================================================================================
    // Add the texture to the texture node and get the corresponding id
    // ================================================================================
    GLuint glName = 0;

    //inform textureNode about the new texture
    if( obj->dataType( DATA_TRIANGLE_MESH ) )
      glName = PluginFunctions::triMeshObject(obj)->textureNode()->add_texture(textureImage);

    if ( obj->dataType( DATA_POLY_MESH ) )
      glName = PluginFunctions::polyMeshObject(obj)->textureNode()->add_texture(textureImage);

    // ================================================================================
    // Store texture information in objects metadata
    // ================================================================================

330
    if (glName != 0) {
331
      texData->addTexture(globalTextures_.textures()[i], glName);
332
      texData->setImage(globalTextures_.textures()[i].name(),textureImage);
333 334 335 336 337 338
    }
    else {
      emit log(LOGERR,"Unable to bind Texture");
      continue;
    }

339 340 341 342 343

    // ================================================================================
    // Update texture mapping in meshNode
    // ================================================================================
    if( obj->dataType( DATA_TRIANGLE_MESH ) ){
344 345
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
346 347 348
    }

    if ( obj->dataType( DATA_POLY_MESH ) ){
349 350
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
351 352
    }

Jan Möbius's avatar
 
Jan Möbius committed
353
  }
354 355 356 357 358

  // Check if the file contains a texture map, store original textures and handle them before adding global textures
  if( obj->dataType( DATA_TRIANGLE_MESH ) ) {
    TriMesh* mesh = PluginFunctions::triMesh(obj);
    if ( mesh )
Jan Möbius's avatar
Jan Möbius committed
359
      handleFileOpenTextures(mesh,_id);
360 361 362
  } else if ( obj->dataType( DATA_POLY_MESH ) ) {
    PolyMesh* mesh = PluginFunctions::polyMesh(obj);
    if ( mesh )
Jan Möbius's avatar
Jan Möbius committed
363
      handleFileOpenTextures(mesh,_id);
364 365
  }

366 367 368
}

void TextureControlPlugin::slotTextureUpdated( QString _textureName , int _identifier ) {
369

370 371 372 373 374 375
  // ================================================================================
  // Get updated object
  // ================================================================================
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _identifier , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_identifier) );
Jan Möbius's avatar
 
Jan Möbius committed
376 377
    return;
  }
378

379 380 381 382
  //skip object if its not a mesh
  if( !obj->dataType( DATA_TRIANGLE_MESH ) && !obj->dataType( DATA_POLY_MESH ) )
    return;

383 384 385 386 387 388
  // ================================================================================
  // Get objects texture data and verify that texture exists
  // ================================================================================
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0){
    std::cerr << "Texture data not found!" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
389
    return;
390
  }
Jan Möbius's avatar
 
Jan Möbius committed
391

392 393 394
  // ================================================================================
  // Check if texture exists
  // ================================================================================
395
  if ( ! texData->textureExists(_textureName) ) {
396
    emit log(LOGERR,"Texture " + _textureName + " not found on object " + QString::number(_identifier) );
397
    return;
398
  }
Jan Möbius's avatar
 
Jan Möbius committed
399

400 401 402
  // ================================================================================
  // If texture is not enabled, mark it as dirty and defer update to visualization update
  // ================================================================================
403
  if ( ! texData->texture(_textureName).enabled() ) {
404
    texData->texture(_textureName).setDirty();
405 406
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
407

408
  QImage textureImage;
Dirk Wilden's avatar
 
Dirk Wilden committed
409 410 411 412

  if ( !getImage( texData->texture(_textureName).filename(), textureImage) )
    emit log(LOGERR, "Cannot update texture '" + texData->texture(_textureName).name() +
                     "'. File not found '" + texData->texture(_textureName).filename() + "'");
413

414 415 416 417 418 419 420
  // ================================================================================
  // As the current texture is active, update it
  // ================================================================================
  if( obj->dataType( DATA_TRIANGLE_MESH ) ) {
    TriMesh* mesh = PluginFunctions::triMesh(obj);
    doUpdateTexture(texData->texture(_textureName), *mesh);
    PluginFunctions::triMeshObject(obj)->textureNode()->set_repeat(texData->texture(_textureName).parameters.repeat);
421
    PluginFunctions::triMeshObject(obj)->textureNode()->set_texture( textureImage );
422 423 424 425
  } else if ( obj->dataType( DATA_POLY_MESH ) ) {
    PolyMesh* mesh = PluginFunctions::polyMesh(obj);
    doUpdateTexture(texData->texture(_textureName), *mesh);
    PluginFunctions::polyMeshObject(obj)->textureNode()->set_repeat(texData->texture(_textureName).parameters.repeat);
426
    PluginFunctions::polyMeshObject(obj)->textureNode()->set_texture( textureImage );
427
  }
Jan Möbius's avatar
 
Jan Möbius committed
428

429 430 431
  // ================================================================================
  // Mark texture as not dirty
  // ================================================================================
432
  texData->texture(_textureName).clean();
Jan Möbius's avatar
 
Jan Möbius committed
433

434 435 436
  // ================================================================================
  // Enable the right draw mode and update
  // ================================================================================
437
  switchDrawMode( texData->texture( _textureName ).type() );
Jan Möbius's avatar
 
Jan Möbius committed
438 439
}

440 441 442 443 444 445
void TextureControlPlugin::slotUpdateTexture( QString _textureName , int _identifier) {
  if ( _textureName == "Reflection Lines" )
    slotTextureUpdated( _textureName , _identifier );

}

Jan Möbius's avatar
 
Jan Möbius committed
446
template< typename MeshT >
447
void TextureControlPlugin::doUpdateTexture ( Texture& _texture, MeshT& _mesh )
Jan Möbius's avatar
 
Jan Möbius committed
448
{
449

450
  if ( _texture.type() == HALFEDGEBASED ) {
451
    if (_texture.dimension() == 1) {
Jan Möbius's avatar
 
Jan Möbius committed
452 453

      OpenMesh::HPropHandleT< double > texture;
454 455
	  if ( ! _mesh.get_property_handle(texture, _texture.name().toStdString() ) ) {
        emit log(LOGERR,"Unable to get property " + _texture.name() );
Jan Möbius's avatar
 
Jan Möbius committed
456 457 458
        return;
      }

459
      copyTexture(_texture, _mesh, texture);
Jan Möbius's avatar
 
Jan Möbius committed
460

461
    } else if ( _texture.dimension() == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
462 463

      OpenMesh::HPropHandleT< OpenMesh::Vec2d > texture2D;
464 465
	  if ( ! _mesh.get_property_handle( texture2D, _texture.name().toStdString() ) ) {
        emit log(LOGERR,"Unable to get property " + _texture.name() );
Jan Möbius's avatar
 
Jan Möbius committed
466 467 468
        return;
      }

469
      copyTexture( _texture, _mesh, texture2D);
Jan Möbius's avatar
 
Jan Möbius committed
470

471
    } else
472
      emit log(LOGERR, "Unsupported Texture Dimension " + QString::number(_texture.dimension() ) );
473
  } else if ( _texture.type() == VERTEXBASED ) {
474
    if ( _texture.dimension() == 1 ) {
Jan Möbius's avatar
 
Jan Möbius committed
475 476

      OpenMesh::VPropHandleT< double > texture;
477 478
	  if ( ! _mesh.get_property_handle(texture,_texture.name().toStdString() ) ) {
        emit log(LOGERR,"Unable to get property " + _texture.name() );
479
        return;
Jan Möbius's avatar
 
Jan Möbius committed
480 481
      }

482
        copyTexture(_texture, _mesh, texture);
Jan Möbius's avatar
 
Jan Möbius committed
483

484
      } else if ( _texture.dimension() == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
485 486

        OpenMesh::VPropHandleT< OpenMesh::Vec2d >  texture2D;
487 488
		  if ( ! _mesh.get_property_handle(texture2D,_texture.name().toStdString() ) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name() );
489
          return;
Jan Möbius's avatar
 
Jan Möbius committed
490 491
        }

492
        copyTexture( _texture, _mesh, texture2D);
Jan Möbius's avatar
 
Jan Möbius committed
493 494

      } /*else if ( textures_[_textureid].dimension == 3 ) {
495

Jan Möbius's avatar
 
Jan Möbius committed
496
        OpenMesh::VPropHandleT< OpenMesh::Vec3d >  scalarField3D;
497 498
        if ( ! _mesh.get_property_handle(scalarField3D,_texture.name) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name );
499
          return;
Jan Möbius's avatar
 
Jan Möbius committed
500
        }
501

Jan Möbius's avatar
 
Jan Möbius committed
502
        copyTexture(_textureid, _mesh, scalarField3D);
503

Jan Möbius's avatar
 
Jan Möbius committed
504
      }*/ else
505
        emit log(LOGERR, "Unsupported Texture Dimension " + QString::number(_texture.dimension() ) );
506

Jan Möbius's avatar
 
Jan Möbius committed
507 508
    } else
      emit log(LOGERR, "Unsupported Texture type");
509

Jan Möbius's avatar
 
Jan Möbius committed
510 511
}

512 513 514 515 516 517 518 519 520 521
void TextureControlPlugin::computeValue(Texture& _texture, double _min, double _max, double& _value) {
   const bool clamp         = _texture.parameters.clamp ;
   const bool center        = _texture.parameters.center;
   const double max_val     = _texture.parameters.max_val;
   const bool abs           = _texture.parameters.abs;
   const double clamp_max   = _texture.parameters.clamp_max;
   const double clamp_min   = _texture.parameters.clamp_min;
   const bool scale         = _texture.parameters.scale;
   const double scaleFactor = fabs(_max) + fabs(_min);
   const bool repeat        = _texture.parameters.repeat;
522

Jan Möbius's avatar
 
Jan Möbius committed
523 524 525
   // Use absolute value as requested by plugin
   if ( abs )
      _value = fabs(_value);
526

Jan Möbius's avatar
 
Jan Möbius committed
527 528 529 530 531 532 533
   // Clamp if requested
   if ( clamp ) {
      if ( _value > clamp_max )
         _value = clamp_max;
      if (_value < clamp_min)
         _value = clamp_min;
   }
534

Jan Möbius's avatar
 
Jan Möbius committed
535 536 537
   // if the texture should not be repeated, scale to 0..1
   if ( ! repeat ) {
      if (! center ) {
538 539 540
        if ( scale) {
          _value /= scaleFactor;
          _value -= _min/scaleFactor;
Jan Möbius's avatar
 
Jan Möbius committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
        }
      } else {
         // the values above zero are mapped to 0.5..1 the negative ones to 0.5..0
         if (_value > 0.0) {
            _value /= ( _max * 2.0);
            _value += 0.5;
         } else {
            _value /= ( _min * 2.0);
            _value = 0.5 - _value;
         }
      }
   } else {
      _value -= _min;
      _value *= max_val / (_max - _min);
   }
}

void TextureControlPlugin::slotObjectUpdated(int _identifier)
559
{
560 561 562 563 564
  // ================================================================================
  // Get updated object
  // ================================================================================
  if ( _identifier == -1 )
    return;
565

566 567 568 569 570 571
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _identifier , obj ) ) {
    emit log(LOGERR,"slotObjectUpdated: Unable to get Object for id " + QString::number(_identifier) );
    return;
  }

572 573 574 575
  //skip object if its not a mesh
  if( !obj->dataType( DATA_TRIANGLE_MESH ) && !obj->dataType( DATA_POLY_MESH ) )
    return;

576 577 578 579 580 581 582 583 584 585 586 587 588 589
  // ================================================================================
  // Get objects texture data and verify that texture exists
  // ================================================================================
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0){
    std::cerr << "slotObjectUpdated: Texture data not found!" << std::endl;
    return;
  }

  // ================================================================================
  // Set all textures to dirty
  // ================================================================================
  // TODO : if not in the texture rendering mode, do not emit update
  // Involves adding a interface part to react on draw mode changes
590
  // basic check implemented
591
  for ( uint i = 0; i < texData->textures().size(); ++i ) {
592
    texData->textures()[i].setDirty();
593 594 595 596 597 598 599

    bool update = false;
    for ( int j = 0 ; j < PluginFunctions::viewers() ; ++j ) {
       update |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED );
       update |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
    }

600
    if ( update && texData->textures()[i].enabled() )
601
      emit updateTexture( texData->textures()[i].name() , _identifier );
602
  }
603

Jan Möbius's avatar
 
Jan Möbius committed
604 605 606
}

void TextureControlPlugin::slotUpdateAllTextures( ) {
607 608 609 610 611 612
  std::cerr << "slotUpdateAllTextures: not implemented yet ... might be removed" << std::endl;
  // TODO : Check
//    // Force an update of all textures which are available for the updated object
//    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)
//       for ( uint i = 0 ; i < textures_.size() ; ++i )
//          emit updateTexture( textures_[i].name , o_it->id() );
Jan Möbius's avatar
 
Jan Möbius committed
613 614
}

615
bool TextureControlPlugin::parseMode( QString _mode, Texture& _texture ) {
616

617
  bool changed = false;
618

619 620 621 622 623 624
  int i = 0;
  QString nextString = _mode.section(',',i,i);
  while ( nextString != "" ) {
    QString sectionName = nextString.section('=',0,0);
    QString value = nextString.section('=',1,1);

625 626 627
    // Cleanup representation
    value       = value.trimmed();
    sectionName = sectionName.trimmed();
Jan Möbius's avatar
Jan Möbius committed
628
    sectionName = sectionName.toLower();
629

630
    if ( sectionName == "clamp" ) {
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
      if ( StringToBool(value) != _texture.parameters.clamp ) {
        _texture.parameters.clamp = StringToBool(value);
        changed = true;
      }
    } else if ( sectionName == "clamp_max" ) {
      if (value.toDouble() != _texture.parameters.clamp_max){
        _texture.parameters.clamp_max = value.toDouble();
        changed = true;
      }
    } else if ( sectionName == "clamp_min" ) {
      if (value.toDouble() != _texture.parameters.clamp_min){
        _texture.parameters.clamp_min = value.toDouble();
        changed = true;
      }
    } else if ( sectionName == "max_val" ) {
      if (value.toDouble() != _texture.parameters.max_val){
        _texture.parameters.max_val = value.toDouble();
        changed = true;
      }
    } else if ( sectionName == "repeat" ) {
      if ( StringToBool(value) != _texture.parameters.repeat ) {
        _texture.parameters.repeat = StringToBool(value);
        changed = true;
      }
    } else if ( sectionName == "center" ) {
      if ( StringToBool(value) != _texture.parameters.center ) {
        _texture.parameters.center = StringToBool(value);
        changed = true;
      }
    } else if ( sectionName == "scale" ) {
      if ( StringToBool(value) != _texture.parameters.scale ) {
        _texture.parameters.scale = StringToBool(value);
        changed = true;
      }
Jan Möbius's avatar
Jan Möbius committed
665
    }else if ( sectionName == "indexproperty" ) {
666 667 668 669 670 671 672 673 674 675 676
      if ( value != _texture.indexMappingProperty() ) {
        _texture.indexMappingProperty( value );
        changed = true;
      }
    } else if ( sectionName == "type" ) {
        if ( ( value == "halfedgebased" ) && ( _texture.type() != HALFEDGEBASED ) ) {
          _texture.type( HALFEDGEBASED );
          changed = true;
        } else if ( (value == "vertexbased") && (_texture.type() != HALFEDGEBASED)  ) {
          _texture.type( VERTEXBASED );
          changed = true;
Jan Möbius's avatar
Jan Möbius committed
677 678 679
        }  else if ( (value == "environmentmap") && (_texture.type() != ENVIRONMENT)  ) {
          _texture.type( ENVIRONMENT );
          changed = true;
680 681
        } else {
          emit log(LOGERR,"Unknown texture type : " + value + " for texture: " + _texture.name() );
682 683 684
        }
    } else
      emit log(LOGERR,"Unknown texture mode : " + sectionName);
685

686 687 688
    ++i;
    nextString = _mode.section(',',i,i);
  }
689

690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
  return changed;
}

void TextureControlPlugin::slotSetTextureMode(QString _textureName ,QString _mode) {

  // ================================================================================
  // Update texture settings for global textures
  // ================================================================================
  if ( ! globalTextures_.textureExists(_textureName) ) {
    emit log(LOGERR,"Global texture does not exist: " + _textureName);
    return;
  }

  // ================================================================================
  // Parse parameters and update them in the texture data
  // ================================================================================
  Texture& texture = globalTextures_.texture(_textureName);

  // Parse the mode settings
  parseMode(_mode,texture);
Jan Möbius's avatar
Jan Möbius committed
710
  _mode = _mode.toLower();
711

712 713 714
  // ================================================================================
  // Mark updated texture as dirty
  // ================================================================================
715
  texture.setDirty();
716

717

Dirk Wilden's avatar
Dirk Wilden committed
718 719 720 721
  // check if the local textures need to be updated
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it){

    TextureData* texData = dynamic_cast< TextureData* > ( o_it->objectData(TEXTUREDATA) );
722

Dirk Wilden's avatar
Dirk Wilden committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
    if (texData != 0){

      if ( texData->textureExists(_textureName) ){

        Texture& localTex = texData->texture(_textureName);

        //check if something changed
        bool changed = false;

        if ( _mode.contains("clamp") && (texture.parameters.clamp != localTex.parameters.clamp) ){
          localTex.parameters.clamp = texture.parameters.clamp;
          changed = true;
        }

        if ( _mode.contains("clamp_max") && (texture.parameters.clamp_max != localTex.parameters.clamp_max) ){
          localTex.parameters.clamp_max = texture.parameters.clamp_max;
          changed = true;
        }

        if ( _mode.contains("clamp_min") && (texture.parameters.clamp_min != localTex.parameters.clamp_min) ){
          localTex.parameters.clamp_min = texture.parameters.clamp_min;
          changed = true;
        }

        if ( _mode.contains("max_val") && (texture.parameters.max_val != localTex.parameters.max_val) ){
          localTex.parameters.max_val = texture.parameters.max_val;
          changed = true;
        }

        if ( _mode.contains("repeat") && (texture.parameters.repeat != localTex.parameters.repeat) ){
          localTex.parameters.repeat = texture.parameters.repeat;
          changed = true;
        }

        if ( _mode.contains("center") && (texture.parameters.center != localTex.parameters.center) ){
          localTex.parameters.center = texture.parameters.center;
          changed = true;
        }

        if ( _mode.contains("scale") && (texture.parameters.scale != localTex.parameters.scale) ){
          localTex.parameters.scale = texture.parameters.scale;
          changed = true;
        }

767 768
        if ( _mode.contains("type") && (texture.type() != localTex.type() ) ){
          localTex.type( texture.type() );
Dirk Wilden's avatar
Dirk Wilden committed
769 770 771 772 773 774 775 776
          changed = true;
        }

        //only update if the texture is enabled
        if (changed){
          if ( texData->isEnabled(_textureName) )
            emit updateTexture( _textureName, o_it->id() );
          else
777
            localTex.setDirty();
Dirk Wilden's avatar
Dirk Wilden committed
778 779 780 781
        }
      }
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
782 783
}

Dirk Wilden's avatar
Dirk Wilden committed
784 785 786 787 788 789 790
bool TextureControlPlugin::StringToBool(QString _value){
  if (_value == "false")
    return false;
  else
    return true;
}

Dirk Wilden's avatar
Dirk Wilden committed
791
void TextureControlPlugin::slotSetTextureMode(QString _textureName, QString _mode, int _id) {
Dirk Wilden's avatar
Dirk Wilden committed
792

Dirk Wilden's avatar
Dirk Wilden committed
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811
  // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
  }

  // Get Texture data for this object
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );

  if ( texData == 0 || ( !texData->textureExists(_textureName))  ) {
    emit log(LOGERR,"Texture does not exist: " + _textureName + " (object=" + QString::number(_id) + ")");
    return;
  }

  // ================================================================================
  // Parse parameters and update them in the texture data
  // ================================================================================
  Texture& texture = texData->texture(_textureName);

Jan Möbius's avatar
Jan Möbius committed
812

813
  bool changed = parseMode(_mode,texture);
Dirk Wilden's avatar
Dirk Wilden committed
814

Dirk Wilden's avatar
Dirk Wilden committed
815 816 817 818 819
  //only update if the texture is enabled
  if (changed){
    if ( texData->isEnabled(_textureName) )
      emit updateTexture( _textureName, _id );
    else
820
      texture.setDirty();
Dirk Wilden's avatar
Dirk Wilden committed
821
  }
Dirk Wilden's avatar
Dirk Wilden committed
822 823
}

Jan Möbius's avatar
 
Jan Möbius committed
824
void TextureControlPlugin::pluginsInitialized() {
825 826 827
  // ================================================================================
  // Create global texture menu
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
828
  textureMenu_ = new QMenu(tr("&Texture Control"));
829
  textureMenu_->setTearOffEnabled(true);
Jan Möbius's avatar
 
Jan Möbius committed
830
  emit addMenubarAction(textureMenu_->menuAction(), VIEWMENU );
831

832 833 834 835
  // ================================================================================
  // Create Settings dialog
  // ================================================================================
  // TODO : Settings dialog updates required to change global/local textures,...
Jan Möbius's avatar
 
Jan Möbius committed
836
  settingsDialog_ = new texturePropertiesWidget(0);
837 838
  connect( settingsDialog_, SIGNAL( applyProperties(TextureData*,QString,int) ),
           this,              SLOT( applyDialogSettings(TextureData*,QString,int) ));
839

840 841 842 843 844
  connect( settingsDialog_, SIGNAL( getCoordinates1D(QString,int,std::vector< double >&)),
           this,              SLOT( getCoordinates1D(QString,int,std::vector< double >&)));

  settingsDialog_->installEventFilter( this );

845 846 847
  // ================================================================================
  // Create action group and menu for global textures
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
848 849 850 851
  actionGroup_ = new QActionGroup( 0 );
  actionGroup_->setExclusive( true );
  connect( actionGroup_, SIGNAL( triggered( QAction * ) ),
          this, SLOT( slotTextureMenu( QAction * ) ) );
852

Jan Möbius's avatar
 
Jan Möbius committed
853 854 855 856
  QAction* AC_Texture_Settings = new QAction(tr("&Texture Settings"), this);
  AC_Texture_Settings->setStatusTip(tr("Set the texture visualization properties"));
  connect(AC_Texture_Settings, SIGNAL(triggered()), this, SLOT(slotSetTextureProperties()));
  textureMenu_->addAction(AC_Texture_Settings);
857

Jan Möbius's avatar
 
Jan Möbius committed
858 859
  textureMenu_->addSeparator();
  textureMenu_->addActions(actionGroup_->actions());
860 861 862 863 864 865 866 867 868

  // ================================================================================
  // Create basic per object context menu
  // ================================================================================
  contextMenu_ = new QMenu(0);
  contextMenu_->setTitle("Textures");
  emit addContextMenuItem(contextMenu_->menuAction() ,DATA_TRIANGLE_MESH , CONTEXTOBJECTMENU );
  emit addContextMenuItem(contextMenu_->menuAction() ,DATA_POLY_MESH     , CONTEXTOBJECTMENU );

869 870

  slotTextureAdded("Reflection Lines","reflection_map.png",2);
Jan Möbius's avatar
Jan Möbius committed
871
  slotSetTextureMode("Reflection Lines","type=environmentmap");
Jan Möbius's avatar
 
Jan Möbius committed
872
}
873

Jan Möbius's avatar
Jan Möbius committed
874
void TextureControlPlugin::slotSetTextureProperties() {
875

876
  settingsDialog_->show( &globalTextures_, -1);
Jan Möbius's avatar
 
Jan Möbius committed
877
}
878

879
void TextureControlPlugin::applyDialogSettings(TextureData* _texData, QString _textureName, int _id) {
880

881 882
  if (_id != -1){
    //local texture
Jan Möbius's avatar
Jan Möbius committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

    // Get the object
    BaseObjectData* obj;
    if (! PluginFunctions::getObject(  _id , obj ) ) {
      emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
    }

    if ( !_texData->textureExists(_textureName) ) {
      emit log(LOGERR,"Texture does not exist in applyDialogSettings " + _textureName );
    }

    Texture& texture = _texData->texture(_textureName );

    if( obj->dataType( DATA_TRIANGLE_MESH ) ){
      PluginFunctions::triMeshObject(obj)->textureNode()->set_texture(texture.textureImage , texture.glName() );
    } else  if ( obj->dataType( DATA_POLY_MESH ) ) {
      PluginFunctions::polyMeshObject(obj)->textureNode()->set_texture(texture.textureImage , texture.glName() );
    }

    // Always mark texture as dirty
    _texData->texture( _textureName ).setDirty();

    emit updateView();
906

907 908 909
  } else {
    // global texture

910
    _texData->texture( _textureName ).setDirty();
911 912 913 914 915 916 917

    Texture& globalTexture = _texData->texture(_textureName);

    // check if the local textures need to be updated
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it){

      TextureData* texData = dynamic_cast< TextureData* > ( o_it->objectData(TEXTUREDATA) );
Jan Möbius's avatar
 
Jan Möbius committed
918

919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
      if ( texData != 0 && texData->textureExists(_textureName) ){

        //overwrite local parameters
        Texture& localTexture = texData->texture(_textureName);

        bool changed = false;

        if (localTexture.parameters.clamp != globalTexture.parameters.clamp){
          localTexture.parameters.clamp = globalTexture.parameters.clamp;
          changed = true;
        }
        if (localTexture.parameters.clamp_max != globalTexture.parameters.clamp_max){
          localTexture.parameters.clamp_max = globalTexture.parameters.clamp_max;
          changed = true;
        }
        if (localTexture.parameters.clamp_min != globalTexture.parameters.clamp_min){
          localTexture.parameters.clamp_min = globalTexture.parameters.clamp_min;
          changed = true;
        }
        if (localTexture.parameters.max_val != globalTexture.parameters.max_val){
          localTexture.parameters.max_val = globalTexture.parameters.max_val;
          changed = true;
        }
        if (localTexture.parameters.repeat != globalTexture.parameters.repeat){
          localTexture.parameters.repeat = globalTexture.parameters.repeat;
          changed = true;
        }
        if (localTexture.parameters.center != globalTexture.parameters.center){
          localTexture.parameters.center = globalTexture.parameters.center;
          changed = true;
        }
        if (localTexture.parameters.scale != globalTexture.parameters.scale){
          localTexture.parameters.scale = globalTexture.parameters.scale;
          changed = true;
        }

        // update if something has changed
        if ( changed ){
          if ( texData->isEnabled(_textureName) )
            slotTextureUpdated( _textureName  , o_it->id() );
          else
960
            texData->texture( _textureName ).setDirty();
961 962 963 964 965
        }
      }
    }
  }
}
966

Jan Möbius's avatar
 
Jan Möbius committed
967
void TextureControlPlugin::slotTextureMenu(QAction* _action) {
968 969
  // call existing function to switch the texture
  slotSwitchTexture( _action->text() );
Jan Möbius's avatar
 
Jan Möbius committed
970 971
}

Dirk Wilden's avatar
Dirk Wilden committed
972
void TextureControlPlugin::doSwitchTexture( QString _textureName , int _id ) {
973

Dirk Wilden's avatar
Dirk Wilden committed
974 975 976 977 978
  // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
  }
979

Dirk Wilden's avatar
Dirk Wilden committed
980 981 982 983 984 985 986 987
  // ================================================================================
  // Get Texture data for current object
  // ================================================================================
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0) {
    std::cerr << "Object has no texture data" << std::endl;
    return;
  }
988

Dirk Wilden's avatar
Dirk Wilden committed
989
  // ================================================================================
990
  // Check for requested Texture
Dirk Wilden's avatar
Dirk Wilden committed
991
  // ================================================================================
992 993
  if ( !texData->textureExists(_textureName) ) {
    emit log(LOGERR, "Texture not available! " + _textureName );
Dirk Wilden's avatar
Dirk Wilden committed
994 995
    return;
  }
996

997 998 999
  // ================================================================================
  // Enable the given texture exclusively or use multitexture setting
  // ================================================================================
1000
  bool multiTextureMode = ( texData->texture(_textureName).type() == MULTITEXTURE );
1001 1002 1003 1004 1005 1006 1007 1008 1009
  if ( !multiTextureMode ) {
    if ( ! texData->enableTexture( _textureName , true ) ) {
      emit log(LOGERR, "Failed to enabled Texture " + _textureName );
      return;
    }
  } else {
    // get the list of textures for this mode
    QStringList textureList = texData->texture(_textureName).multiTextureList;

1010 1011
    texData->enableTexture(_textureName, true);

1012
    for ( uint i = 0 ; i < texData->textures().size() ; ++i ) {
1013
      if ( textureList.contains( texData->textures()[i].name() ) || (texData->textures()[i].name() == _textureName) )
1014
        texData->enableTexture( texData->textures()[i].name() , false );
1015
       else
1016
        texData->disableTexture( texData->textures()[i].name() );
1017 1018 1019 1020
    }

  }

Dirk Wilden's avatar
Dirk Wilden committed
1021 1022 1023 1024
  // ================================================================================
  // If texture is flagged dirty, update it ( this jumps to texture updated
  // which will update the visualization )
  // ================================================================================
1025

1026
  if ( !multiTextureMode && texData->texture( _textureName).dirty() ) {
1027

Dirk Wilden's avatar
Dirk Wilden committed
1028
    // TODO: maybe introduce lock to prevent extra redraws if updating all objects
1029
    emit updateTexture( texData->texture( _textureName ).name() , obj->id() );
Dirk Wilden's avatar
Dirk Wilden committed
1030 1031
    return;
  }
1032

Dirk Wilden's avatar
Dirk Wilden committed
1033 1034 1035 1036
  // ================================================================================
  // Update texture map from meshNode and activate it
  // ================================================================================
  if( obj->dataType( DATA_TRIANGLE_MESH ) ){
1037
    if (!multiTextureMode) {
Jan Möbius's avatar
Jan Möbius committed
1038 1039
      if ( texData->texture(_textureName).type() != ENVIRONMENT)
        doUpdateTexture(texData->texture(_textureName), *PluginFunctions::triMeshObject(obj)->mesh());
1040
      PluginFunctions::triMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName() );
Jan Möbius's avatar
Bugfix  
Jan Möbius committed
1041
      PluginFunctions::triMeshObject(obj)->meshNode()->set_index_property_name("No Texture Index");
1042 1043 1044
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
    } else {
1045
      PluginFunctions::triMeshObject(obj)->meshNode()->set_index_property_name( texData->texture( _textureName ).indexMappingProperty().toStdString() );
1046
      PluginFunctions::triMeshObject(obj)->textureNode()->activateTexture(0);
1047 1048 1049 1050
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
    }

Dirk Wilden's avatar
Dirk Wilden committed
1051
  }
1052

Dirk Wilden's avatar
Dirk Wilden committed
1053
  if ( obj->dataType( DATA_POLY_MESH ) ){
1054
    if (!multiTextureMode) {
Jan Möbius's avatar
Jan Möbius committed
1055 1056
      if ( texData->texture(_textureName).type() != ENVIRONMENT)
        doUpdateTexture(texData->texture(_textureName), *PluginFunctions::polyMeshObject(obj)->mesh());
1057
      PluginFunctions::polyMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName() );
Jan Möbius's avatar
Bugfix  
Jan Möbius committed
1058
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_index_property_name("No Texture Index");
1059 1060 1061
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
    } else {
1062
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_index_property_name( texData->texture( _textureName ).indexMappingProperty().toStdString() );
1063
      PluginFunctions::polyMeshObject(obj)->textureNode()->activateTexture(0);
1064 1065 1066
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
    }
Dirk Wilden's avatar
Dirk Wilden committed
1067 1068 1069 1070 1071
  }

  // ================================================================================
  // Switch to a texture drawMode
  // ================================================================================
1072
  switchDrawMode(texData->texture( _textureName ).type());
1073 1074 1075

}

1076
void TextureControlPlugin::switchDrawMode( TextureType _type ) {
1077

Dirk Wilden's avatar
Dirk Wilden committed
1078 1079
  bool textureMode = false;
  for ( int j = 0 ; j < PluginFunctions::viewers() ; ++j ) {
1080 1081 1082
    switch (_type) {
      case MULTITEXTURE:
      case HALFEDGEBASED:
1083 1084
        textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_2DTEXTURED_FACE );
        textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_2DTEXTURED_FACE_SHADED );
1085 1086
        break;
      case VERTEXBASED:
1087 1088
        textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED );
        textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
1089
        break;
Jan Möbius's avatar
Jan Möbius committed
1090 1091 1092
      case ENVIRONMENT:
        textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_ENV_MAPPED );
        break;
1093 1094 1095 1096
      case UNSET:
        emit log(LOGERR,"Switching drawmode for unknonw Texture Type!");
        break;
    }
Jan Möbius's avatar
 
Jan Möbius committed
1097
  }
1098

1099
  if ( !textureMode ) {
1100 1101 1102
    switch (_type) {
      case MULTITEXTURE:
      case HALFEDGEBASED:
Jan Möbius's avatar
Jan Möbius committed
1103
        PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_2DTEXTURED_FACE_SHADED, PluginFunctions::ALL_VIEWERS );
1104 1105
        break;
      case VERTEXBASED:
Jan Möbius's avatar
Jan Möbius committed
1106
        PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED , PluginFunctions::ALL_VIEWERS);
1107
        break;
Jan Möbius's avatar
Jan Möbius committed
1108 1109 1110
      case ENVIRONMENT:
        PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_ENV_MAPPED , PluginFunctions::ALL_VIEWERS);
        break;
1111 1112 1113 1114
      case UNSET:
        emit log(LOGERR,"Switching drawmode for unknonw Texture Type!");
        break;
    }
1115
  }
Dirk Wilden's avatar
Dirk Wilden committed
1116 1117

  emit updateView();
1118
}
Dirk Wilden's avatar
Dirk Wilden committed
1119

1120
void TextureControlPlugin::slotSwitchTexture( QString _textureName , int _id ) {
Dirk Wilden's avatar
Dirk Wilden committed
1121

1122
  doSwitchTexture(_textureName, _id);
Dirk Wilden's avatar
Dirk Wilden committed
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
}

void TextureControlPlugin::slotSwitchTexture( QString _textureName ) {

  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
                                        o_it != PluginFunctions::objectsEnd();
                                        ++o_it)

    doSwitchTexture(_textureName, o_it->id() );

Jan Möbius's avatar
 
Jan Möbius committed
1133 1134 1135
}


1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
void TextureControlPlugin::slotUpdateContextMenu( int _objectId ) {

  // ================================================================================
  // Get picking object object
  // ================================================================================
  if ( _objectId == -1 )
    return;

  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _objectId , obj ) ) {
    emit log(LOGERR,"slotUpdateContextMenu: Unable to get Object for id " + QString::number(_objectId) );
    return;
  }

1150 1151 1152 1153
  //skip object if its not a mesh
  if( !obj->dataType( DATA_TRIANGLE_MESH ) && !obj->dataType( DATA_POLY_MESH ) )
    return;

1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
  // ================================================================================
  // Get objects texture data and verify that texture exists
  // ================================================================================
  TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );
  if (texData == 0){
    std::cerr << "TextureControlPlugin::slotUpdateContextMenu: Texture data not found!" << std::endl;
    return;
  }

  // ================================================================================
  // Prepare Texture menu
  // ================================================================================
  contextMenu_->clear();
  QActionGroup* actionGroup = new QActionGroup(0);
  actionGroup->setExclusive( true );
  connect( actionGroup, SIGNAL( triggered( QAction * ) ),
          this, SLOT( slotTextureContextMenu( QAction * ) ) );

1172 1173 1174 1175 1176
  QAction* action = actionGroup->addAction( "Texture Settings" );

  contextMenu_->addAction( action );
  contextMenu_->addSeparator();

1177
  for ( uint i = 0 ; i < texData->textures().size() ; ++i ) {
1178

1179 1180 1181
    if ( texData->textures()[i].hidden() )
      continue;

1182
    action = actionGroup->addAction( texData->textures()[i].name() );
1183

1184
    action->setCheckable(true);
1185

1186
    if ( texData->textures()[i].enabled() )
1187
      action->setChecked(true);
1188

1189
    contextMenu_->addAction( action );
1190 1191 1192 1193
  }
}


1194
void TextureControlPlugin::slotTextureContextMenu( QAction * _action ) {
1195

1196 1197 1198 1199
  // id of object for which the context menu is created, is stored in the action
  QVariant idVariant = _action->data( );
  int id = idVariant.toInt();

1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
  if (_action->text() == "Texture Settings"){

    BaseObjectData* obj;
    if (! PluginFunctions::getObject(  id , obj ) ) {
      emit log(LOGERR,"Unable to get Object for id " + QString::number(id) );
      return;
    }

    TextureData* texData = dynamic_cast< TextureData* > ( obj->objectData(TEXTUREDATA) );

    if (texData == 0){

      QMessageBox msgBox;
      msgBox.setText("Cannot show Properties. No Textures available!");
      msgBox.exec();
      return;

    } else {
      settingsDialog_->show( texData, id, obj->name() );
    }

  } else {

    slotSwitchTexture( _action->text() , id );
  }
1225 1226 1227

}

1228 1229 1230 1231 1232 1233 1234