GLSLFX Shaders

GLSLFX files make the bridge between the application and the glsl shader files.
It allows to use any glsl shader without having to modify the code.

File Format

GLSLFX file format is a XML file. Comments are supported.

Header and Root node

The XML root node element is named glslfx.


<?xml version="1.0" encoding="UTF-8"?>

<glslfx version="1.0.0" author="allegorithmic.com">

    <!-- BODY -->

    <!-- ... -->

</glslfx>

Body

Technique

XML Element that describes a technique. A technique is a variation of the current FX. A GLSLFX can contain multiple techniques but at least one technique has to be defined.

The geometry will be rendered with one of the techniques defined by the application.

XML element definition

Name: technique

Attributes:

  • name: Any string used to name the technique

The XML element can have multiples children. The elements defined in a techniques override the elements defined globally.

For example, it is used to override some uniforms values and obtain FX variation for this technique.

Render Pass

XML Element that describes a render pass. A render pass describes the rendering of the geometry.

A technique can contain multiple render passes that will be executed sequentially. A technique containing no render pass is equivalent to a technique containing an ‘onscreen’ render pass.

The elements defined in a render pass override the elements defined in the parent technique.

XML element definition

Name: pass

Attributes:

  • output

  • offscreen: The rendering will be done into user defined render targets

  • onscreen: The rendering will be done into the default render target

Shaders

Set the GLSL shader files for each type.

XML Element Definition:

XML element definition

Name: shader

Attributes:

  • type: The GLSL shader type;

  • filename: The path of the glsl shader file. Can be absolute or relative to the GLSLFX file;

  • primitiveType: The method to render the primitive.

table 0-row-2 1-row-2 2-row-2 3-row-2 4-row-2 5-row-2
‘type’ Value Description
vertex Vertex shader
geometry Geometry shader
tess_control Tessellation Control shader
tess_eval Tessellation Evaluation shader
fragment Fragment shader
table 0-row-2 1-row-2 2-row-2 3-row-2
‘primitiveType’ Value Description
point Render as points
lineloop Render as line loop
patch[1…N] Render as patches with [1…N] vertices

Properties

Allow to set up some part of the OpenGL state.

XML element definition

Name: property

Attributes:

  • name: The name of the property to set. The name is based on the OpenGL function or glEnum name:

    • Enums syntax: Without the ‘GL_’ prefix, in lower case. Examples: glEnable(GL_BLEND_ENABLE) => “”“, glDisable(GL_CULL_FACE) => “””
    • Functions syntax: without the ‘gl’ prefix, in lower case and with all words separated with ‘_’ character. Example: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) => “”
  • Enums syntax: Without the ‘GL_’ prefix, in lower case. Examples: glEnable(GL_BLEND_ENABLE) => “”“, glDisable(GL_CULL_FACE) => “””

  • Functions syntax: without the ‘gl’ prefix, in lower case and with all words separated with ‘_’ character. Example: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) => “”

  • value: The value of the property.

table 0-row-3 1-row-3 2-row-3 3-row-3 4-row-3 5-row-3 6-row-3 7-row-3 8-row-3 9-row-3 10-row-3 11-row-3 12-row-3 13-row-3 14-row-3 15-row-3 16-row-3 17-row-3 18-row-3 19-row-3 20-row-3 21-row-3 22-row-3 23-row-3 24-row-3 25-row-3 26-row-3 27-row-3 28-row-3 29-row-3 30-row-3 31-row-3 32-row-3 33-row-3 34-row-3 35-row-3 36-row-3 37-row-3 38-row-3 39-row-3
‘name’ Values ‘value’ Values Description
blend_enabled boolean Enable/disable the blending mode
true
false
blend_func string, string Set the sources and destination blending functions
zero for OpenGL enum GL_ZERO
one for OpenGL enum GL_ONE
src_color for OpenGL enum GL_SRC_COLOR
one_minus_src_color for OpenGL enum GL_ONE_MINUS_SRC_COLOR
dst_color for OpenGL enum GL_DST_COLOR
one_minus_dst_color for OpenGL enum GL_ONE_MINUS_DST_COLOR
src_alpha for OpenGL enum GL_SRC_ALPHA
one_minus_src_alpha for OpenGL enum GL_ONE_MINUS_SRC_ALPHA
dst_alpha for OpenGL enum GL_DST_ALPHA
one_minus_dst_alpha for OpenGL enum GL_ONE_MINUS_DST_ALPHA
constant_color for OpenGL enum GL_CONSTANT_COLOR
one_minus_constant_color for OpenGL enum GL_ONE_MINUS_CONSTANT_COLOR
constant_alpha for OpenGL enum GL_CONSTANT_ALPHA
one_minus_constant_alpha for OpenGL enum GL_ONE_MINUS_CONSTANT_ALPHA
src_alpha_saturate for OpenGL enum GL_SRC_ALPHA_SATURATE
src1_color for OpenGL enum GL_SRC1_COLOR
one_minus_src1_color for OpenGL enum GL_ONE_MINUS_SRC1_COLOR
src1_alpha for OpenGL enum GL_SRC1_ALPHA
one_minus_src1_alpha for OpenGL enum GL_ONE_MINUS_SRC1_ALPHA
cull_face_enabled boolean Enable/disable the face culling
true
false
cull_face_mode string Set the face culling mode
front for OpenGL enum GL_FRONT
back for OpenGL enum GL_BACK
front_and_back for OpenGL enum GL_FRONT_AND_BACK
depth_func string Set the depth compare function
never for OpenGL enum GL_NEVER
less for OpenGL enum GL_LESS
lequal for OpenGL enum GL_LEQUAL
equal for OpenGL enum GL_EQUAL
notequal for OpenGL enum GL_NOTEQUAL
gequal for OpenGL enum GL_GEQUAL
greater for OpenGL enum GL_GREATER
always for OpenGL enum GL_ALWAYS

