TextureControl.cc 28.4 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 44 45

#include "OpenFlipper/BasePlugin/PluginFunctions.hh"
#include "OpenFlipper/common/GlobalOptions.hh"

46 47 48 49
#define TEXTUREDATA "TextureData"



Jan Möbius's avatar
 
Jan Möbius committed
50 51
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension)
{
52 53 54 55 56 57 58 59 60 61
  std::cerr << "slotGlobalTextureAdded " << _textureName.toStdString() << std::endl;

  // Add this texture to the list of global textures
  if ( ! globalTextures_.textureExists(_textureName) ) {
    globalTextures_.addTexture(_textureName,_filename,_dimension,0);
    globalTextures_.texture(_textureName).enabled = false;
  } else {
    emit log(LOGERR,"Trying to add already existing global texture " + _textureName );
    return;
  }
62

63
  // Add a new entry to the global Texture menu
Jan Möbius's avatar
 
Jan Möbius committed
64
  QAction* new_texture = new QAction(_textureName, this);
65
  new_texture->setStatusTip(tr("Switch all objects to this Texture ( if available )"));
Jan Möbius's avatar
 
Jan Möbius committed
66 67 68 69 70
  new_texture->setCheckable(true);
  actionGroup_->addAction(new_texture);
  textureMenu_->addAction(new_texture);
  new_texture->setChecked(true);
  textureActions_.push_back(new_texture);
71

Jan Möbius's avatar
 
Jan Möbius committed
72 73
}

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

77
  std::cerr << "File opened : " << _id << std::endl;
78

79 80 81 82 83 84 85 86 87 88
  // 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;
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
  // 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) {

    std::cerr << "Adding texture " << i << " " << globalTextures_.textures()[i].name.toStdString() << std::endl;

    // ================================================================================
    // Get the image file
    // ================================================================================
    // TODO: support arbitrary paths!
    QString filename = OpenFlipper::Options::textureDir().absolutePath() +
                       OpenFlipper::Options::dirSeparator() +
                       globalTextures_.textures()[i].filename;

    QImage textureImage;
    if ( !textureImage.load( filename ) ){
        emit log(LOGERR, "Cannot load texture " + filename );
        continue;
Jan Möbius's avatar
 
Jan Möbius committed
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

    // ================================================================================
    // 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)
      texData->addTexture(globalTextures_.textures()[i], glName);

    // ================================================================================
    // Update texture mapping in meshNode
    // ================================================================================
    if( obj->dataType( DATA_TRIANGLE_MESH ) ){
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
    }

    if ( obj->dataType( DATA_POLY_MESH ) ){
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
    }

Jan Möbius's avatar
 
Jan Möbius committed
149
  }
150 151 152
}

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

154
  std::cerr << "slotTextureUpdated called for texture " << _textureName.toStdString() << " "  << _identifier << std::endl;
155

156 157 158 159 160 161
  // ================================================================================
  // 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
162 163
    return;
  }
164

165 166 167 168 169 170
  // ================================================================================
  // 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
171
    return;
172
  }
Jan Möbius's avatar
 
Jan Möbius committed
173

174 175 176
  if ( ! texData->textureExists(_textureName) ) {
    std::cerr << "Texture not found on object" << std::endl;
    return;
177
  }
Jan Möbius's avatar
 
Jan Möbius committed
178

179 180 181 182 183
  if ( ! texData->texture(_textureName).enabled ) {
    texData->texture(_textureName).dirty = true;
    std::cerr << "Texture is not active on object" << std::endl;
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
184

185 186 187 188 189 190 191 192 193 194 195 196
  // ================================================================================
  // 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);
  } 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);
197
  }
Jan Möbius's avatar
 
Jan Möbius committed
198

199 200
  // Mark the texture as not dirty
  texData->texture(_textureName).dirty = false;
Jan Möbius's avatar
 
Jan Möbius committed
201

Jan Möbius's avatar
 
Jan Möbius committed
202
  emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
203 204 205
}

template< typename MeshT >
206
void TextureControlPlugin::doUpdateTexture ( Texture& _texture, MeshT& _mesh )
Jan Möbius's avatar
 
Jan Möbius committed
207
{
208 209
  if ( _texture.type == HALFEDGEBASED ) {
    if (_texture.dimension == 1) {
Jan Möbius's avatar
 
Jan Möbius committed
210 211

      OpenMesh::HPropHandleT< double > texture;
212 213
	  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
214 215 216
        return;
      }

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

219
    } else if ( _texture.dimension == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
220 221

      OpenMesh::HPropHandleT< OpenMesh::Vec2d > texture2D;
222 223
	  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
224 225 226
        return;
      }

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

229
    } else
