TextureControl.cc 20.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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

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

void TextureControlPlugin::slotTextureAdded( QString _textureName , QString _filename , uint _dimension)
{
  Texture tex;
  tex.name = _textureName;
  tex.filename = _filename;
  tex.dimension = _dimension;
  tex.scale = true;
  tex.clamp_min = FLT_MIN;
  tex.clamp_max = FLT_MAX;
  tex.clamp = false;
  tex.repeat = false;
  tex.center = false;
  tex.abs = false;
  tex.max_val = 1.0;
  tex.type = VERTEXBASED;
  textures_.push_back( tex );
62

Jan Möbius's avatar
 
Jan Möbius committed
63
  activeTexture_ = _textureName;
64

Jan Möbius's avatar
 
Jan Möbius committed
65 66 67 68 69 70
  QAction* new_texture = new QAction(_textureName, this);
  new_texture->setStatusTip(tr("Switch to this Texture"));
  new_texture->setCheckable(true);
  actionGroup_->addAction(new_texture);
  textureMenu_->addAction(new_texture);
  new_texture->setChecked(true);
71

Jan Möbius's avatar
 
Jan Möbius committed
72
  textureActions_.push_back(new_texture);
73

Jan Möbius's avatar
 
Jan Möbius committed
74 75 76 77 78
  ///@todo if a texture is added later, update or generate it for all objects
//   emit updateTexture(_textureName,-1);
}

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

Jan Möbius's avatar
 
Jan Möbius committed
80 81 82
  BaseObjectData* object;
  if (! PluginFunctions::get_object(  _identifier , object ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_identifier) );
83 84 85
    return;
  }

Jan Möbius's avatar
 
Jan Möbius committed
86 87 88 89 90 91 92 93
  // Search the list of textures if we have the texture
  int textureid = -1;
  for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
    if ( textures_[i].name == _textureName ) {
      textureid = i;
      break;
    }
  }
94 95


Jan Möbius's avatar
 
Jan Möbius committed
96 97 98 99
  if ( textureid == -1 ) {
    emit log(LOGERR,"Unable to find texture with name " + _textureName );
    return;
  }
100

Jan Möbius's avatar
 
Jan Möbius committed
101
  // Only update if the active texture is the current texture
102
  if ( activeTexture_ != _textureName)
Jan Möbius's avatar
 
Jan Möbius committed
103 104 105 106 107
    return;

  const bool repeat = textures_[textureid].repeat;

  if( object->dataType( DATA_TRIANGLE_MESH ) ) {
108

Jan Möbius's avatar
 
Jan Möbius committed
109 110 111
    TriMesh* mesh = PluginFunctions::triMesh(object);
    doUpdateTexture(textureid, *mesh);
    PluginFunctions::triMeshObject(object)->textureNode()->set_repeat(repeat);
112 113
    QString filename = OpenFlipper::Options::textureDir().absolutePath() +
      OpenFlipper::Options::dirSeparator() +
Jan Möbius's avatar
 
Jan Möbius committed
114 115 116
      textures_[textureid].filename;

    PluginFunctions::triMeshObject(object)->textureNode()->read(filename.toUtf8());
117
  }
Jan Möbius's avatar
 
Jan Möbius committed
118 119

  if ( object->dataType( DATA_POLY_MESH ) ) {
120

Jan Möbius's avatar
 
Jan Möbius committed
121 122 123 124
    PolyMesh* mesh = PluginFunctions::polyMesh(object);
    doUpdateTexture(textureid, *mesh);

    PluginFunctions::polyMeshObject(object)->textureNode()->set_repeat(repeat);
125 126
    QString filename = OpenFlipper::Options::textureDir().absolutePath() +
      OpenFlipper::Options::dirSeparator() +
Jan Möbius's avatar
 
Jan Möbius committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
      textures_[textureid].filename;

    PluginFunctions::polyMeshObject(object)->textureNode()->read(filename.toUtf8());
  }
  emit update_view();

}