Uniforms

Allow to override some uniforms defined globally or in the parent technique. This allow to change shader behavior for this technique or render pass.

See the Uniforms section below for more details about their definition.

+++Example

+++

Render Targets

For ‘offscreen’ render passes, render targets must be defined in the render pass.

XML element definition

Name: output

Attributes:

  • attachment: The OpenGL attachment point, inspired by the OpenGL names:
    GL_COLOR_ATTACHMENT[0…3] => ‘color[0…3]’
    GL_DEPTH_ATTACHMENT => ‘depth’

attachment: The OpenGL attachment point, inspired by the OpenGL names:
GL_COLOR_ATTACHMENT[0…3] => ‘color[0…3]’
GL_DEPTH_ATTACHMENT => ‘depth’

  • name: the name of the render target.
    It can be used in a later render pass to bind this render target as a sampler.

name: the name of the render target.
It can be used in a later render pass to bind this render target as a sampler.

  • format: the internal format of the render target.

format: the internal format of the render target.

  • clear: optional attribute that defines a clear value.
    If present, the render target will be cleared to this value at the beginning of the render pass.
    If missing, the render target will keep its previous content.
NOTE
Color render targets are forbidden in an ‘onscreen’ render pass, but a depth render target can be shared with any render pass (but it’s likely to break rendering when mixing multiple materials in the scene).

About formats

For depth formats, all depth-only (no stencil) OpenGL formats are supported:

  • GL_DEPTH_COMPONENT16 => ‘depth26’
  • GL_DEPTH_COMPONENT24 => ‘depth34’
  • GL_DEPTH_COMPONENT32 => ‘depth42’
  • GL_DEPTH_COMPONENT32F => ‘depth42f’

For color formats, the name is based on OpenGL enum names, without the ‘GL_’ prefix, in lower case.
Three channel formats (RGB) are not supported, use an RGBA format instead.
Bit depth per channel supported:

  • Normalized unsigned integer: 8, 16
  • Floating point: 16, 32

An exception to these rules is the GL_R11F_G11F_B10F format which is supported.:

  • GL_RGBA8 => ‘rgba8’
  • GL_RGBA16F => ‘rgba16f’
  • GL_SRGB8_ALPHA8 => ‘srgb8_alpha8’
  • GL_R11F_G11F_B10F => ‘r11f_g11f_b10f’
  • GL_RG16 => “rg16”

Samplers

Allow to override some samplers defined globally, they cannot be defined in a technique. This allow to define a sampler usage for this render pass, or to read from a render target of a previous render pass.

See the Samplers section for more details about their definition.

+++Example

+++

Input Vertex Format

This allow to define the semantic of each attributes define in the vertex shader.

XML Element Definition:

Name: ‘vertexformat’

Attributes:

  • ‘name’: The name of the attribute as defined in the vertex shader.
  • ‘semantic’: The semantic of the attribute.
‘semantic’ Value
Description
position
Vertex position (float3)
normal
Vertex normal (float3)
texcoord[0…N]
Vertex texture coordinate buffer N (float2)
tangent[0…N]
Vertex tangent buffer N (float4)
binormal[0…N]
Vertex binormal buffer N (float4)

Example:


<?xml version="1.0" encoding="UTF-8"?>

