Setting Up a Cg Program Vertex Shader

This procedure shows you how to set up a vertex shader on an object using the CgProgram node.

1. Open a render tree (press 7) and connect an OGL Draw shader (Nodes > RealTime > OpenGL > More...) to the realtime port of the object’s Material node.

 

2. Connect a Cg Program node (Nodes > Realtime > Cg > Cg Program) to the OGL Draw node’s previous port.

 

3. Double-click the Cg Program node to open its property editor and start typing your code. But first, let’s take a look at what’s in the property editor.

 

• The Build options allow you to do any one of the following:

- Disable the Cg Program.

- Compile the program without executing it.

- Compile and execute the program.

• You can see compilation results by activating the Debug messages option.

 

Compilation results are logged and available from the script editor’s log window.

• The Profile options let you choose the type of program you want to execute. This is where you specify which hardware you are targeting and whether it’s a vertex or a pixel (fragment) program.

 

If you are not specifically targeting nVidia hardware, you should choose the ARB types.

In the property editor shown above, the shader was set up to shade the object to which it’s applied in red. The parameters in the property editor were set as follows:

• A Cg Program was typed in the text box.

• The Build option was set to Compile and execute.

• The Debug messages were activated.

• The Profile was set to ARB Vertex Program.

You can see that the compilation succeeded in the script editor log window, where the following appeared:

'Compiling Cg Program
'Program is compiled
'LAST LISTING--------
'---- PROGRAM BEGIN ----
'!!ARBvp1.0
'# ARB_vertex_program generated by NVIDIA Cg compiler
'# cgc version 1.2.0001, build date Feb 19 2004  10:44:56
'# command line args: -q -profile arbvp1 -entry main
'# nv30vp backend compiling 'main' program
'PARAM c4 = { 1, 0, 0, 1 };
'#vendor NVIDIA Corporation
'#version 1.0.02
'#profile arbvp1
'#program main
'#semantic main.simodelviewproj
'#var float4x4 simodelviewproj :  : c[0], 4 : 1 : 1
'#var float4 pos : $vin.POSITION : POSITION : 0 : 1
'#var float4 main.hpos : $vout.HPOS : HPOS : -1 : 1
'#var float4 main.color : $vout.COL0 : COL0 : -1 : 1
'ATTRIB v16 = vertex.position;
'PARAM c0[4] = { program.local[0..3] };
'DP4 result.position.x, c0[0], v16;
'DP4 result.position.y, c0[1], v16;
'DP4 result.position.z, c0[2], v16;
'DP4 result.position.w, c0[3], v16;
'MOV result.color.front.primary, c4.xyyx;
'END
'# 5 instructions
'# 0 temp registers
'# End of program
'---- PROGRAM END ----

Setting Vertex Shader Parameters

Once that you have your program code written, you must set the values for the uniform parameters. There are three methods for setting uniform parameters:

• Using Softimage semantics.

• Using custom parameter sets.

• Using Cg Matrix, CgVector, Cg Color, or Cg Light tracker shaders.

This section discusses all three methods.

Using Softimage Semantics

Semantics are reserved uniform parameter names that are automatically set by Autodesk Softimage prior to executing the program. They set matrices, camera information, and light information.

Once the semantic is used in the program, no additional steps are required to fill in these parameters. This is particularly useful because it filters out information that would otherwise complicate the interface.

Here is a list of Softimage semantics:

siviewportsize

Size of the viewport in which the object is being rendered.

sieyepos

Camera position.

simodelviewproj

Combined model, view and projection matrix.

simodelviewprojI

Inverted combined model, view and projection matrix.

simodelviewprojT

Transposed combined model, view and projection matrix.

simodelviewprojIT

Inverted and transposed combined model, view and projection matrix.

simodelview

Combined model and view matrix.

simodelviewI

Inverted combined model and view matrix.

simodelviewT

Transposed combined model and view matrix.

simodelviewIT

Inverted and transposed model and view matrix.

siprojection

Projection matrix.

siprojectionI

Inverted projection matrix.

siprojectionT

Transposed projection matrix.

siprojectionIT

Inverted and transposed projection matrix.

siview

View matrix.

siviewI

Inverted view matrix.

siviewT

Transposed view matrix.

siviewIT

Inverted and transposed view matrix.

simodel

Model matrix.

simodelI

Inverted model matrix.

simodelT

Transposed model matrix.

simodelIT

Inverted and transposed model matrix.

For light information, where LIGHTID is the id of the light to track (for example, silightdiffuse_0 is the diffuse color of light 0):

silighttype_LIGHTID

Light type.

silightambient_LIGHTID

Light ambient color.

silightdiffuse_LIGHTID

Light diffuse color.

silightspecular_LIGHTID

Light specular color.

silightposition_LIGHTID

Light position.

silightdirection_LIGHTI

Light direction.

silightfalloff_LIGHTID

Light falloff (spotlight).

silightattenuation_LIGHTID

Light attenuations.

silightcone_LIGHTID

Light cone (spotlight).

 

In older versions of Softimage, you had to use a Cg Matrix, CgVector, Cg Color, or Cg Light tracker shader to set values into these types of uniform parameters. Using semantics simplifies the interface and the shading process considerably.

Using Custom Parameter Sets

You can add custom parameter sets to shaders. The Cg Program node can use these parameters with the following naming convention:

<name of custom parameter set>_<name of parameter>

For example, if you have a custom parameter set called testPSet and a parameter called testParam, then you can specify a uniform parameter of type float that is called testPSet_testParam to be used in the program.

To use a custom parameter set

1. In the explorer, select the shader to which you want to add a custom parameter set.

2. From the Animation toolbar, choose Create > Parameter > New Custom Parameter Set.

3. Add parameters to the custom parameter set.

For more information about how to do this, as well as general information about working with custom parameter sets, see Custom and Proxy Parameters [Customization].

You can then reference the custom parameter set in the shader code using the proper naming convention, as in the following code example, demonstrating references to the TestColor_red custom parameter:

struct v2f
{
   float4     hpos : HPOS;
   float4     color : COL0;
};

v2f main
(
   float4     pos : POSITION,
   uniform       float4x4       simodelviewproj,
   uniform       float TestColor_red
)
{
   v2f OUT;

   OUT.hpos = mul( simodelviewproj, pos);
   OUT.color = float4(TestColor_red,0,0,1);

   return OUT;
}

Notice how the custom parameter control is laid out when you open the shader property page. You can now adjust the custom parameter’s slider to change the value of the uniform parameter and see the result in the viewport.

 

 

You can quickly create a custom parameter set that adds a color parameter to a shader by selecting the shader and, from the Render toolbar, choosing Get > Property > Custom Color.

The advantage of adding a color this way is that the custom parameter set contains the standard color controls, including the color sliders and the color chip, which you can click to open the Color Editor.

Using the Cg Matrix, Cg Vector, Cg Color, and Cg Light Tracker

In general, you should not have to use these shader nodes which are kept for reasons of backward compatibility.

Cg Matrix sets values for a float4x4 type uniform parameter.

Cg Vector and Cg Color set values for a float4 type uniform parameter.

Cg Light Tracker tracks lights information and sets values in float4 type uniform parameters.



Autodesk Softimage v.7.5