]> SALOME platform Git repositories - modules/visu.git/commitdiff
Salome HOME
Texture for Point Sprites
authorouv <ouv@opencascade.com>
Thu, 22 Sep 2005 13:01:35 +0000 (13:01 +0000)
committerouv <ouv@opencascade.com>
Thu, 22 Sep 2005 13:01:35 +0000 (13:01 +0000)
src/PIPELINE/VISU_GaussPointsPL.cxx
src/PIPELINE/VISU_GaussPointsPL.hxx
src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx
src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx
src/VISUGUI/VisuGUI_GaussPointsDlg.cxx
src/VISUGUI/VisuGUI_GaussPointsDlg.h
src/VISU_I/VISU_GaussPoints_i.cc
src/VISU_I/VISU_GaussPoints_i.hh

index 57e4f1e3167dfc8a09da07e4a390188ed8883a51..c335a5206059f368c448c0390cc6885369dae5da 100644 (file)
@@ -36,6 +36,7 @@
 #include <vtkImageGaussianSource.h>
 #include <vtkXMLImageDataReader.h>
 #include <vtkGeometryFilter.h>
+#include <vtkImageData.h>
 
 //----------------------------------------------------------------------------
 vtkStandardNewMacro(VISU_GaussPointsPL);
@@ -209,7 +210,6 @@ void
 VISU_GaussPointsPL
 ::SetClamp(float theClamp)
 {
-  //cout << "VISU_GaussPointsPL::SetClamp " << theClamp << endl;;
   myPSMapper->SetPointSpriteClamp( theClamp );
 
   Modified();
@@ -352,3 +352,13 @@ VISU_GaussPointsPL
   return myAverageCellSize*myRelativeMaxSize;
 }
 
+//----------------------------------------------------------------------------
+void
+VISU_GaussPointsPL
+::SetImageData(vtkImageData* theImageData)
+{
+  //cout << "VISU_GaussPointsPL::SetImageData " << theImageData << endl;
+  //theImageData->Print( cout );
+
+  myPSMapper->SetImageData( theImageData );
+}
index da44167a331d587c59155424d69a2f31305154fe..f559016df81234b2653cd439569ab4a3923cc8bc 100644 (file)
@@ -35,6 +35,7 @@ class VISU_OpenGLPointSpriteMapper;
 
 class vtkGeometryFilter;
 class vtkDataArray;
+class vtkImageData;
 
 class VISU_GaussPointsPL : public VISU_ScalarMapPL
 {
@@ -146,6 +147,9 @@ public:
   float
   GetPointSize(vtkIdType theID, vtkDataArray* theScalarArray);
 
+  void
+  SetImageData(vtkImageData* theImageData);
+
 protected:
   VISU_OpenGLPointSpriteMapper* myPSMapper;
   vtkGeometryFilter* myGeomFilter;
index 7912a6bb418c60c23c0f9b672bb51477f56b5346..c4e69e3553af227d859b45a93de1cda98fb3972e 100755 (executable)
@@ -125,12 +125,15 @@ VISU_OpenGLPointSpriteMapper::VISU_OpenGLPointSpriteMapper()
   this->OpenGLLibrary            = 0;
 #endif
 
+  this->AlphaThreshold           = 0.1;
+
   this->PointSpriteClamp         = 100.0;
   this->PointSpriteSize          =   0.0;
   this->PointSpriteMinSize       =  15.0;
   this->PointSpriteMaxSize       =  50.0;
   this->PointSpriteMagnification =   1.0;
 
+  this->ImageData                = 0;
   this->XMLImageDataReader       = vtkXMLImageDataReader::New();
   this->PointSpriteTexture       = 0;
   this->IsUsingOpenGLMapper      = false;
@@ -154,6 +157,11 @@ VISU_OpenGLPointSpriteMapper::~VISU_OpenGLPointSpriteMapper()
     delete [] this->SizeChannelArray;
     this->SizeChannelArray = NULL;
     }