<glslfx version="1.0.0" author="allegorithmic.com">

     <!-- BODY -->

     <!-- ... -->



     <!-- INPUT VERTEX FORMAT -->

     <vertexformat name="iVS_Position" semantic="position"/>

     <vertexformat name="iVS_Normal" semantic="normal"/>

     <vertexformat name="iVS_UV" semantic="texcoord0"/>

     <vertexformat name="iVS_Tangent" semantic="tangent0"/>

     <vertexformat name="iVS_Binormal" semantic="binormal0"/>

</glslfx>

Samplers

This allow to define the usage of each samplers.
It is used by the application to known which texture to set in the specified samplers.

XML Element Definition:

Name: ‘sampler’

Attributes:

  • ‘name’: The name of the sampler variable in the shader file.
  • ‘usage’: The usage of the sampler. It matches the usage specified in the Ouput node of the graph.
‘usage’ Value
Description
diffuse
Diffuse map
opacity
Opacity map
emissive
Emissive map
ambientocclusion
Ambient occlusion map
ambient
Ambient map
mask
Mask map
detailnormal
Detail Normal map
normal
Normal map
bump
Bump map
height
Height map
displacement
Displacement map
specularlevel
Specular level map
specularcolor
Specular color map
specular
Specular map
glossiness
Glossiness map
roughness
Roughness map
anisotropylevel
Anisothropy level map
anisotropyangle
Anisothropy angle map
transmissive
Transmissive map
reflection
Reflection map
refraction
Refraction map
environment
Environment map (cube map)
panorama
The Panorama Map (Latitude/Longitude Map)
bluenoisemask
A 256x256 dithering texture
  • Multiple usages are supported.
    • Example:

   <!-- SAMPLERS -->

    <sampler name="baseColorMap" usage="basecolor,diffuse"/>

     <!-- ... -->

‘isHidden’: Boolean that indicates if the sampler should appear in the GUI

  • Example:

     <!-- SAMPLERS -->

    <sampler name="bluenoiseMask" usage="bluenoisemask" ishidden="true"/>

     <!-- ... -->

Wrapping Mode:

Name
Value
texture_wrap_s, texture_wrap_t, texture_wrap_r
clamp_to_edge
clamp_to_border
mirrored_repeat
repeat

Texture Filter

Name
Value
texture_min_filter, texture_mag_filter
nearest
linear
nearest_mipmap_nearest
linear_mipmap_nearest
nearest_mipmap_linear
linear_mipmap_linear

Example:


<?xml version="1.0" encoding="UTF-8"?>

<glslfx version="1.0.0" author="allegorithmic.com">

     <!-- BODY -->

     <!-- ... -->



     <!-- SAMPLERS -->

     <sampler name="baseColorMap" usage="basecolor,diffuse"/>

     <sampler name="heightMap" usage="height"/>

     <sampler name="normalMap" usage="normal"/>

     <sampler name="detailNormalMap" usage="detailNormal"/>

     <sampler name="environmentMap" usage="environment"/>

     <sampler name="bluenoiseMask" usage="bluenoisemask" ishidden="true"/>

     <sampler name="sssDiffuseMap" usage="sssDiffuse"/>

</glslfx>

Uniforms

This allows you to add additional information on each shader uniforms.

XML Element Definition:

Name: ‘uniform’

Attributes:

‘name’: The name of the uniform in the shader file.

‘semantic’ Value
Description
world
World Matrix (float16)
worldinversetranspose
World Inverse Transpose Matrix (float16)
worldviewprojection
World View Projection Matrix (float16)
viewinverse
World Inverse Matrix (float16)
worldview
World View Matrix (float16)
modelview
Model View Matrix (float16)
projection
Projection Matrix (float16)
ambient
Scene Ambient Color (float3)
lightposition[0…N]
Position of the scene’s Nth light (float3)
lightcolor[0…N]
Color of the scene’s Nth light (float3)
lightintensity[0…N]
Intensity of the scene’s Nth light (float)
globaltime
Current time in secs (float)
resolution
Viewport resolution (int2)
mouse
Mouse position (int2)
samplespostablesize
Number of samples to use to compute the environment lighting (int)
irradianceshcoefs
The array of spherical harmonics vectors (float3[10])
panoramamipmapheight
Number of mipmap levels in the panorama map (float)
panoramarotation
Angle Rotation angle of the panorama map (float)
panoramaintensity
Intensity of the panorama map (float)
computebinormalinfragmentshader
Does the binormal be computedt per fragment ? (if not then per vertex) (bool)
isdirectxnormal
Is the normal map format DirectX ? (bool)
uvwscale
Scale values of u, v, w (float3)
renderuvtile
Render only 1 UV tile ? (bool)
uvtilecoords
UV tile coordinate to render (int2)

