TextureControl.cc 44.3 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//=============================================================================
//
//                               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
#include <math.h>

48
49
50
51
#define TEXTUREDATA "TextureData"



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

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

68
  if ( texData->textureExists(_textureName) ) {
Dirk Wilden's avatar
Dirk Wilden committed
69
70
71
    emit log(LOGERR,"Trying to add already existing texture " + _textureName + " for object " + QString::number(_id) );
    return;
  }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

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

  QImage textureImage;
  getImage(_filename,textureImage);

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

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

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

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

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

  texData->addTexture(_textureName,_filename,_dimension,glName);
102
  texData->texture(_textureName).disable();
Dirk Wilden's avatar
Dirk Wilden committed
103
104
}

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

    QImage textureImage;
113
    getImage(_filename,textureImage);
114
115
    globalTextures_.texture(_textureName).textureImage = textureImage;

116
117
118
119
  } else {
    emit log(LOGERR,"Trying to add already existing global texture " + _textureName );
    return;
  }
120

121
  // Add a new entry to the global Texture menu
Jan Möbius's avatar
 
Jan Möbius committed
122
  QAction* new_texture = new QAction(_textureName, this);
123
  new_texture->setStatusTip(tr("Switch all objects to this Texture ( if available )"));
Jan Möbius's avatar
 
Jan Möbius committed
124
125
126
127
128
  new_texture->setCheckable(true);
  actionGroup_->addAction(new_texture);
  textureMenu_->addAction(new_texture);
  new_texture->setChecked(true);
  textureActions_.push_back(new_texture);
129

130
}
131

132
void TextureControlPlugin::slotMultiTextureAdded( QString _textureGroup , QString _name , QString _filename , int _id , int& _textureId ) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
   // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
  }

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

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

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

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

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

162
163
164
  //hide the texture (its accessible through the multiTexture)
  texData->texture(_name).hidden( true );

165
166
167
168
169
170
171
172
173
174
175
176
177
  QImage textureImage;
  getImage(_filename,textureImage);
  texData->texture(_name).textureImage = textureImage;

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

}

178
void TextureControlPlugin::getImage( QString _fileName, QImage& _image ) {
179
180
  QString loadFilename;

181
  if ( _fileName.startsWith("/") || _fileName.startsWith(".") )
182
183
184
185
186
187
188
189
190
191
192
193
194
    loadFilename = _fileName;
  else
    loadFilename = OpenFlipper::Options::textureDirStr() + QDir::separator() + _fileName;

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

}

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

197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
template< typename MeshT >
void TextureControlPlugin::handleFileOpenTextures( MeshT*& _mesh , int _objectId ) {
  // If this property is available we have a mapping between face_index_property and
  // available textures stored in the map
  OpenMesh::MPropHandleT< std::map< int, std::string > > property;
  if ( _mesh->get_property_handle(property,"TextureMapping") ) {

    // As internal texture indices might differ from the available ones,
    // We have to remap them after loading the textures!
    std::map< int,int > newMapping;

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

      // Convert the indices stored in the mesh to the actual ones used for rendering
      OpenMesh::FPropHandleT< int > newIndexProperty;
      if (! _mesh->get_property_handle(newIndexProperty,"TextureControl: OriginalFileIndexMapping") ) {
        _mesh->add_property(newIndexProperty,"TextureControl: OriginalFileIndexMapping");
      }

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

      // We use a different property for storing the IndexProperty to prevent overwriting them
      slotSetTextureMode("OBJ Data","indexProperty=TextureControl: OriginalFileIndexMapping", _objectId);
      std::cerr << "Done" << std::endl;
    }
  }

}

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

238
239
240
241
242
243
244
245
246
247
  // 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;
248
249
  }

250
251
252
253
254
255
256
  // 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);
  }