+  if (this->ImageData)
+  {
+    this->ImageData->Delete();
+    this->ImageData = NULL;
+  }
   if (this->XMLImageDataReader)
   {
     this->XMLImageDataReader->Delete();
@@ -171,6 +179,8 @@ void VISU_OpenGLPointSpriteMapper::ShallowCopy( vtkAbstractMapper* mapper )
     this->SetPointSpriteMinSize( m->GetPointSpriteMinSize() );
     this->SetPointSpriteMaxSize( m->GetPointSpriteMaxSize() );
     this->SetPointSpriteMagnification( m->GetPointSpriteMagnification() );
+
+    this->SetImageData( m->GetImageData() );
   }
   MAPPER_SUPERCLASS::ShallowCopy(mapper);
 
@@ -672,6 +682,7 @@ void VISU_OpenGLPointSpriteMapper::InitSprites()
       glDisable(GL_DEPTH_TEST);
       glEnable( GL_BLEND );
       glBlendFunc( GL_SRC_ALPHA, GL_ONE );
+      glAlphaFunc( GL_GREATER, this->AlphaThreshold );
       break;
     }
 
@@ -730,20 +741,23 @@ void VISU_OpenGLPointSpriteMapper::CleanupSprites()
   //glDisable( GL_VERTEX_PROGRAM_ARB );
 }
 //-----------------------------------------------------------------------------
-void VISU_OpenGLPointSpriteMapper::InitTexture( vtkImageData* imageData )
+void VISU_OpenGLPointSpriteMapper::SetImageData( vtkImageData* theImageData )
+{
+  //cout << "VISU_OpenGLPointSpriteMapper::SetImageData " << this << " " << theImageData << endl;
+  //theImageData->Print( cout );
+
+  this->ImageData = theImageData;
+
+  //cout << this->ImageData << endl;
+}
+//-----------------------------------------------------------------------------
+void VISU_OpenGLPointSpriteMapper::InitTexture()
 {
-  //cout << "VISU_OpenGLPointSpriteMapper::InitTexture" << endl;
+  //cout << "VISU_OpenGLPointSpriteMapper::InitTexture " << this << " " << this->ImageData << endl;
 
-  //imageData->DebugOn();
-  imageData->RequestExactExtentOn();
-  imageData->UpdateInformation();
-  imageData->SetUpdateExtentToWholeExtent();
-  imageData->Update();
-  //cout << "Dimension of image data : " << imageData->GetDataDimension() << endl;
+  //this->ImageData->Print( cout );
 
-  int* size = imageData->GetDimensions();
-  vtkDataArray* scalars = imageData->GetPointData()->GetScalars();
-  unsigned char* dataPtr = static_cast<vtkUnsignedCharArray*>(scalars)->GetPointer(0);
+  unsigned char* dataPtr = (unsigned char*)this->ImageData->GetScalarPointer();
 
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
@@ -753,7 +767,7 @@ void VISU_OpenGLPointSpriteMapper::InitTexture( vtkImageData* imageData )
   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
   glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
-               GL_RGBA, GL_UNSIGNED_BYTE, dataPtr);
+               GL_RGBA, GL_UNSIGNED_BYTE, dataPtr );
 
   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
   glEnable( GL_TEXTURE_2D );
@@ -773,11 +787,13 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
                                              vtkCellArray *cells,
                                              vtkRenderer *ren)
 {
-  this->XMLImageDataReader->SetFileName( "/dn06/salome/ouv/SALOME/TextureAlphaChannel.vti" );
-  vtkImageData* imageData = this->XMLImageDataReader->GetOutput();
-  this->InitTexture( imageData );
+  //this->XMLImageDataReader->SetFileName( "/dn06/salome/ouv/SALOME/TextureAlphaChannel.vti" );
+  //vtkImageData* imageData = this->XMLImageDataReader->GetOutput();
+
+  if( this->ImageData )
+    this->InitTexture();
 
-  InitSprites();
+  this->InitSprites();
 
   glPointSize( this->DefaultPointSize );
 
index d2086b117183765373856c1064b4d8877e616791..45a96bb1bc620fdafc98d1d37df64d72ee0f1f92 100755 (executable)
@@ -134,7 +134,7 @@ public:
 
   // Description:
   // Initializing texture for Point Sprites
-  void InitTexture( vtkImageData* );
+  void InitTexture();
 
   // Description:
   // Return the maximum point size supported by the graphics hardware.
@@ -154,6 +154,10 @@ public:
   void SetPointSpriteMaxSize( float );
   void SetPointSpriteMagnification( float );
 
+  void SetImageData( vtkImageData* );
+  //vtkSetMacro(ImageData, vtkImageData*);
+  vtkGetMacro(ImageData, vtkImageData*);
+
 protected:
   VISU_OpenGLPointSpriteMapper();
   ~VISU_OpenGLPointSpriteMapper();
@@ -193,6 +197,8 @@ protected:
   void*             OpenGLLibrary;
 #endif
 
+  float             AlphaThreshold;
+
   float             PointSpriteClamp;
   float             PointSpriteSize;
   float             PointSpriteMinSize;
@@ -202,6 +208,8 @@ protected:
   vtkXMLImageDataReader* XMLImageDataReader;
   GLuint            PointSpriteTexture;
 
+  vtkImageData*     ImageData;
+
   bool              IsUsingOpenGLMapper;
 };
 