‘semantic’: The semantic of the uniform. (All matrices are float16).

Example:


<?xml version="1.0" encoding="UTF-8"?>

<glslfx version="1.0.0" author="allegorithmic.com">

     <!-- BODY -->

     <!-- ... -->



     <!-- MATRICES -->

     <uniform name="worldMatrix" semantic="world"/>

     <uniform name="worldViewProjMatrix" semantic="worldviewprojection"/>

     <uniform name="worldViewMatrix" semantic="worldview"/>

     <uniform name="worldInverseTransposeMatrix" semantic="worldinversetranspose"/>

     <uniform name="viewInverseMatrix" semantic="viewinverse"/>

     <uniform name="modelViewMatrix" semantic="modelview"/>

     <uniform name="projectionMatrix" semantic="projection"/>

</glslfx>

Example:


<?xml version="1.0" encoding="UTF-8"?>

<glslfx version="1.0.0" author="allegorithmic.com">

    <!-- BODY -->

    <!-- ... -->



    <!-- SCENE PARAMETERS -->

    <uniform name="AmbiColor" semantic="ambient"/>

    <uniform name="Lamp0Pos" semantic="lightposition0"/>

    <uniform name="Lamp0Color" semantic="lightcolor0"/>

    <uniform name="Lamp1Pos" semantic="lightposition1"/>

    <uniform name="Lamp1Color" semantic="lightcolor1"/>

</glslfx>

Other parameters

Other additional information can be add to each uniforms to:

  • define the default value
  • clamp values
  • control the way the uniform will be display in the application:
  • set the label
  • set the widget info used to edit the value in the application:
  • widget name, min, max, increment/decrement step
  • group uniforms in group widgets

As the Uniforms can be override for each techniques, it allows to display a specific gui setup for each techniques.

XML Element Definition:

Name: ‘uniform’

Attributes:

  • ‘name’: The name of the uniform in the shader file.
  • ‘default’: The uniform default value
  • ‘min’: The min value of the validity range
  • ‘max’: The max value of the validity range
  • ‘guiName’: The name of the uniform in the GUI of the application
  • ‘guiGroup’: The name of the group to put the uniform in the GUI of the application
  • ‘guiWidget’: The name of the widget used to edit the uniform value in the GUI of application
‘guiWidget’ Value
Description
slider
Slider widget for floatN
angle
Angle widget for float
color
Color widget for float3, float4 color
checkbox
CheckBox widget for bool
  • ‘guiMin’: The min value of the widget
  • ‘guiMax’: The max value of the widget

Example: Tessellation/Parallax

Parallax Vertex Shader File

Located in .\tessellation_parallax\parallax\vs.glsl

Content:

> #version 120

attribute vec4 iVS\_Position;
attribute vec4 iVS\_Normal;
attribute vec2 iVS\_UV;
attribute vec4 iVS\_Tangent;
attribute vec4 iVS\_Binormal;

varying vec3 iFS\_Normal;
varying vec2 iFS\_UV;
varying vec3 iFS\_Tangent;
varying vec3 iFS\_Binormal;
varying vec3 iFS\_PointWS;

uniform mat4 worldMatrix;
uniform mat4 worldViewProjMatrix;

void main()
{
gl\_Position = worldViewProjMatrix \* iVS\_Position;
iFS\_Normal = iVS\_Normal.xyz;
iFS\_UV = iVS\_UV;
iFS\_Tangent = iVS\_Tangent.xyz;
iFS\_Binormal = iVS\_Binormal.xyz;
iFS\_PointWS = (worldMatrix \* iVS\_Position).xyz;
}

Tessellation Vertex Shader File

Located in .\tessellation_parallax\tessellation\vs.glsl

Content:

>

#version 120

attribute vec4 iVS\_Position;
attribute vec4 iVS\_Normal;
attribute vec2 iVS\_UV;
attribute vec4 iVS\_Tangent;
attribute vec4 iVS\_Binormal;

varying vec4 oVS\_Normal;
varying vec2 oVS\_UV;
varying vec4 oVS\_Tangent;
varying vec4 oVS\_Binormal;

void main()
{
gl\_Position = iVS\_Position;
oVS\_Normal = iVS\_Normal;
oVS\_UV = iVS\_UV;
oVS\_Tangent = iVS\_Tangent;
oVS\_Binormal = iVS\_Binormal;
}

Tessellation Control Shader File

Located in .\tessellation_parallax\tessellation\tcs.glsl