257
258
259
260
261
262
263
264
265
266
267
  // Check if the file contains a texture map and handle it before adding global textures
  if( obj->dataType( DATA_TRIANGLE_MESH ) ) {
      TriMesh* mesh = PluginFunctions::triMesh(obj);
      if ( mesh )
        handleFileOpenTextures(mesh,_id);
  } else if ( obj->dataType( DATA_POLY_MESH ) ) {
    PolyMesh* mesh = PluginFunctions::polyMesh(obj);
    if ( mesh )
      handleFileOpenTextures(mesh,_id);
  }

268
269
270
271
272
273
274
275
  // Iterate over all available global textures and add them to the object
  for ( uint i = 0 ; i < globalTextures_.textures().size() ; ++i) {

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

    QImage textureImage;
276
    getImage(globalTextures_.textures()[i].filename(),textureImage);
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

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

294
    if (glName != 0) {
295
      texData->addTexture(globalTextures_.textures()[i], glName);
296
      texData->setImage(globalTextures_.textures()[i].name(),textureImage);
297
298
299
300
301
302
    }
    else {
      emit log(LOGERR,"Unable to bind Texture");
      continue;
    }

303
304
305
306
307

    // ================================================================================
    // Update texture mapping in meshNode
    // ================================================================================
    if( obj->dataType( DATA_TRIANGLE_MESH ) ){
308
309
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
310
311
312
    }

    if ( obj->dataType( DATA_POLY_MESH ) ){
313
314
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
315
316
    }

Jan Möbius's avatar
 
Jan Möbius committed
317
  }
318
319
320
}

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

322
323
324
325
326
327
  // ================================================================================
  // 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
328
329
    return;
  }
330

331
332
333
334
335
336
  // ================================================================================
  // 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
337
    return;
338
  }
Jan Möbius's avatar
 
Jan Möbius committed
339

340
341
342
  // ================================================================================
  // Check if texture exists
  // ================================================================================
343
  if ( ! texData->textureExists(_textureName) ) {
344
    emit log(LOGERR,"Texture " + _textureName + " not found on object " + QString::number(_identifier) );
345
    return;
346
  }
Jan Möbius's avatar
 
Jan Möbius committed
347

348
349
350
  // ================================================================================
  // If texture is not enabled, mark it as dirty and defer update to visualization update
  // ================================================================================
351
  if ( ! texData->texture(_textureName).enabled() ) {
352
    texData->texture(_textureName).setDirty();
353
354
    return;
  }
Jan Möbius's avatar
 
Jan Möbius committed
355

356
357
358
  QImage textureImage;
  getImage( texData->texture(_textureName).filename(), textureImage);

359
360
361
362
363
364
365
  // ================================================================================
  // 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);
366
    PluginFunctions::triMeshObject(obj)->textureNode()->set_texture( textureImage );
367
368
369
370
  } 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);
371
    PluginFunctions::polyMeshObject(obj)->textureNode()->set_texture( textureImage );
372
  }
Jan Möbius's avatar
 
Jan Möbius committed
373

374
375
376
  // ================================================================================
  // Mark texture as not dirty
  // ================================================================================
377
  texData->texture(_textureName).clean();
Jan Möbius's avatar
 
Jan Möbius committed
378

379
380
381
382
  // ================================================================================
  // Enable the right draw mode and update
  // ================================================================================
  switchDrawMode(false);
Jan Möbius's avatar
 
Jan Möbius committed
383
384
385
}

template< typename MeshT >
386
void TextureControlPlugin::doUpdateTexture ( Texture& _texture, MeshT& _mesh )
Jan Möbius's avatar
 
Jan Möbius committed
387
{
388

389
  if ( _texture.type() == HALFEDGEBASED ) {
390
    if (_texture.dimension() == 1) {
Jan Möbius's avatar
 
Jan Möbius committed
391
392

      OpenMesh::HPropHandleT< double > texture;
393
394
	  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
395
396
397
        return;
      }

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

400
    } else if ( _texture.dimension() == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
401
402

      OpenMesh::HPropHandleT< OpenMesh::Vec2d > texture2D;
403
404
	  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
405
406
407
        return;
      }

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

410
    } else