index 3344117bce64df695fa5d7bf979f0e314cc39114..f4ff3629032bb42eef6aadfc87ff9b6f7456f59d 100644 (file)
@@ -24,7 +24,6 @@
 //  File   : VisuGUI_MagnitudeDlg.cxx
 //  Author : Laurent CORNABE & Hubert ROLLAND
 //  Module : VISU
-//  $Header$
 
 #include "VisuGUI_GaussPointsDlg.h"
 
@@ -962,6 +961,9 @@ void VisuGUI_GaussPointsDlg::initFromPrsObject( VISU::GaussPoints_i* thePrs )
   myMagnificationSpinBox->setValue( thePrs->GetGaussPointsPL()->GetMagnification() * 100.0 );
   myIncrementSpinBox->setValue( thePrs->GetGaussPointsPL()->GetMagnificationIncrement() * 100.0 );
 
+  myMainTextureLineEdit->setText( thePrs->GetMainTexture().section( '/', -1 ) );
+  myAlphaChannelTextureLineEdit->setText( thePrs->GetAlphaChannelTexture().section( '/', -1 ) );
+
   myScalarPane->initFromPrsObject(thePrs);
 }
 
@@ -985,29 +987,70 @@ int VisuGUI_GaussPointsDlg::storeToPrsObject( VISU::GaussPoints_i* thePrs )
   thePrs->GetGaussPointsPL()->SetMagnification( myMagnificationSpinBox->value() / 100.0 );
   thePrs->GetGaussPointsPL()->SetMagnificationIncrement( myIncrementSpinBox->value() / 100.0 );
 