Content:

>

#version 400 core
#extension GL\_ARB\_tessellation\_shader : enable

layout(vertices = 3) out;

in vec4 oVS\_Normal[];
in vec2 oVS\_UV[];
in vec4 oVS\_Tangent[];
in vec4 oVS\_Binormal[];

out vec4 oTCS\_Normal[];
out vec2 oTCS\_UV[];
out vec4 oTCS\_Tangent[];
out vec4 oTCS\_Binormal[];

uniform float tessellationFactor;

void main()
{
gl\_TessLevelOuter[0] = tessellationFactor;
gl\_TessLevelOuter[1] = tessellationFactor;
gl\_TessLevelOuter[2] = tessellationFactor;
gl\_TessLevelInner[0] = tessellationFactor;
gl\_out[gl\_InvocationID].gl\_Position = gl\_in[gl\_InvocationID].gl\_Position;

oTCS\_Normal[gl\_InvocationID] = oVS\_Normal[gl\_InvocationID];
oTCS\_UV[gl\_InvocationID] = oVS\_UV[gl\_InvocationID];
oTCS\_Tangent[gl\_InvocationID] = oVS\_Tangent[gl\_InvocationID];
oTCS\_Binormal[gl\_InvocationID] = oVS\_Binormal[gl\_InvocationID];
}

Tessellation Evaluation Shader File

Located in .\tessellation_parallax\tessellation\tcs.glsl

Content:

>

#version 400 core

layout(triangles, equal\_spacing, ccw) in;

in vec4 oTCS\_Normal[];
in vec2 oTCS\_UV[];
in vec4 oTCS\_Tangent[];
in vec4 oTCS\_Binormal[];

uniform mat4 worldMatrix;
uniform mat4 worldViewProjMatrix;

uniform sampler2D heightMap;

uniform float tiling = 1.0f;
uniform float heightMapScale = 1.0f;

out vec3 iFS\_Normal;
out vec2 iFS\_UV;
out vec3 iFS\_Tangent;
out vec3 iFS\_Binormal;
out vec3 iFS\_PointWS;

vec3 interpolate3D(vec3 v0, vec3 v1, vec3 v2, vec3 uvw)
{
return uvw.x \* v0 + uvw.y \* v1 + uvw.z \* v2;
}

vec2 interpolate2D(vec2 v0, vec2 v1, vec2 v2, vec3 uvw)
{
return uvw.x \* v0 + uvw.y \* v1 + uvw.z \* v2;
}

void main()
{
vec3 uvw = gl\_TessCoord.xyz;

vec3 newPos = interpolate3D(gl\_in[0].gl\_Position.xyz, gl\_in[1].gl\_Position.xyz, gl\_in[2].gl\_Position.xyz, uvw);
vec3 newNormal = normalize(interpolate3D(oTCS\_Normal[0].xyz, oTCS\_Normal[1].xyz, oTCS\_Normal[2].xyz, uvw));
vec3 newTangent = normalize(interpolate3D(oTCS\_Tangent[0].xyz, oTCS\_Tangent[1].xyz, oTCS\_Tangent[2].xyz, uvw));
vec3 newBinormal = normalize(interpolate3D(oTCS\_Binormal[0].xyz, oTCS\_Binormal[1].xyz, oTCS\_Binormal[2].xyz, uvw));
vec2 newUV = interpolate2D(oTCS\_UV[0], oTCS\_UV[1], oTCS\_UV[2], uvw);

float heightTexSample = texture(heightMap, newUV \* tiling).x \* 2.0 - 1.0;
newPos += newNormal \* heightTexSample \* heightMapScale;

vec4 obj\_pos = vec4(newPos, 1);
gl\_Position = worldViewProjMatrix \* obj\_pos;

iFS\_UV = newUV \* tiling;
iFS\_Tangent = newTangent;
iFS\_Binormal = newBinormal;
iFS\_Normal = newNormal;
iFS\_PointWS = (worldMatrix \* obj\_pos).xyz;
}

Fragment Shader File

Located in .\tessellation_parallax\fs.glsl

Content:

>

#version 120

// #define ALG\_NORMAL\_DIRECTX
#define ALG\_NORMAL\_OPENGL

#ifdef ALG\_NORMAL\_DIRECTX
// #define FLIP\_NORMAL\_X
#define FLIP\_NORMAL\_Y
// #define FLIP\_NORMAL\_Z
#endif //#ifdef ALG\_NORMAL\_DIRECTX

