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

Changes to move towards full use of shaders with Functy.

parent 2c24742b
......@@ -22,98 +22,6 @@ int mod (int nDividend, int nDivisor) {
return nRemainder;
}
// Note: hue, saturation and range values all fall within the interval 0 to 1.
// Usually this is 0...360, 0...100 and 0...256 respectively.
vec3 RGBtoHSV (vec3 vColour) {
float fHue;
float fSaturation;
float fValue;
float fDiff;
float fMinCol;
float fMaxCol;
int nMinIndex;
int nMaxIndex;
nMinIndex = (vColour.r < vColour.g ? (vColour.b < vColour.r ? BLUE : RED) : (vColour.b < vColour.g ? BLUE : GREEN));
nMaxIndex = (vColour.r > vColour.g ? (vColour.b > vColour.r ? BLUE : RED) : (vColour.b > vColour.g ? BLUE : GREEN));
fMinCol = vColour[nMinIndex];
fMaxCol = vColour[nMaxIndex];
fDiff = fMaxCol - fMinCol;
// Calculate Hue
if (fDiff < fEpsilon){
fHue = 0.0;
}
else if (nMaxIndex == RED) {
fHue = ((1.0 / 6.0) * ((vColour.g - vColour.b) / fDiff)) + 1.0;
fHue = fract (fHue);
}
else if (nMaxIndex == GREEN) {
fHue = ((1.0 / 6.0) * ((vColour.b - vColour.r) / fDiff)) + (1.0 / 3.0);
}
else if (nMaxIndex == BLUE) {
fHue = ((1.0 / 6.0) * ((vColour.r - vColour.g) / fDiff)) + (2.0 / 3.0);
}
// Saturation
if (fMaxCol < fEpsilon) {
fSaturation = 0.0;
}
else {
fSaturation = fDiff / fMaxCol;
}
// Value
fValue = fMaxCol;
return vec3 (fHue, fSaturation, fValue);
}
vec3 HSVtoRGB (vec3 vColour) {
float fF;
float fP;
float fQ;
float fT;
int nSector;
float fSector;
float fHue;
float fSaturation;
float fValue;
vec3 vResult;
fHue = vColour.r;
fSaturation = vColour.g;
fValue = vColour.b;
fSector = floor (fHue * 6.0);
nSector = mod (int (fSector), 6);
fF = (fHue * 6.0) - fSector;
fP = fValue * (1.0 - fSaturation);
fQ = fValue * (1.0 - (fF * fSaturation));
fT = fValue * (1.0 - ((1.0 - fF) * fSaturation));
if (nSector == 0) {
vResult = vec3 (fValue, fT, fP);
}
else if (nSector == 1) {
vResult = vec3 (fQ, fValue, fP);
}
else if (nSector == 2) {
vResult = vec3 (fP, fValue, fT);
}
else if (nSector == 3) {
vResult = vec3 (fP, fQ, fValue);
}
else if (nSector == 4) {
vResult = vec3 (fT, fP, fValue);
}
else {
vResult = vec3 (fValue, fP, fQ);
}
return vResult;
}
void main () {
vec3 vHalf;
vec3 vView;
......@@ -126,26 +34,28 @@ void main () {
float fSteps = 2.0;
vec3 vRGB;
vec3 vHSV;
vec3 vNormalBidir;
if (gl_FrontFacing) {
vNormalBidir = vNormal;
}
else {
vNormalBidir = -vNormal;
}
// Calculate dot product between normal and light direction
fNDotL = max (dot (vNormal, normalize (vDir)), 0.0);
fNDotL = max (dot (vNormalBidir, normalize (vDir)), 0.0);
if (fNDotL > 0.0) {
fAttenuation = 1.0 / (gl_LightSource[0].constantAttenuation + gl_LightSource[0].linearAttenuation * fDist + gl_LightSource[0].quadraticAttenuation * fDist * fDist);
vColour += fAttenuation * (vDiffuse * fNDotL + vAmbient);
vHalf = normalize (vHalfVector);
fNDotHV = max (dot (vNormal, vHalf), 0.0);
fNDotHV = max (dot (vNormalBidir, vHalf), 0.0);
vColour += fAttenuation * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow (fNDotHV, gl_FrontMaterial.shininess);
vRGB = vColour.rgb;
vHSV = RGBtoHSV (vRGB);
vHSV[0] = floor (vHSV[0] * fHueSteps) / fHueSteps;
vHSV[1] = floor (vHSV[1] * fSteps) / fSteps;
vHSV[2] = floor (vHSV[2] * fSteps) / fSteps;
vRGB = HSVtoRGB (vHSV);
vColour.rgb = vRGB;
}
gl_FragColor = vColour;
gl_FragColor.a = vDiffuse.a;
}
......@@ -7,12 +7,19 @@ varying float fDist;
void main() {
vec4 vEyeCoordsPos;
vec3 vPos;
vec4 vVertex;
// Calculate new vertex position
vVertex = gl_Vertex;
//vVertex.z += sin (vVertex.x);
// Transform normal to eye space
vNormal = normalize (gl_NormalMatrix * gl_Normal);
vNormal = gl_Normal;
//vNormal.x += sin (vVertex.x);
vNormal = normalize (gl_NormalMatrix * vNormal);
// Normalise light direction, which is stored in eye space
vEyeCoordsPos = gl_ModelViewMatrix * gl_Vertex;
vEyeCoordsPos = gl_ModelViewMatrix * vVertex;
vPos = vec3 (gl_LightSource[0].position - vEyeCoordsPos);
vDir = normalize (vPos);
......@@ -23,13 +30,14 @@ void main() {
vHalfVector = normalize (gl_LightSource[0].halfVector.xyz);
// Calculate diffuse, ambient and global values
//vDiffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * gl_Color;
//vAmbient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient * gl_Color;
//vGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient * gl_Color;
vDiffuse = gl_LightSource[0].diffuse * gl_Color;
vAmbient = gl_LightSource[0].ambient * gl_Color;
vGlobal = gl_LightModel.ambient * gl_Color;
vDiffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse * gl_Color;
vAmbient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient * gl_Color;
vGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient * gl_Color;
//vDiffuse = gl_LightSource[0].diffuse * gl_Color;
//vAmbient = gl_LightSource[0].ambient * gl_Color;
//vGlobal = gl_LightModel.ambient * gl_Color;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
//gl_FrontColor = gl_Color;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vVertex;
}
......@@ -144,6 +144,7 @@ struct _VisPersist {
float fYMomentumFunction;
float fZMomentumFunction;
bool boRangeChange;
bool boShader;
GLint anViewPort[4];
GLdouble afModel[16];
GLdouble afProjection[16];
......@@ -200,6 +201,7 @@ void SetFunctionPositionCallback (gpointer data, gpointer user_data);
void CalculateGridScale (VisPersist * psVisData);
void RecentreGraph (FuncPersist * psFuncData);
void InitShaders (VisPersist * psVisData);
void ToggleShader (VisPersist * psVisData);
///////////////////////////////////////////////////////////////////
// Function definitions
......@@ -271,6 +273,7 @@ VisPersist * NewVisPersist (void) {
psVisData->fYMomentumFunction = 0.0f;
psVisData->fZMomentumFunction = 0.0f;
psVisData->boRangeChange = FALSE;
psVisData->boShader = TRUE;
// Make sure the function list is empty
psVisData->psFuncList = NULL;
......@@ -417,7 +420,6 @@ void InitShaders (VisPersist * psVisData) {
LoadVertexShader (FUNCTYDIR "/vertex.vs", psVisData->psShaderData);
LoadFragmentShader (FUNCTYDIR "/fragment.fs", psVisData->psShaderData);
//ActivateShader (psVisData->psShaderData);
}
void Spin (VisPersist * psVisData) {
......@@ -1047,6 +1049,10 @@ void DrawGraphs (VisPersist * psVisData) {
glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
}
if (psVisData->boShader) {
ActivateShader (psVisData->psShaderData);
}
// Render the graphs
psFuncList = psVisData->psFuncList;
while (psFuncList) {
......@@ -1055,6 +1061,10 @@ void DrawGraphs (VisPersist * psVisData) {
psFuncList = g_slist_next (psFuncList);
}
if (psVisData->boShader) {
DeactivateShader (psVisData->psShaderData);
}
if (psVisData->boWireframe) {
// Turn off wireframe
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
......@@ -1586,6 +1596,17 @@ void ToggleSpin (VisPersist * psVisData) {
psVisData->boSpin = !psVisData->boSpin;
}
void ToggleShader (VisPersist * psVisData) {
psVisData->boShader = !psVisData->boShader;
if (psVisData->boShader) {
printf ("Shader: on\n");
}
else {
printf ("Shader: off\n");
}
}
void ToggleWireframe (VisPersist * psVisData) {
psVisData->boWireframe = !psVisData->boWireframe;
}
......@@ -1673,6 +1694,7 @@ void Key (unsigned int key, int x, int y, unsigned int uKeyModifiers, VisPersist
case 'm':
break;
case 's':
ToggleShader (psVisData);
break;
case GDK_Up:
break;
......
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