From b19498556fb25d93382158bc64304a2f86d5cc52 Mon Sep 17 00:00:00 2001 From: ouv Date: Tue, 6 Sep 2005 05:46:50 +0000 Subject: [PATCH] Using OpenGL 2.0 Shading Language --- resources/Vertex_Program_ARB.txt | 133 +++++--- src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx | 294 +++++++++--------- 2 files changed, 247 insertions(+), 180 deletions(-) diff --git a/resources/Vertex_Program_ARB.txt b/resources/Vertex_Program_ARB.txt index 89f2feb9..eb252776 100755 --- a/resources/Vertex_Program_ARB.txt +++ b/resources/Vertex_Program_ARB.txt @@ -1,55 +1,106 @@ -!!ARBvp1.0 +// simple vertex shader +/* +struct VS_INPUT { + float3 Pos : POSITION; + float I : TEXCOORD0; +}; -# Constant Parameters -PARAM mvp[4] = { state.matrix.mvp }; # modelview projection matrix -PARAM constSize = program.env[0]; # constant size -PARAM constColor = program.env[1]; # constant color (defined by the application) -PARAM coefficient = program.env[2]; +struct VS_OUTPUT { +// float4 Pos : POSITION; +// float4 Col : COLOR0; + float Size : PSIZE; +}; -# constColor.x == 1.0 -# constColor.y == 1.0 -# constColor.z == 1.0 -# constColor.w == 1.0 +float ComputePointSize(float3 inVertex, float inSize) { + float4 viewPos = mul(float4(inVertex, 1), WorldView); + float dist = (viewPos.x * viewPos.x + viewPos.y * viewPos.y + viewPos.z * viewPos.z); + float size = viewportSize.y * inSize * globalPointSize * rsqrt((1 + dist)); -# constSize.x == 50.0 -# constSize.y == 1.0 -# constSize.z == 1.0 -# constSize.w == 1.0 + return max( 16.0f, size ); +} -# coefficient.x == 1.0 -# coefficient.y == 10.0 -# coefficient.z == 100.0 -# coefficient.w == 1000.0 +VS_OUTPUT VertexShader_PointSpritePalette(VS_INPUT vIn) { + VS_OUTPUT Out; -# Per-vertex inputs -ATTRIB inPosition = vertex.position; -ATTRIB inColor = vertex.color; +// Out.Pos = mul(float4(vIn.Pos, 1), WorldViewProjection); -# Per-vertex outputs -OUTPUT outPosition = result.position; -OUTPUT outColor = result.color; -OUTPUT outSize = result.pointsize; + if( vIn.I < limitMin || vIn.I > limitMax ) { + Out.Size = 0; + } else { + float stepValue = vIn.I; -DP4 outPosition.x, mvp[0], inPosition; # Transform the x component of the per-vertex position into clip-space -DP4 outPosition.y, mvp[1], inPosition; # Transform the y component of the per-vertex position into clip-space -DP4 outPosition.z, mvp[2], inPosition; # Transform the z component of the per-vertex position into clip-space -DP4 outPosition.w, mvp[3], inPosition; # Transform the w component of the per-vertex position into clip-space + float size1 = 1.0f; + float size2 = 4.0f; + float pSize = size1 + stepValue * (size2 - size1); + Out.Size = ComputePointSize(vIn.Pos, pSize) * 0.5; + } -# Color management -MOV outColor, inColor; # Use the original per-vertex color specified -#MOV outColor, constColor; # Uncomment this to use the constant color stored at "program.env[0]" + return Out; +} +*/ -# Size management -TEMP size1; +float3 ComputePointSize( float3 inColor ) +{ + int r = inColor.x * 255; + int g = inColor.y * 255; + int b = inColor.z * 255; + int h = 0; -MOV size1, constSize; + int max = r; + int whatmax = 0; + if( g > max ) + { + max = g; + whatmax = 1; + } + if( b > max ) + { + max = b; + whatmax = 2; + } + int min = r; + if ( g < min ) min = g; + if ( b < min ) min = b; + int delta = max-min; -#DP4 size1.x, inColor, inColor; -#MUL size1.x, size1.x, coefficient.y; + if( whatmax == 0 ) + { + if ( g >= b ) + h = (120*(g-b)+delta)/(2*delta); + else + h = (120*(g-b+delta)+delta)/(2*delta) + 300; + } + else if( whatmax == 1 ) + { + if ( b > r ) + h = 120 + (120*(b-r)+delta)/(2*delta); + else + h = 60 + (120*(b-r+delta)+delta)/(2*delta); + } + else + { + if ( r > g ) + h = 240 + (120*(r-g)+delta)/(2*delta); + else + h = 180 + (120*(r-g+delta)+delta)/(2*delta); + } -#MUL size1.x, size1.x, inColor.w; -#MUL size1.x, size1.x, coefficient.y; + float minSize = 20.0f; + float maxSize = 40.0f; -MOV outSize, size1; + float size = minSize + ( maxSize - minSize ) * ( 1 - h / 256.0f ); -END \ No newline at end of file + return size; +} + +void main() +{ + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; + + gl_FrontColor = gl_Color; + + float size = ComputePointSize( gl_Color ); + + gl_PointSize = size * gl_ProjectionMatrix[0].x; + +} diff --git a/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx b/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx index f3bf63c9..73868c2c 100755 --- a/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx +++ b/src/PIPELINE/VISU_OpenGLPointSpriteMapper.cxx @@ -49,14 +49,8 @@ #include "vtkXMLImageDataReader.h" #include "vtkImageData.h" -#ifdef __cplusplus -extern "C" { -#endif #include #include -#ifdef __cplusplus -} -#endif #include @@ -79,28 +73,22 @@ vtkStandardNewMacro(VISU_OpenGLPointSpriteMapper); #define VTK_PDPSM_OPAQUE_COLORS 0x0040 #define VTK_PDPSM_ALPHA_ARRAY 0x0080 -#ifdef __cplusplus -extern "C" { -#endif - -#define GL_POINT_SPRITE_ARB 0x8861 -#define GL_COORD_REPLACE_ARB 0x8862 - -#define GL_ARB_point_parameters 1 - -//GLAPI void APIENTRY glPointParameterfARB (GLenum, GLfloat); -//GLAPI void APIENTRY glPointParameterfvARB (GLenum, const GLfloat *); - -#ifdef __cplusplus -} -#endif - 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; + unsigned int shaderId; //----------------------------------------------------------------------------- @@ -188,47 +176,76 @@ VISU_OpenGLPointSpriteMapper::~VISU_OpenGLPointSpriteMapper() } } //----------------------------------------------------------------------------- -// Name: readShaderFile() -// Desc: -//----------------------------------------------------------------------------- -unsigned char *readShaderFile( std::string fileName ) +char* readFromFile( std::string fileName ) { FILE* file = fopen( fileName.c_str(), "r" ); - unsigned char* buffer = new unsigned char[ 2000 ]; - int bytes = fread( buffer, 1, 2000, file ); - buffer[bytes] = 0; - - unsigned char* shader = new unsigned char[ bytes ]; - for( int i = 0; i < bytes; i++ ) - shader[i] = buffer[i]; + char* content = NULL; + int count = 0; - fclose( file ); + if( file != NULL ) + { + fseek( file, 0, SEEK_END ); + count = ftell( file ); + rewind( file ); - delete buffer; + if( count > 0 ) + { + content = ( char* )malloc( sizeof( char ) * ( count + 1 ) ); + count = fread( content, sizeof( char ), count, file ); + content[ count ] = '\0'; + } + fclose( file ); + } - return shader; + return content; } //----------------------------------------------------------------------------- -// Name: initShader() -// Desc: Assemble the shader +void printInfoLog( GLhandleARB obj ) +{ + int infologLength = 0; + int charsWritten = 0; + char* infoLog; + + glGetObjectParameterivARB( obj, GL_OBJECT_INFO_LOG_LENGTH_ARB, &infologLength ); + + if( infologLength > 0 ) + { + infoLog = ( char* )malloc( infologLength ); + glGetInfoLogARB( obj, infologLength, &charsWritten, infoLog ); + printf( "%s\n", infoLog ); + free( infoLog ); + } +} //----------------------------------------------------------------------------- void initShader() { std::string fileName = std::string( getenv( "VISU_ROOT_DIR") ) + "/share/salome/resources/Vertex_Program_ARB.txt"; - unsigned char* shader = readShaderFile( fileName ); - int size = strlen( (char*)shader ); + //std::string fileName = std::string( "/dn06/salome/ouv/SALOME3/VISU_SRC/resources/Vertex_Program_ARB.txt"); - glGenProgramsARB( 1, &shaderId ); - glBindProgramARB( GL_VERTEX_PROGRAM_ARB, shaderId ); + char* shader = readFromFile( fileName ); - glProgramStringARB( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, - size, shader ); + //glGenProgramsARB( 1, &shaderId ); + //glBindProgramARB( GL_VERTEX_PROGRAM_ARB, shaderId ); + //glProgramStringARB( GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen( shader ), shader ); + + GLhandleARB v = glCreateShaderObjectARB( GL_VERTEX_SHADER_ARB ); + glShaderSourceARB( v, 1, (const GLcharARB**)&shader, NULL ); + glCompileShaderARB( v ); + //printInfoLog( v ); + + GLhandleARB p = glCreateProgramObjectARB(); + glAttachObjectARB( p, v ); + + glLinkProgramARB( p ); + //printInfoLog( p ); + + glUseProgramObjectARB( p ); /* cout << "Shader from " << fileName << endl; - for( int i = 0; i < size; i++ ) + for( int i = 0; i < strlen( shader ); i++ ) cout << shader[i]; cout << endl; @@ -293,18 +310,20 @@ 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"); - - /* - glGenProgramsARB = (PFNGLGENPROGRAMSARBPROC)glXGetProcAddressARB((const GLubyte*)"glGenProgramsARB"); - glDeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)glXGetProcAddressARB((const GLubyte*)"glDeleteProgramsARB"); - glBindProgramARB = (PFNGLBINDPROGRAMARBPROC)glXGetProcAddressARB((const GLubyte*)"glBindProgramARB"); - glProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)glXGetProcAddressARB((const GLubyte*)"glProgramStringARB"); - glProgramEnvParameter4fARB = (PFNGLPROGRAMENVPARAMETER4FARBPROC)glXGetProcAddressARB((const GLubyte*)"glProgramEnvParameter4fARB"); - */ + glDeleteProgramsARB = (PFNGLDELETEPROGRAMSARBPROC)dlsym( libHandle, "glDeleteProgramsARB" ); + glBindProgramARB = (PFNGLBINDPROGRAMARBPROC)dlsym( libHandle, "glBindProgramARB" ); + glProgramStringARB = (PFNGLPROGRAMSTRINGARBPROC)dlsym( libHandle, "glProgramStringARB" ); + glProgramEnvParameter4fARB = (PFNGLPROGRAMENVPARAMETER4FARBPROC)dlsym( libHandle, "glProgramEnvParameter4fARB" ); + + glShaderSourceARB = (PFNGLSHADERSOURCEARBPROC)dlsym( libHandle, "glShaderSourceARB" ); + glCreateShaderObjectARB = (PFNGLCREATESHADEROBJECTARBPROC)dlsym( libHandle, "glCreateShaderObjectARB" ); + glCompileShaderARB = (PFNGLCOMPILESHADERARBPROC)dlsym( libHandle, "glCompileShaderARB" ); + glCreateProgramObjectARB = (PFNGLCREATEPROGRAMOBJECTARBPROC)dlsym( libHandle, "glCreateProgramObjectARB" ); + glAttachObjectARB = (PFNGLATTACHOBJECTARBPROC)dlsym( libHandle, "glAttachObjectARB" ); + glLinkProgramARB = (PFNGLLINKPROGRAMPROC)dlsym( libHandle, "glLinkProgramARB" ); + glUseProgramObjectARB = (PFNGLUSEPROGRAMOBJECTARBPROC)dlsym( libHandle, "glUseProgramObjectARB" ); + glGetObjectParameterivARB = (PFNGLGETOBJECTPARAMETERIVARBPROC)dlsym( libHandle, "glGetObjectParameterivARB" ); + glGetInfoLogARB = (PFNGLGETINFOLOGARBPROC)dlsym( libHandle, "glGetInfoLogARB" ); initShader(); setShaderConstants(); @@ -672,68 +691,10 @@ void VISU_OpenGLPointSpriteMapper::RenderPiece(vtkRenderer *ren, vtkActor *act) glEnd(); \ } //----------------------------------------------------------------------------- -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_BLEND ); - - glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB ); - glDisable( GL_POINT_SPRITE_ARB ); -} -//----------------------------------------------------------------------------- -GLuint VISU_OpenGLPointSpriteMapper::LoadTexture( vtkImageData* imageData ) -{ - //this->XMLImageDataReader->SetFileName( fileName ); - //vtkImageData* imageData = this->XMLImageDataReader->GetOutput(); - //imageData->DebugOn(); - imageData->RequestExactExtentOn(); - imageData->UpdateInformation(); - imageData->SetUpdateExtentToWholeExtent(); - imageData->Update(); - //cout << "Dimension of image data : " << imageData->GetDataDimension() << endl; - - int* size = imageData->GetDimensions(); - vtkDataArray* scalars = imageData->GetPointData()->GetScalars(); - unsigned char* dataPtr = static_cast(scalars)->GetPointer(0); - - 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 ); - - 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 ); - - glEnable( GL_POINT_SPRITE_ARB ); - glEnable( GL_TEXTURE_2D ); - glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); - //glAlphaFunc( GL_GREATER, 0.95F ); - //glEnable( GL_ALPHA_TEST ); - - glBindTexture( GL_TEXTURE_2D, texture ); - glBegin( GL_POINTS ); - - glVertex3f( x, y, z ); - - glEnd(); - glFlush(); - - //glDisable( GL_ALPHA_TEST ); - glDisable( GL_TEXTURE_2D ); -} -//----------------------------------------------------------------------------- void VISU_OpenGLPointSpriteMapper::initSprites() { + glBindTexture( GL_TEXTURE_2D, TextureIntensity ); + switch (this->RenderMode) { case VISU_OpenGLPointSpriteMapper::Accumulate: @@ -789,6 +750,8 @@ 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 ); @@ -810,14 +773,75 @@ 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_BLEND ); + + glDisable( GL_VERTEX_PROGRAM_POINT_SIZE_ARB ); + glDisable( GL_POINT_SPRITE_ARB ); + + glDisable( GL_TEXTURE_2D ); +} +//----------------------------------------------------------------------------- +GLuint VISU_OpenGLPointSpriteMapper::LoadTexture( vtkImageData* imageData ) +{ + //this->XMLImageDataReader->SetFileName( fileName ); + //vtkImageData* imageData = this->XMLImageDataReader->GetOutput(); + //imageData->DebugOn(); + imageData->RequestExactExtentOn(); + imageData->UpdateInformation(); + imageData->SetUpdateExtentToWholeExtent(); + imageData->Update(); + //cout << "Dimension of image data : " << imageData->GetDataDimension() << endl; + + int* size = imageData->GetDimensions(); + vtkDataArray* scalars = imageData->GetPointData()->GetScalars(); + unsigned char* dataPtr = static_cast(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 ); + + 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 ); + + glVertex3f( x, y, z ); + + glEnd(); + glFlush(); + + //glDisable( GL_ALPHA_TEST ); + //glDisable( GL_TEXTURE_2D ); +} +//----------------------------------------------------------------------------- void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, - vtkPoints *p, - vtkUnsignedCharArray *colors, - vtkFloatArray *alpha, - vtkIdType &cellNum, - int &noAbort, - vtkCellArray *cells, - vtkRenderer *ren) + vtkPoints *p, + vtkUnsignedCharArray *colors, + vtkFloatArray *alpha, + vtkIdType &cellNum, + int &noAbort, + vtkCellArray *cells, + vtkRenderer *ren) { void *voidPoints = p->GetVoidPointer(0); unsigned char *rgba; @@ -876,12 +900,11 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, initSprites(); - glEnable( GL_VERTEX_PROGRAM_ARB ); + //glEnable( GL_VERTEX_PROGRAM_ARB ); + //glBindProgramARB( GL_VERTEX_PROGRAM_ARB, shaderId ); //glColor4f( 1.0, 1.0, 1.0, 1.0 ); - glBindProgramARB( GL_VERTEX_PROGRAM_ARB, shaderId ); - vtkIdType *pts = 0; vtkIdType npts = 0; unsigned short count = 0; @@ -910,12 +933,7 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, } } - //cout << "POINT : "; - //cout << p->GetPoint(pts[0])[0] << " "; - //cout << p->GetPoint(pts[0])[1] << " "; - //cout << p->GetPoint(pts[0])[2] << endl; - - glVertex3fv(p->GetPoint(pts[0])); + glVertex3fv(p->GetPoint(pts[0])); /* vtkXMLImageDataReader* aReader = vtkXMLImageDataReader::New(); @@ -928,8 +946,7 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, TextureAlphaChannel = this->LoadTexture( aReader->GetOutput() ); aReader->Delete(); */ - - /* + /* this->DrawTexture( TextureIntensity, p->GetPoint(pts[0])[0], p->GetPoint(pts[0])[1], @@ -941,6 +958,7 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, p->GetPoint(pts[0])[2] ); */ + //glVertex3fv(p->GetPoint(pts[0])); // check for abort condition if (count == 10000) @@ -957,12 +975,10 @@ void VISU_OpenGLPointSpriteMapper::DrawPoints(int idx, } glEnd(); - glDisable( GL_VERTEX_PROGRAM_ARB ); + //glDisable( GL_VERTEX_PROGRAM_ARB ); } } - // - // restore Gl state to normal - // + cleanupSprites(); } -- 2.39.2