From 05cf1c8e924501a7a3532b28b595f2572c02b370 Mon Sep 17 00:00:00 2001 From: ouv Date: Wed, 14 Sep 2005 06:41:07 +0000 Subject: [PATCH] Working with shader corrected --- src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx | 203 ++++++++++-------- src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx | 22 +- 2 files changed, 137 insertions(+), 88 deletions(-) diff --git a/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx b/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx index 4364d1fc..421de65f 100755 --- a/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx +++ b/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx @@ -73,31 +73,33 @@ 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; -PFNGLCOMPILESHADERARBPROC glCompileShaderARB = NULL; -PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB = NULL; -PFNGLATTACHOBJECTARBPROC glAttachObjectARB = NULL; -PFNGLLINKPROGRAMPROC glLinkProgramARB = NULL; -PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB = NULL; -PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB = NULL; -PFNGLGETINFOLOGARBPROC glGetInfoLogARB = NULL; -#endif #ifndef GL_ARB_point_sprite -#define GL_POINT_SPRITE_ARB 0x8861 -#define GL_COORD_REPLACE_ARB 0x8862 +#define GL_POINT_SPRITE_ARB 0x8861 +#define GL_COORD_REPLACE_ARB 0x8862 #endif - -unsigned int shaderId; +/* +#ifdef GL_ARB_shader_objects +PFNGLGENPROGRAMSARBPROC glGenProgramsARB = NULL; +PFNGLDELETEPROGRAMSARBPROC glDeleteProgramsARB = NULL; +PFNGLBINDPROGRAMARBPROC glBindProgramARB = NULL; +PFNGLPROGRAMSTRINGARBPROC glProgramStringARB = NULL; +PFNGLPROGRAMENVPARAMETER4FARBPROC glProgramEnvParameter4fARB = NULL; + +PFNGLSHADERSOURCEARBPROC glShaderSourceARB = NULL; +PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB = NULL; +PFNGLCOMPILESHADERARBPROC glCompileShaderARB = NULL; +PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB = NULL; +PFNGLATTACHOBJECTARBPROC glAttachObjectARB = NULL; +PFNGLLINKPROGRAMPROC glLinkProgramARB = NULL; +PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB = NULL; +PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB = NULL; +PFNGLGETINFOLOGARBPROC glGetInfoLogARB = NULL; +PFNGLGETATTRIBLOCATIONARBPROC glGetAttribLocationARB = NULL; +PFNGLENABLEVERTEXATTRIBARRAYARBPROC glEnableVertexAttribArrayARB = NULL; +PFNGLVERTEXATTRIB1FARBPROC glVertexAttrib1fARB = NULL; +#endif +*/ //----------------------------------------------------------------------------- vtkCxxSetObjectMacro(VISU_OpenGLPointSpriteMapper, ParticleImage, vtkImageData); @@ -120,7 +122,7 @@ VISU_OpenGLPointSpriteMapper::VISU_OpenGLPointSpriteMapper() { this->bmpReader = vtkBMPReader::New(); } - this->DefaultPointSize = 50.0; //-1.0; + this->DefaultPointSize = 30.0; //-1.0; this->MaximumSupportedSize = 0.0; this->QuadraticPointDistanceAttenuation[0] = 1.0; this->QuadraticPointDistanceAttenuation[1] = 0.0; @@ -128,12 +130,17 @@ VISU_OpenGLPointSpriteMapper::VISU_OpenGLPointSpriteMapper() this->RenderMode = VISU_OpenGLPointSpriteMapper::Accumulate; +#ifdef GL_ARB_shader_objects + this->OpenGLLibrary = 0; + this->VertexShader = 0; + this->VertexProgram = 0; +#endif + this->XMLImageDataReader = vtkXMLImageDataReader::New(); this->PointSpriteTexture = 0; this->IsUsingOpenGLMapper = false; } //----------------------------------------------------------------------------- -// Destructor (don't call ReleaseGraphicsResources() since it is virtual VISU_OpenGLPointSpriteMapper::~VISU_OpenGLPointSpriteMapper() { glDeleteTextures( 1, &PointSpriteTexture ); @@ -210,8 +217,11 @@ char* readFromFile( std::string fileName ) } //----------------------------------------------------------------------------- #ifdef GL_ARB_shader_objects -void printInfoLog( GLhandleARB obj ) +void VISU_OpenGLPointSpriteMapper::printInfoLog( GLhandleARB obj ) { + PFNGLGETOBJECTPARAMETERIVARBPROC glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)dlsym( this->OpenGLLibrary, "glGetObjectParameterivARB" ); + PFNGLGETINFOLOGARBPROC glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)dlsym( this->OpenGLLibrary, "glGetInfoLogARB" ); + int infologLength = 0; int charsWritten = 0; char* infoLog; @@ -228,30 +238,41 @@ void printInfoLog( GLhandleARB obj ) } #endif //----------------------------------------------------------------------------- -void initShader() +void VISU_OpenGLPointSpriteMapper::InitShader() { +#ifdef GL_ARB_shader_objects + //cout << "Initializing vertex program" << endl; + std::string fileName = std::string( getenv( "VISU_ROOT_DIR") ) + - "/share/salome/resources/Vertex_Program_ARB.txt"; + "/share/salome/resources/Vertex_Program_ARB.txt"; //std::string fileName = std::string( "/dn06/salome/ouv/SALOME3/VISU_SRC/resources/Vertex_Program_ARB.txt"); - char* shader = readFromFile( fileName ); -#ifdef GL_ARB_shader_objects - GLhandleARB v = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB ); - glShaderSourceARB( v, 1, (const GLcharARB**)&shader, NULL ); - glCompileShaderARB( v ); - //printInfoLog( v ); + PFNGLSHADERSOURCEARBPROC glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)dlsym( this->OpenGLLibrary, "glShaderSourceARB" ); + PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)dlsym( this->OpenGLLibrary, "glCreateShaderObjectARB" ); + PFNGLCOMPILESHADERARBPROC glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)dlsym( this->OpenGLLibrary, "glCompileShaderARB" ); + PFNGLCREATEPROGRAMOBJECTARBPROC glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)dlsym( this->OpenGLLibrary, "glCreateProgramObjectARB" ); + PFNGLATTACHOBJECTARBPROC glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)dlsym( this->OpenGLLibrary, "glAttachObjectARB" ); + PFNGLLINKPROGRAMPROC glLinkProgramARB = (PFNGLLINKPROGRAMPROC)dlsym( this->OpenGLLibrary, "glLinkProgramARB" ); + PFNGLUSEPROGRAMOBJECTARBPROC glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)dlsym( this->OpenGLLibrary, "glUseProgramObjectARB" ); - GLhandleARB p = glCreateProgramObjectARB(); - glAttachObjectARB( p, v ); + this->VertexShader = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB ); + glShaderSourceARB( this->VertexShader, 1, (const GLcharARB**)&shader, NULL ); + glCompileShaderARB( this->VertexShader ); + //printInfoLog( this->VertexShader ); - glLinkProgramARB( p ); - //printInfoLog( p ); + this->VertexProgram = glCreateProgramObjectARB(); + glAttachObjectARB( this->VertexProgram, this->VertexShader ); - glUseProgramObjectARB( p ); -#endif + glLinkProgramARB( this->VertexProgram ); + //printInfoLog( this->VertexProgram ); + + glUseProgramObjectARB( this->VertexProgram ); + this->SetShaderVariable( "minSize", 15.0 ); + this->SetShaderVariable( "maxSize", 50.0 ); + this->SetShaderVariable( "clamp", 100.0 ); /* cout << "Shader from " << fileName << endl; for( int i = 0; i < strlen( shader ); i++ ) @@ -264,44 +285,26 @@ void initShader() cout << "Loading vertex program... failed" << endl << endl; */ delete shader; -} -//----------------------------------------------------------------------------- -// Name: setShaderConstants() -// Desc: -//----------------------------------------------------------------------------- -void setShaderConstants() -{ +#endif + glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glTranslatef( 0.0f, 0.0f, -4.0f ); 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 ); } //----------------------------------------------------------------------------- -// Release the graphics resources used by this mapper. In this case, release -// the display list if any. -void VISU_OpenGLPointSpriteMapper::ReleaseGraphicsResources(vtkWindow *win) +void VISU_OpenGLPointSpriteMapper::SetShaderVariable( const char* variable, float value ) { - if (this->ListId && win) - { - win->MakeCurrent(); - glDeleteLists(this->ListId,1); - this->ListId = 0; - } - this->LastWindow = NULL; - // We actually only want to release our texture when the texture needs reloading, or when - // we are supposed to free up resources - if (this->ParticleTexture) - { - this->ParticleTexture->ReleaseGraphicsResources(win); - } +#ifdef GL_ARB_shader_objects + PFNGLGETATTRIBLOCATIONARBPROC glGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)dlsym( this->OpenGLLibrary, "glGetAttribLocationARB" ); + PFNGLVERTEXATTRIB1FARBPROC glVertexAttrib1fARB = (PFNGLVERTEXATTRIB1FARBPROC)dlsym( this->OpenGLLibrary, "glVertexAttrib1fARB" ); + + glVertexAttrib1fARB( glGetAttribLocationARB( this->VertexProgram, variable ), value ); +#endif } //----------------------------------------------------------------------------- -void VISU_OpenGLPointSpriteMapper::InitializeExtensions() +void VISU_OpenGLPointSpriteMapper::InitExtensions() { char* ext = (char*)glGetString( GL_EXTENSIONS ); //cout << "OpenGL extensions : " << ext << endl; @@ -316,15 +319,16 @@ void VISU_OpenGLPointSpriteMapper::InitializeExtensions() return; } - void* libHandle = dlopen( "libGL.so", RTLD_NOW ); /* +#ifdef GL_ARB_shader_objects + void* libHandle = dlopen( "libGL.so", RTLD_LAZY ); + 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" ); glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)dlsym( libHandle, "glCompileShaderARB" ); @@ -334,10 +338,17 @@ void VISU_OpenGLPointSpriteMapper::InitializeExtensions() glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)dlsym( libHandle, "glUseProgramObjectARB" ); glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)dlsym( libHandle, "glGetObjectParameterivARB" ); glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)dlsym( libHandle, "glGetInfoLogARB" ); + glGetAttribLocationARB = (PFNGLGETATTRIBLOCATIONARBPROC)dlsym( libHandle, "glGetAttribLocationARB" ); + glEnableVertexAttribArrayARB = (PFNGLENABLEVERTEXATTRIBARRAYARBPROC)dlsym( libHandle, "glEnableVertexAttribArrayARB" ); + glVertexAttrib1fARB = (PFNGLVERTEXATTRIB1FARBPROC)dlsym( libHandle, "glVertexAttrib1fARB" ); #endif + */ - initShader(); - setShaderConstants(); +#ifdef GL_ARB_shader_objects + this->OpenGLLibrary = dlopen( "libGL.so", RTLD_LAZY ); + + this->InitShader(); +#endif this->ExtensionsOK = 1; this->ExtensionsInitialized = 1; @@ -350,7 +361,7 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act) { if( !this->ExtensionsInitialized && !this->IsUsingOpenGLMapper ) { - this->InitializeExtensions(); + this->InitExtensions(); act->GetProperty()->SetPointSize( 10.0f ); } @@ -409,13 +420,12 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act) // Make sure open GL extensions are initialized if (!this->ExtensionsInitialized) { - this->InitializeExtensions(); + this->InitExtensions(); } if (!this->ExtensionsOK) { return; } - */ vtkImageData *particlesource = NULL; if (this->ParticleImageFileName && !this->ParticleImage) @@ -503,7 +513,7 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act) // this->ParticleTexture->DebugOn(); // this->ParticleTexture->Update(); - +*/ // setup clippingplanes clipPlanes = this->ClippingPlanes; if (clipPlanes == NULL) @@ -704,7 +714,7 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act) glEnd(); \ } //----------------------------------------------------------------------------- -void VISU_OpenGLPointSpriteMapper::initSprites() +void VISU_OpenGLPointSpriteMapper::InitSprites() { switch (this->RenderMode) { @@ -750,6 +760,7 @@ void VISU_OpenGLPointSpriteMapper::initSprites() if (this->MaximumSupportedSize==0.0) { glGetFloatv( GL_POINT_SIZE_MAX_ARB, &this->MaximumSupportedSize ); + //cout << this->MaximumSupportedSize << endl; } if (this->DefaultPointSize==-1) { @@ -779,7 +790,7 @@ void VISU_OpenGLPointSpriteMapper::initSprites() */ } //----------------------------------------------------------------------------- -void VISU_OpenGLPointSpriteMapper::cleanupSprites() +void VISU_OpenGLPointSpriteMapper::CleanupSprites() { // Set GL params back to normal to stop other vtkMappers diusplaying wrongly glEnable( GL_DEPTH_TEST ); @@ -833,7 +844,7 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, vtkImageData* imageData = this->XMLImageDataReader->GetOutput(); this->InitTexture( imageData ); - initSprites(); + InitSprites(); void *voidPoints = p->GetVoidPointer(0); unsigned char *rgba; @@ -856,29 +867,29 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, { case VTK_PDPSM_POINT_TYPE_FLOAT: //std::cout << "VTK_PDPSM_POINT_TYPE_FLOAT " << std::endl; - vtkDrawPointsMacro(float, glVertex3fv(points + 3**ptIds);, initSprites();); + vtkDrawPointsMacro(float, glVertex3fv(points + 3**ptIds);, InitSprites();); break; case VTK_PDPSM_POINT_TYPE_DOUBLE: //std::cout << "VTK_PDPSM_POINT_TYPE_DOUBLE " << std::endl; - vtkDrawPointsMacro(double, glVertex3dv(points + 3**ptIds);, initSprites();); + vtkDrawPointsMacro(double, glVertex3dv(points + 3**ptIds);, InitSprites();); break; case VTK_PDPSM_POINT_TYPE_FLOAT | VTK_PDPSM_COLORS: //std::cout << "VTK_PDPSM_POINT_TYPE_FLOAT | VTK_PDPSM_COLORS " << std::endl; vtkDrawPointsMacro(float, glColor4ubv(rgba + 4**ptIds); - glVertex3fv(points + 3**ptIds);, initSprites();); + 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();); + 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; vtkDrawPointsMacro(float, glColor3ubv(rgba + 4**ptIds); - glVertex3fv(points + 3**ptIds);, initSprites();); + glVertex3fv(points + 3**ptIds);, InitSprites();); break; default: { @@ -933,7 +944,7 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, } } - cleanupSprites(); + CleanupSprites(); } //----------------------------------------------------------------------------- @@ -1020,6 +1031,28 @@ int VISU_OpenGLPointSpriteMapper::Draw(vtkRenderer *aren, vtkActor *act) return noAbort; } //----------------------------------------------------------------------------- +// Release the graphics resources used by this mapper. In this case, release +// the display list if any. +void VISU_OpenGLPointSpriteMapper::ReleaseGraphicsResources(vtkWindow *win) +{ + this->Superclass::ReleaseGraphicsResources(win); + /* + if (this->ListId && win) + { + win->MakeCurrent(); + glDeleteLists(this->ListId,1); + this->ListId = 0; + } + this->LastWindow = NULL; + // We actually only want to release our texture when the texture needs reloading, or when + // we are supposed to free up resources + if (this->ParticleTexture) + { + this->ParticleTexture->ReleaseGraphicsResources(win); + } + */ +} +//----------------------------------------------------------------------------- void VISU_OpenGLPointSpriteMapper::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os,indent); diff --git a/src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx b/src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx index 3dd034a9..81039239 100755 --- a/src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx +++ b/src/PIPELINE/VISU_OpenGLPointSpriteMapper.hxx @@ -28,6 +28,8 @@ #ifndef VISU_OpenGLPointSpriteMapper_HeaderFile #define VISU_OpenGLPointSpriteMapper_HeaderFile +//#undef GL_ARB_shader_objects + #if defined(_MSC_VER) # pragma warning ( disable : 4275 ) #endif @@ -149,6 +151,8 @@ public: void InitTexture( vtkImageData* ); + void SetShaderVariable( const char*, float ); + protected: VISU_OpenGLPointSpriteMapper(); ~VISU_OpenGLPointSpriteMapper(); @@ -162,9 +166,12 @@ protected: vtkCellArray *ca, vtkRenderer *ren); - void InitializeExtensions(); - void initSprites(); - void cleanupSprites(); + void InitShader(); + + void InitExtensions(); + + void InitSprites(); + void CleanupSprites(); vtkIdType TotalCells; @@ -185,6 +192,15 @@ protected: vtkImageData *gaussian; int RenderMode; +#ifdef GL_ARB_shader_objects + void* OpenGLLibrary; + + GLhandleARB VertexShader; + GLhandleARB VertexProgram; + + void printInfoLog( GLhandleARB ); +#endif + vtkXMLImageDataReader* XMLImageDataReader; GLuint PointSpriteTexture; -- 2.39.2