template< typename MeshT >
void TextureControlPlugin::doUpdateTexture ( int _textureid, MeshT& _mesh )
{
  if ( textures_[_textureid].type == HALFEDGEBASED ) {
    if (textures_[_textureid].dimension == 1) {

      OpenMesh::HPropHandleT< double > texture;
Jan Möbius's avatar
 
Jan Möbius committed
142
	  if ( ! _mesh.get_property_handle(texture, textures_[_textureid].name.toStdString() ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
143 144 145 146 147 148 149 150 151
        emit log(LOGERR,"Unable to get property " + textures_[_textureid].name );
        return;
      }

      copyTexture(_textureid, _mesh, texture);

    } else if ( textures_[_textureid].dimension == 2 ) {

      OpenMesh::HPropHandleT< OpenMesh::Vec2d > texture2D;
Jan Möbius's avatar
 
Jan Möbius committed
152
	  if ( ! _mesh.get_property_handle( texture2D, textures_[_textureid].name.toStdString() ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
153 154 155 156 157 158
        emit log(LOGERR,"Unable to get property " + textures_[_textureid].name);
        return;
      }

      copyTexture(_textureid, _mesh, texture2D);

159
    } else
Jan Möbius's avatar
 
Jan Möbius committed
160 161 162 163 164
      emit log(LOGERR, "Unsupported Texture Dimension " + QString::number(textures_[_textureid].dimension) );
  } else if ( textures_[_textureid].type == VERTEXBASED ) {
    if ( textures_[_textureid].dimension == 1 ) {

      OpenMesh::VPropHandleT< double > texture;
Jan Möbius's avatar
 
Jan Möbius committed
165
	  if ( ! _mesh.get_property_handle(texture,textures_[_textureid].name.toStdString() ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
166
        emit log(LOGERR,"Unable to get property " + textures_[_textureid].name );
167
        return;
Jan Möbius's avatar
 
Jan Möbius committed
168 169 170 171 172 173 174
      }

        copyTexture(_textureid, _mesh, texture);

      } else if ( textures_[_textureid].dimension == 2 ) {

        OpenMesh::VPropHandleT< OpenMesh::Vec2d >  texture2D;
Jan Möbius's avatar
 
Jan Möbius committed
175
		if ( ! _mesh.get_property_handle(texture2D,textures_[_textureid].name.toStdString() ) ) {
Jan Möbius's avatar
 
Jan Möbius committed
176
          emit log(LOGERR,"Unable to get property " + textures_[_textureid].name );
177
          return;
Jan Möbius's avatar
 
Jan Möbius committed
178 179 180 181 182
        }

        copyTexture(_textureid, _mesh, texture2D);

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

Jan Möbius's avatar
 
Jan Möbius committed
184
        OpenMesh::VPropHandleT< OpenMesh::Vec3d >  scalarField3D;
Jan Möbius's avatar
 
Jan Möbius committed
185
        if ( ! _mesh.get_property_handle(scalarField3D,textures_[_textureid].name) ) {
Jan Möbius's avatar
 
Jan Möbius committed
186
          emit log(LOGERR,"Unable to get property " + textures_[_textureid].name );
187
          return;
Jan Möbius's avatar
 
Jan Möbius committed
188
        }
189

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

Jan Möbius's avatar
 
Jan Möbius committed
192 193
      }*/ else
        emit log(LOGERR, "Unsupported Texture Dimension " + QString::number(textures_[_textureid].dimension) );
194

Jan Möbius's avatar
 
Jan Möbius committed
195 196
    } else
      emit log(LOGERR, "Unsupported Texture type");
197

Jan Möbius's avatar
 
Jan Möbius committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
}

template< typename MeshT >
void TextureControlPlugin::copyTexture ( int _textureid, MeshT& _mesh, OpenMesh::VPropHandleT< double > _texProp )
{
  double max,min;
  computeMinMaxScalar(_textureid, _mesh, _texProp, min, max);

  for ( typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it) {
    double value = _mesh.property(_texProp, v_it);
    computeValue(_textureid, min, max, value);
    _mesh.set_texcoord2D( v_it, ACG::Vec2f(float(value), float(value) ) );
  }
}

template< typename MeshT >
Dirk Wilden's avatar
Dirk Wilden committed
214
void TextureControlPlugin::copyTexture ( int /*_textureid*/, MeshT& _mesh, OpenMesh::VPropHandleT< OpenMesh::Vec2d > _texProp )
Jan Möbius's avatar
 
Jan Möbius committed
215 216 217 218 219 220
{
  for ( typename MeshT::VertexIter v_it = _mesh.vertices_begin(); v_it != _mesh.vertices_end(); ++v_it) {
    OpenMesh::Vec2d value = _mesh.property(_texProp, v_it);
    _mesh.set_texcoord2D( v_it, ACG::Vec2f(float(value[0]), float(value[1]) ) );
  }
}
221

Jan Möbius's avatar
 
Jan Möbius committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235
template< typename MeshT >
void TextureControlPlugin::copyTexture ( int _textureid, MeshT& _mesh, OpenMesh::HPropHandleT< double > _texProp )
{
  double max,min;
  computeMinMaxScalar(_textureid, _mesh, _texProp, min, max);

  for ( typename MeshT::HalfedgeIter h_it = _mesh.halfedges_begin(); h_it != _mesh.halfedges_end(); ++h_it) {
    double value = _mesh.property(_texProp, h_it);
    computeValue(_textureid, min, max, value);
    _mesh.set_texcoord2D( h_it, ACG::Vec2f(float(value), float(value) ) );
  }
}

template< typename MeshT >
Dirk Wilden's avatar
Dirk Wilden committed
236
void TextureControlPlugin::copyTexture ( int /*_textureid*/, MeshT& _mesh, OpenMesh::HPropHandleT< OpenMesh::Vec2d > _texProp )
Jan Möbius's avatar
 
Jan Möbius committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250
{
  for ( typename MeshT::HalfedgeIter h_it = _mesh.halfedges_begin(); h_it != _mesh.halfedges_end(); ++h_it) {
    OpenMesh::Vec2d value = _mesh.property(_texProp, h_it);
    _mesh.set_texcoord2D( h_it, ACG::Vec2f(float(value[0]), float(value[1]) ) );
  }
}

template< typename MeshT >
void TextureControlPlugin::computeMinMaxScalar(int _textureid, MeshT& _mesh,OpenMesh::VPropHandleT< double > _texture,
                                                               double& _min , double& _max) {
   const bool   abs = textures_[_textureid].abs;
   const bool   clamp = textures_[_textureid].clamp ;
   const double clamp_max = textures_[_textureid].clamp_max;
   const double clamp_min = textures_[_textureid].clamp_min;
251

Jan Möbius's avatar
 
Jan Möbius committed
252 253
   _max = FLT_MIN;
   _min = FLT_MAX;
254

Jan Möbius's avatar
 
Jan Möbius committed
255 256 257 258 259 260 261 262
   for ( typename MeshT::VertexIter v_it = _mesh.vertices_begin() ; v_it != _mesh.vertices_end(); ++v_it) {
      if ( abs ) {
         _max = std::max( fabs(_mesh.property(_texture,v_it)) , _max);
         _min = std::min( fabs(_mesh.property(_texture,v_it)) , _min);
      } else {
         _max = std::max( _mesh.property(_texture,v_it) , _max);
         _min = std::min( _mesh.property(_texture,v_it) , _min);
      }
263 264
   }

Jan Möbius's avatar
 
Jan Möbius committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
   if ( clamp ) {
      if ( _max > clamp_max )
         _max = clamp_max;
      if (_min < clamp_min)
         _min = clamp_min;
   }
}

template< typename MeshT >
void TextureControlPlugin::computeMinMaxScalar(int _textureid, MeshT& _mesh,OpenMesh::HPropHandleT< double > _texture,
                                                               double& _min , double& _max) {
   const bool   abs = textures_[_textureid].abs;
   const bool   clamp = textures_[_textureid].clamp ;
   const double clamp_max = textures_[_textureid].clamp_max;
   const double clamp_min = textures_[_textureid].clamp_min;
280

Jan Möbius's avatar
 
Jan Möbius committed
281 282
   _max = FLT_MIN;
   _min = FLT_MAX;
283

Jan Möbius's avatar
 
Jan Möbius committed
284 285 286 287 288 289 290 291
   for ( typename MeshT::HalfedgeIter h_it = _mesh.halfedges_begin() ; h_it != _mesh.halfedges_end(); ++h_it) {
      if ( abs ) {
         _max = std::max( fabs(_mesh.property(_texture,h_it)) , _max);
         _min = std::min( fabs(_mesh.property(_texture,h_it)) , _min);
      } else {
         _max = std::max( _mesh.property(_texture,h_it) , _max);
         _min = std::min( _mesh.property(_texture,h_it) , _min);
      }
292 293
   }

Jan Möbius's avatar
 
Jan Möbius committed
294 295 296 297 298 299
   if ( clamp ) {
      if ( _max > clamp_max )
         _max = clamp_max;
      if (_min < clamp_min)
         _min = clamp_min;
   }
300 301
}

Jan Möbius's avatar
 
Jan Möbius committed
302 303 304 305 306 307 308 309 310
void TextureControlPlugin::computeValue(int _textureid, double _min, double _max, double& _value) {
   const bool clamp = textures_[_textureid].clamp ;
   const bool center = textures_[_textureid].center;
   const double max_val = textures_[_textureid].max_val;
   const bool abs = textures_[_textureid].abs;
   const double clamp_max = textures_[_textureid].clamp_max;
   const double clamp_min = textures_[_textureid].clamp_min;
   const double scale = fabs(_max) + fabs(_min);
   const bool repeat = textures_[_textureid].repeat;
311

Jan Möbius's avatar
 
Jan Möbius committed
312 313 314
   // Use absolute value as requested by plugin
   if ( abs )
      _value = fabs(_value);
315

Jan Möbius's avatar
 
Jan Möbius committed
316 317 318 319 320 321 322
   // Clamp if requested
   if ( clamp ) {
      if ( _value > clamp_max )
         _value = clamp_max;
      if (_value < clamp_min)
         _value = clamp_min;
   }
323

Jan Möbius's avatar
 
Jan Möbius committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
   // if the texture should not be repeated, scale to 0..1
   if ( ! repeat ) {
      if (! center ) {
        if ( textures_[_textureid].scale) {
          _value /= scale;
          _value -= _min/scale;
        }
      } 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)
348
{
Jan Möbius's avatar
 
Jan Möbius committed
349 350 351 352
   // Object erased, so do nothing
   if ( _identifier == -1 ) {
      return;
   }
353

Jan Möbius's avatar
 
Jan Möbius committed
354 355 356 357
   // Force an update of all textures which are available for the updated object
   for ( uint i = 0 ; i < textures_.size() ; ++i ) {
      emit updateTexture( textures_[i].name , _identifier );
   }
358

Jan Möbius's avatar
 
Jan Möbius committed
359 360 361 362
   emit update_view();
}

void TextureControlPlugin::slotUpdateAllTextures( ) {
363 364 365
   // Force an update of all textures which are available for the updated object
   for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objects_end(); ++o_it)
      for ( uint i = 0 ; i < textures_.size() ; ++i )
Jan Möbius's avatar
 
Jan Möbius committed
366 367 368 369 370 371 372 373 374 375 376
         emit updateTexture( textures_[i].name , o_it->id() );
}

void TextureControlPlugin::slotSetTextureMode(QString _textureName ,QString _mode) {
   int textureid = -1;
   for ( int j = 0 ; j < (int)textures_.size() ; ++j ) {
        if ( textures_[j].name == _textureName ) {
             textureid = j;
             break;
        }
   }
377

Jan Möbius's avatar
 
Jan Möbius committed
378 379 380 381
   if ( textureid == -1 ) {
     emit log(LOGERR,"Texture Mode setting requested for " + _textureName + " but texture not found" );
     return;
   }
382

Jan Möbius's avatar
 
Jan Möbius committed
383 384 385 386 387
   int i = 0;
   QString nextString = _mode.section(',',i,i);
   while ( nextString != "" ) {
      QString sectionName = nextString.section('=',0,0);
      QString value = nextString.section('=',1,1);
388

Jan Möbius's avatar
 
Jan Möbius committed
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
      if ( sectionName == "clamp" ) {
         if (value == "false") {
              textures_[textureid].clamp = false;
         } else {
              textures_[textureid].clamp = true;
         }
      } else
      if ( sectionName == "clamp_max" ) {
              textures_[textureid].clamp_max = value.toDouble();
      } else
      if ( sectionName == "clamp_min" ) {
              textures_[textureid].clamp_min = value.toDouble();
      } else
      if ( sectionName == "max_val" ) {
              textures_[textureid].max_val = value.toDouble();
      } else
      if ( sectionName == "repeat" ) {
         if (value == "false") {
              textures_[textureid].repeat = false;
         } else {
              textures_[textureid].repeat = true;
410
         }
Jan Möbius's avatar
 
Jan Möbius committed
411 412 413 414 415 416
      } else
      if ( sectionName == "center" ) {
         if (value == "false") {
              textures_[textureid].center = false;
         } else {
              textures_[textureid].center = true;
417
         }
Jan Möbius's avatar
 
Jan Möbius committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
      } else
      if ( sectionName == "scale" ) {
         if (value == "false") {
              textures_[textureid].scale = false;
         } else {
              textures_[textureid].scale = true;
         }
      } else
      if ( sectionName == "type" ) {
         if (value == "halfedgebased") {
              textures_[textureid].type = HALFEDGEBASED;
         } else {
              textures_[textureid].type = VERTEXBASED;
         }
      } else
        emit log(LOGERR,"Unknown texture mode : " + sectionName);
434

Jan Möbius's avatar
 
Jan Möbius committed
435 436 437
      ++i;
      nextString = _mode.section(',',i,i);
   }
438

Jan Möbius's avatar
 
Jan Möbius committed
439
   if ( activeTexture_ == _textureName ) {
440 441 442

      // Force an update of all objects
      for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objects_end(); ++o_it)
Jan Möbius's avatar
 
Jan Möbius committed
443
         updateTexture(  _textureName , o_it->id() );
444

Jan Möbius's avatar
 
Jan Möbius committed
445 446 447 448 449 450 451
      emit update_view();
   }
}

void TextureControlPlugin::pluginsInitialized() {
  // Texture menu
  textureMenu_ = new QMenu(tr("&Texture Control"));
452

453
  textureMenu_->setTearOffEnabled(true);
454 455 456

  emit addMenu( textureMenu_ , VIEWMENU );

Jan Möbius's avatar
 
Jan Möbius committed
457
  settingsDialog_ = new texturePropertiesWidget(0);
458

Jan Möbius's avatar
 
Jan Möbius committed
459 460 461
  connect(settingsDialog_->okButton,SIGNAL(clicked()), this , SLOT (slotTexturePropertiesOk() ) );
  connect(settingsDialog_->applyButton,SIGNAL(clicked()), this , SLOT (slotTexturePropertiesApply() ) );
  connect(settingsDialog_->cancelButton,SIGNAL(clicked()), this , SLOT (slotTexturePropertiesCancel() ) );
462

Jan Möbius's avatar
 
Jan Möbius committed
463 464 465 466
  actionGroup_ = new QActionGroup( 0 );
  actionGroup_->setExclusive( true );
  connect( actionGroup_, SIGNAL( triggered( QAction * ) ),
          this, SLOT( slotTextureMenu( QAction * ) ) );
467

Jan Möbius's avatar
 
Jan Möbius committed
468 469 470 471
  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);
472

Jan Möbius's avatar
 
Jan Möbius committed
473 474 475
  textureMenu_->addSeparator();
  textureMenu_->addActions(actionGroup_->actions());
}
476

Jan Möbius's avatar
 
Jan Möbius committed
477 478 479
 void TextureControlPlugin::updateDialog() {
   if ( textures_.size() == 0 )
      return;
480

Jan Möbius's avatar
 
Jan Möbius committed
481 482 483 484 485 486 487
   int textureid = -1;
   for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
        if ( textures_[i].name == activeTexture_ ) {
             textureid = i;
             break;
        }
   }
488

Jan Möbius's avatar
 
Jan Möbius committed
489 490
  if ( textureid == -1 ) {
     emit log(LOGERR,"Active Texture not found");
491
     return;
Jan Möbius's avatar
 
Jan Möbius committed
492
  }
493

Jan Möbius's avatar
 
Jan Möbius committed
494 495 496 497 498 499 500 501 502 503
   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);
