TextureControl.cc 36.6 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"



Dirk Wilden's avatar
Dirk Wilden committed
50 51 52 53
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension , int _id)
{
  std::cerr << "slotLocalTextureAdded " << _textureName.toStdString() << std::endl;

54 55
  // TODO: Load texture?! ...

Dirk Wilden's avatar
Dirk Wilden committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  // 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 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);
  }


  // Add this texture to the list of global textures
  if ( ! texData->textureExists(_textureName) ) {
    texData->addTexture(_textureName,_filename,_dimension,0);
    texData->texture(_textureName).enabled = false;
  } else {
    emit log(LOGERR,"Trying to add already existing texture " + _textureName + " for object " + QString::number(_id) );
    return;
  }
}

Jan Möbius's avatar
 
Jan Möbius committed
80 81
void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension)
{
82 83 84 85
  // Add this texture to the list of global textures
  if ( ! globalTextures_.textureExists(_textureName) ) {
    globalTextures_.addTexture(_textureName,_filename,_dimension,0);
    globalTextures_.texture(_textureName).enabled = false;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

    QString loadFilename;

    if ( _filename.startsWith("/") )
      loadFilename = _filename;
    else
      loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _filename;

    QImage textureImage;
    if ( !textureImage.load( loadFilename ) ){
        emit log(LOGERR, "Cannot load texture " +  _filename );
        textureImage.load(OpenFlipper::Options::textureDirStr() + QDir::separator() + "unknown.png");
    }

    globalTextures_.texture(_textureName).textureImage = textureImage;

102 103 104 105
  } else {
    emit log(LOGERR,"Trying to add already existing global texture " + _textureName );
    return;
  }
106

107
  // Add a new entry to the global Texture menu
Jan Möbius's avatar
 
Jan Möbius committed
108
  QAction* new_texture = new QAction(_textureName, this);
109
  new_texture->setStatusTip(tr("Switch all objects to this Texture ( if available )"));
Jan Möbius's avatar
 
Jan Möbius committed
110 111 112 113 114
  new_texture->setCheckable(true);
  actionGroup_->addAction(new_texture);
  textureMenu_->addAction(new_texture);
  new_texture->setChecked(true);
  textureActions_.push_back(new_texture);
115

116
}
117

118 119
void TextureControlPlugin::slotMultiTextureAdded( QString _textureGroup , QString _name , QString _filename , int _id , int& _textureId ) {
  std::cerr << "slotMultiTextureAdded" << std::endl;
Jan Möbius's avatar
 
Jan Möbius committed
120 121
}

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

125 126 127 128 129 130 131 132 133 134
  // 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;
135 136
  }

137 138 139 140 141 142 143 144 145 146 147 148 149 150
  // 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
    // ================================================================================
    // TODO: support arbitrary paths!
151 152 153 154 155 156 157

    QString loadFilename;

    if ( globalTextures_.textures()[i].filename.startsWith("/") )
      loadFilename = globalTextures_.textures()[i].filename;
    else
      loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + globalTextures_.textures()[i].filename;
158 159

    QImage textureImage;
160 161 162
    if ( !textureImage.load( loadFilename ) ){
        emit log(LOGERR, "Cannot load texture " + globalTextures_.textures()[i].filename );
        textureImage.load(OpenFlipper::Options::textureDirStr() + QDir::separator() + "unknown.png");
Jan Möbius's avatar
 
Jan Möbius committed
163
    }
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

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

181
    if (glName != 0) {
182
      texData->addTexture(globalTextures_.textures()[i], glName);
183 184 185 186 187 188 189
      texData->setImage(globalTextures_.textures()[i].name,textureImage);
    }
    else {
      emit log(LOGERR,"Unable to bind Texture");
      continue;
    }

190 191 192 193 194

    // ================================================================================
    // Update texture mapping in meshNode
    // ================================================================================
    if( obj->dataType( DATA_TRIANGLE_MESH ) ){
195 196 197 198 199 200 201
//       PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
//       PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );

      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );

      // TODO: Multitexturing has to set the right map here!
