A shader program is a user-defined program written in OpenGL Shading Language (GLSL) that is executed by the graphics processing unit (GPU) of the graphics card. This topic provides an overview of the process of using shader programs with IDL.

Note: Your graphics card must support OpenGL 2.0 functionality and you will need to have the latest drivers installed to take advantage of shader programs in IDL.

Shader programs can produce results that are not possible using the fixed-function rendering pipeline exposed through IDL object properties. For example, if you create an IDLgrPolygon and set the COLOR property to green and the SHADING property to flat, OpenGL takes over rendering of a green polygon with flat shading; more precise control is not possible. However, a shader program provides far more control and lets you configure lighting and texture effects on a per-pixel basis.

The interaction of a shader program within the graphics system is shown in the following figure. The graphics card GPU switches between executing fixed-function and shader program code.

Note: Shader program attributes override all fixed-function attributes (those defined using object properties). If you define a blue sphere in IDL object graphics, but define a shader program to draw a green sphere, the displayed sphere will be green if there is suitable hardware support for the shader program.

Vertex and Fragment Shaders


Shader programs are highly configurable because each one consists of two required parts: a vertex shader and a fragment shader.

Tip: A fragment is the same thing as a pixel, but with extra information such as depth.

The shader program compiler built into OpenGL compiles the vertex and fragment shaders separately, then links them to form a complete shader program.

When a shader program is active, OpenGL calls the vertex shader program once for every vertex in the primitive it is currently drawing. Along with the expected position information (x, y, z, w) for the vertex, there is also color, normal, texture coordinate, lighting, and other associated information that is available. The vertices, connectivity, and transformation information are used to construct the primitive. The primitive undergoes rasterization, which converts the vertex representation to pixel representation. This defines the fragments.

OpenGL calls the fragment shader for every pixel that OpenGL intends to modify on the graphics device. The fragment shader determines the color of the pixel according to information it may obtain from the vertex program and from its own calculations. The shader program may also include computing normals to apply per-fragment lighting effects. It then tells OpenGL what color to use for drawing the pixel.

For the primitive shown in the previous figure, OpenGL calls the vertex shader three times, once for each corner of the triangle, and calls the fragment shader program once for every pixel covered by the triangle. Vertex attributes are interpolated across the fragments based on the vertex connectivity and the resulting distance of a fragment from a vertex.