YarUnderoaker

GLSL tessellation shader does not want link

Discussion created by YarUnderoaker on Mar 16, 2011
Latest reply on Mar 21, 2011 by YarUnderoaker

Vertex, control, evaluation and fragment shader objects compiled succussfuly, but program does not want link, it log containe only four "unexpected error."

 Catalist 11.2 WinXP SP3 Radeon HD5450

// Vertex shader

#version 400 compatibility

#define gl_Tangent gl_MultiTexCoord1.xyz
#define gl_Binormal gl_MultiTexCoord2.xyz

out vec3 tcWorldPos;
out vec3 tcNormal;
out float tcVertexDistanceFactor;
out vec2 tcTexCoord;
out vec3 tcLightTS;
out vec3 tcViewTS;

uniform MainConstants {
mat4 WorldMatrix; // World matrix
mat4 ViewMatrix; // View matrix
mat4 ProjectionMatrix; // Projection matrix
mat4 WorldViewProjectionMatrix; // WVP matrix
mat4 ViewProjectionMatrix; // VP matrix
mat4 InvViewMatrix; // Inverse of view matrix
vec4 ScreenResolution; // Screen resolution

vec4 MeshColor; // Mesh color
vec4 TessellationFactor; // Edge, inside, minimum tessellation factor and 1/desired triangle size
vec4 DetailTessellationHeightScale; // Height scale for detail tessellation of grid surface
vec4 GridSize; // Grid size
};

uniform Material {
vec4 MaterialAmbientColor; // Material's ambient color
vec4 MaterialDiffuseColor; // Material's diffuse color
vec4 MaterialSpecularColor; // Material's specular color
vec4 SpecularExponent; // Material's specular exponent

vec4 LightPosition; // Light's position in world space
vec4 LightDiffuse; // Light's diffuse color
vec4 LightAmbient; // Light's ambient color

vec4 Eye; // Camera's location
vec4 BaseTextureRepeat; // The tiling factor for base and normal map textures
vec4 POMHeightMapScale; // Describes the useful range of values for the height field

vec4 ShadowSoftening; // Blurring factor for the soft shadows computation

int MinSamples; // The minimum number of samples for sampling the height field profile
int MaxSamples; // The maximum number of samples for sampling the height field profile
};

void main()
{
// Compute position in world space
vec4 vPositionWS = WorldMatrix * gl_Vertex;

// Compute denormalized light vector in world space
vec3 vLightWS = LightPosition.xyz - vPositionWS.xyz;
// Need to invert Z for correct lighting
vLightWS.z = -vLightWS.z;

// Propagate texture coordinate through:
tcTexCoord = gl_MultiTexCoord0.xy * BaseTextureRepeat.x;

// Transform normal, tangent and binormal vectors from object
// space to homogeneous projection space and normalize them
vec3 vNormalWS = mat3(WorldMatrix) * gl_Normal;
vec3 vTangentWS = mat3(WorldMatrix) * gl_Tangent;
vec3 vBinormalWS = mat3(WorldMatrix) * gl_Binormal;

// Normalize tangent space vectors
vNormalWS = normalize( vNormalWS );
vTangentWS = normalize( vTangentWS );
vBinormalWS = normalize( vBinormalWS );

// Output normal
tcNormal = vNormalWS;

// Calculate tangent basis
mat3 WorldToTangent = mat3( vTangentWS, vBinormalWS, vNormalWS );

// Propagate the light vector (in tangent space)
tcLightTS = vLightWS * WorldToTangent;

// Compute and output the world view vector (unnormalized)
vec3 vViewWS = Eye.xyz - vPositionWS.xyz;
tcViewTS = vViewWS * WorldToTangent;

// Write world position
tcWorldPos = vec3( vPositionWS.xyz );

// Min and max distance should be chosen according to scene quality requirements
const float MinDistance = 20.0;
const float MaxDistance = 250.0;

// Calculate distance between vertex and camera, and a vertex distance factor issued from it
float Distance = distance( vPositionWS.xyz, Eye.xyz );
tcVertexDistanceFactor = 1.0 - clamp( ( ( Distance - MinDistance ) / ( MaxDistance - MinDistance ) ),
0.0, 1.0 - TessellationFactor.z/TessellationFactor.x);
}

// Control shader

#version 400
layout(vertices = 3) out;

in vec3 tcWorldPos[];
in vec3 tcNormal[];
in float tcVertexDistanceFactor[];
in vec2 tcTexCoord[];
in vec3 tcLightTS[];
in vec3 tcViewTS[];