411
      emit log(LOGERR, "Unsupported Texture Dimension " + QString::number(_texture.dimension() ) );
412
  } else if ( _texture.type() == VERTEXBASED ) {
413
    if ( _texture.dimension() == 1 ) {
Jan Möbius's avatar
 
Jan Möbius committed
414
415

      OpenMesh::VPropHandleT< double > texture;
416
417
	  if ( ! _mesh.get_property_handle(texture,_texture.name().toStdString() ) ) {
        emit log(LOGERR,"Unable to get property " + _texture.name() );
418
        return;
Jan Möbius's avatar
 
Jan Möbius committed
419
420
      }

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

423
      } else if ( _texture.dimension() == 2 ) {
Jan Möbius's avatar
 
Jan Möbius committed
424
425

        OpenMesh::VPropHandleT< OpenMesh::Vec2d >  texture2D;
426
427
		  if ( ! _mesh.get_property_handle(texture2D,_texture.name().toStdString() ) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name() );
428
          return;
Jan Möbius's avatar
 
Jan Möbius committed
429
430
        }

431
        copyTexture( _texture, _mesh, texture2D);
Jan Möbius's avatar
 
Jan Möbius committed
432
433

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

Jan Möbius's avatar
 
Jan Möbius committed
435
        OpenMesh::VPropHandleT< OpenMesh::Vec3d >  scalarField3D;
436
437
        if ( ! _mesh.get_property_handle(scalarField3D,_texture.name) ) {
          emit log(LOGERR,"Unable to get property " + _texture.name );
438
          return;
Jan Möbius's avatar
 
Jan Möbius committed
439
        }
440

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

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

Jan Möbius's avatar
 
Jan Möbius committed
446
447
    } else
      emit log(LOGERR, "Unsupported Texture type");
448

Jan Möbius's avatar
 
Jan Möbius committed
449
450
}

451
452
453
454
455
456
457
458
459
460
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;
461

Jan Möbius's avatar
 
Jan Möbius committed
462
463
464
   // Use absolute value as requested by plugin
   if ( abs )
      _value = fabs(_value);
465

Jan Möbius's avatar
 
Jan Möbius committed
466
467
468
469
470
471
472
   // Clamp if requested
   if ( clamp ) {
      if ( _value > clamp_max )
         _value = clamp_max;
      if (_value < clamp_min)
         _value = clamp_min;
   }
473

Jan Möbius's avatar
 
Jan Möbius committed
474
475
476
   // if the texture should not be repeated, scale to 0..1
   if ( ! repeat ) {
      if (! center ) {
477
478
479
        if ( scale) {
          _value /= scaleFactor;
          _value -= _min/scaleFactor;
Jan Möbius's avatar
 
Jan Möbius committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
        }
      } 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)
498
{
499
500
501
502
503
  // ================================================================================
  // Get updated object
  // ================================================================================
  if ( _identifier == -1 )
    return;
504

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
  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
525
  // basic check implemented
526
  for ( uint i = 0; i < texData->textures().size(); ++i ) {
527
    texData->textures()[i].setDirty();
528
529
530
531
532
533
534

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

535
    if ( update && texData->textures()[i].enabled() )
536
      emit updateTexture( texData->textures()[i].name() , _identifier );
537
  }
538

Jan Möbius's avatar
 
Jan Möbius committed
539
540
541
}

void TextureControlPlugin::slotUpdateAllTextures( ) {
542
543
544
545
546
547
  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
548
549
550
}

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

552
553
554
555
556
557
558
  // ================================================================================
  // Update texture settings for global textures
  // ================================================================================
  if ( ! globalTextures_.textureExists(_textureName) ) {
    emit log(LOGERR,"Global texture does not exist: " + _textureName);
    return;
  }
559

560
561
562
563
564
565
566
567
568
569
570
571
  // ================================================================================
  // 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
572
      texture.parameters.clamp = StringToBool(value);
