diff options
author | Heikkinen Miikka <miikka.heikkinen@digia.com> | 2013-05-17 12:21:40 +0300 |
---|---|---|
committer | Tomi Korpipää <tomi.korpipaa@digia.com> | 2013-05-17 12:37:26 +0300 |
commit | 3dafad59b97532e8e68c0b7abca796f3a9527a6e (patch) | |
tree | 6dcb5bf6edc5aee39490b35db11202a20929d858 /src/datavis3d/engine/shaders | |
parent | 5963d52c1923cf2468c1fa4738e04a175196cc7d (diff) |
Added datavis3d back under src
Change-Id: I209e8b3228b2ce0085c897db6fb2ea78e93afa67
Reviewed-by: Tomi Korpipää <tomi.korpipaa@digia.com>
Diffstat (limited to 'src/datavis3d/engine/shaders')
18 files changed, 702 insertions, 0 deletions
diff --git a/src/datavis3d/engine/shaders/fragmentDepthTest b/src/datavis3d/engine/shaders/fragmentDepthTest new file mode 100644 index 00000000..d6c502e1 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentDepthTest @@ -0,0 +1,7 @@ +uniform sampler2D textureSampler; + +varying highp vec2 UV; + +void main() { + gl_FragColor = texture2D(textureSampler, UV); +} diff --git a/src/datavis3d/engine/shaders/fragmentShader b/src/datavis3d/engine/shaders/fragmentShader new file mode 100644 index 00000000..5bf9c654 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShader @@ -0,0 +1,36 @@ +#version 120 + +varying highp vec2 UV; +varying highp vec2 coords_mdl; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; + +uniform highp vec3 lightPosition_wrld; +uniform highp vec3 color_mdl; +uniform highp float lightStrength; +uniform highp float ambientStrength; + +void main() { + highp vec3 materialDiffuseColor = color_mdl.rgb; + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp float distance = length(lightPosition_wrld - position_wrld); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + gl_FragColor.rgb = + materialAmbientColor + + materialDiffuseColor * lightStrength * pow(cosTheta, 2) / distance + + materialSpecularColor * lightStrength * pow(cosAlpha, 10) / distance; + gl_FragColor.a = 1.0; +} + diff --git a/src/datavis3d/engine/shaders/fragmentShaderAmbient b/src/datavis3d/engine/shaders/fragmentShaderAmbient new file mode 100644 index 00000000..88f850f3 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShaderAmbient @@ -0,0 +1,32 @@ +#version 120 + +uniform highp vec3 lightPosition_wrld; +uniform highp vec3 color_mdl; + +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; + +void main() { + highp vec3 lightColor = vec3(1.0, 1.0, 1.0); + highp float lightPower = 10.0; + highp vec3 materialAmbientColor = vec3(0.5, 0.5, 0.5) * color_mdl; + highp vec3 materialSpecularColor = vec3(0.3, 0.3, 0.3) * color_mdl; + + highp float distance = length(lightPosition_wrld - position_wrld); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + gl_FragColor.rgb = + materialAmbientColor + + materialSpecularColor * lightColor * lightPower * pow(cosAlpha, 5) / (distance * distance); + gl_FragColor.a = 1.0; +} + diff --git a/src/datavis3d/engine/shaders/fragmentShaderColorOnY b/src/datavis3d/engine/shaders/fragmentShaderColorOnY new file mode 100644 index 00000000..ee57e8e5 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShaderColorOnY @@ -0,0 +1,33 @@ +#version 120 + +uniform highp vec3 lightPosition_wrld; +uniform highp vec3 color_mdl; +uniform highp float lightStrength; +uniform highp float ambientStrength; + +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; +varying highp vec2 coords_mdl; + +void main() { + highp vec3 materialDiffuseColor = vec3(coords_mdl.y * color_mdl.x, coords_mdl.y * color_mdl.y, coords_mdl.y * color_mdl.z); + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp float distance = length(lightPosition_wrld - position_wrld); + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + gl_FragColor.rgb = + materialAmbientColor + + materialDiffuseColor * lightStrength * (cosTheta * cosTheta) / (distance * distance) + + materialSpecularColor * lightStrength * pow(cosAlpha, 5) / (distance * distance); +} + diff --git a/src/datavis3d/engine/shaders/fragmentShaderDepth b/src/datavis3d/engine/shaders/fragmentShaderDepth new file mode 100644 index 00000000..5cfd4b10 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShaderDepth @@ -0,0 +1,3 @@ +void main() { + gl_FragDepth = gl_FragCoord.z; +} diff --git a/src/datavis3d/engine/shaders/fragmentShaderLabel b/src/datavis3d/engine/shaders/fragmentShaderLabel new file mode 100644 index 00000000..86021410 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShaderLabel @@ -0,0 +1,8 @@ +uniform sampler2D textureSampler; + +varying highp vec2 UV; + +void main() { + gl_FragColor = texture2D(textureSampler, UV); +} + diff --git a/src/datavis3d/engine/shaders/fragmentShaderSelection b/src/datavis3d/engine/shaders/fragmentShaderSelection new file mode 100644 index 00000000..66370224 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShaderSelection @@ -0,0 +1,7 @@ +uniform highp vec3 color_mdl; +//uniform highp vec4 color_mdl; +void main() { + gl_FragColor.rgb = color_mdl; + //gl_FragColor = color_mdl; +} + diff --git a/src/datavis3d/engine/shaders/fragmentShaderTexture b/src/datavis3d/engine/shaders/fragmentShaderTexture new file mode 100644 index 00000000..d271f985 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShaderTexture @@ -0,0 +1,35 @@ +#version 120 + +uniform highp vec3 lightPosition_wrld; +uniform highp vec3 color_mdl; +uniform highp float lightStrength; +uniform highp float ambientStrength; +uniform sampler2D textureSampler; + +varying highp vec2 UV; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; + +void main() { + highp vec3 materialDiffuseColor = texture2D(textureSampler, UV).rgb; + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp float distance = length(lightPosition_wrld - position_wrld); + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + gl_FragColor.rgb = + materialAmbientColor + + materialDiffuseColor * lightStrength * (cosTheta * cosTheta) / distance + + materialSpecularColor * lightStrength * pow(cosAlpha, 10) / distance; + gl_FragColor.a = 1.0; +} + diff --git a/src/datavis3d/engine/shaders/fragmentShadow b/src/datavis3d/engine/shaders/fragmentShadow new file mode 100644 index 00000000..3a6ffc5a --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShadow @@ -0,0 +1,140 @@ +#version 120 +//#version 130 // use with version 1 + +uniform highp float lightStrength; +uniform highp float ambientStrength; +uniform highp float shadowQuality; +uniform highp sampler2D textureSampler; +uniform highp sampler2DShadow shadowMap; // use with version 2 +//uniform highp sampler2D shadowMap; // use with version 1 + +varying highp vec4 shadowCoord; +varying highp vec2 UV; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; + +const highp vec2 poissonDisk[16] = vec2[](vec2(-0.94201624, -0.39906216), + vec2(0.94558609, -0.76890725), + vec2(-0.094184101, -0.92938870), + vec2(0.34495938, 0.29387760), + vec2(-0.91588581, 0.45771432), + vec2(-0.81544232, -0.87912464), + vec2(-0.38277543, 0.27676845), + vec2(0.97484398, 0.75648379), + vec2(0.44323325, -0.97511554), + vec2(0.53742981, -0.47373420), + vec2(-0.26496911, -0.41893023), + vec2(0.79197514, 0.19090188), + vec2(-0.24188840, 0.99706507), + vec2(-0.81409955, 0.91437590), + vec2(0.19984126, 0.78641367), + vec2(0.14383161, -0.14100790)); + +/*float random(vec3 seed, int i) { + vec4 seed4 = vec4(seed, i); + float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673)); + return fract(sin(dot_product) * 43758.5453); +}*/ + +// Version 1: Causes self-shadowing, but shadows are smooth +/*void main() { + float shadowFactor = 1.0; // default to '1' meaning "no shadow" + //float epsilon = 0.01; // increase value to remove little artifacts + vec4 shadCoordsPD = shadowCoord; + shadCoordsPD.z += 0.005; + shadCoordsPD /= shadowCoord.w; + if (shadowCoord.w <= 0.0) { // ignore negative projection + shadowFactor = 1.0; + } else if (shadCoordsPD.x < 0.0 || shadCoordsPD.y < 0.0) { // outside light frustum, ignore + shadowFactor = 1.0; + } else if (shadCoordsPD.x >= 1.0 || shadCoordsPD.y >= 1.0) { // outside light frustum, ignore + shadowFactor = 1.0; + } else { + // This does not work perfectly. It causes self-shadowing, which we should get rid of. + // Requires at least #version 130 + float shadow = 0.0; + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(0, 0)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(-1, 1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(1, 1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(-1, -1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(1, -1)); + shadow *= 0.2; + shadowFactor = shadow; + //float shadow = texture2D(shadowMap, shadCoordsPD.xy).x; + //if (shadow + epsilon < shadCoordsPD.z) { + // shadowFactor = 0.0; + //} + } + // shadow is dark gray, other parts light gray + //gl_FragColor = vec4(0.8, 0.8, 0.8, 1.0) * shadowFactor;// + vec4(0.2, 0.2, 0.2, 1.0); + // simple shadows with no effect from light + //gl_FragColor = texture2D(textureSampler, UV) * shadowFactor; + + // shadows including effects from light + highp vec3 materialDiffuseColor = texture2D(textureSampler, UV).rgb; + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(0.2, 0.2, 0.2); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + // Shadow strength can be adjusted by using pow(shadowFactor, strength), but it causes self-shadowing issues that need to be solved + gl_FragColor.rgb = + shadowFactor * (materialAmbientColor + + materialDiffuseColor * lightStrength * cosTheta + + materialSpecularColor * lightStrength * pow(cosAlpha, 10)); + gl_FragColor.a = 1.0; +}*/ + +// Version 2: Shadows are a bit rugged (may be fixed with poisson disk?) +void main() { + highp vec3 materialDiffuseColor = texture2D(textureSampler, UV).rgb; + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(0.2, 0.2, 0.2); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + highp float bias = 0.005 * tan(acos(cosTheta)); + bias = clamp(bias, 0.0, 0.01); + + vec4 shadCoords = shadowCoord; + shadCoords.z -= bias; + // adjust shadow strength by increasing the multiplier and lowering the addition (their sum must be 1) + // direct method; needs large shadow texture to look good + //highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoords).r + 0.25; + // poisson disk sampling; smoothes edges + highp float visibility = 0.2; + for (int i = 0; i < 15; i++) { + vec4 shadCoordsPD = shadCoords; + shadCoordsPD.x += cos(poissonDisk[i].x) / shadowQuality; + shadCoordsPD.y += sin(poissonDisk[i].y) / shadowQuality; + visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r; + } + // stratified poisson; produces noise but hides pixel edges well + /*for (int i = 0; i < 15; i++) { + vec4 shadCoordsPD = shadCoords; + int index = int(16.0 * random(gl_FragCoord.xyy, i)); + shadCoordsPD.xy += poissonDisk[index] / 150.0; + visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r; + } + */ + + gl_FragColor.rgb = + visibility * (materialAmbientColor + + materialDiffuseColor * lightStrength * cosTheta + + materialSpecularColor * lightStrength * pow(cosAlpha, 10)); + gl_FragColor.a = 1.0; +} diff --git a/src/datavis3d/engine/shaders/fragmentShadowNoTex b/src/datavis3d/engine/shaders/fragmentShadowNoTex new file mode 100644 index 00000000..a08b6b40 --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShadowNoTex @@ -0,0 +1,147 @@ +#version 120 +//#version 130 // use with version 1 + +uniform highp float lightStrength; +uniform highp float ambientStrength; +uniform highp float shadowQuality; +uniform highp vec3 color_mdl; +uniform highp sampler2DShadow shadowMap; // use with version 2 +//uniform highp sampler2D shadowMap; // use with version 1 + +varying highp vec4 shadowCoord; +varying highp vec2 UV; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; + +const highp vec2 poissonDisk[16] = vec2[](vec2(-0.94201624, -0.39906216), + vec2(0.94558609, -0.76890725), + vec2(-0.094184101, -0.92938870), + vec2(0.34495938, 0.29387760), + vec2(-0.91588581, 0.45771432), + vec2(-0.81544232, -0.87912464), + vec2(-0.38277543, 0.27676845), + vec2(0.97484398, 0.75648379), + vec2(0.44323325, -0.97511554), + vec2(0.53742981, -0.47373420), + vec2(-0.26496911, -0.41893023), + vec2(0.79197514, 0.19090188), + vec2(-0.24188840, 0.99706507), + vec2(-0.81409955, 0.91437590), + vec2(0.19984126, 0.78641367), + vec2(0.14383161, -0.14100790)); + +/*const highp vec2 poissonDisk[16] = vec2[](vec2(-0.25, -0.25), + vec2(0.25, -0.25), + vec2(-0.25, 0.25), + vec2(0.25, 0.25), + vec2(-0.5, -0.5), + vec2(0.5, -0.5), + vec2(-0.5, 0.5), + vec2(0.5, 0.5), + vec2(-0.75, -0.75), + vec2(0.75, -0.75), + vec2(-0.75, 0.75), + vec2(0.75, 0.75), + vec2(-1.0, -1.0), + vec2(1.0, -1.0), + vec2(-1.0, 1.0), + vec2(1.0, 1.0));*/ + +/*float random(vec3 seed, int i) { + vec4 seed4 = vec4(seed, i); + float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673)); + return fract(sin(dot_product) * 43758.5453); +}*/ + +// Version 1: Causes self-shadowing, but shadows are smooth +/*void main() { + float shadowFactor = 1.0; // default to '1' meaning "no shadow" + vec4 shadCoordsPD = shadowCoord; + shadCoordsPD.z += 0.005; + shadCoordsPD /= shadowCoord.w; + if (shadowCoord.w <= 0.0) { // ignore negative projection + shadowFactor = 1.0; + } else if (shadCoordsPD.x < 0.0 || shadCoordsPD.y < 0.0) { // outside light frustum, ignore + shadowFactor = 1.0; + } else if (shadCoordsPD.x >= 1.0 || shadCoordsPD.y >= 1.0) { // outside light frustum, ignore + shadowFactor = 1.0; + } else { + // This does not work perfectly. It causes self-shadowing, which we should get rid of. + // Requires at least #version 130 + float shadow = 0.0; + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(0, 0)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(-1, 1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(1, 1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(-1, -1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(1, -1)); + shadow *= 0.2; + shadowFactor = shadow; + } + + highp vec3 materialDiffuseColor = color_mdl.rgb; + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + // Shadow strength can be adjusted by using pow(shadowFactor, strength), but it causes self-shadowing issues that need to be solved + gl_FragColor.rgb = + shadowFactor * (materialAmbientColor + + materialDiffuseColor * lightStrength * cosTheta + + materialSpecularColor * lightStrength * pow(cosAlpha, 20)); + gl_FragColor.a = 1.0; +}*/ + +// Version 2: Shadows are a bit rugged (may be fixed with poisson disk?) +void main() { + highp vec3 materialDiffuseColor = color_mdl.rgb; + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + highp float bias = 0.005 * tan(acos(cosTheta)); + bias = clamp(bias, 0.0, 0.01); + + vec4 shadCoords = shadowCoord; + shadCoords.z -= bias; + // adjust shadow strength by increasing the multiplier and lowering the addition (their sum must be 1) + // direct method; needs large shadow texture to look good + //highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoords).r + 0.25; + // poisson disk sampling; smoothes edges + highp float visibility = 0.2; + for (int i = 0; i < 15; i++) { + vec4 shadCoordsPD = shadCoords; + shadCoordsPD.x += cos(poissonDisk[i].x) / shadowQuality; + shadCoordsPD.y += sin(poissonDisk[i].y) / shadowQuality; + visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r; + } + // stratified poisson; produces noise but hides pixel edges well + /*for (int i = 0; i < 15; i++) { + vec4 shadCoordsPD = shadCoords; + int index = int(16.0 * random(gl_FragCoord.xyy, i)); + shadCoordsPD.xy += poissonDisk[index] / 150.0; + visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r; + } + */ + + gl_FragColor.rgb = + visibility * (materialAmbientColor + + materialDiffuseColor * lightStrength * cosTheta + + materialSpecularColor * lightStrength * pow(cosAlpha, 10)); + gl_FragColor.a = 1.0; +} diff --git a/src/datavis3d/engine/shaders/fragmentShadowNoTexColorOnY b/src/datavis3d/engine/shaders/fragmentShadowNoTexColorOnY new file mode 100644 index 00000000..d208c5fd --- /dev/null +++ b/src/datavis3d/engine/shaders/fragmentShadowNoTexColorOnY @@ -0,0 +1,128 @@ +#version 120 +//#version 130 // use with version 1 + +uniform highp float lightStrength; +uniform highp float ambientStrength; +uniform highp float shadowQuality; +uniform highp vec3 color_mdl; +uniform highp sampler2DShadow shadowMap; // use with version 2 +//uniform highp sampler2D shadowMap; // use with version 1 + +varying highp vec4 shadowCoord; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; +varying highp vec2 coords_mdl; + +const highp vec2 poissonDisk[16] = vec2[](vec2(-0.94201624, -0.39906216), + vec2(0.94558609, -0.76890725), + vec2(-0.094184101, -0.92938870), + vec2(0.34495938, 0.29387760), + vec2(-0.91588581, 0.45771432), + vec2(-0.81544232, -0.87912464), + vec2(-0.38277543, 0.27676845), + vec2(0.97484398, 0.75648379), + vec2(0.44323325, -0.97511554), + vec2(0.53742981, -0.47373420), + vec2(-0.26496911, -0.41893023), + vec2(0.79197514, 0.19090188), + vec2(-0.24188840, 0.99706507), + vec2(-0.81409955, 0.91437590), + vec2(0.19984126, 0.78641367), + vec2(0.14383161, -0.14100790)); + +/*float random(vec3 seed, int i) { + vec4 seed4 = vec4(seed, i); + float dot_product = dot(seed4, vec4(12.9898, 78.233, 45.164, 94.673)); + return fract(sin(dot_product) * 43758.5453); +}*/ + +// Version 1: Causes self-shadowing, but shadows are smooth +/*void main() { + float shadowFactor = 1.0; // default to '1' meaning "no shadow" + vec4 shadCoordsPD = shadowCoord; + shadCoordsPD.z += 0.005; + shadCoordsPD /= shadowCoord.w; + if (shadowCoord.w <= 0.0) { // ignore negative projection + shadowFactor = 1.0; + } else if (shadCoordsPD.x < 0.0 || shadCoordsPD.y < 0.0) { // outside light frustum, ignore + shadowFactor = 1.0; + } else if (shadCoordsPD.x >= 1.0 || shadCoordsPD.y >= 1.0) { // outside light frustum, ignore + shadowFactor = 1.0; + } else { + // This does not work perfectly. It causes self-shadowing, which we should get rid of. + // Requires at least #version 130 + float shadow = 0.0; + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(0, 0)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(-1, 1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(1, 1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(-1, -1)); + shadow += textureProjOffset(shadowMap, shadCoordsPD, ivec2(1, -1)); + shadow *= 0.2; + shadowFactor = shadow; + } + + highp vec3 materialDiffuseColor = vec3(coords_mdl.y * color_mdl.x, coords_mdl.y * color_mdl.y, coords_mdl.y * color_mdl.z); + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + // Shadow strength can be adjusted by using pow(shadowFactor, strength), but it causes self-shadowing issues that need to be solved + gl_FragColor.rgb = + shadowFactor * (materialAmbientColor + + materialDiffuseColor * lightStrength * cosTheta + + materialSpecularColor * lightStrength * pow(cosAlpha, 20)); + gl_FragColor.a = 1.0; +}*/ + +// Version 2: Shadows are a bit rugged (may be fixed with poisson disk?) +void main() { + highp vec3 materialDiffuseColor = vec3(coords_mdl.y * color_mdl.x, coords_mdl.y * color_mdl.y, coords_mdl.y * color_mdl.z); + highp vec3 materialAmbientColor = vec3(ambientStrength, ambientStrength, ambientStrength) * materialDiffuseColor; + highp vec3 materialSpecularColor = vec3(1.0, 1.0, 1.0); + + highp vec3 n = normalize(normal_cmr); + highp vec3 l = normalize(lightDirection_cmr); + highp float cosTheta = clamp(dot(n, l), 0.0, 1.0); + + highp vec3 E = normalize(eyeDirection_cmr); + highp vec3 R = reflect(-l, n); + highp float cosAlpha = clamp(dot(E, R), 0.0, 1.0); + + highp float bias = 0.005 * tan(acos(cosTheta)); + bias = clamp(bias, 0.0, 0.01); + + vec4 shadCoords = shadowCoord; + shadCoords.z -= bias; + // adjust shadow strength by increasing the multiplier and lowering the addition (their sum must be 1) + // direct method; needs large shadow texture to look good + //highp float visibility = 0.75 * shadow2DProj(shadowMap, shadCoords).r + 0.25; + // poisson disk sampling; smoothes edges + highp float visibility = 0.2; + for (int i = 0; i < 15; i++) { + vec4 shadCoordsPD = shadCoords; + shadCoordsPD.x += cos(poissonDisk[i].x) / shadowQuality; + shadCoordsPD.y += sin(poissonDisk[i].y) / shadowQuality; + visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r; + } + /*for (int i = 0; i < 15; i++) { + vec4 shadCoordsPD = shadCoords; + int index = int(16.0 * random(gl_FragCoord.xyy, i)); + shadCoordsPD.xy += poissonDisk[index] / 150.0; + visibility += 0.05 * shadow2DProj(shadowMap, shadCoordsPD).r; + }*/ + + gl_FragColor.rgb = + visibility * (materialAmbientColor + + materialDiffuseColor * lightStrength * cosTheta + + materialSpecularColor * lightStrength * pow(cosAlpha, 10)); + gl_FragColor.a = 1.0; +} diff --git a/src/datavis3d/engine/shaders/vertexDepthTest b/src/datavis3d/engine/shaders/vertexDepthTest new file mode 100644 index 00000000..d62f1aa7 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexDepthTest @@ -0,0 +1,9 @@ +attribute highp vec3 vertexPosition_mdl; + +varying highp vec2 UV; + +void main() { + gl_Position = vec4(vertexPosition_mdl, 1.0); + UV = (vertexPosition_mdl.xy + vec2(1.0, 1.0)) / 2.0; +} + diff --git a/src/datavis3d/engine/shaders/vertexShader b/src/datavis3d/engine/shaders/vertexShader new file mode 100644 index 00000000..14f77773 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexShader @@ -0,0 +1,28 @@ +#version 120 + +attribute highp vec3 vertexPosition_mdl; +attribute highp vec2 vertexUV; +attribute highp vec3 vertexNormal_mdl; + +uniform highp mat4 MVP; +uniform highp mat4 V; +uniform highp mat4 M; +uniform highp mat4 itM; +uniform highp vec3 lightPosition_wrld; + +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; +varying highp vec2 coords_mdl; + +void main() { + gl_Position = MVP * vec4(vertexPosition_mdl, 1.0); + coords_mdl = vertexPosition_mdl.xy; + position_wrld = (M * vec4(vertexPosition_mdl, 1.0)).xyz; + vec3 vertexPosition_cmr = (V * M * vec4(vertexPosition_mdl, 1.0)).xyz; + eyeDirection_cmr = vec3(0.0, 0.0, 0.0) - vertexPosition_cmr; + vec3 lightPosition_cmr = (V * vec4(lightPosition_wrld, 1.0)).xyz; + lightDirection_cmr = lightPosition_cmr + eyeDirection_cmr; + normal_cmr = (V * itM * vec4(vertexNormal_mdl, 0.0)).xyz; +} diff --git a/src/datavis3d/engine/shaders/vertexShaderDepth b/src/datavis3d/engine/shaders/vertexShaderDepth new file mode 100644 index 00000000..9fe5a193 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexShaderDepth @@ -0,0 +1,8 @@ +uniform highp mat4 MVP; + +attribute highp vec3 vertexPosition_mdl; + +void main() { + gl_Position = MVP * vec4(vertexPosition_mdl, 1.0); +} + diff --git a/src/datavis3d/engine/shaders/vertexShaderLabel b/src/datavis3d/engine/shaders/vertexShaderLabel new file mode 100644 index 00000000..b4debe53 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexShaderLabel @@ -0,0 +1,11 @@ +uniform highp mat4 MVP; + +attribute highp vec3 vertexPosition_mdl; +attribute highp vec2 vertexUV; + +varying highp vec2 UV; + +void main() { + gl_Position = MVP * vec4(vertexPosition_mdl, 1.0); + UV = vertexUV; +} diff --git a/src/datavis3d/engine/shaders/vertexShaderSelection b/src/datavis3d/engine/shaders/vertexShaderSelection new file mode 100644 index 00000000..64d17e15 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexShaderSelection @@ -0,0 +1,7 @@ +uniform highp mat4 MVP; + +attribute highp vec3 vertexPosition_mdl; + +void main() { + gl_Position = MVP * vec4(vertexPosition_mdl, 1.0); +} diff --git a/src/datavis3d/engine/shaders/vertexShaderTexture b/src/datavis3d/engine/shaders/vertexShaderTexture new file mode 100644 index 00000000..01f922e0 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexShaderTexture @@ -0,0 +1,26 @@ +uniform highp mat4 MVP; +uniform highp mat4 V; +uniform highp mat4 M; +uniform highp mat4 itM; +uniform highp vec3 lightPosition_wrld; + +attribute highp vec3 vertexPosition_mdl; +attribute highp vec2 vertexUV; +attribute highp vec3 vertexNormal_mdl; + +varying highp vec2 UV; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; + +void main() { + gl_Position = MVP * vec4(vertexPosition_mdl, 1.0); + position_wrld = (M * vec4(vertexPosition_mdl, 1.0)).xyz; + vec3 vertexPosition_cmr = (V * M * vec4(vertexPosition_mdl, 1.0)).xyz; + eyeDirection_cmr = vec3(0.0, 0.0, 0.0) - vertexPosition_cmr; + vec3 lightPosition_cmr = (V * vec4(lightPosition_wrld, 1.0)).xyz; + lightDirection_cmr = lightPosition_cmr + eyeDirection_cmr; + normal_cmr = (V * itM * vec4(vertexNormal_mdl, 0.0)).xyz; + UV = vertexUV; +} diff --git a/src/datavis3d/engine/shaders/vertexShadow b/src/datavis3d/engine/shaders/vertexShadow new file mode 100644 index 00000000..e29a8a30 --- /dev/null +++ b/src/datavis3d/engine/shaders/vertexShadow @@ -0,0 +1,37 @@ +#version 120 + +uniform highp mat4 MVP; +uniform highp mat4 V; +uniform highp mat4 M; +uniform highp mat4 itM; +uniform highp mat4 depthMVP; +uniform highp vec3 lightPosition_wrld; + +attribute highp vec3 vertexPosition_mdl; +attribute highp vec3 vertexNormal_mdl; +attribute highp vec2 vertexUV; + +varying highp vec2 UV; +varying highp vec3 position_wrld; +varying highp vec3 normal_cmr; +varying highp vec3 eyeDirection_cmr; +varying highp vec3 lightDirection_cmr; +varying highp vec4 shadowCoord; +varying highp vec2 coords_mdl; + +const highp mat4 bias = mat4(0.5, 0.0, 0.0, 0.0, + 0.0, 0.5, 0.0, 0.0, + 0.0, 0.0, 0.5, 0.0, + 0.5, 0.5, 0.5, 1.0); + +void main() { + gl_Position = MVP * vec4(vertexPosition_mdl, 1.0); + coords_mdl = vertexPosition_mdl.xy; + shadowCoord = bias * depthMVP * vec4(vertexPosition_mdl, 1.0); + position_wrld = (M * vec4(vertexPosition_mdl, 1.0)).xyz; + vec3 vertexPosition_cmr = (V * M * vec4(vertexPosition_mdl, 1.0)).xyz; + eyeDirection_cmr = vec3(0.0, 0.0, 0.0) - vertexPosition_cmr; + lightDirection_cmr = (V * vec4(lightPosition_wrld, 0.0)).xyz; + normal_cmr = (V * itM * vec4(vertexNormal_mdl, 0.0)).xyz; + UV = vertexUV; +} |