[vtkusers] How to direct use GLSL in vtk?

Francois Bertel francois.bertel at kitware.com
Fri Jan 12 12:28:31 EST 2007


I bet you forgot to load the relevant OpenGL extensions for the current OpenGL context. So probably the function pointer vtkgl::CreateShader is null.

1. First have a render window

vtkRenderWindow *renWin = vtkRenderWindow::New();

2. Create an OpenGL extension manager object and feed it with the render window

vtkOpenGLExtensionManager *extensions=vtkOpenGLExtensionManager::New();
extensions->SetRenderWindow(renWin);


3. Check if the extensions you want to use are supported by the current OpenGL context (I mean the combination of your graphic card+current graphic mode)

int supports_GL_ARB_shader_objects=extensions->ExtensionSupported("GL_ARB_shader_objects");
int supports_GL_ARB_vertex_shader=extensions->ExtensionSupported("GL_ARB_vertex_shader");
int supports_GL_ARB_fragment_shader=extensions->ExtensionSupported("GL_ARB_fragment_shader");

4. Load the extensions (it will initialized the function pointers associated with those extensions)

if(supports_GL_ARB_shader_objects && supports_GL_ARB_vertex_shader && supports_GL_ARB_fragment_shader)
{
 extensions->LoadExtension("GL_ARB_vertex_shader");
 extensions->LoadExtension("GL_ARB_shader_objects");
 extensions->LoadExtension("GL_ARB_fragment_shader");
}

5. Optionally delete the extension manager if you don't need it anymore at this point:

extensions->Delete();

5. Use the functions defined by the extensions:

GLuint shader=vtkgl::CreateShaderObjectARB(vtkgl::FRAGMENT_SHADER_ARB);


Note in your example you are using vtkgl::CreateShader instead of vtkgl::CreateShaderObjectARB which is not defined in the "GL_ARB_shader_objects" extension but in OpenGL 2.0 itself ("GL_VERSION_2_0" extension in VTK).
If you really want to require the current context to be OpenGL 2.0 compatible, you should have something just like:

int supports_GL_VERSION_2_0=extensions->ExtensionSupported("GL_VERSION_2_0");

if(supports_GL_VERSION_2_0)
{
extensions->LoadExtension("GL_VERSION_2_0");
GLuint shader=vtkgl::CreateShader(vtkgl::FRAGMENT_SHADER);
}

With extension "GL_VERSION_2_0" you are loading all of the OpenGL 2.0 functions and the function pointers are different from ARB extensions. If you try to load extension "GL_ARB_shader_objects" only,
the function pointer vtkgl::CreateShaderObjectARB will be initialized but the function pointer vtkgl::CreateShader will be null. If you load extension "GL_VERSION_2_0",
the function pointer vtkgl::CreateShader will be initialized but the function pointer vtkgl::CreateShaderObjectARB will be null.

Now the question is what is the best? loading only the ARB extensions required by your program or the all OpenGL 2.0 extensions? If your program only use the vertex shader and fragment shader features of OpenGL 2.0, I recommend to load the ARB extensions. This way, your program may work on graphic cards that supports vertex and fragment shaders without being OpenGL 2.0 compatible.
VTK is OpenGL 1.1 compatible, any other feature introduced in more recent OpenGL version should be load as an extension, either with the OpenGL version ("GL_VERSION_1_5"), either with the name of the specific extension
("GL_ARB_fragment_shader").

-- 
François Bertel, PhD  | Kitware Inc. Suite 204
1 (518) 371 3971 x113 | 28 Corporate Drive
                      | Clifton Park NY 12065, USA



More information about the vtkusers mailing list