573
574
    } else
    if ( sectionName == "clamp_max" ) {
Dirk Wilden's avatar
Dirk Wilden committed
575
      texture.parameters.clamp_max = value.toDouble();
576
577
    } else
    if ( sectionName == "clamp_min" ) {
Dirk Wilden's avatar
Dirk Wilden committed
578
      texture.parameters.clamp_min = value.toDouble();
579
580
    } else
    if ( sectionName == "max_val" ) {
Dirk Wilden's avatar
Dirk Wilden committed
581
      texture.parameters.max_val = value.toDouble();
582
583
    } else
    if ( sectionName == "repeat" ) {
Dirk Wilden's avatar
Dirk Wilden committed
584
      texture.parameters.repeat = StringToBool(value);
585
586
    } else
    if ( sectionName == "center" ) {
Dirk Wilden's avatar
Dirk Wilden committed
587
      texture.parameters.center = StringToBool(value);
588
589
    } else
    if ( sectionName == "scale" ) {
Dirk Wilden's avatar
Dirk Wilden committed
590
      texture.parameters.scale = StringToBool(value);
591
592
593
    } else
    if ( sectionName == "type" ) {
        if (value == "halfedgebased") {
594
            texture.type( HALFEDGEBASED );
595
        } else {
596
            texture.type( VERTEXBASED );
597
598
599
        }
    } else
      emit log(LOGERR,"Unknown texture mode : " + sectionName);
600

601
602
603
    ++i;
    nextString = _mode.section(',',i,i);
  }
604

605
606
607
  // ================================================================================
  // Mark updated texture as dirty
  // ================================================================================
608
  texture.setDirty();
609

610

Dirk Wilden's avatar
Dirk Wilden committed
611
612
613
614
  // 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) );
615

Dirk Wilden's avatar
Dirk Wilden committed
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
    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;
        }

660
661
        if ( _mode.contains("type") && (texture.type() != localTex.type() ) ){
          localTex.type( texture.type() );
Dirk Wilden's avatar
Dirk Wilden committed
662
663
664
665
666
667
668
669
          changed = true;
        }

        //only update if the texture is enabled
        if (changed){
          if ( texData->isEnabled(_textureName) )
            emit updateTexture( _textureName, o_it->id() );
          else
670
            localTex.setDirty();
Dirk Wilden's avatar
Dirk Wilden committed
671
672
673
674
        }
      }
    }
  }
Jan Möbius's avatar
 
Jan Möbius committed
675
676
}

Dirk Wilden's avatar
Dirk Wilden committed
677
678
679
680
681
682
683
bool TextureControlPlugin::StringToBool(QString _value){
  if (_value == "false")
    return false;
  else
    return true;
}

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

Dirk Wilden's avatar
Dirk Wilden committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
  // 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
705
706
  bool changed = false;

Dirk Wilden's avatar
Dirk Wilden committed
707
708
709
710
711
712
713
  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
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
      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;
      }
748
749
750
751
752
    }else if ( sectionName == "indexProperty" ) {
      if ( value != texture.indexMappingProperty() ) {
        texture.indexMappingProperty( value );
        changed = true;
      }
Dirk Wilden's avatar
Dirk Wilden committed
753
    } else if ( sectionName == "type" ) {
Dirk Wilden's avatar
Dirk Wilden committed
754
        if (value == "halfedgebased") {
755
          texture.type( HALFEDGEBASED );
Dirk Wilden's avatar
Dirk Wilden committed
756
          changed = true;
Dirk Wilden's avatar
Dirk Wilden committed
757
        } else {
758
          texture.type( VERTEXBASED );
Dirk Wilden's avatar
Dirk Wilden committed
759
          changed = true;
Dirk Wilden's avatar
Dirk Wilden committed
760
761
762
763
764
765
766
767
        }
    } else
      emit log(LOGERR,"Unknown texture mode : " + sectionName);

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

Dirk Wilden's avatar
Dirk Wilden committed
768
769
770
771
772
  //only update if the texture is enabled
  if (changed){
    if ( texData->isEnabled(_textureName) )
      emit updateTexture( _textureName, _id );
    else
773
      texture.setDirty();
Dirk Wilden's avatar
Dirk Wilden committed
774
  }