230 231 232
      emit log(LOGERR, "Unsupported Texture Dimension " + QString::number(_texture.dimension) );
  } else if ( _texture.type == VERTEXBASED ) {
    if ( _texture.dimension == 1 ) {
Jan Möbius's avatar
 
Jan Möbius committed
233 234

      OpenMesh::VPropHandleT< double > texture;
235 236
	  if ( ! _mesh.get_property_handle(texture,_texture.name.toStdString() ) ) {
        emit log(LOGERR,"Unable to get property " + _texture.name );
237
        return;
Jan Möbius's avatar
 
Jan Möbius committed
238 239
      }

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

242
      } else if ( _texture.dimension == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
243 244

        OpenMesh::VPropHandleT< OpenMesh::Vec2d >  texture2D;
245 246
		  if ( ! _mesh.get_property_handle(texture2D,_texture.name.toStdString() ) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name );
247
          return;
Jan Möbius's avatar
 
Jan Möbius committed
248 249
        }

250
        copyTexture( _texture, _mesh, texture2D);
Jan Möbius's avatar
 
Jan Möbius committed
251 252

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

Jan Möbius's avatar
 
Jan Möbius committed
254
        OpenMesh::VPropHandleT< OpenMesh::Vec3d >  scalarField3D;
255 256
        if ( ! _mesh.get_property_handle(scalarField3D,_texture.name) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name );
257
          return;
Jan Möbius's avatar
 
Jan Möbius committed
258
        }
259

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

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

Jan Möbius's avatar
 
Jan Möbius committed
265 266
    } else
      emit log(LOGERR, "Unsupported Texture type");
267

Jan Möbius's avatar
 
Jan Möbius committed
268 269
}

270 271 272 273 274 275 276 277 278 279
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;
280

Jan Möbius's avatar
 
Jan Möbius committed
281 282 283
   // Use absolute value as requested by plugin
   if ( abs )
      _value = fabs(_value);
284

Jan Möbius's avatar
 
Jan Möbius committed
285 286 287 288 289 290 291
   // Clamp if requested
   if ( clamp ) {
      if ( _value > clamp_max )
         _value = clamp_max;
      if (_value < clamp_min)
         _value = clamp_min;
   }
292

Jan Möbius's avatar
 
Jan Möbius committed
293 294 295
   // if the texture should not be repeated, scale to 0..1
   if ( ! repeat ) {
      if (! center ) {
296 297 298
        if ( scale) {
          _value /= scaleFactor;
          _value -= _min/scaleFactor;
Jan Möbius's avatar
 
Jan Möbius committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
        }
      } 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)
317
{
318 319 320 321 322 323
  std::cerr << "slotObjectUpdated called for : " << _identifier << std::endl;
  // ================================================================================
  // Get updated object
  // ================================================================================
  if ( _identifier == -1 )
    return;
324

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _identifier , obj ) ) {
    emit log(LOGERR,"slotObjectUpdated: Unable to get Object for id " + QString::number(_identifier) );
    return;
  }

  // ================================================================================
  // 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
345
  // basic check implemented
346 347
  for ( uint i = 0; i < texData->textures().size(); ++i ) {
    texData->textures()[i].dirty = true;
348 349 350 351 352 353 354 355

    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 );
    }

    if ( update && texData->textures()[i].enabled )
356 357
      emit updateTexture( texData->textures()[i].name , _identifier );
  }
358

Jan Möbius's avatar
 
Jan Möbius committed
359 360 361
}

void TextureControlPlugin::slotUpdateAllTextures( ) {
362 363 364 365 366 367
  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
368 369 370
}

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

372
  std::cerr << "TextureControlPlugin::slotSetTextureMode called for : " << _textureName.toStdString() << std::endl;
373