-  return myScalarPane->storeToPrsObject(thePrs);
+  myScalarPane->storeToPrsObject(thePrs);
+
+  // Texture
+  if( myMainTexture.isNull() || myAlphaChannelTexture.isNull() )
+  {
+    //return 1;
+    myMainTexture = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/sprite_texture.bmp";
+    myAlphaChannelTexture = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/sprite_alpha.bmp";
+  }
+
+  QString mainTextureImage = myMainTexture;
+  QString mainTextureFormat = mainTextureImage.section( '.', -1 );
+  QString mainTextureVTI = mainTextureImage.section( '.', 0, -2 ) + ".vti";
+
+  QString command1 = QString( "VISU_img2vti " ) + mainTextureFormat + " " + mainTextureImage + " " + mainTextureVTI;
+  cout << command1.latin1() << endl;
+  system( command1.latin1() );
+
+  QString alphaTextureImage = myAlphaChannelTexture;
+  QString alphaTextureFormat = alphaTextureImage.section( '.', -1 );
+  QString alphaTextureVTI = alphaTextureImage.section( '.', 0, -2 ) + ".vti";
+
+  QString command2 = QString( "VISU_img2vti " ) + alphaTextureFormat + " " + alphaTextureImage + " " + alphaTextureVTI;
+  cout << command2.latin1() << endl;
+  system( command2.latin1() );
+
+  cout << mainTextureVTI.latin1() << endl;
+  cout << alphaTextureVTI.latin1() << endl;
+
+  thePrs->SetMainTexture( mainTextureImage );
+  thePrs->SetAlphaChannelTexture( alphaTextureImage );
+  thePrs->MakeTexture( mainTextureVTI, alphaTextureVTI );
+
+  return 1;
 }
 
 void VisuGUI_GaussPointsDlg::accept()
 {
   //if( myScalarPane->check() )
-    QDialog::accept();
+  QDialog::accept();
 }
 
 void VisuGUI_GaussPointsDlg::onBrowseMainTexture()
 {
-  QString aFileName = QFileDialog::getOpenFileName( QString::null, "Bitmap (*.jpg *.gif *.png)", this );
+  QString aRootDir = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/";
+  QString aFileName = QFileDialog::getOpenFileName( aRootDir, "Bitmap (*.bmp *.jpg *.png)", this );
+
+  if( aFileName.isNull() )
+    return;
 
-  if( !aFileName.isNull() )
-    myMainTextureLineEdit->setText( aFileName );
+  myMainTexture = aFileName;
+  myMainTextureLineEdit->setText( aFileName.section( '/', -1 ) );
 }
 
 void VisuGUI_GaussPointsDlg::onBrowseAlphaChannelTexture()
 {
-  QString aFileName = QFileDialog::getOpenFileName( QString::null, "Bitmap (*.jpg *.gif *.png)", this );
+  QString aRootDir = QString( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/";
+  QString aFileName = QFileDialog::getOpenFileName( aRootDir, "Bitmap (*.bmp *.jpg *.png)", this );
+
+  if( aFileName.isNull() )
+    return;
 
-  if( !aFileName.isNull() )
-    myAlphaChannelTextureLineEdit->setText( aFileName );
+  myAlphaChannelTexture = aFileName;
+  myAlphaChannelTextureLineEdit->setText( aFileName.section( '/', -1 ) );
 }
 
 void VisuGUI_GaussPointsDlg::onColorButtonPressed()
index fdcda88ccea74292b5b09033d2c97d7df9cd86d0..b15310b037f5448706f3040ad4af5c3d715d5ce8 100644 (file)
@@ -137,6 +137,9 @@ protected slots:
 private:
   VisuGUI_GaussScalarBarPane*   myScalarPane;
 
+  QString myMainTexture;
+  QString myAlphaChannelTexture;
+
   QtxDblSpinBox*           myClampSpinBox;
 
   QLineEdit*               myMainTextureLineEdit;
index 0e8e12e653b8a461e7ecbebf21e160dfd46ff157..ff7789064958f2e94f5139bf2ae78ef0c6c09db7 100644 (file)
@@ -31,6 +31,8 @@
 #include "VISU_GaussPointsPL.hxx"
 
 
+#include "VISU_OpenGLPointSpriteMapper.hxx"
+
 using namespace VISU;
 using namespace std;
 
@@ -40,6 +42,9 @@ static int MYDEBUG = 1;
 static int MYDEBUG = 0;
 #endif
 
+#include <vtkImageData.h>
+#include <vtkXMLImageDataReader.h>
+
 //----------------------------------------------------------------------------
 int
 VISU::GaussPoints_i
@@ -284,6 +289,8 @@ VISU::GaussPoints_i
                                       myColor.blue() / 255.0);
     }
     VISU::ScalarMap_i::UpdateActor(theActor);
+    ((VISU_GaussPtsAct*)theActor)->GetPSMapper()->ShallowCopy( myPipeLine->GetMapper() );
+    theActor->GetPipeLine()->ShallowCopy(myPipeLine);
   }
 }
 
@@ -297,3 +304,80 @@ VISU::GaussPoints_i
   myGaussPointsPL->ChangeMagnification(aChangeMagnification);
   UpdateActors();
 }