Dirk Wilden's avatar
Dirk Wilden committed
775
776
}

Jan Möbius's avatar
 
Jan Möbius committed
777
void TextureControlPlugin::pluginsInitialized() {
778
779
780
  // ================================================================================
  // Create global texture menu
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
781
  textureMenu_ = new QMenu(tr("&Texture Control"));
782
  textureMenu_->setTearOffEnabled(true);
Jan Möbius's avatar
   
Jan Möbius committed
783
  emit addMenubarAction(textureMenu_->menuAction(), VIEWMENU );
784

785
786
787
788
  // ================================================================================
  // Create Settings dialog
  // ================================================================================
  // TODO : Settings dialog updates required to change global/local textures,...
Jan Möbius's avatar
 
Jan Möbius committed
789
  settingsDialog_ = new texturePropertiesWidget(0);
790
791
  connect( settingsDialog_, SIGNAL( applyProperties(TextureData*,QString,int) ),
           this,              SLOT( applyDialogSettings(TextureData*,QString,int) ));
792

793
794
795
796
797
  connect( settingsDialog_, SIGNAL( getCoordinates1D(QString,int,std::vector< double >&)),
           this,              SLOT( getCoordinates1D(QString,int,std::vector< double >&)));

  settingsDialog_->installEventFilter( this );

798
799
800
  // ================================================================================
  // Create action group and menu for global textures
  // ================================================================================
Jan Möbius's avatar
 
Jan Möbius committed
801
802
803
804
  actionGroup_ = new QActionGroup( 0 );
  actionGroup_->setExclusive( true );
  connect( actionGroup_, SIGNAL( triggered( QAction * ) ),
          this, SLOT( slotTextureMenu( QAction * ) ) );
805

Jan Möbius's avatar
 
Jan Möbius committed
806
807
808
809
  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);
810

Jan Möbius's avatar
 
Jan Möbius committed
811
812
  textureMenu_->addSeparator();
  textureMenu_->addActions(actionGroup_->actions());
813
814
815
816
817
818
819
820
821

  // ================================================================================
  // 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
822
}
823

Jan Möbius's avatar
Jan Möbius committed
824
void TextureControlPlugin::slotSetTextureProperties() {
825

826
  settingsDialog_->show( &globalTextures_, -1);
Jan Möbius's avatar
 
Jan Möbius committed
827
}
828

829
void TextureControlPlugin::applyDialogSettings(TextureData* _texData, QString _textureName, int _id) {
830

831
832
833
834
835
836
  if (_id != -1){
    //local texture
    if ( _texData->isEnabled(_textureName) ){
      slotTextureUpdated( _textureName  , _id );
      emit updateView();
    }else
837
      _texData->texture( _textureName ).setDirty();
838

839
840
841
  } else {
    // global texture

842
    _texData->texture( _textureName ).setDirty();
843
844
845
846
847
848
849

    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
850

851
852
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
879
880
881
882
883
884
885
886
887
888
889
890
891
      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
892
            texData->texture( _textureName ).setDirty();
893
894
895
896
897
        }
      }
    }
  }
}
898

Jan Möbius's avatar
 
Jan Möbius committed
899
void TextureControlPlugin::slotTextureMenu(QAction* _action) {
900
901
  // call existing function to switch the texture
  slotSwitchTexture( _action->text() );
Jan Möbius's avatar
 
Jan Möbius committed
902
903
}

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

Dirk Wilden's avatar
Dirk Wilden committed
906
907
908
909
910
  // Get the new object
  BaseObjectData* obj;
  if (! PluginFunctions::getObject(  _id , obj ) ) {
    emit log(LOGERR,"Unable to get Object for id " + QString::number(_id) );
  }
911

Dirk Wilden's avatar
Dirk Wilden committed
912
913
914
915
916
917
918
919
  // ================================================================================
  // 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;
  }
920

Dirk Wilden's avatar
Dirk Wilden committed
921
  // ================================================================================