374 375 376 377 378 379 380
  // ================================================================================
  // Update texture settings for global textures
  // ================================================================================
  if ( ! globalTextures_.textureExists(_textureName) ) {
    emit log(LOGERR,"Global texture does not exist: " + _textureName);
    return;
  }
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
  // ================================================================================
  // Parse parameters and update them in the texture data
  // ================================================================================
  Texture& texture = globalTextures_.texture(_textureName);

  int i = 0;
  QString nextString = _mode.section(',',i,i);
  while ( nextString != "" ) {
    QString sectionName = nextString.section('=',0,0);
    QString value = nextString.section('=',1,1);

    if ( sectionName == "clamp" ) {
        if (value == "false") {
            texture.parameters.clamp = false;
        } else {
            texture.parameters.clamp = true;
        }
    } else
    if ( sectionName == "clamp_max" ) {
            texture.parameters.clamp_max = value.toDouble();
    } else
    if ( sectionName == "clamp_min" ) {
            texture.parameters.clamp_min = value.toDouble();
    } else
    if ( sectionName == "max_val" ) {
            texture.parameters.max_val = value.toDouble();
    } else
    if ( sectionName == "repeat" ) {
        if (value == "false") {
            texture.parameters.repeat = false;
        } else {
            texture.parameters.repeat = true;
        }
    } else
    if ( sectionName == "center" ) {
        if (value == "false") {
            texture.parameters.center = false;
        } else {
            texture.parameters.center = true;
        }
    } else
    if ( sectionName == "scale" ) {
        if (value == "false") {
            texture.parameters.scale = false;
        } else {
            texture.parameters.scale = true;
        }
    } else
    if ( sectionName == "type" ) {
        if (value == "halfedgebased") {
            texture.type = HALFEDGEBASED;
        } else {
            texture.type = VERTEXBASED;
        }
    } else
      emit log(LOGERR,"Unknown texture mode : " + sectionName);
438

439 440 441
    ++i;
    nextString = _mode.section(',',i,i);
  }
442

443 444 445 446
  // ================================================================================
  // Mark updated texture as dirty
  // ================================================================================
  texture.dirty = true;
447

448 449 450 451 452 453 454 455 456
  // TODO: call updates for global texture for all objects
  // ... emit( updateTexture ... )

//       // Force an update of all objects
//       for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)
//          updateTexture(  _textureName , o_it->id() );
//
//       emit updateView();
//    }
457

Jan Möbius's avatar
 
Jan Möbius committed
458 459 460
}

void TextureControlPlugin::pluginsInitialized() {
461 462 463
  // ================================================================================
  // Create global texture menu
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
464
  textureMenu_ = new QMenu(tr("&Texture Control"));
465
  textureMenu_->setTearOffEnabled(true);
Jan Möbius's avatar
 
Jan Möbius committed
466
  emit addMenubarAction(textureMenu_->menuAction(), VIEWMENU );
467

468 469 470 471
  // ================================================================================
  // Create Settings dialog
  // ================================================================================
  // TODO : Settings dialog updates required to change global/local textures,...
Jan Möbius's avatar
 
Jan Möbius committed
472
  settingsDialog_ = new texturePropertiesWidget(0);
473

Jan Möbius's avatar
 
Jan Möbius committed
474 475 476
  connect(settingsDialog_->okButton,SIGNAL(clicked()), this , SLOT (slotTexturePropertiesOk() ) );
  connect(settingsDialog_->applyButton,SIGNAL(clicked()), this , SLOT (slotTexturePropertiesApply() ) );
  connect(settingsDialog_->cancelButton,SIGNAL(clicked()), this , SLOT (slotTexturePropertiesCancel() ) );
477

478 479 480
  // ================================================================================
  // Create action group and menu for global textures
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
481 482 483 484
  actionGroup_ = new QActionGroup( 0 );
  actionGroup_->setExclusive( true );
  connect( actionGroup_, SIGNAL( triggered( QAction * ) ),
          this, SLOT( slotTextureMenu( QAction * ) ) );
485

Jan Möbius's avatar
 
Jan Möbius committed
486 487 488 489
  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);
490

Jan Möbius's avatar
 
Jan Möbius committed
491 492
  textureMenu_->addSeparator();
  textureMenu_->addActions(actionGroup_->actions());
493 494 495 496 497 498 499 500 501

  // ================================================================================
  // 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 );

Jan Möbius's avatar
 
Jan Möbius committed
502
}
503

Jan Möbius's avatar
Jan Möbius committed
504
void TextureControlPlugin::updateDialog() {
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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  std::cerr << "Not implemented : updateDialog" << std::endl;
  // TODO : Check
//   if ( textures_.size() == 0 )
//     return;
//
//   int textureid = -1;
//   for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
//       if ( textures_[i].name == activeTexture_ ) {
//             textureid = i;
//             break;
//       }
//   }
//
//   if ( textureid == -1 ) {
//       emit log(LOGERR,"Active Texture not found");
//       return;
//   }
//
//   settingsDialog_->repeatBox->setChecked(textures_[textureid].repeat);
//   settingsDialog_->clampBox->setChecked(textures_[textureid].clamp);
//   settingsDialog_->centerBox->setChecked(textures_[textureid].center);
//   settingsDialog_->absBox->setChecked(textures_[textureid].abs);
//   QString tmp;
//   tmp.setNum(textures_[textureid].max_val);
//   settingsDialog_->max_val->setText( tmp );
//   tmp.setNum(textures_[textureid].clamp_min);
//   settingsDialog_->clamp_min->setText(tmp);
//   tmp.setNum(textures_[textureid].clamp_max);
//   settingsDialog_->clamp_max->setText(tmp);
//
//   // update plot only when dimension is 1
//   if ( textures_[textureid].dimension == 1) {
//     std::vector< double > x,y;
//
//     x.push_back(100.0);
//     y.push_back(100.0);
//
//     x.push_back(200.0);
//     y.push_back(150.0);
//
//     x.push_back(300.0);
//     y.push_back(10.0);
//
//     settingsDialog_->setOriginalData(x,y);
//
// //     getOriginalHistogram();
//
//   }
553
}
Jan Möbius's avatar
Jan Möbius committed
554 555

void TextureControlPlugin::slotSetTextureProperties() {
556 557 558 559 560 561 562
  std::cerr << "Not implemented : slotSetTextureProperties" << std::endl;
  // TODO : Check
//   updateDialog();
//   if ( textures_.size() == 0 )
//       return;
//
//   settingsDialog_->show();
Jan Möbius's avatar
Jan Möbius committed
563
}
564

Jan Möbius's avatar
 
Jan Möbius committed
565
void TextureControlPlugin::applyDialogSettings() {
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 599 600 601 602 603
  std::cerr << "Not implemented : applyDialogSettings" << std::endl;
  // TODO : Check
//   if ( textures_.size() == 0 )
//       return;
//
//   int textureid = -1;
//   for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
//         if ( textures_[i].name == activeTexture_ ) {
//               textureid = i;
//               break;
//         }
//   }
//
//   if (textureid == -1) {
//       emit log(LOGERR,"Unable to get active Texture");
//   }
//
//   textures_[textureid].repeat=settingsDialog_->repeatBox->isChecked();
//   textures_[textureid].clamp=settingsDialog_->clampBox->isChecked();
//   textures_[textureid].center=settingsDialog_->centerBox->isChecked();
//   textures_[textureid].abs=settingsDialog_->absBox->isChecked();
//   textures_[textureid].scale=settingsDialog_->scaleBox->isChecked();
//
//   QString tmp;
//   tmp = settingsDialog_->max_val->text();
//   textures_[textureid].max_val = tmp.toDouble();
//
//   tmp = settingsDialog_->clamp_min->text();
//   textures_[textureid].clamp_min = tmp.toDouble();
//
//   tmp = settingsDialog_->clamp_max->text();
//   textures_[textureid].clamp_max = tmp.toDouble();
//
//   // Update the corresponding meshes
//   for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objectsEnd(); ++o_it)
//       slotTextureUpdated(  activeTexture_ , o_it->id() );
//
//   emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
604
}
605

Jan Möbius's avatar
Jan Möbius committed
606
void TextureControlPlugin::slotTexturePropertiesOk() {
607 608 609
  // TODO : Check
//   applyDialogSettings();
//   settingsDialog_->hide();
Jan Möbius's avatar
 
Jan Möbius committed
610
}
611

612 613 614
void TextureControlPlugin::slotTexturePropertiesApply() {
  // TODO : Check
//     applyDialogSettings();
Jan Möbius's avatar
Jan Möbius committed
615
}
616

Jan Möbius's avatar
Jan Möbius committed
617
void TextureControlPlugin::slotTexturePropertiesCancel() {
618 619
  // TODO : Check
//     settingsDialog_->hide();
Jan Möbius's avatar
Jan Möbius committed
620
}
Jan Möbius's avatar
 
Jan Möbius committed
621

622

Jan Möbius's avatar
 
Jan Möbius committed
623
void TextureControlPlugin::slotTextureMenu(QAction* _action) {
624 625
  // call existing function to switch the texture
  slotSwitchTexture( _action->text() );
Jan Möbius's avatar
 
Jan Möbius committed
626 627 628
}

void TextureControlPlugin::slotSwitchTexture( QString _textureName ) {
629

630
  std::cerr << "TextureControlPlugin::slotSwitchTexture : " << _textureName.toStdString() << std::endl;
631

632 633 634
  for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ;
                                        o_it != PluginFunctions::objectsEnd();
                                        ++o_it) {
635

636 637 638 639 640 641 642 643
    // ================================================================================
    // Get Texture data for current object
    // ================================================================================
    TextureData* texData = dynamic_cast< TextureData* > ( o_it->objectData(TEXTUREDATA) );
    if (texData == 0) {
      std::cerr << "Object has no texture data" << std::endl;
      continue;
    }
644

645 646 647 648 649 650 651 652 653
    // ================================================================================
    // Enable the given texture exclusively
    // ================================================================================
    if ( texData->enableTexture( _textureName , true ) ) {
      std::cerr << "Enabled Texture " << _textureName.toStdString() << std::endl;
    } else {
      std::cerr << "Failed to enabled Texture" << std::endl;
      continue;
    }
654

655 656 657 658 659 660 661 662 663
    // ================================================================================
    // If texture is flagged dirty, update it ( this jumps to texture updated
    // which will update the visualization )
    // ================================================================================
    if ( texData->texture( _textureName).dirty ) {
      // TODO: maybe introduce lock to prevent extra redraws if updating all objects
      emit updateTexture( texData->texture( _textureName ).name , o_it->id() );
      continue;
    }
664

665 666 667 668
    // ================================================================================
    // Update texture map from meshNode and activate it
    // ================================================================================
    if( o_it->dataType( DATA_TRIANGLE_MESH ) ){
669
      doUpdateTexture(texData->texture(_textureName), *PluginFunctions::triMeshObject(o_it)->mesh());
670 671 672 673
      PluginFunctions::triMeshObject(o_it)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
      PluginFunctions::triMeshObject(o_it)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::triMeshObject(o_it)->meshNode()->set_property_map( texData->propertyMap() );
    }
674

675
    if ( o_it->dataType( DATA_POLY_MESH ) ){
676
      doUpdateTexture(texData->texture(_textureName), *PluginFunctions::polyMeshObject(o_it)->mesh());
677 678 679
      PluginFunctions::polyMeshObject(o_it)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
      PluginFunctions::polyMeshObject(o_it)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::polyMeshObject(o_it)->meshNode()->set_property_map( texData->propertyMap() );
680
    }
681

Jan Möbius's avatar
 
Jan Möbius committed
682
  }
683

684
  // ================================================================================
685
  // Switch to a texture drawMode
686 687 688 689 690 691 692 693 694
  // ================================================================================
  bool textureMode = false;
  for ( int j = 0 ; j < PluginFunctions::viewers() ; ++j ) {
      textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED );
      textureMode |= ( PluginFunctions::drawMode(j) == ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
  }

  if ( !textureMode )
    PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
695 696 697 698 699 700 701 702 703 704 705 706

  emit updateView();

  // TODO: Update menu
//     QList<QAction *> menuEntries = actionGroup_->actions();
//
//     for ( int i = 0 ; i < menuEntries.size(); ++i ) {
//       if ( menuEntries[i]->text() == _textureName )
//         menuEntries[i]->setChecked(true);
//     }

//     updateDialog();
Jan Möbius's avatar
 
Jan Möbius committed
707 708 709
}


710 711 712 713 714 715 716 717 718 719 720 721 722 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
void TextureControlPlugin::slotUpdateContextMenu( int _objectId ) {

  std::cerr << "TextureControlPlugin::slotUpdateContextMenu " << _objectId << std::endl;

  // ================================================================================
  // 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;
  }

  // ================================================================================
  // 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 * ) ) );

  for ( uint i = 0 ; i < texData->textures().size() ; ++i ) {
    QAction* action = actionGroup->addAction( texData->textures()[i].name );
    action->setCheckable(true);
    if ( texData->textures()[i].enabled )
      action->setChecked(true);
  }

  contextMenu_->addActions(actionGroup->actions());

}


756 757 758 759
void TextureControlPlugin::slotTextureContextMenu( QAction * _action ) {

}

Jan Möbius's avatar
 
Jan Möbius committed
760 761
Q_EXPORT_PLUGIN2( texturecontrolplugin , TextureControlPlugin );