+
+void
+VISU::GaussPoints_i
+::MakeTexture( const QString& theMainTexture, const QString& theAlphaChannelTexture )
+{
+  if( theMainTexture.isNull() || theAlphaChannelTexture.isNull() )
+    return;
+
+  vtkXMLImageDataReader* aReader1 = vtkXMLImageDataReader::New();
+  vtkXMLImageDataReader* aReader2 = vtkXMLImageDataReader::New();
+
+  aReader1->SetFileName( theMainTexture );
+  aReader2->SetFileName( theAlphaChannelTexture );
+
+  aReader1->Update();
+  aReader2->Update();
+
+  vtkImageData* imageData1 = aReader1->GetOutput();
+  vtkImageData* imageData2 = aReader2->GetOutput();
+
+  unsigned char* dataPtr1 = (unsigned char*)imageData1->GetScalarPointer();
+  unsigned char* dataPtr2 = (unsigned char*)imageData2->GetScalarPointer();
+
+  int xSize = 16;
+  int ySize = 16;
+  int N1 = imageData1->GetNumberOfScalarComponents();
+  int N2 = imageData2->GetNumberOfScalarComponents();
+
+  vtkImageData* imageData = vtkImageData::New();
+  imageData->SetDimensions(xSize, ySize, 1);
+  imageData->SetScalarTypeToUnsignedChar();        
+  imageData->SetNumberOfScalarComponents(4);
+  imageData->AllocateScalars();
+  unsigned char *dataPtr = (unsigned char * )imageData->GetScalarPointer();
+  /*
+  cout << "--------------------" << endl;
+  cout << "--------DATA1-------" << endl;
+  cout << "--------------------" << endl;
+  for( int i = 0; i < xSize*ySize*N1; i += N1 )
+  {
+    cout << i / N1 << " " << (int)dataPtr1[ i ] << " " << (int)dataPtr1[i+1] << " " << (int)dataPtr1[i+2] << endl;
+    if( !( (i/N1+1) % xSize ) )
+      cout << "--------------------" << endl;
+  }
+
+  cout << "--------------------" << endl;
+  cout << "--------DATA2-------" << endl;
+  cout << "--------------------" << endl;
+  for( int i = 0; i < xSize*ySize*N2; i += N2 )
+  {
+    cout << i / N2 << " " << (int)dataPtr2[ i ] << " " << (int)dataPtr2[i+1] << " " << (int)dataPtr2[i+2] << endl;
+    if( !( (i/N2+1) % xSize ) )
+      cout << "--------------------" << endl;
+  }
+
+  cout << "--------------------" << endl;
+  cout << "--------DATA--------" << endl;
+  cout << "--------------------" << endl;
+  */
+  for( int i = 0; i < xSize * ySize * 4; i += 4 )
+  {
+    dataPtr[ i ] = dataPtr2[ i / 4 * N2 ];
+    dataPtr[i+1] = dataPtr2[ i / 4 * N2 + 1 ];
+    dataPtr[i+2] = dataPtr2[ i / 4 * N2 + 2 ];
+    dataPtr[i+3] = dataPtr1[ i / 4 * N1 ];
+    /*
+    cout << i / 4 << " " << (int)dataPtr[ i ] << " " << (int)dataPtr[i+1] <<
+                     " " << (int)dataPtr[i+2] << " " << (int)dataPtr[i+3] << endl;
+    if( !( (i/4+1) % xSize ) )
+      cout << "--------------------" << endl;
+    */
+  }
+  imageData->Update();
+  //imageData->Print( cout );
+
+  myGaussPointsPL->SetImageData( imageData );
+}
index becaaada3afd57b2484aea7681494a6396fc500d..916c1f95bd03199d4b56ff5ed5a79a840306db03 100644 (file)
@@ -70,6 +70,21 @@ namespace VISU
     void
     ShowColored( bool theColored );
 
+    void SetMainTexture( const QString& theMainTexture ) {
+      myMainTexture = theMainTexture;
+    }
+
+    QString GetMainTexture() { return myMainTexture; }
+
+    void SetAlphaChannelTexture( const QString& theAlphaChannelTexture ) {
+      myAlphaChannelTexture = theAlphaChannelTexture;
+    }
+
+    QString GetAlphaChannelTexture() { return myAlphaChannelTexture; }
+
+    void MakeTexture( const QString& theMainTexture,
+                     const QString& theAlphaChannelTexture );
+
   protected:
     virtual
     void
@@ -84,9 +99,13 @@ namespace VISU
     GetPipeLine();
 
     VISU_GaussPointsPL *myGaussPointsPL;
+
     bool myIsColored;
     QColor myColor;
 
+    QString myMainTexture;
+    QString myAlphaChannelTexture;
+
   public:
     static 
     int