504
   settingsDialog_->clamp_max->setText(tmp);
Jan Möbius's avatar
 
Jan Möbius committed
505
 }
506

Jan Möbius's avatar
 
Jan Möbius committed
507 508 509 510
 void TextureControlPlugin::slotSetTextureProperties() {
   updateDialog();
   if ( textures_.size() == 0 )
      return;
511

Jan Möbius's avatar
 
Jan Möbius committed
512 513
   settingsDialog_->show();
 }
514

Jan Möbius's avatar
 
Jan Möbius committed
515 516 517
void TextureControlPlugin::applyDialogSettings() {
   if ( textures_.size() == 0 )
      return;
518

Jan Möbius's avatar
 
Jan Möbius committed
519 520 521 522 523 524 525
   int textureid = -1;
   for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
         if ( textures_[i].name == activeTexture_ ) {
               textureid = i;
               break;
         }
   }
526

Jan Möbius's avatar
 
Jan Möbius committed
527 528 529
   if (textureid == -1) {
      emit log(LOGERR,"Unable to get active Texture");
   }
530

Jan Möbius's avatar
 
Jan Möbius committed
531 532 533 534 535
   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();
536

Jan Möbius's avatar
 
Jan Möbius committed
537 538 539
   QString tmp;
   tmp = settingsDialog_->max_val->text();
   textures_[textureid].max_val = tmp.toDouble();