out vec3 teWorldPos[];
out vec3 teNormal[];
out float teVertexDistanceFactor[];
out vec2 teTexCoord[];
out vec3 teLightTS[];
out vec3 teViewTS[];

uniform MainConstants {
mat4 WorldMatrix; // World matrix
mat4 ViewMatrix; // View matrix
mat4 ProjectionMatrix; // Projection matrix
mat4 WorldViewProjectionMatrix; // WVP matrix
mat4 ViewProjectionMatrix; // VP matrix
mat4 InvViewMatrix; // Inverse of view matrix
vec4 ScreenResolution; // Screen resolution

vec4 MeshColor; // Mesh color
vec4 TessellationFactor; // Edge, inside, minimum tessellation factor and 1/desired triangle size
vec4 DetailTessellationHeightScale; // Height scale for detail tessellation of grid surface
vec4 GridSize; // Grid size
};

void main()
{
// Copy inputs to outputs
teWorldPos[gl_InvocationID] = tcWorldPos[gl_InvocationID];
teNormal[gl_InvocationID] = tcNormal[gl_InvocationID];
teVertexDistanceFactor[gl_InvocationID] = tcVertexDistanceFactor[gl_InvocationID];
teTexCoord[gl_InvocationID] = tcTexCoord[gl_InvocationID];
teLightTS[gl_InvocationID] = tcLightTS[gl_InvocationID];
teViewTS[gl_InvocationID] = tcViewTS[gl_InvocationID];

vec4 vEdgeTessellationFactors = TessellationFactor.xxxy;

// Calculate edge scale factor from vertex scale factor: simply compute
// average tess factor between the two vertices making up an edge
vEdgeTessellationFactors.x = 0.5 * ( tcVertexDistanceFactor[1] + tcVertexDistanceFactor[2] );
vEdgeTessellationFactors.y = 0.5 * ( tcVertexDistanceFactor[2] + tcVertexDistanceFactor[0] );
vEdgeTessellationFactors.z = 0.5 * ( tcVertexDistanceFactor[0] + tcVertexDistanceFactor[1] );
vEdgeTessellationFactors.w = vEdgeTessellationFactors.x;

// Multiply them by global tessellation factor
vEdgeTessellationFactors *= TessellationFactor.xxxy;

// Assign tessellation levels
gl_TessLevelOuter[0] = vEdgeTessellationFactors.x;
gl_TessLevelOuter[1] = vEdgeTessellationFactors.y;
gl_TessLevelOuter[2] = vEdgeTessellationFactors.z;
gl_TessLevelInner[0] = vEdgeTessellationFactors.w;
}

// Evaluation shader

#version 400
layout(triangles, fractional_odd_spacing, cw) in;

in vec3 tcWorldPos[];
in vec3 tcNormal[];
in float tcVertexDistanceFactor[];
in vec2 tcTexCoord[];
in vec3 tcLightTS[];
in vec3 tcViewTS[];

out vec2 fTexCoord;
out vec3 fLightTS;
out vec3 fViewTS;

uniform MainConstants {
mat4 WorldMatrix; // World matrix
mat4 ViewMatrix; // View matrix
mat4 ProjectionMatrix; // Projection matrix
mat4 WorldViewProjectionMatrix; // WVP matrix
mat4 ViewProjectionMatrix; // VP matrix
mat4 InvViewMatrix; // Inverse of view matrix
vec4 ScreenResolution; // Screen resolution

vec4 MeshColor; // Mesh color
vec4 TessellationFactor; // Edge, inside, minimum tessellation factor and 1/desired triangle size
vec4 DetailTessellationHeightScale; // Height scale for detail tessellation of grid surface
vec4 GridSize; // Grid size
};

uniform Material {
vec4 MaterialAmbientColor; // Material's ambient color
vec4 MaterialDiffuseColor; // Material's diffuse color
vec4 MaterialSpecularColor; // Material's specular color
vec4 SpecularExponent; // Material's specular exponent

vec4 LightPosition; // Light's position in world space
vec4 LightDiffuse; // Light's diffuse color
vec4 LightAmbient; // Light's ambient color

vec4 Eye; // Camera's location
vec4 BaseTextureRepeat; // The tiling factor for base and normal map textures
vec4 POMHeightMapScale; // Describes the useful range of values for the height field

vec4 ShadowSoftening; // Blurring factor for the soft shadows computation

int MinSamples; // The minimum number of samples for sampling the height field profile
int MaxSamples; // The maximum number of samples for sampling the height field profile
};

uniform sampler2D HeightTexture; // Normal map and height map texture pair

