Shading
OpenGL Shaders
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.
-
Choose File > New operator type.
-
Click on SHOP Type.
-
In the Network Type menu, choose GLSL Shader.
-
Enter an internal Name for the shader and a human-readable Label.
-
Click Accept. The type properties window for the new shader type appears.
-
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.
-
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 auto-uniforms
Houdini automatically generates values for the following uniforms.
|
|
The inverse of the view matrix (not to be confused with OpenGL’s |
|
|
An array that indicates which of OpenGL’s lights are enabled. |
|
|
The view matrix (not to be confused with OpenGL’s |
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:
-
Set
gl_Positionto the result offtransform()in all vertex shaders, to be compatible with shadow mapping. -
Modulate material emission values by the
glH_Emissionuniform. -
Modulate material ambient values by the
gl_LightModel.ambientuniform. -
Only add light contributions from enabled light sources. Use the
glH_LightEnableduniform 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. Houdini’s OpenGL viewport renderer, used for projective textures and shadows, does five main passes: an emission pass; a global ambient pass; a pass for point, spot, and distant lights; a shadow map pass; and a pass for unlit geometry. GLSL shaders may be executed in all but the last two passes.
Houdini supports light color components that are greater than 1.0; however, OpenGL clamps diffuse, specular, and ambient light values to the range . To overcome this, the multi-pass renderer normalizes these values and then compensates for this normalization at a later stage.
|
Emission Pass |
During the emission pass, the |
|
Global Ambient Pass |
During the global ambient pass, the |
|
Individual Light Pass |
During an individual light pass for a point, spot, or distant light, the |
|
Shadow Map Pass |
GLSL shaders do not execute during the creation of shadow maps. You must set |
|
Unlit Pass |
This pass is not relevant to GLSL shaders. |
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". The multi-pass renderer does not support transparency at this time.
Applying an OpenGL shader
| To... | Do this |
|---|---|
|
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 |
|
Troubleshooting
|
My Geometry is Invisible |
|
|
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. |