540

Jan Möbius's avatar
 
Jan Möbius committed
541 542
   tmp = settingsDialog_->clamp_min->text();
   textures_[textureid].clamp_min = tmp.toDouble();
543

Jan Möbius's avatar
 
Jan Möbius committed
544 545
   tmp = settingsDialog_->clamp_max->text();
   textures_[textureid].clamp_max = tmp.toDouble();
546

Jan Möbius's avatar
 
Jan Möbius committed
547
   // Update the corresponding meshes
548
   for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objects_end(); ++o_it)
Jan Möbius's avatar
 
Jan Möbius committed
549
      slotTextureUpdated(  activeTexture_ , o_it->id() );
550

Jan Möbius's avatar
 
Jan Möbius committed
551 552
   emit update_view();
}
553

Jan Möbius's avatar
 
Jan Möbius committed
554 555 556 557
 void TextureControlPlugin::slotTexturePropertiesOk() {
   applyDialogSettings();
   settingsDialog_->hide();
}
558

Jan Möbius's avatar
 
Jan Möbius committed
559 560 561
  void TextureControlPlugin::slotTexturePropertiesApply() {
     applyDialogSettings();
 }
562

Jan Möbius's avatar
 
Jan Möbius committed
563 564 565 566
 void TextureControlPlugin::slotTexturePropertiesCancel() {
    settingsDialog_->hide();
 }