void main()
{
// Interpolate world space position with barycentric coordinates
vec3 vWorldPos = gl_TessCoord.x * tcWorldPos[0] +
gl_TessCoord.y * tcWorldPos[1] +
gl_TessCoord.z * tcWorldPos[2];

// Interpolate world space normal and renormalize it
vec3 vNormal = gl_TessCoord.x * tcNormal[0] +
gl_TessCoord.y * tcNormal[1] +
gl_TessCoord.z * tcNormal[2];
vNormal = normalize( vNormal );

// Interpolate other inputs with barycentric coordinates
fTexCoord = gl_TessCoord.x * tcTexCoord[0] +
gl_TessCoord.y * tcTexCoord[1] +
gl_TessCoord.z * tcTexCoord[2];
vec3 vLightTS = gl_TessCoord.x * tcLightTS[0] +
gl_TessCoord.y * tcLightTS[1] +
gl_TessCoord.z * tcLightTS[2];

// Calculate MIP level to fetch normal from
float HeightMapMIPLevel = clamp( ( distance( vWorldPos, Eye.xyz ) - 100.0 ) / 100.0, 0.0, 3.0);

// Sample normal and height map
vec4 vNormalHeight = textureLod(HeightTexture, fTexCoord, HeightMapMIPLevel );

// Displace vertex along normal
vWorldPos += vNormal * ( DetailTessellationHeightScale.x * ( vNormalHeight.w-1.0 ) );

// Transform world position with viewprojection matrix
gl_Position = ViewProjectionMatrix * vec4( vWorldPos.xyz, 1.0 );

// Per-pixel lighting: pass tangent space light vector to pixel shader
fLightTS = vLightTS;

// Also pass tangent space view vector
fViewTS = gl_TessCoord.x * tcViewTS[0] +
gl_TessCoord.y * tcViewTS[1] +
gl_TessCoord.z * tcViewTS[2];
}

// Fragment shader

#version 400
in vec2 fTexCoord;
in vec3 fLightTS;
in vec3 fViewTS;
out vec4 FragColor;

uniform Material {
vec4 MaterialAmbientColor; // Material's ambient color
vec4 MaterialDiffuseColor; // Material's diffuse color
vec4 MaterialSpecularColor; // Material's specular color
vec4 SpecularExponent; // Material's specular exponent

vec4 LightPosition; // Light's position in world space
vec4 LightDiffuse; // Light's diffuse color
vec4 LightAmbient; // Light's ambient color

vec4 Eye; // Camera's location
vec4 BaseTextureRepeat; // The tiling factor for base and normal map textures
vec4 POMHeightMapScale; // Describes the useful range of values for the height field

vec4 ShadowSoftening; // Blurring factor for the soft shadows computation

int MinSamples; // The minimum number of samples for sampling the height field profile
int MaxSamples; // The maximum number of samples for sampling the height field profile
};

uniform sampler2D ColorTexture; // Base color texture
uniform sampler2D HeightTexture; // Normal map and height map texture pair

//--------------------------------------------------------------------------------------
// Function: ComputeIllumination
//
// Description: Computes phong illumination for the given pixel using its attribute
// textures and a light vector.
//--------------------------------------------------------------------------------------
vec4 ComputeIllumination( vec2 texCoord, vec3 vLightTS, vec3 vViewTS )
{
// Sample the normal from the normal map for the given texture sample:
vec3 vNormalTS = normalize( texture(HeightTexture, texCoord).xyz * 2.0 - 1.0 );

// Sample base map
vec4 cBaseColor = texture( ColorTexture, texCoord );

// Compute diffuse color component:
vec4 cDiffuse = clamp( dot( vNormalTS, vLightTS ), 0.0, 1.0 ) * MaterialDiffuseColor;

// Compute the specular component if desired:
vec4 cSpecular = vec4(0.0);

vec3 vReflectionTS = normalize( 2 * dot( vViewTS, vNormalTS ) * vNormalTS - vViewTS );
float fRdotL = clamp( dot( vReflectionTS, vLightTS ), 0.0, 1.0);
cSpecular = pow( fRdotL, SpecularExponent.x ) * MaterialSpecularColor;

// Composite the final color:
vec4 cFinalColor = ( MaterialAmbientColor + cDiffuse ) * cBaseColor + cSpecular;

return cFinalColor;
}

void main()
{
// Normalize tangent space light vector
vec3 vLightTS = normalize( fLightTS );

// Normalize tangent space view vector
vec3 vViewTS = normalize( fViewTS );

// Compute resulting color for the pixel:
FragColor = ComputeIllumination( fTexCoord, vLightTS, vViewTS );
}

 

Outcomes