#ifdef ALG\_NORMAL\_OPENGL
// #define FLIP\_NORMAL\_X
#define FLIP\_NORMAL\_Y
// #define FLIP\_NORMAL\_Z
#endif //#ifdef ALG\_NORMAL\_OPENGL

varying vec3 iFS\_Normal;
varying vec2 iFS\_UV;
varying vec3 iFS\_Tangent;
varying vec3 iFS\_Binormal;
varying vec3 iFS\_PointWS;

uniform vec3 Lamp0Pos = vec3(0.0f,0.0f,70.0f);
uniform vec3 Lamp0Color = vec3(1.0f,1.0f,1.0f);
uniform vec3 Lamp1Pos = vec3(70.0f,0.0f,0.0f);
uniform vec3 Lamp1Color = vec3(0.198f,0.198f,0.198f);
uniform bool flipNormal = true;
uniform float TilingDetail = 3.0f;
uniform float SpecExpon = 50.0;
uniform float Ks = 1.0;
uniform int parallax\_mode = 0;
uniform float tessellationFactor = 4.0;
uniform float heightMapScale = 1.0f;
uniform float Depth\_detail = 0.5f;
uniform float Kr = 0.5f;
uniform int KF\_on = 1;
uniform float KFs = 1.0f;
uniform vec3 AmbiColor = vec3(0.07f,0.07f,0.07f);
uniform float tiling = 1.0f;
uniform int enableTilingInFS = 0;

uniform sampler2D heightMap;
uniform sampler2D normalMap;
uniform sampler2D detailNormalMap;
uniform sampler2D emissiveMap;
uniform sampler2D diffuseMap;
uniform sampler2D specularMap;
uniform sampler2D opacityMap;
uniform samplerCube environmentMap;

uniform mat4 worldMatrix;
uniform mat4 worldInverseTransposeMatrix;
uniform mat4 viewInverseMatrix;

vec4 litFct(float NdotL, float NdotH, float specExp)
{
float ambient = 1.0;
float diffuse = max(NdotL, 0.0);
float specular = step(0.0, NdotL) \* pow(max(0.0, NdotH), specExp);
return vec4(ambient, diffuse, specular, 1.0);
}

vec3 lerpFct(vec3 v0, vec3 v1, float percent)
{
return v0 + (v1-v0) \* percent;
}

// Phong Shading
void phong\_shading(
in vec3 LightColor,
in vec3 normalWS,
in vec3 pointToLightDirWS,
in vec3 pointToCameraDirWS,
inout vec3 DiffuseContrib,
inout vec3 SpecularContrib)
{
vec3 Hn = normalize(pointToCameraDirWS + pointToLightDirWS);
vec4 litV = litFct(dot(normalWS, pointToLightDirWS), dot(normalWS, Hn), SpecExpon);
DiffuseContrib = litV.y \* LightColor;
SpecularContrib = litV.y \* litV.z \* Ks \* LightColor;
}

vec3 fixNormalSample(vec3 v)
{
vec3 result = v - vec3(0.5,0.5,0.5);

#ifdef FLIP\_NORMAL\_X
result.x = -result.x;
#endif // ifdef FLIP\_NORMAL\_X
#ifdef FLIP\_NORMAL\_Y
result.y = -result.y;
#endif // ifdef FLIP\_NORMAL\_Y
#ifdef FLIP\_NORMAL\_Z
result.z = -result.z;
#endif // ifdef FLIP\_NORMAL\_Z

return result;
}

vec3 normalVecOSToWS(vec3 normal)
{
return normal;
}

