Started work on dynamic terrain tessellation example

This commit is contained in:
saschawillems 2016-06-18 19:24:37 +02:00
parent 4ba4d67935
commit 0f66037da0
17 changed files with 1285 additions and 0 deletions

View file

@ -0,0 +1,7 @@
glslangvalidator -V terrain.vert -o terrain.vert.spv
glslangvalidator -V terrain.frag -o terrain.frag.spv
glslangvalidator -V skysphere.vert -o skysphere.vert.spv
glslangvalidator -V skysphere.frag -o skysphere.frag.spv
glslangvalidator -V terrain.tesc -o terrain.tesc.spv
glslangvalidator -V terrain.tese -o terrain.tese.spv

View file

@ -0,0 +1,13 @@
#version 450 core
layout (location = 0) in vec2 inUV;
layout (set = 0, binding = 1) uniform sampler2D samplerColorMap;
layout (location = 0) out vec4 outFragColor;
void main(void)
{
vec4 color = texture(samplerColorMap, inUV);
outFragColor = vec4(color.rgb, 1.0);
}

Binary file not shown.

View file

@ -0,0 +1,23 @@
#version 450 core
layout (location = 0) in vec3 inPos;
layout (location = 2) in vec2 inUV;
layout (location = 0) out vec2 outUV;
layout (set = 0, binding = 0) uniform UBO
{
mat4 mvp;
} ubo;
out gl_PerVertex
{
vec4 gl_Position;
};
void main(void)
{
gl_Position = ubo.mvp * vec4(inPos, 1.0);
outUV = inUV;
outUV.t = 1.0 - outUV.t;
}

Binary file not shown.

View file

@ -0,0 +1,55 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (set = 0, binding = 1) uniform sampler2D displacementMap;
layout (set = 0, binding = 2) uniform sampler2DArray terrainLayers;
layout (location = 0) in vec3 inNormal;
layout (location = 1) in vec2 inUV;
layout (location = 2) in vec3 inEyePos;
layout (location = 3) in vec3 inLightVec;
layout (location = 0) out vec4 outFragColor;
vec4 sampleTerrainLayer()
{
// Define some layer ranges for sampling depending on terrain height
vec2 layers[4];
layers[0] = vec2(0.0, 5.0);
layers[1] = vec2(5.0, 50.0);
layers[2] = vec2(50.0, 120.0);
layers[3] = vec2(120.0, 255.0);
vec3 color = vec3(0.0);
// Get height from displacement map
float height = textureLod(displacementMap, inUV, 0.0).r * 255.0;
for (int i = 0; i < 4; i++)
{
float range = layers[i].y - layers[i].x;
float weight = (range - abs(height - layers[i].y)) / range;
weight = max(0.05, weight);
color += weight * texture(terrainLayers, vec3(inUV * 16.0, i)).rgb;
}
return vec4(color, 1.0);
}
void main()
{
/* todo: no lighting yet
vec3 N = normalize(inNormal);
vec3 L = normalize(vec3(1.0));
vec3 Eye = normalize(inEyePos);
vec3 Reflected = normalize(reflect(-inLightVec, inNormal));
vec4 IAmbient = vec4(vec3(0.15), 1.0);
vec4 IDiffuse = vec4(1.0) * max(dot(inNormal, inLightVec), 0.0);
outFragColor = vec4((IAmbient + IDiffuse) * vec4(texture(terrainLayers, vec3(inUV, 0.0)).rgb, 1.0));
*/
outFragColor = sampleTerrainLayer();
}

Binary file not shown.

View file

