Using the include directive in an ogsfx file to load your vertex and fragment shaders

An example is available in the presets\GLSL\examples folder of your Maya install that demonstrates the use of the #include directive in an .ogsfx file to load your vertex and fragment shaders.

Using the #include directive, you can retain your existing GLSL shader code instead of having to combine it all into one .ogsfx file. This allows you to share the same GLSL fragments between different applications.

The example consists of the following files:

Uniform definitions

The .ogsfx file starts with defining the uniform parameters.

HIDE_OGSFX_UNIFORMS is set to 0, and the #include directive is used to import the Brix.glslf and Brix.glslv shader files.

Code in the Brix.glslf and Brix.glslv shader files that are wrapped within #if !HIDE_OGSFX_UNIFORMS and #endif are inserted (by the preprocessor) into an expanded intermediate temporary .ogsfx file, which is then compiled. This temporary .ogsfx file is compiled only after the original .ogsfx file has been fully pre-processed.

Within the #if !HIDE_OGSFX_UNIFORMS and #endif wrappers, #if OGSFX and #endif statements are used to wrap OGSFX specific code.

Use #else within the #if and #endif wrappers to define your uniforms for other 3rd party applications that use GLSL.

In the OGSFX uniform definitions, semantics such as World and WorldInverseTranspose are used to indicate to Maya the meaning of the uniform.

For example:

uniform mat4 gWorldITXf : WorldInverseTranspose < string UIWidget="None"; >;

indicates to Maya that gWorldITXf is a matrix 4 that contains the World transformation information, inverted and transposed.

See Shader semantics supported by Viewport 2.0 in the Viewport 2.0 API section of the Maya Developer Help for a list of semantics that is supported in Maya Viewport 2.0.

Light specific semantics such as DIRECTION and AMBIENT allow a light to be driven by Maya. For example, AMBIENT allows the user to set the ambient light color in the Maya Attribute Editor.

All semantics listed on Semantics and annotations supported by the dx11Shader plug-in in Viewport 2.0 are supported by GLSL shaders in Maya.

Input and output stream definitions

The .ogsfx file then defines the input and output streams.

HIDE_OGSFX_STREAMS is set to 0, and the #include directive is used to import the Brix.glslf and Brix.glslv shader files.

Code in the vertex and fragment shader files that are wrapped within #if !HIDE_OGSFX_STREAMS and #endif are compiled.

Within the #if !HIDE_OGSFX_STREAMS and #endif wrappers, #if OGSFX and #endif statements are again used to wrap OGSFX specific code.

Implementing the main functions of the vertex and fragment shaders

The .ogsfx file then implements the main() functions of the vertex and fragment shaders.

HIDE_OGSFX_CODE is set to 0, and the vertex and fragment shaders are loaded within separate GLSLShader blocks using the #include directive; for example, as follows:

GLSLShader VS
{
#include "Brix.glslv"
}

The main() functions in the vertex and fragment shaders that are wrapped within the #if !HIDE_OGSFX_CODE and #endif statements are compiled. Only one main() function is required, but additional functions can be defined and called by main().

Technique definition

A technique can contain one or more passes. Each pass defines a certain way of rendering an object, and specifies the vertex shader and pixel shader that should be used.

You can specify more than one technique, and if you have more than one vertex shader and/or more than one fragment shader, you can specify which is to be used for each technique. In this example, only one main technique is defined.

In the code handling section, Brix.glslv was loaded and defined as VS.

GLSLShader VS
{
#include "Brix.glslv"
}

Here, in the technique definition, VS is used identify the same vertex shader file:

VertexShader (in appdata, out brixVertexOutput) = VS;

Ensure that the input and output parameters listed while specifying the vertex and fragment shader are consistent with the parameters defined in the vertex shader and fragment shader files.

For example, there must be a matching input named appdata and a matching output named brixVertexOutput in Brix.glslv.

VertexShader (in appdata, out brixVertexOutput) = VS;