202 203 204
    }

    if ( obj->dataType( DATA_POLY_MESH ) ){
205 206 207
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
      // TODO: Multitexturing has to set the right map here!
208 209
    }

Jan Möbius's avatar
 
Jan Möbius committed
210
  }
211 212 213
}

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

215 216 217 218 219 220
  // ================================================================================
  // 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
221 222
    return;
  }
223

224 225 226 227 228 229
  // ================================================================================
  // 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
230
    return;
231
  }
Jan Möbius's avatar
 
Jan Möbius committed
232

233 234 235
  if ( ! texData->textureExists(_textureName) ) {
    std::cerr << "Texture not found on object" << std::endl;
    return;
236
  }
Jan Möbius's avatar
 
Jan Möbius committed
237

238 239 240 241 242
  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
243

244 245 246 247 248 249 250 251 252 253 254 255
  // ================================================================================
  // 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);
256
  }
Jan Möbius's avatar
 
Jan Möbius committed
257

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

Jan Möbius's avatar
 
Jan Möbius committed
261
  emit updateView();
Jan Möbius's avatar
 
Jan Möbius committed
262 263 264
}

template< typename MeshT >
265
void TextureControlPlugin::doUpdateTexture ( Texture& _texture, MeshT& _mesh )
Jan Möbius's avatar
 
Jan Möbius committed
266
{
267 268
  if ( _texture.type == HALFEDGEBASED ) {
    if (_texture.dimension == 1) {
Jan Möbius's avatar
 
Jan Möbius committed
269 270

      OpenMesh::HPropHandleT< double > texture;
271 272
	  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
273 274 275
        return;
      }

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

278
    } else if ( _texture.dimension == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
279 280

      OpenMesh::HPropHandleT< OpenMesh::Vec2d > texture2D;
281 282
	  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
283 284 285
        return;
      }

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

288
    } else
289 290 291
      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
292 293

      OpenMesh::VPropHandleT< double > texture;
294 295
	  if ( ! _mesh.get_property_handle(texture,_texture.name.toStdString() ) ) {
        emit log(LOGERR,"Unable to get property " + _texture.name );
296
        return;
Jan Möbius's avatar
 
Jan Möbius committed
297 298
      }

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

301
      } else if ( _texture.dimension == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
302 303

        OpenMesh::VPropHandleT< OpenMesh::Vec2d >  texture2D;
304 305
		  if ( ! _mesh.get_property_handle(texture2D,_texture.name.toStdString() ) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name );
306
          return;
Jan Möbius's avatar
 
Jan Möbius committed
307 308
        }

309
        copyTexture( _texture, _mesh, texture2D);
Jan Möbius's avatar
 
Jan Möbius committed
310 311

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

Jan Möbius's avatar
 
Jan Möbius committed
313
        OpenMesh::VPropHandleT< OpenMesh::Vec3d >  scalarField3D;
314 315
        if ( ! _mesh.get_property_handle(scalarField3D,_texture.name) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name );
316
          return;
Jan Möbius's avatar
 
Jan Möbius committed
317
        }
318

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

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

Jan Möbius's avatar
 
Jan Möbius committed
324 325
    } else
      emit log(LOGERR, "Unsupported Texture type");
326

Jan Möbius's avatar
 
Jan Möbius committed
327 328
}

329 330 331 332 333 334 335 336 337 338
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;
339

Jan Möbius's avatar
 
Jan Möbius committed
340 341 342
   // Use absolute value as requested by plugin
   if ( abs )
      _value = fabs(_value);
343

Jan Möbius's avatar
 
Jan Möbius committed
344 345 346 347 348 349 350
   // Clamp if requested
   if ( clamp ) {
      if ( _value > clamp_max )
         _value = clamp_max;
      if (_value < clamp_min)
         _value = clamp_min;
   }
351

Jan Möbius's avatar
 