922
  // Check for requested Texture
Dirk Wilden's avatar
Dirk Wilden committed
923
  // ================================================================================
924
925
  if ( !texData->textureExists(_textureName) ) {
    emit log(LOGERR, "Texture not available! " + _textureName );
Dirk Wilden's avatar
Dirk Wilden committed
926
927
    return;
  }
928

929
930
931
  // ================================================================================
  // Enable the given texture exclusively or use multitexture setting
  // ================================================================================
932
  bool multiTextureMode = ( texData->texture(_textureName).type() == MULTITEXTURE );
933
934
935
936
937
938
939
940
941
942
  if ( !multiTextureMode ) {
    if ( ! texData->enableTexture( _textureName , true ) ) {
      emit log(LOGERR, "Failed to enabled Texture " + _textureName );
      return;
    }
  } else {
    // get the list of textures for this mode
    QStringList textureList = texData->texture(_textureName).multiTextureList;

    for ( uint i = 0 ; i < texData->textures().size() ; ++i ) {
943
944
      if ( textureList.contains( texData->textures()[i].name() ) )
        texData->enableTexture( texData->textures()[i].name() , false );
945
       else
946
        texData->disableTexture( texData->textures()[i].name() );
947
948
949
950
951
952

    }

    std::cerr << "Trying to enable multitexture" << std::endl;
  }

Dirk Wilden's avatar
Dirk Wilden committed
953
954
955
956
  // ================================================================================
  // If texture is flagged dirty, update it ( this jumps to texture updated
  // which will update the visualization )
  // ================================================================================
957

958
  if ( !multiTextureMode && texData->texture( _textureName).dirty() ) {
959

Dirk Wilden's avatar
Dirk Wilden committed
960
    // TODO: maybe introduce lock to prevent extra redraws if updating all objects
961
    emit updateTexture( texData->texture( _textureName ).name() , obj->id() );
Dirk Wilden's avatar
Dirk Wilden committed
962
963
    return;
  }
964

Dirk Wilden's avatar
Dirk Wilden committed
965
966
967
968
  // ================================================================================
  // Update texture map from meshNode and activate it
  // ================================================================================
  if( obj->dataType( DATA_TRIANGLE_MESH ) ){
969
970
    if (!multiTextureMode) {
      doUpdateTexture(texData->texture(_textureName), *PluginFunctions::triMeshObject(obj)->mesh());
971
      PluginFunctions::triMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName() );
972
      PluginFunctions::triMeshObject(obj)->meshNode()->set_index_property_name(0);
973
974
975
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
    } else {
976
      PluginFunctions::triMeshObject(obj)->meshNode()->set_index_property_name( texData->texture( _textureName ).indexMappingProperty().toStdString() );
977
978
979
980
      PluginFunctions::triMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::triMeshObject(obj)->meshNode()->set_property_map( 0 );
    }

Dirk Wilden's avatar
Dirk Wilden committed
981
  }
982

Dirk Wilden's avatar
Dirk Wilden committed
983
  if ( obj->dataType( DATA_POLY_MESH ) ){
984
985
    if (!multiTextureMode) {
      doUpdateTexture(texData->texture(_textureName), *PluginFunctions::polyMeshObject(obj)->mesh());
986
      PluginFunctions::polyMeshObject(obj)->textureNode()->activateTexture( texData->texture( _textureName ).glName() );
987
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_index_property_name(0);
988
989
990
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( 0 );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
    } else {
991
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_index_property_name( texData->texture( _textureName ).indexMappingProperty().toStdString() );
992
993
994
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_texture_map( texData->textureMap() );
      PluginFunctions::polyMeshObject(obj)->meshNode()->set_property_map( 0 );
    }
Dirk Wilden's avatar
Dirk Wilden committed
995
996
997
998
999
  }

  // ================================================================================
  // Switch to a texture drawMode
  // ================================================================================
1000
  switchDrawMode(multiTextureMode);
For faster browsing, not all history is shown. View entire blame