Commit 37810427 authored by David Llewellyn-Jones's avatar David Llewellyn-Jones

Shadow texture calculations now also applied to cartesian functions and parametric curves.

parent 1f53082e
#version 120
#define SHADOW_STRENGTH (0.7)
uniform vec3 vPosition, vScale;
uniform float fTime;
varying vec4 vDiffuse, vGlobal, vAmbient;
varying vec3 vDir, vHalfVector;
varying float fDist;
varying vec3 vFuncParams;
varying mat3 vNormalMatrix;
varying mat3 mNormalMatrix;
uniform sampler2D tTexture;
uniform sampler2D tBumpMap;
uniform sampler2DShadow tShadow;
uniform float fBumpScale;
uniform float fTextureStrength;
varying vec4 vShadowPos;
uniform mat4 mLightTransform;
uniform float fShadowBias;
uniform float fShadowBlurX;
uniform float fShadowBlurY;
/*REPLACE:controlvars*//*END*/
......@@ -22,9 +34,15 @@ void main () {
vec4 vColourRaw;
float fAttenuation;
vec3 vNormalBidir;
vec3 vNormal;
vec3 vTangentX;
vec3 vTangentY;
vec3 vNormal;
vec3 vBump;
vec3 vNormInt;
vec4 vShadowTexPos;
float fShadowStrength;
int nShadowX;
int nShadowY;
// Calculate two tangents to the curve at the point where the fragment touches
vTangentX.x = 1.0f;
......@@ -37,7 +55,7 @@ void main () {
// Calculate the normal from the tangents
vNormal = cross (vTangentX, vTangentY);
vNormal = normalize (vNormalMatrix * vNormal);
vNormal = normalize (mNormalMatrix * vNormal);
if (gl_FrontFacing) {
vNormalBidir = vNormal;
......@@ -63,6 +81,23 @@ void main () {
}
gl_FragColor = vColour;
// Convert the fragment coordinate into a shadow texture coordinate
vShadowTexPos = ((vShadowPos / vShadowPos.w) + 1.0f) / 2.0f;
// Add slight bias to avoid shadow acne
vShadowTexPos.z -= fShadowBias;
// Blur the shadow
fShadowStrength = 0.0;
for (nShadowX = -1; nShadowX < 2; nShadowX++) {
for (nShadowY = -1; nShadowY < 2; nShadowY++) {
fShadowStrength += shadow2D (tShadow, vShadowTexPos.xyz + vec3(nShadowX * fShadowBlurX, nShadowY * fShadowBlurY, 0.0f)).x;
}
}
// Darken the colour in proportion to the shadow level
gl_FragColor *= vec4 ((1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH)) + (SHADOW_STRENGTH * fShadowStrength / 9.0f);
gl_FragColor.a = vColourRaw.a;
}
......
......@@ -6,7 +6,11 @@ varying vec4 vDiffuse, vGlobal, vAmbient;
varying vec3 vDir, vHalfVector;
varying float fDist;
varying vec3 vFuncParams;
varying mat3 vNormalMatrix;
varying mat3 mNormalMatrix;
varying vec4 vShadowPos;
uniform mat4 mLightTransform;
#define M_PI (3.1415926535897932384626433832795)
/*REPLACE:controlvars*//*END*/
......@@ -26,7 +30,7 @@ void main() {
// There appears to be a bug accessing gl_NormalMatrix from the fragment shader
// We therefore pass it across using a varying variable
vNormalMatrix = gl_NormalMatrix;
mNormalMatrix = gl_NormalMatrix;
// Normalise light direction, which is stored in eye space
vEyeCoordsPos = gl_ModelViewMatrix * vVertex;
......@@ -49,6 +53,12 @@ void main() {
// Specify position of vertex
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vVertex;
// Calculate position in relatiion to light for shadow calculations
vShadowPos = mLightTransform * vVertex;
gl_TexCoord[0].s = 0.1f + (vVertex.x) / 1.2f;
gl_TexCoord[0].t = 0.1f + (vVertex.y) / 2.2f;
}
float FunctionZ (float x, float y, float t) {
......
#version 120
#define SHADOW_STRENGTH (0.7)
uniform vec3 vPosition, vScale;
uniform float fTime;
varying vec4 vDiffuse, vGlobal, vAmbient;
varying vec3 vDir, vHalfVector;
varying float fDist;
varying vec3 vFuncParams;
varying mat3 vNormalMatrix;
varying mat3 mNormalMatrix;
uniform sampler2D tTexture;
uniform sampler2D tBumpMap;
uniform sampler2DShadow tShadow;
uniform float fBumpScale;
uniform float fTextureStrength;
varying vec4 vShadowPos;
uniform mat4 mLightTransform;
uniform float fShadowBias;
uniform float fShadowBlurX;
uniform float fShadowBlurY;
/*REPLACE:controlvars*//*END*/
......@@ -23,6 +35,7 @@ void main () {
float fNDotHV;
vec4 vColour;
vec4 vColourRaw;
vec4 vTexColour;
float fAttenuation;
vec3 vNormalBidir;
vec3 vNormal;
......@@ -43,6 +56,12 @@ void main () {
float fLen;
float fCos;
float fSin;
vec3 vBump;
vec3 vNormInt;
vec4 vShadowTexPos;
float fShadowStrength;
int nShadowX;
int nShadowY;
fA = vFuncParams.x;
fP = vFuncParams.y;
......@@ -79,14 +98,24 @@ void main () {
vTangentP.z = 0.0;
// Calculate the normal from the tangents
vBump = normalize (texture2D (tBumpMap, gl_TexCoord[0].st).xyz * 2.0 - 1.0);
vBump.x *= -fBumpScale;
// The y (longitudinal) height has far less effect than the x (radial) height, so we have to scale it accordingly
vBump.y *= -fBumpScale * 100.0;
vN = cross (vTangentP, vTangentA);
//vNormInt = cross (vTangentP, vTangentA);
//vN = vTangentP * vBump.y;
//vN += vTangentA * vBump.x;
//vN += vNormInt * vBump.z;
//vN = cross (vTangentP, vTangentA);
vN = normalize (vN);
vNormal.x = (vNormalC.x * vN.x) + (vBinormal.x * vN.y) + (vTangent.x * vN.z);
vNormal.y = (vNormalC.y * vN.x) + (vBinormal.y * vN.y) + (vTangent.y * vN.z);
vNormal.z = (vNormalC.z * vN.x) + (vBinormal.z * vN.y) + (vTangent.z * vN.z);
vNormal = normalize (vNormalMatrix * vNormal);
vNormal = normalize (mNormalMatrix * vNormal);
if (gl_FrontFacing) {
vNormalBidir = vNormal;
......@@ -111,7 +140,26 @@ void main () {
vColour += fAttenuation * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow (fNDotHV, gl_FrontMaterial.shininess);
}
vTexColour = 1.0 - ((1.0 - texture2D (tTexture, gl_TexCoord[0].st)) * fTextureStrength);
//gl_FragColor = vTexColour * vColour;
gl_FragColor = vColour;
// Convert the fragment coordinate into a shadow texture coordinate
vShadowTexPos = ((vShadowPos / vShadowPos.w) + 1.0f) / 2.0f;
// Add slight bias to avoid shadow acne
vShadowTexPos.z -= fShadowBias;
// Blur the shadow
fShadowStrength = 0.0;
for (nShadowX = -1; nShadowX < 2; nShadowX++) {
for (nShadowY = -1; nShadowY < 2; nShadowY++) {
fShadowStrength += shadow2D (tShadow, vShadowTexPos.xyz + vec3(nShadowX * fShadowBlurX, nShadowY * fShadowBlurY, 0.0f)).x;
}
}
// Darken the colour in proportion to the shadow level
gl_FragColor *= vec4 ((1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH), (1.0 - SHADOW_STRENGTH)) + (SHADOW_STRENGTH * fShadowStrength / 9.0f);
gl_FragColor.a = vColourRaw.a;
}
......
......@@ -6,7 +6,11 @@ varying vec4 vDiffuse, vGlobal, vAmbient;
varying vec3 vDir, vHalfVector;
varying float fDist;
varying vec3 vFuncParams;
varying mat3 vNormalMatrix;
varying mat3 mNormalMatrix;
varying vec4 vShadowPos;
uniform mat4 mLightTransform;
#define M_PI (3.1415926535897932384626433832795)
/*REPLACE:controlvars*//*END*/
......@@ -63,7 +67,7 @@ void main() {
// There appears to be a bug accessing gl_NormalMatrix from the fragment shader
// We therefore pass it across using a varying variable
vNormalMatrix = gl_NormalMatrix;
mNormalMatrix = gl_NormalMatrix;
// Normalise light direction, which is stored in eye space
vEyeCoordsPos = gl_ModelViewMatrix * vVertex;
......@@ -86,6 +90,12 @@ void main() {
// Specify position of vertex
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vVertex;
// Calculate position in relatiion to light for shadow calculations
vShadowPos = mLightTransform * vVertex;
gl_TexCoord[0].s = 1.0f * (0.1f + (fA / 1.2f));
gl_TexCoord[0].t = 1.0f * (0.1f + (fP / (M_PI)) / 15.0);
}
vec3 FunctionC (float a, float t) {
......
......@@ -958,7 +958,9 @@ void RenderShadow (VisPersist * psVisData) {
// Draw the graph axes
if (psVisData->boDrawAxes) {
glDisable (GL_CULL_FACE);
DrawAxes (psVisData);
glEnable (GL_CULL_FACE);
}
// Draw the graph
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment