]> SALOME platform Git repositories - modules/visu.git/commitdiff
Salome HOME
Using OpenGL textures for drawing point sprites
authorouv <ouv@opencascade.com>
Wed, 7 Sep 2005 09:38:35 +0000 (09:38 +0000)
committerouv <ouv@opencascade.com>
Wed, 7 Sep 2005 09:38:35 +0000 (09:38 +0000)
src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx
src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx

index d12d064988e1ab7f7ea47e5f86f4e3943ded3d98..4364d1fc3e74436df362eb45286291e877a4f42c 100755 (executable)
@@ -57,6 +57,7 @@
 #include <stdio.h>
 #include <cmath>
 #include <string>
+#include <vector>
 
 #ifndef VTK_IMPLEMENT_MESA_CXX
 vtkCxxRevisionMacro(VISU_OpenGLPointSpriteMapper, "$Revision$");
@@ -72,13 +73,13 @@ vtkStandardNewMacro(VISU_OpenGLPointSpriteMapper);
 #define VTK_PDPSM_NORMAL_TYPE_DOUBLE 0x0020
 #define VTK_PDPSM_OPAQUE_COLORS      0x0040
 #define VTK_PDPSM_ALPHA_ARRAY        0x0080
-
+/*
 PFNGLGENPROGRAMSARBPROC           glGenProgramsARB           = NULL;
 PFNGLDELETEPROGRAMSARBPROC        glDeleteProgramsARB        = NULL;
 PFNGLBINDPROGRAMARBPROC           glBindProgramARB           = NULL;
 PFNGLPROGRAMSTRINGARBPROC         glProgramStringARB         = NULL;
 PFNGLPROGRAMENVPARAMETER4FARBPROC glProgramEnvParameter4fARB = NULL;
-
+*/
 #ifdef GL_ARB_shader_objects
 PFNGLSHADERSOURCEARBPROC          glShaderSourceARB          = NULL;
 PFNGLCREATESHADEROBJECTARBPROC    glCreateShaderObjectARB    = NULL;
@@ -128,15 +129,15 @@ VISU_OpenGLPointSpriteMapper::VISU_OpenGLPointSpriteMapper()
   this->RenderMode            = VISU_OpenGLPointSpriteMapper::Accumulate;
 
   this->XMLImageDataReader    = vtkXMLImageDataReader::New();
-  TextureIntensity            = 0;
-  TextureAlphaChannel         = 0;
-
+  this->PointSpriteTexture    = 0;
   this->IsUsingOpenGLMapper   = false;
 }
 //-----------------------------------------------------------------------------
 // Destructor (don't call ReleaseGraphicsResources() since it is virtual
 VISU_OpenGLPointSpriteMapper::~VISU_OpenGLPointSpriteMapper()
 {
+  glDeleteTextures( 1, &PointSpriteTexture );
+
   if (this->LastWindow)
     {
     this->ReleaseGraphicsResources(this->LastWindow);
@@ -236,10 +237,6 @@ void initShader()
 
   char* shader = readFromFile( fileName );
 
-  //glGenProgramsARB( 1, &shaderId );
-  //glBindProgramARB( GL_VERTEX_PROGRAM_ARB, shaderId );
-  //glProgramStringARB( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen( shader ), shader );
-
 #ifdef GL_ARB_shader_objects
   GLhandleARB v = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB );
   glShaderSourceARB( v, 1, (const GLcharARB**)&shader, NULL );
@@ -280,9 +277,9 @@ void setShaderConstants()
   glRotatef( 0.0f, 1.0f, 0.0f, 0.0f );
   glRotatef( 0.0f, 0.0f, 1.0f, 0.0f );
 
-  glProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 0, 50.0f, 1.0f, 1.0f, 1.0f );
-  glProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 1, 1.0f, 1.0f, 1.0f, 1.0f );
-  glProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 2, 1.0f, 10.0f, 100.0f, 1000.0f );
+  //glProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 0, 50.0f, 1.0f, 1.0f, 1.0f );
+  //glProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 1, 1.0f, 1.0f, 1.0f, 1.0f );
+  //glProgramEnvParameter4fARB( GL_VERTEX_PROGRAM_ARB, 2, 1.0f, 10.0f, 100.0f, 1000.0f );
 }
 //-----------------------------------------------------------------------------
 // Release the graphics resources used by this mapper.  In this case, release
@@ -320,13 +317,13 @@ void VISU_OpenGLPointSpriteMapper::InitializeExtensions()
   }
 
   void* libHandle = dlopen( "libGL.so", RTLD_NOW );
-
+  /*
   glGenProgramsARB           = (PFNGLGENPROGRAMSARBPROC)dlsym( libHandle, "glGenProgramsARB" );
   glDeleteProgramsARB        = (PFNGLDELETEPROGRAMSARBPROC)dlsym( libHandle, "glDeleteProgramsARB" );
   glBindProgramARB           = (PFNGLBINDPROGRAMARBPROC)dlsym( libHandle, "glBindProgramARB" );
   glProgramStringARB         = (PFNGLPROGRAMSTRINGARBPROC)dlsym( libHandle, "glProgramStringARB" );
   glProgramEnvParameter4fARB = (PFNGLPROGRAMENVPARAMETER4FARBPROC)dlsym( libHandle, "glProgramEnvParameter4fARB" );
-
+  */
 #ifdef GL_ARB_shader_objects
   glShaderSourceARB          = (PFNGLSHADERSOURCEARBPROC)dlsym( libHandle, "glShaderSourceARB" );
   glCreateShaderObjectARB    = (PFNGLCREATESHADEROBJECTARBPROC)dlsym( libHandle, "glCreateShaderObjectARB" );
@@ -604,7 +601,7 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
       this->LastWindow = ren->GetRenderWindow();
 
       // Load the texture for the particle into gl
-      this->ParticleTexture->Load(ren);
+      //this->ParticleTexture->Load(ren);
 
       // get a unique display list id
       this->ListId = glGenLists(1);
@@ -635,7 +632,7 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
         !this->GetGlobalImmediateModeRendering())
       {
       // Load the texture for the particle into gl
-      this->ParticleTexture->Load(ren);
+      //this->ParticleTexture->Load(ren);
 
       // Time the actual drawing
       this->Timer->StartTimer();
@@ -650,7 +647,7 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
       this->GetGlobalImmediateModeRendering())
     {
     // Load the texture for the particle into gl
-    this->ParticleTexture->Load(ren);
+    //this->ParticleTexture->Load(ren);
 
     // Time the actual drawing
     this->Timer->StartTimer();
@@ -658,6 +655,8 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
     this->Timer->StopTimer();
     }
 
+  //this->Texture->Render( ren );
+
   this->TimeToDraw = (float)this->Timer->GetElapsedTime();
 
   // If the timer is not accurate enough, set it to a small
@@ -707,8 +706,6 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act)
 //-----------------------------------------------------------------------------
 void VISU_OpenGLPointSpriteMapper::initSprites()
 {
-  glBindTexture( GL_TEXTURE_2D, TextureIntensity );
-
   switch (this->RenderMode)
   {
     case VISU_OpenGLPointSpriteMapper::Accumulate:
@@ -764,11 +761,6 @@ void VISU_OpenGLPointSpriteMapper::initSprites()
   }
   glPointSize( CurrentPointSize );
 
-  glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
-
-  glEnable( GL_POINT_SPRITE_ARB );
-  glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
-
   /*
   // Set Quadratic Attenuation parameters
   glPointParameterfvARB( GL_POINT_DISTANCE_ATTENUATION_ARB, this->QuadraticPointDistanceAttenuation );
@@ -790,20 +782,15 @@ void VISU_OpenGLPointSpriteMapper::initSprites()
 void VISU_OpenGLPointSpriteMapper::cleanupSprites()
 {
   // Set GL params back to normal to stop other vtkMappers diusplaying wrongly
-  glEnable(GL_DEPTH_TEST);
-  glEnable(GL_LIGHTING);
+  glEnable( GL_DEPTH_TEST );
+  glEnable( GL_LIGHTING );
   glEnable( GL_BLEND );
-
-  glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
-  glDisable( GL_POINT_SPRITE_ARB );
-
-  glDisable( GL_TEXTURE_2D );
 }
 //-----------------------------------------------------------------------------
-GLuint VISU_OpenGLPointSpriteMapper::LoadTexture( vtkImageData* imageData )
+void VISU_OpenGLPointSpriteMapper::InitTexture( vtkImageData* imageData )
 {
-  //this->XMLImageDataReader->SetFileName( fileName );
-  //vtkImageData* imageData = this->XMLImageDataReader->GetOutput();
+  //cout << "VISU_OpenGLPointSpriteMapper::InitTexture" << endl;
+
   //imageData->DebugOn();
   imageData->RequestExactExtentOn();
   imageData->UpdateInformation();
@@ -813,39 +800,24 @@ GLuint VISU_OpenGLPointSpriteMapper::LoadTexture( vtkImageData* imageData )
 
   int* size = imageData->GetDimensions();
   vtkDataArray* scalars = imageData->GetPointData()->GetScalars();
-  unsigned char* dataPtr = static_cast<vtkUnsignedCharArray *>(scalars)->GetPointer(0);
-
-  glEnable( GL_TEXTURE_2D );
-
-  GLuint texture;
-  glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );
-  glGenTextures( 1, &texture );
-  glBindTexture( GL_TEXTURE_2D, texture );
-  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
-  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
-  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, dataPtr );
+  unsigned char* dataPtr = static_cast<vtkUnsignedCharArray*>(scalars)->GetPointer(0);
 
-  return texture;
-}
-//-----------------------------------------------------------------------------
-void VISU_OpenGLPointSpriteMapper::DrawTexture( GLuint texture, GLfloat x, GLfloat y, GLfloat z )
-{
-  //cout << "DrawTexture : " << texture << endl;
-  //glColor4f( 1.0, 1.0, 1.0, 1.0 );
-
-  //glAlphaFunc( GL_GREATER, 0.95F );
-  //glEnable( GL_ALPHA_TEST );
-    
-  //glBindTexture( GL_TEXTURE_2D, texture );
-  glBegin( GL_POINTS );
+  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
+  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
+  //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
+  //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
+  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
+  glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
-  glVertex3f( x, y, z );
+  glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0,
+               GL_RGBA, GL_UNSIGNED_BYTE, dataPtr);
 
-  glEnd();
-  glFlush();
-
-  //glDisable( GL_ALPHA_TEST );
-  //glDisable( GL_TEXTURE_2D );
+  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
+  glEnable( GL_TEXTURE_2D );
+  glEnable( GL_POINT_SPRITE_ARB );
+  glEnable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB );
+  glTexEnvf( GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE );
+  glBindTexture( GL_TEXTURE_2D, this->PointSpriteTexture );
 }
 //-----------------------------------------------------------------------------
 void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
@@ -857,6 +829,12 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
                                              vtkCellArray *cells,
                                              vtkRenderer *ren)
 {
+  this->XMLImageDataReader->SetFileName( "/dn06/salome/ouv/SALOME3/TextureAlphaChannel.vti" );
+  vtkImageData* imageData = this->XMLImageDataReader->GetOutput();
+  this->InitTexture( imageData );
+
+  initSprites();
+
   void *voidPoints = p->GetVoidPointer(0);
   unsigned char *rgba;
   float *alphadata;
@@ -875,7 +853,7 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
 
   // draw all the elements, use fast path if available
   switch (idx)
-    {
+  {
     case VTK_PDPSM_POINT_TYPE_FLOAT:
       //std::cout << "VTK_PDPSM_POINT_TYPE_FLOAT " << std::endl;
       vtkDrawPointsMacro(float, glVertex3fv(points + 3**ptIds);, initSprites(););
@@ -889,14 +867,12 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
       vtkDrawPointsMacro(float,
                          glColor4ubv(rgba + 4**ptIds);
                          glVertex3fv(points + 3**ptIds);, initSprites(););
-
       break;
     case VTK_PDPSM_POINT_TYPE_DOUBLE | VTK_PDPSM_COLORS:
       //std::cout << "VTK_PDPSM_POINT_TYPE_DOUBLE | VTK_PDPSM_COLORS " << std::endl;
       vtkDrawPointsMacro(double,
                          glColor4ubv(rgba + 4**ptIds);
                          glVertex3dv(points + 3**ptIds);, initSprites(););
-
       break;
     case VTK_PDPSM_POINT_TYPE_FLOAT | VTK_PDPSM_COLORS | VTK_PDPSM_OPAQUE_COLORS:
       //std::cout << "VTK_PDPSM_POINT_TYPE_FLOAT | VTK_PDPSM_COLORS | VTK_PDPSM_OPAQUE_COLORS " << std::endl;
@@ -905,19 +881,10 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
                          glVertex3fv(points + 3**ptIds);, initSprites(););
       break;
     default:
-      {
-       //std::cout << "Default " << std::endl;
+    {
+      //std::cout << "Default " << std::endl;
       if (idx & VTK_PDPSM_ALPHA_ARRAY)
-      {
         alphadata = alpha->GetPointer(0);
-      }
-
-      initSprites();
-
-      //glEnable( GL_VERTEX_PROGRAM_ARB );
-      //glBindProgramARB( GL_VERTEX_PROGRAM_ARB, shaderId );
-
-      //glColor4f( 1.0, 1.0, 1.0, 1.0 );
 
       vtkIdType *pts = 0;
       vtkIdType npts = 0;
@@ -949,51 +916,24 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx,
 
        glVertex3fv(p->GetPoint(pts[0]));
 
-       /*
-       vtkXMLImageDataReader* aReader = vtkXMLImageDataReader::New();
-       aReader->SetFileName( "/dn06/salome/ouv/SALOME3/TextureIntensity.vti" );
-       TextureIntensity = this->LoadTexture( aReader->GetOutput() );
-       aReader->Delete();
-
-       aReader = vtkXMLImageDataReader::New();
-       aReader->SetFileName( "/dn06/salome/ouv/SALOME3/TextureAlphaChannel.vti" );
-       TextureAlphaChannel = this->LoadTexture( aReader->GetOutput() );
-       aReader->Delete();
-       */
-       /*      
-       this->DrawTexture( TextureIntensity,
-                          p->GetPoint(pts[0])[0],
-                          p->GetPoint(pts[0])[1],
-                          p->GetPoint(pts[0])[2] );
-       /*
-       this->DrawTexture( TextureAlphaChannel,
-                          p->GetPoint(pts[0])[0],
-                          p->GetPoint(pts[0])[1],
-                          p->GetPoint(pts[0])[2] );
-       */
-
-       //glVertex3fv(p->GetPoint(pts[0]));
-
         // check for abort condition
         if (count == 10000)
-          {
+        {
           count = 0;
           // report progress
           this->UpdateProgress((double)cellNum/this->TotalCells);
           if (ren->GetRenderWindow()->CheckAbortStatus())
-            {
             noAbort = 0;
-            }
-          }
-        ++cellNum;
-        }
-      glEnd();
 
-      //glDisable( GL_VERTEX_PROGRAM_ARB );
+       }
+        ++cellNum;
       }
+
+      glEnd();
     }
+  }
 
-    cleanupSprites();
+  cleanupSprites();
 }
 
 //-----------------------------------------------------------------------------
index 9e32f75a7222059fd9ffb5a3fea08fdd503dbe27..3dd034a98a3e9af91fd7c182c78b4e0104664926 100755 (executable)
@@ -147,11 +147,7 @@ public:
   // Draw method for OpenGL.
   virtual int Draw(vtkRenderer *ren, vtkActor *a);
 
-  GLuint  LoadTexture( vtkImageData* );
-  void    DrawTexture( GLuint texture, GLfloat x, GLfloat y, GLfloat z );
-
-  vtkSetMacro(TextureIntensity, GLuint);
-  vtkSetMacro(TextureAlphaChannel, GLuint);
+  void InitTexture( vtkImageData* );
 
 protected:
   VISU_OpenGLPointSpriteMapper();
@@ -189,11 +185,10 @@ protected:
   vtkImageData     *gaussian;
   int               RenderMode;
 
-  vtkXMLImageDataReader*  XMLImageDataReader;
-  GLuint                  TextureIntensity;
-  GLuint                  TextureAlphaChannel;
+  vtkXMLImageDataReader* XMLImageDataReader;
+  GLuint            PointSpriteTexture;
 
-  bool                     IsUsingOpenGLMapper;
+  bool              IsUsingOpenGLMapper;
 
 private:
   VISU_OpenGLPointSpriteMapper(const VISU_OpenGLPointSpriteMapper&);  // Not implemented.