void main()
{
vec3 cameraPosWS = viewInverseMatrix[3].xyz;
vec3 pointToLight0DirWS = normalize(Lamp0Pos - iFS\_PointWS);
vec3 pointToLight1DirWS = normalize(Lamp1Pos - iFS\_PointWS);
vec3 pointToCameraDirWS = normalize(cameraPosWS);
vec3 normalOS = normalize(iFS\_Normal);
vec3 tangentOS = normalize(iFS\_Tangent);
vec3 binormalOS = normalize(iFS\_Binormal);

// ------------------------------------------
// Make sure the TBN is Orthonormalized
binormalOS = normalize(cross(normalOS, tangentOS));
tangentOS = normalize(cross(binormalOS, normalOS));

vec3 cumulatedNormalOS = normalOS;

// ------------------------------------------
// Update UV
float a = dot(normalOS,-pointToCameraDirWS);
vec3 s = vec3(dot(pointToCameraDirWS,tangentOS), dot(pointToCameraDirWS,binormalOS), a);
vec2 uv = enableTilingInFS == 0 ? iFS\_UV : (iFS\_UV \* tiling);
float height = texture2D(heightMap,uv).x \* 2.0 - 1.0 ;
float parallax = parallax\_mode == 0 ? (tessellationFactor / 100000.f + heightMapScale / 500.f) : (heightMapScale / 50.f);
uv += (height \* s.xy \* parallax) ;

// ------------------------------------------
// Add Normal from normalMap
vec3 normalTS = texture2D(normalMap,uv).xyz;
normalTS = fixNormalSample(normalTS);
vec3 normalMapOS = normalTS.x\*tangentOS + normalTS.y\*binormalOS;
cumulatedNormalOS = cumulatedNormalOS + normalMapOS;
cumulatedNormalOS = normalize(cumulatedNormalOS);

// ------------------------------------------
// Add detail normalmap
vec3 normalDetailTS = texture2D(detailNormalMap,uv\*TilingDetail).xyz;
normalDetailTS = fixNormalSample(normalDetailTS);
vec3 variableNormalDetailTS = lerpFct(vec3(0.0,0.0,0.5),normalDetailTS,Depth\_detail);
vec3 normalDetailOS = variableNormalDetailTS.x\*tangentOS + variableNormalDetailTS.y\*binormalOS;
cumulatedNormalOS = cumulatedNormalOS + normalDetailOS;
cumulatedNormalOS = normalize(cumulatedNormalOS);

if (length(normalTS)<0.0001)
cumulatedNormalOS = normalOS;

vec3 cumulatedNormalWS = normalVecOSToWS(cumulatedNormalOS);

// ------------------------------------------
// Compute Diffuse & Specular

// Light 0 contribution
vec3 diffContrib = vec3(0, 0, 0);
vec3 specContrib = vec3(0, 0, 0);
phong\_shading(Lamp0Color, cumulatedNormalWS, pointToLight0DirWS, pointToCameraDirWS, diffContrib, specContrib);

// Light 1 contribution
vec3 diffContrib2 = vec3(0, 0, 0);
vec3 specContrib2 = vec3(0, 0, 0);
phong\_shading(Lamp1Color, cumulatedNormalWS, pointToLight1DirWS, pointToCameraDirWS, diffContrib2, specContrib2);

diffContrib += diffContrib2;
specContrib += specContrib2;

vec4 diffuseColor = texture2D(diffuseMap,uv);

vec3 specularColor = texture2D(specularMap,uv).rgb;
vec3 R = reflect(pointToCameraDirWS,cumulatedNormalWS);
vec3 reflColor = Kr \* textureCube(environmentMap,R.xyz).bgr;

float FallofRefl;

if (KFs >= 0.0)
FallofRefl = max((1-dot(pointToCameraDirWS/(KFs),cumulatedNormalWS)),0)\*KF\_on;
else
FallofRefl = (1-max(((1-dot(pointToCameraDirWS/(-KFs),cumulatedNormalWS))),0))\*KF\_on;

if (KF\_on == 0)
FallofRefl=1.0;

vec3 Ambiant\_final = diffuseColor.rgb\*AmbiColor;

// ------------------------------------------
vec3 emissive = texture2D(emissiveMap,uv).xyz;

vec3 finalcolor = Ambiant\_final
+ specularColor\*specContrib
+ diffuseColor.rgb\*diffContrib
+ (reflColor\*specularColor\*FallofRefl)
+ emissive;

// Final Color
vec4 finalColor4 = vec4(finalcolor, texture2D(opacityMap,uv));

gl\_FragColor = finalColor4;
}

GLSLFX File

The glslfx file defines two techniques to render the geometry:

  • One uses the hardware tessellation technique
  • The other based on a parallax effect that will be used as a fall-back if the user hardware don’t support the Tessellation.

Located in .\tessellation_parallax\fs.glsl

Content:


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sbsbatchnode SYSTEM "glslfx.dtd">

<glslfx version="1.0.0" author="allegorithmic.com">



    <!-- TECHNIQUES -->

    <technique name="Tesselation">

        <!-- PROPERTIES -->

        <property name="blend_enabled" value="true"/>

        <property name="blend_func" value="src_alpha,one_minus_src_alpha"/>

        <property name="cull_face_enabled" value="true"/>

        <property name="cull_face_mode" value="back"/>



        <!-- SHADERS -->

        <shader type="vertex" filename="tessellation_parallax/tessellation/vs.glsl" primitiveType="patch4"/>

        <shader type="tess_control" filename="tessellation_parallax/tessellation/tcs.glsl"/>

        <shader type="tess_eval" filename="tessellation_parallax/tessellation/tes.glsl"/>

        <shader type="fragment" filename="tessellation_parallax/fs.glsl"/>



        <!-- UNIFORMS -->

        <uniform name="parallax_mode" guiName="Parallax Mode" min="0" max="0" />

        <uniform name="enableTilingInFS" guiName="Tiling Enabled In FS" min="0" max="0" />

        <uniform name="tessellationFactor" guiName="Tessellation Factor" default="4" min="1" max="64" guiStep="1" guiWidget="slider"/>

    </technique>



    <technique name="Parallax">

        <!-- PROPERTIES -->

        <property name="blend_enabled" value="true"/>

        <property name="blend_func" value="src_alpha,one_minus_src_alpha"/>

        <property name="cull_face_enabled" value="true"/>

        <property name="cull_face_mode" value="back"/>



        <!-- SHADERS -->

        <shader type="vertex" filename="tessellation_parallax/parallax/vs.glsl"/>

        <shader type="fragment" filename="tessellation_parallax/fs.glsl"/>



        <!-- UNIFORMS -->

        <uniform name="parallax_mode" guiName="Parallax Mode" min="1" max="1" />

        <uniform name="enableTilingInFS" guiName="Tiling Enabled In FS" min="1" max="1" />



    </technique>



    <!-- INPUT VERTEX FORMAT -->

    <vertexformat name="iVS_Position" semantic="position"/>

    <vertexformat name="iVS_Normal" semantic="normal"/>

    <vertexformat name="iVS_UV" semantic="texcoord0"/>

    <vertexformat name="iVS_Tangent" semantic="tangent0"/>

    <vertexformat name="iVS_Binormal" semantic="binormal0"/>



    <!-- SAMPLERS -->

    <sampler name="diffuseMap" usage="diffuse"/>

    <sampler name="heightMap" usage="height"/>

    <sampler name="normalMap" usage="normal"/>

    <sampler name="detailNormalMap" usage="detailNormal"/>

    <sampler name="emissiveMap" usage="emissive"/>

    <sampler name="specularMap" usage="specular"/>

    <sampler name="opacityMap" usage="opacity"/>

    <sampler name="environmentMap" usage="environment"/>



    <!-- MATRICES -->

    <uniform name="worldMatrix" semantic="world"/>

    <uniform name="worldViewProjMatrix" semantic="worldviewprojection"/>

    <uniform name="worldViewMatrix" semantic="worldview"/>

    <uniform name="worldInverseTransposeMatrix" semantic="worldinversetranspose"/>

    <uniform name="viewInverseMatrix" semantic="viewinverse"/>

    <uniform name="modelViewMatrix" semantic="modelview"/>

    <uniform name="projectionMatrix" semantic="projection"/>



    <!-- SCENE PARAMETERS -->

    <uniform name="AmbiColor" semantic="ambient"/>

    <uniform name="Lamp0Pos" semantic="lightposition0"/>

    <uniform name="Lamp0Color" semantic="lightcolor0"/>

    <uniform name="Lamp1Pos" semantic="lightposition1"/>

    <uniform name="Lamp1Color" semantic="lightcolor1"/>



    <!-- UNIFORMS -->

    <uniform name="tiling" guiName="Tiling" default="1" min="1" guiWidget="slider" guiMax="10"/>

    <uniform name="heightMapScale" guiGroup="Height" guiName="Scale" default="1" min="0" guiWidget="slider" guiMin="-50" guiMax="50" />

    <uniform name="TilingDetail" guiGroup="Detail Normal" guiName="Tiling" default="3" min="1" guiWidget="slider" guiMax="10"/>

    <uniform name="Depth_detail" guiGroup="Detail Normal" guiName="Intensity" default="0.5" min="0" max="1" guiStep="0.05" guiWidget="slider"/>

    <uniform name="SpecExpon" guiGroup="Specular" guiName="Power" default="50" min="1" guiWidget="slider" guiMax="128"/>

    <uniform name="Ks" guiGroup="Specular" guiName="Intensity" default="1" min="0" guiWidget="slider" guiMax="3"/>

    <uniform name="Kr" guiGroup="Reflection" guiName="Intensity" default="0.5" min="0" max="1" guiStep="0.01" guiWidget="slider"/>

    <uniform name="KF_on" guiGroup="Reflection" guiName="Falloff" default="1" min="0" max="1" guiStep="1" guiWidget="slider"/>

    <uniform name="KFs" guiGroup="Reflection" guiName="Falloff Size" default="1" min="-1" max="1" guiStep="0.05" guiWidget="slider"/>



</glslfx>
recommendation-more-help
dd48abc6-e399-43d2-9777-578ec70c5a23