Commit 384783a1 authored by David Llewellyn-Jones's avatar David Llewellyn-Jones

Added shadow map rendering with debug view.

parent b7ecf61a
gladedir = $(datadir)/@PACKAGE@
glade_DATA = Functy.glade cartesian.vs cartesian.fs cartesian-shadow.vs cartesian-shadow.fs spherical.vs spherical.fs spherical-shadow.vs spherical-shadow.fs curve.vs curve.fs curve-shadow.vs curve-shadow.fs screen.fs screen.vs
glade_DATA = Functy.glade cartesian.vs cartesian.fs cartesian-shadow.vs cartesian-shadow.fs spherical.vs spherical.fs spherical-shadow.vs spherical-shadow.fs curve.vs curve.fs curve-shadow.vs curve-shadow.fs screen.fs screen.vs depth.fs depth.vs
EXTRA_DIST = $(glade_DATA)
#version 120
uniform vec3 vPosition, vScale;
uniform float fTime;
uniform sampler2D framebufferTexture;
uniform sampler2D framebufferDepth;
void main () {
gl_FragColor = 1.0 - log (7.0 - (6.0 * texture2D (framebufferDepth, gl_TexCoord[0].st))) / 1.0;
}
#version 120
uniform vec3 vPosition, vScale;
uniform float fTime;
void main() {
// Specify position of vertex
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
}
......@@ -165,6 +165,7 @@ struct _VisPersist {
GLuint uShadowBuffer;
GLuint auFrameTextures[FRAMEBUFFER_TEXTURES];
ShaderPersist * psScreenShader;
ShaderPersist * psDebugDepthShader;
// Shadow variables
ShadowPersist * psShadowData;
......@@ -343,6 +344,7 @@ VisPersist * NewVisPersist (void) {
psVisData->auFrameTextures[nFramebuffer] = 0u;
}
psVisData->psScreenShader = NULL;
psVisData->psDebugDepthShader = NULL;
// Shadow data
psVisData->psShadowData = NewShadowPersist ();
......@@ -369,6 +371,12 @@ void DeleteVisPersist (VisPersist * psVisData) {
psVisData->psScreenShader = NULL;
}
// Free the debug shader if there is one
if (psVisData->psDebugDepthShader != NULL) {
DeleteShaderPersist (psVisData->psDebugDepthShader);
psVisData->psDebugDepthShader = NULL;
}
// Free the shadow data
DeleteShadowPersist (psVisData->psShadowData);
psVisData->psShadowData = NULL;
......@@ -644,6 +652,7 @@ void RenderFramebufferToScreen (VisPersist * psVisData) {
}
void RenderDebugOverlay (VisPersist * psVisData) {
GLuint uShaderProgram;
float fScale = 0.5f;
GLfloat afVertices[(4 * 3)] = {0.5f, 0.0f, 0.0f, 0.5f, 0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f};
static const GLfloat afTexCoords[(4 * 2)] = {1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f};
......@@ -663,17 +672,23 @@ void RenderDebugOverlay (VisPersist * psVisData) {
glPushMatrix ();
glLoadIdentity ();
ActivateShader (psVisData->psDebugDepthShader);
uShaderProgram = GetShaderProgram (psVisData->psDebugDepthShader);
glDisable (GL_DEPTH_TEST);
glEnable (GL_TEXTURE_2D);
glActiveTexture (GL_TEXTURE0);
glBindTexture (GL_TEXTURE_2D, GetShadowTexture (psVisData->psShadowData));
//glBindTexture (GL_TEXTURE_2D, psVisData->auFrameTextures[2]);
glUniform1i (glGetUniformLocation (uShaderProgram, "framebufferDepth" ), 0);
// Render the box
glColor4f (1.0, 1.0, 1.0, 1.0);
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_TEXTURE_COORD_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, afVertices);
glTexCoordPointer (2, GL_FLOAT, 0, afTexCoords);
glDrawElements (GL_TRIANGLES, (3 * 2), GL_UNSIGNED_BYTE, auIndices);
......@@ -683,6 +698,11 @@ void RenderDebugOverlay (VisPersist * psVisData) {
glDisable (GL_TEXTURE_2D);
glEnable (GL_DEPTH_TEST);
DeactivateShader (psVisData->psDebugDepthShader);
glDisableClientState (GL_VERTEX_ARRAY);
glDisableClientState (GL_TEXTURE_COORD_ARRAY);
// Reset the co-ordinate system back
glPopMatrix ();
glMatrixMode (GL_PROJECTION);
......@@ -709,7 +729,20 @@ void Init (VisPersist * psVisData) {
ShaderRegenerateVertex (szShaderVertexSource, psVisData->psScreenShader);
ShaderRegenerateFragment (szShaderFragmentSource, psVisData->psScreenShader);
// Free up any previously loaded shader source
// Free up the previously loaded shader source
g_free (szShaderVertexSource);
g_free (szShaderFragmentSource);
// Initialise the debug depth shader
psVisData->psDebugDepthShader = NewShaderPersist ();
szShaderVertexSource = LoadShaderFile (FUNCTYDIR "/depth.vs");
szShaderFragmentSource = LoadShaderFile (FUNCTYDIR "/depth.fs");
// Generate the debug shaders
ShaderRegenerateVertex (szShaderVertexSource, psVisData->psDebugDepthShader);
ShaderRegenerateFragment (szShaderFragmentSource, psVisData->psDebugDepthShader);
// Free up the previously loaded debug shader source
g_free (szShaderVertexSource);
g_free (szShaderFragmentSource);
......@@ -893,13 +926,16 @@ void SetLightTransformMatrix (VisPersist * psVisData) {
//afMVPShadowInvert
}
#define SHADOWSHIFT_UP (2.0f)
#define SHADOWSHIFT_SIDEWAYS (SHADOWSHIFT_UP)
void CalculateLightPos (VisPersist * psVisData) {
Vector3 vSideways;
vSideways = CrossProduct (& psVisData->vCamera, & psVisData->vUp);
psVisData->vLightPos.fX = (psVisData->fViewRadius) * psVisData->vCamera.fX + (psVisData->vUp.fX * 2.0f) - (vSideways.fX * 2.0f);
psVisData->vLightPos.fY = (psVisData->fViewRadius) * psVisData->vCamera.fY + (psVisData->vUp.fY * 2.0f) - (vSideways.fY * 2.0f);
psVisData->vLightPos.fZ = (psVisData->fViewRadius) * psVisData->vCamera.fZ + (psVisData->vUp.fZ * 2.0f) - (vSideways.fZ * 2.0f);
psVisData->vLightPos.fX = (psVisData->fViewRadius) * psVisData->vCamera.fX + (psVisData->vUp.fX * SHADOWSHIFT_UP) - (vSideways.fX * SHADOWSHIFT_SIDEWAYS);
psVisData->vLightPos.fY = (psVisData->fViewRadius) * psVisData->vCamera.fY + (psVisData->vUp.fY * SHADOWSHIFT_UP) - (vSideways.fY * SHADOWSHIFT_SIDEWAYS);
psVisData->vLightPos.fZ = (psVisData->fViewRadius) * psVisData->vCamera.fZ + (psVisData->vUp.fZ * SHADOWSHIFT_UP) - (vSideways.fZ * SHADOWSHIFT_SIDEWAYS);
//psVisData->vLightPos.fX = (psVisData->fViewRadius) * psVisData->vCamera.fX;
//psVisData->vLightPos.fY = (psVisData->fViewRadius) * psVisData->vCamera.fY;
......@@ -915,6 +951,10 @@ void RenderShadow (VisPersist * psVisData) {
glLoadIdentity ();
gluLookAt (psVisData->vLightPos.fX, psVisData->vLightPos.fY, psVisData->vLightPos.fZ, 0.0, 0.0, 0.0, psVisData->vUp.fX, psVisData->vUp.fY, psVisData->vUp.fZ);
//gluLookAt ((psVisData->fViewRadius) * psVisData->vCamera.fX, (psVisData->fViewRadius) * psVisData->vCamera.fY, (psVisData->fViewRadius) * psVisData->vCamera.fZ, 0.0, 0.0, 0.0, psVisData->vUp.fX, psVisData->vUp.fY, psVisData->vUp.fZ);
glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* pmLightTransform = GetLightTransform (psVisData->psShadowData);*/
......@@ -927,6 +967,9 @@ void RenderShadow (VisPersist * psVisData) {
/* }*/
/* }*/
SetLightTransformMatrix (psVisData);
//glGetDoublev (GL_MODELVIEW_MATRIX, psVisData->afModel);
//glGetDoublev (GL_PROJECTION_MATRIX, psVisData->afProjection);
//glGetIntegerv (GL_VIEWPORT, psVisData->anViewPort);
// Draw the graph axes
if (psVisData->boDrawAxes) {
......@@ -935,10 +978,10 @@ void RenderShadow (VisPersist * psVisData) {
// Draw the graph
glDisable (GL_CULL_FACE);
glFrontFace (GL_CW);
//glFrontFace (GL_CW);
DrawGraphs (psVisData);
glEnable (GL_CULL_FACE);
glFrontFace (GL_CCW);
//glFrontFace (GL_CCW);
}
void DrawAxes (VisPersist * psVisData) {
......@@ -1802,7 +1845,7 @@ void Reshape (int nWidth, int nHeight, VisPersist * psVisData) {
void Redraw (VisPersist * psVisData) {
// Render to the shadow buffer
//RenderToShadowBuffer (psVisData);
RenderToShadowBuffer (psVisData);
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix ();
......@@ -1835,7 +1878,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