Commit 1f53082e authored by David Llewellyn-Jones's avatar David Llewellyn-Jones

Shadow texture calculations and transformations applied correctly for spherical functions.

parent 843044dd
#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*/
......@@ -19,6 +31,7 @@ void main () {
float fNDotL;
float fNDotHV;
vec4 vColour;
vec4 vTexColour;
vec4 vColourRaw;
float fAttenuation;
vec3 vNormalBidir;
......@@ -30,6 +43,12 @@ void main () {
float fA;
float fP;
float fR;
vec3 vBump;
vec3 vNormInt;
vec4 vShadowTexPos;
float fShadowStrength;
int nShadowX;
int nShadowY;
fA = vFuncParams.x;
fP = vFuncParams.y;
......@@ -54,7 +73,7 @@ void main () {
if (fP == 0.0f) {
vNormal = vec3(0.0f, 0.0f, -1.0f);
}
vNormal = normalize (vNormalMatrix * vNormal);
vNormal = normalize (mNormalMatrix * vNormal);
if (gl_FrontFacing) {
vNormalBidir = vNormal;
......@@ -80,6 +99,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*/
......@@ -34,7 +38,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;
......@@ -57,6 +61,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 + (fA / (2.0 * M_PI)) / 1.2f;
gl_TexCoord[0].t = 0.1f + (fP / (M_PI)) / 2.2f;
}
float FunctionR (float a, float p, float t) {
......
......@@ -515,6 +515,7 @@ void CartesianSetFunctionPosition (double fXMin, double fYMin, double fZMin, Fun
vPosition.fY = fYMin + ((AXIS_YHSIZE * psFuncData->fYWidth) / AXIS_YSIZE);
vPosition.fZ = fZMin + ((AXIS_ZHSIZE * psFuncData->fZWidth) / AXIS_ZSIZE);
SetShaderPosition (& vPosition, psFuncData->psShaderData);
SetShaderPosition (& vPosition, psFuncData->psShaderShadowData);
PopulateVertices (psFuncData);
}
......
......@@ -1021,6 +1021,7 @@ void CurveSetFunctionPosition (double fXMin, double fYMin, double fZMin, FuncPer
vPosition.fY = AXIS_YHSIZE;
vPosition.fZ = AXIS_ZHSIZE;
SetShaderPosition (& vPosition, psFuncData->psShaderData);
SetShaderPosition (& vPosition, psFuncData->psShaderShadowData);
// It's not necessary to regenerate the vertices for the curve function
// unless it's being scaled
......
......@@ -583,6 +583,7 @@ void SetFunctionRange (double fXMin, double fYMin, double fZMin, double fXWidth,
vScale.fY = fYWidth / AXIS_YSIZE;
vScale.fZ = fZWidth / AXIS_ZSIZE;
SetShaderScale (& vScale, psFuncData->psShaderData);
SetShaderScale (& vScale, psFuncData->psShaderShadowData);
SetFunctionPosition (fXMin, fYMin, fZMin, psFuncData);
}
......@@ -745,11 +746,26 @@ bool GetTimeDependent (FuncPersist * psFuncData) {
}
void SetFunctionTime (double fTime, FuncPersist * psFuncData) {
switch (psFuncData->eType) {
case FUNCTYPE_SPHERICAL:
SphericalSetFunctionTime (fTime, psFuncData);
SphericalUpdateCentre (psFuncData);
break;
case FUNCTYPE_CURVE:
CurveSetFunctionTime (fTime, psFuncData);
CurveUpdateCentre (psFuncData);
break;
default:
// Do nothing
break;
}
if (psFuncData->psVariableT) {
SetVariable (psFuncData->psVariableT, fTime);
}
SetShaderTime (fTime, psFuncData->psShaderData);
SetShaderTime (fTime, psFuncData->psShaderShadowData);
}
void SetFunctionTexture (GLuint uTexture, FuncPersist * psFuncData) {
......@@ -1046,6 +1062,7 @@ void DrawGraph (FuncPersist * psFuncData) {
glEnable (GL_CLIP_PLANE3);
RecentreGraph (psFuncData);
glMultMatrixf (psFuncData->mStructureTransform.afM);
}
pmLightTransform = GetLightTransform (psFuncData->psShadowData);
......@@ -1203,6 +1220,7 @@ void DrawGraphShadow (FuncPersist * psFuncData) {
glEnable (GL_CLIP_PLANE3);
RecentreGraph (psFuncData);
glMultMatrixf (psFuncData->mStructureTransform.afM);
}
glEnableClientState (GL_VERTEX_ARRAY);
......@@ -1241,7 +1259,8 @@ void RecentreGraph (FuncPersist * psFuncData) {
vTranslate.fY = (((afCentre[1] - afRange[1]) * (2 * AXIS_YHSIZE)) / (afRange[4]));
vTranslate.fZ = (((afCentre[2] - afRange[2]) * (2 * AXIS_ZHSIZE)) / (afRange[5]));
glTranslatef (vTranslate.fX, vTranslate.fY, vTranslate.fZ);
//glTranslatef (vTranslate.fX, vTranslate.fY, vTranslate.fZ);
MatrixSetIdentity4 (& psFuncData->mStructureTransform);
MatrixTranslate4 (& psFuncData->mStructureTransform, & vTranslate);
}
......
......@@ -17,7 +17,7 @@
///////////////////////////////////////////////////////////////////
// Defines
#define BIAS_DEFAULT (0.005)
#define BIAS_DEFAULT (0.0005)
///////////////////////////////////////////////////////////////////
// Structures and enumerations
......
......@@ -699,6 +699,7 @@ void SphericalSetFunctionPosition (double fXMin, double fYMin, double fZMin, Fun
vPosition.fY = AXIS_YHSIZE;
vPosition.fZ = AXIS_ZHSIZE;
SetShaderPosition (& vPosition, psFuncData->psShaderData);
SetShaderPosition (& vPosition, psFuncData->psShaderShadowData);
// It's not necessary to regenerate the vertices for the spherical function
// unless it's being scaled
......
......@@ -73,6 +73,8 @@
#define GRIDLINES_MAX (14)
#define FRAMEBUFFER_TEXTURES (3)
#define SHADOWSHIFT_UP (20.0f)
#define SHADOWSHIFT_SIDEWAYS (SHADOWSHIFT_UP)
///////////////////////////////////////////////////////////////////
// Structures and enumerations
......@@ -199,6 +201,7 @@ void inline swap (float * pfVar1, float * pfVar2);
void inline order (float * pfVar1, float * pfVar2);
void DrawAxes (VisPersist * psVisData);
void DrawGraphs (VisPersist * psVisData);
void DrawGraphsShadow (VisPersist * psVisData);
GLfloat SelectFunction (int nXPos, int nYPos, VisPersist * psVisData);
void ConvertCoords (GLdouble fXMousePos, GLdouble fYMousePos, float fZPos, double * pfX, double * pfY, double * pfZ, VisPersist * psVisData);
void ShuntFunctionPosition (double fX, double fY, double fZ, VisPersist * psVisData);
......@@ -785,7 +788,6 @@ void AnimateFunction (VisPersist * psVisData) {
GSList * psFuncList;
double fTimeChange;
FuncPersist * psFuncData;
FUNCTYPE eFuncType;
fTimeChange = psVisData->fCurrentTime - psVisData->fFunctionTimePrev;
if (fTimeChange > 0.005f) {
......@@ -803,25 +805,12 @@ void AnimateFunction (VisPersist * psVisData) {
while (psFuncList) {
psFuncData = (FuncPersist *)(psFuncList->data);
eFuncType = GetFunctionType (psFuncData);
switch (eFuncType) {
case FUNCTYPE_SPHERICAL:
SphericalSetFunctionTime (psVisData->fFunctionTime, psFuncData);
SphericalUpdateCentre (psFuncData);
break;
case FUNCTYPE_CURVE:
CurveSetFunctionTime (psVisData->fFunctionTime, psFuncData);
CurveUpdateCentre (psFuncData);
break;
default:
// Do nothing
break;
}
if (GetTimeDependent (psFuncData)) {
SetFunctionTime (psVisData->fFunctionTime, psFuncData);
PopulateVertices (psFuncData);
if (!psVisData->boShader) {
PopulateVertices (psFuncData);
}
}
psFuncList = g_slist_next (psFuncList);
......@@ -925,9 +914,6 @@ void SetLightTransformMatrix (VisPersist * psVisData) {
//afMVPShadowInvert
}
#define SHADOWSHIFT_UP (2.0f)
#define SHADOWSHIFT_SIDEWAYS (SHADOWSHIFT_UP)
void CalculateLightPos (VisPersist * psVisData) {
Vector3 vSideways;
......@@ -978,7 +964,7 @@ void RenderShadow (VisPersist * psVisData) {
// Draw the graph
glDisable (GL_CULL_FACE);
//glFrontFace (GL_CW);
DrawGraphs (psVisData);
DrawGraphsShadow (psVisData);
glEnable (GL_CULL_FACE);
//glFrontFace (GL_CCW);
}
......@@ -1500,6 +1486,30 @@ void DrawGraphs (VisPersist * psVisData) {
}
}
void DrawGraphsShadow (VisPersist * psVisData) {
GSList * psFuncList;
glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
if (psVisData->boWireframe) {
// Turn on wireframe
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
}
// Render the graphs
psFuncList = psVisData->psFuncList;
while (psFuncList) {
DrawGraphShadow ((FuncPersist *)(psFuncList->data));
psFuncList = g_slist_next (psFuncList);
}
if (psVisData->boWireframe) {
// Turn off wireframe
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
}
}
void DrawTextOverlay (VisPersist * psVisData) {
GString * szString;
//int nStringWidth;
......@@ -1742,7 +1752,7 @@ void Redraw (VisPersist * psVisData) {
RenderFramebufferToScreen (psVisData);
// Render the debug texture overlay to the screen
RenderDebugOverlay (psVisData);
//RenderDebugOverlay (psVisData);
glFlush ();
//glutSwapBuffers();
......
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