Jan Möbius committed
352 353 354
   // if the texture should not be repeated, scale to 0..1
   if ( ! repeat ) {
      if (! center ) {
355 356 357
        if ( scale) {
          _value /= scaleFactor;
          _value -= _min/scaleFactor;
Jan Möbius's avatar
 
Jan Möbius committed
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
        }
      } 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)
376
{
377 378 379 380 381
  // ================================================================================
  // Get updated object
  // ================================================================================
  if ( _identifier == -1 )
    return;
382

383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
  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
403
  // basic check implemented
404 405
  for ( uint i = 0; i < texData->textures().size(); ++i ) {
    texData->textures()[i].dirty = true;
406 407 408 409 410 411 412 413

    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 )
414 415
      emit updateTexture( texData->textures()[i].name , _identifier );
  }
416

Jan Möbius's avatar
 
Jan Möbius committed
417 418 419
}

void TextureControlPlugin::slotUpdateAllTextures( ) {
420 421 422 423 424 425
  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
426 427 428
}

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

430 431 432 433 434 435 436
  // ================================================================================
  // Update texture settings for global textures
  // ================================================================================
  if ( ! globalTextures_.textureExists(_textureName) ) {
    emit log(LOGERR,"Global texture does not exist: " + _textureName);
    return;
  }
437

438 439 440 441 442 443 444 445 446 447 448 449
  // ================================================================================
  // 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" ) {
Dirk Wilden's avatar
Dirk Wilden committed
450
      texture.parameters.clamp = StringToBool(value);
451 452
    } else
    if ( sectionName == "clamp_max" ) {
Dirk Wilden's avatar
Dirk Wilden committed
453
      texture.parameters.clamp_max = value.toDouble();
454 455
    } else
    if ( sectionName == "clamp_min" ) {
Dirk Wilden's avatar
Dirk Wilden committed
456
      texture.parameters.clamp_min = value.toDouble();
457 458
    } else
    if ( sectionName == "max_val" ) {
Dirk Wilden's avatar
Dirk Wilden committed
459
      texture.parameters.max_val = value.toDouble();
460 461
    } else
    if ( sectionName == "repeat" ) {
Dirk Wilden's avatar
Dirk Wilden committed
462
      texture.parameters.repeat = StringToBool(value);
463 464
    } else
    if ( sectionName == "center" ) {
Dirk Wilden's avatar
Dirk Wilden committed
465
      texture.parameters.center = StringToBool(value);
466 467
    } else
    if ( sectionName == "scale" ) {
Dirk Wilden's avatar
Dirk Wilden committed
468
      texture.parameters.scale = StringToBool(value);
469 470 471 472 473 474 475 476 477
    } else
    if ( sectionName == "type" ) {
        if (value == "halfedgebased") {
            texture.type = HALFEDGEBASED;
        } else {
            texture.type = VERTEXBASED;
        }
    } else
      emit log(LOGERR,"Unknown texture mode : " + sectionName);
478

479 480 481
    ++i;
    nextString = _mode.section(',',i,i);
  }
482

483 484 485 486
  // ================================================================================
  // Mark updated texture as dirty
  // ================================================================================
  texture.dirty = true;
487

488

Dirk Wilden's avatar
Dirk Wilden committed
489 490 491 492
  // 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) );
493

Dirk Wilden's avatar
Dirk Wilden committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
    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;
        }

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

        //only update if the texture is enabled
        if (changed){
          if ( texData->isEnabled(_textureName) )
            emit updateTexture( _textureName, o_it->id() );
          else
            localTex.dirty = true;
        }
      }
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
553 554
}

Dirk Wilden's avatar
Dirk Wilden committed
555 556 557 558 559 560 561
bool TextureControlPlugin::StringToBool(QString _value){
  if (_value == "false")
    return false;
  else
    return true;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
  // 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);

Dirk Wilden's avatar
Dirk Wilden committed
583 584
  bool changed = false;

Dirk Wilden's avatar
Dirk Wilden committed
585 586 587 588 589 590 591
  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" ) {
Dirk Wilden's avatar
Dirk Wilden committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
      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;
      }
    } else if ( sectionName == "type" ) {
Dirk Wilden's avatar
Dirk Wilden committed
627
        if (value == "halfedgebased") {
Dirk Wilden's avatar
Dirk Wilden committed
628 629
          texture.type = HALFEDGEBASED;
          changed = true;
Dirk Wilden's avatar
Dirk Wilden committed
630
        } else {
Dirk Wilden's avatar
Dirk Wilden committed
631 632
          texture.type = VERTEXBASED;
          changed = true;
Dirk Wilden's avatar
Dirk Wilden committed
633 634 635 636 637 638 639 640
        }
    } else
      emit log(LOGERR,"Unknown texture mode : " + sectionName);

    ++i;
    nextString = _mode.section(',',i,i);
  }

Dirk Wilden's avatar
Dirk Wilden committed
641 642 643 644 645 646 647
  //only update if the texture is enabled
  if (changed){
    if ( texData->isEnabled(_textureName) )
      emit updateTexture( _textureName, _id );
    else
      texture.dirty = true;
  }
Dirk Wilden's avatar
Dirk Wilden committed
648 649
}

Jan Möbius's avatar
 
Jan Möbius committed
650
void TextureControlPlugin::pluginsInitialized() {
651 652 653
  // ================================================================================
  // Create global texture menu
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
654
  textureMenu_ = new QMenu(tr("&Texture Control"));
655
  textureMenu_->setTearOffEnabled(true);
Jan Möbius's avatar
 
Jan Möbius committed
656
  emit addMenubarAction(textureMenu_->menuAction(), VIEWMENU );
657

658 659 660 661
  // ================================================================================
  // Create Settings dialog
  // ================================================================================
  // TODO : Settings dialog updates required to change global/local textures,...
Jan Möbius's avatar
 
Jan Möbius committed
662
  settingsDialog_ = new texturePropertiesWidget(0);
663 664
  connect( settingsDialog_, SIGNAL( applyProperties(TextureData*,QString,int) ),
           this,              SLOT( applyDialogSettings(TextureData*,QString,int) ));
665

666 667 668
  // ================================================================================
  // Create action group and menu for global textures
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
669 670 671 672
  actionGroup_ = new QActionGroup( 0 );
  actionGroup_->setExclusive( true );
  connect( actionGroup_, SIGNAL( triggered( QAction * ) ),
          this, SLOT( slotTextureMenu( QAction * ) ) );
673

Jan Möbius's avatar
 
Jan Möbius committed
674 675 676 677
  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);
678

Jan Möbius's avatar
 
Jan Möbius committed
679 680
  textureMenu_->addSeparator();
  textureMenu_->addActions(actionGroup_->actions());
681 682 683 684 685 686 687 688 689

  // ================================================================================
  // 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
690
}
691

Jan Möbius's avatar
Jan Möbius committed
692
void TextureControlPlugin::slotSetTextureProperties() {
693

694
  settingsDialog_->show( &globalTextures_, -1);
Jan Möbius's avatar
 
Jan Möbius committed
695
}
696

697
void TextureControlPlugin::applyDialogSettings(TextureData* _texData, QString _textureName, int _id) {
698

699 700 701 702 703 704 705
  if (_id != -1){
    //local texture
    if ( _texData->isEnabled(_textureName) ){
      slotTextureUpdated( _textureName  , _id );
      emit updateView();
    }else
      _texData->texture( _textureName ).dirty = true;
706

707 708 709 710 711 712 713 714 715 716 717
  } else {
    // global texture

    _texData->texture( _textureName ).dirty = true;

    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
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 756 757 758 759 760 761 762 763 764 765
      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
            texData->texture( _textureName ).dirty = true;
        }
      }
    }
  }
}
766

Jan Möbius's avatar
 
Jan Möbius committed
767
void TextureControlPlugin::slotTextureMenu(QAction* _action) {
768 769
  // call existing function to switch the texture
  slotSwitchTexture( _action->text() );
Jan Möbius's avatar
 
Jan Möbius committed
770 771
}

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

Dirk Wilden's avatar
Dirk Wilden committed
774 775
  std::cerr << "TextureControlPlugin::doSwitchTexture : " << _textureName.toStdString();
  std::cerr << " object=" << _id << std::endl;
776

Dirk Wilden's avatar
Dirk Wilden committed
777 778 779 780 781
  // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
  }
782

Dirk Wilden's avatar
Dirk Wilden committed
783 784 785 786 787 788 789 790
  // ================================================================================
  // 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;
  }
791

Dirk Wilden's avatar
Dirk Wilden committed
792 793 794 795 796 797 798 799 800
  // ================================================================================
  // 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;
    return;
  }
801

Dirk Wilden's avatar
Dirk Wilden committed
802 803 804 805 806 807 808 809 810
  // ================================================================================
  // 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 , obj->id() );
    return;
  }
811

Dirk Wilden's avatar
Dirk Wilden committed
812 813 814 815 816 817
  // ================================================================================
  // Update texture map from meshNode and activate it
  // ================================================================================
  if( obj->dataType( DATA_TRIANGLE_MESH ) ){
    doUpdateTexture(texData->texture(_textureName), *PluginFunctions::triMeshObject(obj)->mesh());
    PluginFunctions::triMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
818 819 820 821 822
//     PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
//     PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
    PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
    PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
    // TODO: Multitexturing has to set the right map here!
Dirk Wilden's avatar
Dirk Wilden committed
823
  }
824

Dirk Wilden's avatar
Dirk Wilden committed
825 826 827
  if ( obj->dataType( DATA_POLY_MESH ) ){
    doUpdateTexture(texData->texture(_textureName), *PluginFunctions::polyMeshObject(obj)->mesh());
    PluginFunctions::polyMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName );
828 829 830 831 832
//     PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
//     PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( texData->propertyMap() );
       PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
       PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
       // TODO: Multitexturing has to set the right map here!
Dirk Wilden's avatar
Dirk Wilden committed
833 834 835
  }

}
836

Dirk Wilden's avatar
Dirk Wilden committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850
void TextureControlPlugin::slotSwitchTexture( QString _textureName , int _id ) {

  std::cerr << "TextureControlPlugin::slotSwitchTexture : " << _textureName.toStdString();
  std::cerr << "object=" << _id << std::endl;

  doSwitchTexture(_textureName, _id);

  // ================================================================================
  // Switch to a texture drawMode
  // ================================================================================
  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 );
Jan Möbius's avatar
 
Jan Möbius committed
851
  }
852

Dirk Wilden's avatar
Dirk Wilden committed
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
  if ( !textureMode )
    PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );

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

void TextureControlPlugin::slotSwitchTexture( QString _textureName ) {

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

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

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

879
  // ================================================================================
880
  // Switch to a texture drawMode
881 882 883 884 885 886 887 888 889
  // ================================================================================
  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 );
890 891 892 893 894 895 896 897 898 899 900 901

  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
902 903 904
}


905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
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 * ) ) );

939 940 941 942 943
  QAction* action = actionGroup->addAction( "Texture Settings" );

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

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

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

948
    action->setCheckable(true);
949

950 951
    if ( texData->textures()[i].enabled )
      action->setChecked(true);
952

953
    contextMenu_->addAction( action );
954 955 956 957
  }
}


958
void TextureControlPlugin::slotTextureContextMenu( QAction * _action ) {
959

960 961 962 963
  // id of object for which the context menu is created, is stored in the action
  QVariant idVariant = _action->data( );
  int id = idVariant.toInt();

964
  std::cerr << "TextureControlPlugin::slotTextureContextMenu : " << id << std::endl;
965

966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
  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 );
  }
991 992 993

}

Jan Möbius's avatar
 
Jan Möbius committed
994 995
Q_EXPORT_PLUGIN2( texturecontrolplugin , TextureControlPlugin );