Programmable OpenGL Shading

Programmable shaders allow you to replace the fixed function pipeline with your own program to control shading at the vertex and pixel level. This allows you to create more complex effects, but it also requires that you re-implement code that the fixed-function pipeline executes automatically, like transforms and shading of vertices, texture coordinate generation and transforms, and texture modulation modes.

There are two programmable shader types: vertex shaders and pixel shaders. To create OpenGL-based effects, you can use nVidia’s Cg language or the OpenGL Shading Language (GLSL) to write vertex and pixel shader code.

 

All internet addresses have been checked prior to publication. However, because of the transient nature of the Internet, they may no longer be valid.

Vertex Shaders

Programmable vertex shaders are executed for every vertex of the shaded object. They take input parameters, process them, and generate output data to be sent to a pixel shader.

The input parameters of a vertex shader can be:

• Vertex position in object space.

• Vertex normal in object space.

• Vertex colors.

• Texture coordinates.

• Uniform parameters, which are constants that can be used in the shader code.

The output of a vertex shader can be:

• Vertex position in screen space.

• Vertex colors.

• Texture coordinates.

Pixel Shaders

Pixel shaders (also known as fragment shaders) are executed for every pixel generated of the object. They take input parameters, process them, and generate output data to be sent to the blending core of the GPU before going to the frame buffer. The input parameters come from vertex shaders and are interpolated inside of the triangle to which they belong.

The input parameters of a pixel shader can be:

• Vertex position in screen space.

• Vertex colors.

• Texture coordinates.

• Uniform parameters, which are constants that can be used in the shader code.

The output of a pixel shader can be composed of:

• Pixel color.

• Pixel depth.

Programmable Shader Structure

A programmable shader has a very simple structure: it has input parameters, output parameters, and code. The code is called for every vertex of every pixel.

There are two types of input parameters: uniform and varying. Uniform means that the parameter value is the same for all vertices or pixels. Varying means that the parameter value changes for every vertex and every pixel.

Softimage has shaders that allow you to specify program code and uniform parameters. Varying parameters are specified as part of the program code. For example, in the following code, the varying input parameter is the position of the vertex in object space and the uniform input parameter is the model/view/projection matrix of the object:

struct v2f
{
   float4     hpos : HPOS; // position of the vertex in screen space
   float4     color : COL0; // vertex color
};

v2f main
(
   float4 pos : POSITION,  // position of the vertex in object space
   uniform float4x4 simodelviewproj  // model/view/projection matrix of the object (used to 
                                   // transform the position from object space to screen space)
)
{
   v2f OUT;

   // Compute the color and the position of the vertex in screen space
   OUT.hpos = mul( simodelviewproj, pos);
   OUT.color = float4(1,0,0,1);

   return OUT;
}


Autodesk Softimage v.7.5