Houdini 11 Shading

Overview

OpenGL shaders are hardware accelerated shaders that are executed on the video card. OpenGL shaders only affect the viewport – they are not rendered. You can create an OpenGL shader and include it in a material to show a better representation of the material in the viewport.

Writing an OpenGL shader

Creating a new shader type

OpenGL shaders are written using GLSL (the OpenGL Shader Language). Before you can create an OpenGL shader you need to understand GLSL and its related concepts such as vertex shaders and fragment shaders.

  1. Choose File > New operator type.

  2. Click on SHOP Type.

  3. In the Network Type menu, choose GLSL Shader.

  4. Enter an internal Name for the shader and a human-readable Label.

  5. Click Accept. The type properties window for the new shader type appears.

  6. Click the Code tab. The node starts with some default shader code. You can use this as a starting point for creating your shader.

    The Code tab provides a simple editor for working with GLSL code. The editor has three text boxes: one for the vertex shader, one for the fragment shader, and one for compiler output. You can drag the dividers between the three panes to resize them.

  7. Click Test compile to try compiling the default code. The output of the compiler appears in the third pane.

See the OpenGL GLSL documentation for more information on programming in GLSL.

You cannot currently create a GLSL shader using VOPs.

Houdini built-in functions

Houdini will automatically load and link the following GLSL functions if they are found in the shader code. All the methods are fragment shader specific unless otherwise noted.

void HOUassignOutputs(vec3 emit, vec3 amb, vec3 diff, vec3 spec, float shiny, vec3 P, vec3 N, float alpha)

Assigns the various RGB lighting components to the output color. Additional information, such as P (point position in view space), N (normal), shiny (specular shininess) and alpha (opacity) is required for proper shading in High Quality Lighting and Transparency rendering modes.

void HOUassignDiffuseOnly(vec3 diff, vec3 P, vec3 N, float alpha)

Assigns only the diffuse component. All other components are assumed to be zero. Point, Normal and alpha are required for High Quality Lighting and Transparency passes.

void HOUassignEmissionOnly(vec3 emit, float alpha)

Assigns the emission of the object along with the opacity. This is also used when the shader computes its own illumination, or doesn’t want its color affected by further lighting calculations.

Note

All fragment shaders must use exactly one of the HOUassign methods to write out their color values, instead of directly writing to gl_FragColor or gl_FragData[].

float HOUdiffuse(vec3 P, vec3 N)

Computes the diffuse illumination given point position P (in view space) and normal N.

void HOUlightDiffSpec(in vec3 P, in vec3 N, inout float diff, inout float spec, in float shiny)

Computes the specular and diffuse intensities for all lights in the scene (up to gl_MaxLights) given point position P, normal N and material shininess shiny. The results are added to the diff and spec parameters.

HOUlightingModel(in vec3 P, in vec3 N, inout vec4 amb, inout vec4 diff, inout float spec, in float shiny)

Computes the ambient, diffuse and specular colors for all lights in the scene (up to gl_MaxLights), given point position P, normal N, and material shininess shiny. The results are added to the amb, diff and spec parameters.

vec3 HOUfaceForward(vec3 N, vec3 P)

Returns the normal N if the polygon is forward facing, or -N if it is not. Point position P is required for OpenGL implementations with a broken gl_FrontFacing attribute, which this function attempts to mask.

void HOUdepthPeel(vec3 P)

Discards the fragment if the fragment fails the depth peel test. P is the pixel point position in view space.

float HOUsampleDepth(vec2 pos, int sample)

Returns the depth from UV coordinate pos using the depth map attached to the uniform variable depthMap. If multisampled rendering is enabled, sample represents the sample index to fetch. This is generally not used by user GLSL programs, but can be used in a vertex shader.

vec3 HOUsampleNormal(vec2 pos, int sample)

Returns the normal from UV coordinate pos using the normal map attached to the uniform variable normalMap. If multisampled rendering is enabled, sample represents the sample index to fetch. This is generally not used by user GLSL programs, but can be used in a vertex shader.

Houdini auto-uniforms

Houdini automatically generates values for the following uniforms (all prefixed with glH_) which may be used by all shader stages.

glH_MaterialPass

An integer that stores the current pass being render. These passes are:

  • 0 - normal lighting pass

  • 1 - high quality lighting pass

  • 2 - high quality pass #2

  • 3 - shadow map pass

Lighting calculations should only be performed if glH_MaterialPass is 0. Basic material properties (diffuse, ambient, specular and emission) only need to be provided for pass #1, while Normals only need to be provided for pass #2. Pass #3 only requires alpha and depth.

glH_ViewMatrix

The view matrix (not to be confused with OpenGL’s modelview matrix).

glH_InvViewMatrix