567

Jan Möbius's avatar
 
Jan Möbius committed
568 569 570
void TextureControlPlugin::slotTextureMenu(QAction* _action) {
  if ( activeTexture_ !=  _action->text() ) {
      activeTexture_ =  _action->text();
571 572 573

      // Force an update of all objects
      for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objects_end(); ++o_it)
Jan Möbius's avatar
 
Jan Möbius committed
574
         slotTextureUpdated(  _action->text() , o_it->id() );
575

Jan Möbius's avatar
 
Jan Möbius committed
576
      updateDialog();
577

578
      PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
Jan Möbius's avatar
 
Jan Möbius committed
579 580 581 582 583 584 585 586 587 588 589
  }
}

void TextureControlPlugin::slotSwitchTexture( QString _textureName ) {
  int textureid = -1;
  for ( int i = 0 ; i < (int)textures_.size() ; ++i ) {
        if ( textures_[i].name == _textureName ) {
              textureid = i;
              break;
        }
  }
590

Jan Möbius's avatar
 
Jan Möbius committed
591 592
  if (textureid == -1) {
    emit log(LOGERR,"Unable to switch to texture " + _textureName + " (not found)");
593
    return;
Jan Möbius's avatar
 
Jan Möbius committed
594
  }
595

596
  PluginFunctions::setDrawMode( ACG::SceneGraph::DrawModes::SOLID_TEXTURED_SHADED );
597

Jan Möbius's avatar
 
Jan Möbius committed
598 599
  if ( activeTexture_ !=  _textureName ) {
    activeTexture_ =  _textureName;
600 601 602

    // Force an update of all objects
    for ( PluginFunctions::ObjectIterator o_it(PluginFunctions::ALL_OBJECTS) ; o_it != PluginFunctions::objects_end(); ++o_it)
Jan Möbius's avatar
 
Jan Möbius committed
603
        slotTextureUpdated(  _textureName , o_it->id() );
604

Jan Möbius's avatar
 
Jan Möbius committed
605
    updateDialog();
606 607 608 609 610 611 612

    QList<QAction *> menuEntries = actionGroup_->actions();

    for ( int i = 0 ; i < menuEntries.size(); ++i ) {
      if ( menuEntries[i]->text() == _textureName )
        menuEntries[i]->setChecked(true);
    }
Jan Möbius's avatar
 
Jan Möbius committed
613 614 615 616 617 618
  }
}


Q_EXPORT_PLUGIN2( texturecontrolplugin , TextureControlPlugin );