@ -0,0 +1,86 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout(set = 0, binding = 0) uniform UBO
{
mat4 projection;
mat4 modelview;
vec4 lightPos;
float displacementFactor;
float tessellationFactor;
vec2 viewportDim;
float tessellatedEdgeSize;
} ubo;
layout(set = 0, binding = 2) uniform sampler2D samplerHeight;
layout (vertices = 4) out;
layout (location = 0) in vec3 inNormal[];
layout (location = 1) in vec2 inUV[];
layout (location = 0) out vec3 outNormal[4];
layout (location = 1) out vec2 outUV[4];
// Calculate the tessellation factor based on screen space
// dimensions of the edge
float screenSpaceTessFactor(vec4 p0, vec4 p1)
{
// Calculate edge mid point
vec4 midPoint = 0.5 * (p0 + p1);
// Sphere radius as distance between the control points
float radius = distance(p0, p1) / 2;
// View space
vec4 v0 = ubo.modelview * midPoint;
// Project into clip space
vec4 clip0 = (ubo.projection * (v0 - vec4(radius, vec3(0.0))));
vec4 clip1 = (ubo.projection * (v0 + vec4(radius, vec3(0.0))));
// Get normalized device coordinates
clip0 /= clip0.w;
clip1 /= clip1.w;
// Convert to viewport coordinates
clip0.xy *= ubo.viewportDim;
clip1.xy *= ubo.viewportDim;
// Return the tessellation factor based on the screen size
// given by the distance of the two edge control points in screen space
// and a reference (min.) tessellation size for the edge set by the application
return clamp(distance(clip0, clip1) / ubo.tessellatedEdgeSize * ubo.tessellationFactor, 1.0, 64.0);
}
void main()
{
if (gl_InvocationID == 0)
{
if (ubo.tessellationFactor > 0.0)
{
gl_TessLevelOuter[0] = screenSpaceTessFactor(gl_in[3].gl_Position, gl_in[0].gl_Position);
gl_TessLevelOuter[1] = screenSpaceTessFactor(gl_in[0].gl_Position, gl_in[1].gl_Position);
gl_TessLevelOuter[2] = screenSpaceTessFactor(gl_in[1].gl_Position, gl_in[2].gl_Position);
gl_TessLevelOuter[3] = screenSpaceTessFactor(gl_in[2].gl_Position, gl_in[3].gl_Position);
gl_TessLevelInner[0] = mix(gl_TessLevelOuter[0], gl_TessLevelOuter[3], 0.5);
gl_TessLevelInner[1] = mix(gl_TessLevelOuter[2], gl_TessLevelOuter[1], 0.5);
}
else
{
// Tessellation factor can be set to zero by example
// to demonstrate a simple passthrough
gl_TessLevelInner[0] = 1.0;
gl_TessLevelInner[1] = 1.0;
gl_TessLevelOuter[0] = 1.0;
gl_TessLevelOuter[1] = 1.0;
gl_TessLevelOuter[2] = 1.0;
gl_TessLevelOuter[3] = 1.0;
}
}
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
outNormal[gl_InvocationID] = inNormal[gl_InvocationID];
outUV[gl_InvocationID] = inUV[gl_InvocationID];
}

Binary file not shown.

View file

@ -0,0 +1,51 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (set = 0, binding = 0) uniform UBO
{
mat4 projection;
mat4 modelview;
vec4 lightPos;
float displacementFactor;
float tessellationFalloff;
} ubo;
layout (set = 0, binding = 1) uniform sampler2D displacementMap;
layout(quads, equal_spacing, cw) in;
layout (location = 0) in vec3 inNormal[];
layout (location = 1) in vec2 inUV[];
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
layout (location = 2) out vec3 outEyePos;
layout (location = 3) out vec3 outLightVec;
void main()
{
// Interpolate UV coordinates
vec2 uv1 = mix(inUV[0], inUV[1], gl_TessCoord.x);
vec2 uv2 = mix(inUV[3], inUV[2], gl_TessCoord.x);
outUV = mix(uv1, uv2, gl_TessCoord.y);
// Interpolate normals
vec3 nm1 = mix(inNormal[0], inNormal[1], gl_TessCoord.x);
vec3 nm2 = mix(inNormal[3], inNormal[2], gl_TessCoord.x);
outNormal = mix(nm1, nm2, gl_TessCoord.y);
// Interpolate positions
vec4 pos1 = mix(gl_in[0].gl_Position, gl_in[1].gl_Position, gl_TessCoord.x);
vec4 pos2 = mix(gl_in[3].gl_Position, gl_in[2].gl_Position, gl_TessCoord.x);
vec4 pos = mix(pos1, pos2, gl_TessCoord.y);
// Displace
pos.y -= textureLod(displacementMap, outUV, 0.0).r * ubo.displacementFactor;
// Perspective projection
gl_Position = ubo.projection * ubo.modelview * pos;
// Calculate vectors for lighting based on tessellated position
outEyePos = -pos.xyz;
outLightVec = normalize(ubo.lightPos.xyz + outEyePos);
}

Binary file not shown.

View file

@ -0,0 +1,23 @@
#version 450
#extension GL_ARB_separate_shader_objects : enable
#extension GL_ARB_shading_language_420pack : enable
layout (location = 0) in vec3 inPos;
layout (location = 1) in vec3 inNormal;
layout (location = 2) in vec2 inUV;
layout (location = 0) out vec3 outNormal;
layout (location = 1) out vec2 outUV;
out gl_PerVertex
{
vec4 gl_Position;
};
void main(void)
{
gl_Position = vec4(inPos.xyz, 1.0);
outUV = inUV;
outNormal = inNormal;
}

Binary file not shown.