The inverse of the view matrix (not to be confused with OpenGL’s modelview matrix).

glH_LightEnabled

An array that indicates which of OpenGL’s lights are enabled. glH_LightEnabled[i] is non-zero if (GL_LIGHT0 + i) is enabled.

glH_ScreenSize

A vec2 containing the current dimension of the viewport being rendered.

The follow uniforms are available, but generally only used by the HOUassign() family of built-in Houdini functions. If you do not use them (not recommended) you will need to multiply in the factors, apply the ghost color, handle the depth peeling and two-sided lighting for transparency.

glH_Emission

A float that is 1.0 if emission is used in this pass, 0.0 if not.

glH_Specular

A float that is 1.0 if specular is used in this pass, 0.0 if not.

glH_Diffuse

A float that is 1.0 if diffuse is used in this pass, 0.0 if not.

glH_Ambient

A float that is 1.0 if ambient is used in this pass, 0.0 if not.

glH_GhostColor

A vec4 representing the ghosting color for 'ghost other objects' display mode. It is set regardless of whether the current object is ghosted, though the color will be (0,0,0,0) if the object is not ghosted.

glH_DepthPeelEnable

An integer that is 1 when depth peeling is active, 0 otherwise.

glH_DepthPeelMap

A 2D texture sampler containing the depth map for depth peeling.

glH_AlphaPass

An integer that is 1 when a transparency pass is active, 0 otherwise. Transparency passes always light the polygon face that is facing the light. This is used by the HOUlightingModel(), HOUdiffuse() and HOUlightDiffSpec() lighting methods.

glH_LightMask

An integer that holds a bitmask of the lights that would affect the object being rendered. This is used by the HOUassign() methods in High Quality Lighting mode, and is not generally used by user shaders.

Coding guidelines

Houdini’s viewport renderer has a large number of display settings which users can change. In some cases the viewport renderer renders multiple passes in order to create a certain visual effect. For example, projective textures and shadows both require multiple passes.

To be compatible with Houdini’s various rendering modes, GLSL shaders should adhere to the following guidelines:

  • Use one of the HOUassign...() methods to assign the color output of a fragment shader.

  • Do GL lighting with HOUlightingModel(), HOUdiffuse() or HOUlightDiffSpec(). Otherwise, only add light contributions from enabled light sources. Use the glH_LightEnabled uniform to check which lights are enabled.

The multi-pass viewport render

When writing a GLSL shader in Houdini it is useful to understand the contexts in which the shader will be executed. The uniform glH_MaterialPass can be used to query the pass.

Normal Lighting Pass (0)

Ambient, diffuse, specular and emission components can all be calculated with full lighting. This may be done with the Houdini lighting functions or by the shader.

High Quality Lighting Pass (1)

The raw ambient, diffuse, specular and emissive components of the material should be defined but no lighting calculations should be applied. If a shader does not want to participate in HQ lighting, do the lighting computations and assign it to the emissive component.

Normal Pass (2)

Everything but the normal component is discarded.

Shadow Map Pass (3)

A shadow map is being created. Only the depth and alpha value are used, so all calculations for color can be avoided.

When shading, the glH_AlphaPass variable is set to 1 if a transparency pass is rendering. You may discard the fragment if your material uses transparency and glH_AlphaPass is 0, or if your material is opaque and glH_AlphaPass is 1.

Transparency

The OpenGL viewport renderer distinguishes between transparent and opaque materials. To properly render transparent objects, shaders must provide a hint to the renderer. Currently you do this by declaring a uniform of type float with the name "ogl_alpha".

Applying an OpenGL shader

Assign a GLSL shader for the viewport only

Set an object’s surface shader to an instance of your GLSL shader, or connect an instance of your GLSL shader to the surface color output inside a material.

Assign both a GLSL shader for the viewport and a shader for the renderer
  • You can define both GLSL shader code and VEX or RenderMan code in a GLSL shader type. Houdini will use the GLSL code in the viewport and the VEX/RenderMan code in the render.

    On the Code tab, click the gear menu and turn on VEX/RenderMan shader. This creates another tab where you can write VEX or RenderMan shader code.

  • You can also connect both an OpenGL shader and VEX surface shader to the surface color output of a material using a Select shader node.

Troubleshooting

My Geometry is Invisible
  • Make sure that your GLSL shader compiles.

    Right click the shader node and choose Type properties. Click the Code tab. Click Test compile to try compiling your shader code and check for errors.

  • Some GLSL shaders require vertex attributes to be passed to them (e.g. the tangent space normal mapping shader). Make sure you are passing it the correct data.

My Geometry is Not Receiving Shadows

Some shaders emit light and don’t reflect light (e.g. the decal shader). Such a shader will, by design, not receive shadows.