Dual Paraboloid Shadow Mapping

Hello folks,

I’ve been trying to solve my problem for weeks without success, and now I hope somebody who has experience with dual paraboloid shadow mapping could give me advice. Let’s go straight to the problem and inspect some pictures (picture 1):

http://imgur.com/dHtBsVP

Picture 1 contains some colored arrows I need to explain. Red arrows show the only correct shadows we should see. Other arrows indicate shadowing errors: yellow arrous shows spots caused by tesselation, perhaps blue arrows also, but their location is on the border of front/back hemispheres. And green arrow points to the sawtooth pattern which should’t exist (it is absent in examples we see lately). Now it is important to notice that the picture above was computed with the following code line (code 1):

"swap.z=-sign(swap.z)*swap.z;
" //mostly right results in the main project, but wrong way

This code line is located in GLSL shader program and it is one of the four candidates I’ve been trying without success in the main Project, from where the pictures are taken. However, code 4 does work in a separate test program, as we will see. Code 1 is actually completely wrong way to do DPSM, but it is the only way my main project gets shadowing. Next we look the same scene computed a little different, but still wrong code line (picture 2 and code 2):

http://imgur.com/oJWgwKi

"swap.z=sign(swap.z)*swap.z;
" //almost dark picture, wrong way

Again we look at the same scene, but now we use completely different, an orthodox code line (picture 3 and code 3):

http://imgur.com/Vr1YHc0

"swap.z=-(teDistance-n)/(f-n);
" //lightning is mainly working but no shadows, should be the right way

Finally we look the scene computed by a code line which works (almost) perfectly in examples we see lately (picture 4 and code 4):

http://imgur.com/CvEpYH6

"swap.z=(teDistance-n)/(f-n);
" //almost dark picture, doesn’t work in the main project, but works in the test program, right way

If someone suspects that artifacts seen pictures above are due to the phenomenom called “shadow acne”, well no, I think they’re not. Below is a picture which has shadow acne pattern I intentionally made by setting SHADOW_EPSILON = 0.000005f and switching off blurring (picture 5):

http://imgur.com/U8wXCVt

At this point I need to say, that I’ve run the program on two separate Windows 7.1 laptops, one with nVIDIA GeForce GT 525M and another AMD Radeon R6. The results were identical. Compiler is Visual Studio 2010. It seems to me that this is purely OpenGL related problem.

To solve the problem, I wrote a separate, small test program, and finally got shadow mapping working :smiley: As far as I can see, the test program works pretty much exactly as the program which made pictures 1-5, but there aren’t optimizations and a number of matrix multiplications has been moved from host to the shaders. The relevant parts of the test program source are below. Shaders first:


static const char *vertex1=
"#version 400 core
"

"layout (location=1) in vec3 vertexLocation;
"

"out vec3 vPosition;
"

"void main() {
"
    "vPosition=vertexLocation;
"
"}\0";

static const char *tessIn1=
"#version 400 core
"

"layout (vertices=3) out;
"

"in vec3 vPosition[];
"
"out vec3 tcPosition[];
"

"void main() {
"
    "tcPosition[gl_InvocationID]=vPosition[gl_InvocationID];
"
    "if (gl_InvocationID==0) {
"

        "gl_TessLevelOuter[0]=max(distance(vPosition[1], vPosition[2]), 1.0);
"
        "gl_TessLevelOuter[1]=max(distance(vPosition[2], vPosition[0]), 1.0);
"
        "gl_TessLevelOuter[2]=max(distance(vPosition[0], vPosition[1]), 1.0);
"
        "gl_TessLevelInner[0]=max(0.33*(gl_TessLevelOuter[0]+gl_TessLevelOuter[1]+gl_TessLevelOuter[2]), 1.0);
"

    "}
"
"}\0";


static const char* tessOut1=
"#version 400 core
"

"layout(triangles, equal_spacing, ccw) in;
"

"uniform mat4 model;
"
"uniform mat4 view;
"
"uniform mat4 lightOrientation;
"

"in vec3 tcPosition[];
"
"out float teDistance;
"
"out float teClip;
"

"const float n=0.5;
"
"const float f=20000.0;
"

"void main() {
"

    "vec3 accum=vec3(0.0);
"
    "accum=accum+gl_TessCoord.x*tcPosition[0];
"
    "accum=accum+gl_TessCoord.y*tcPosition[1];
"
    "accum=accum+gl_TessCoord.z*tcPosition[2];
"

// Transform position to the paraboloid's view space
    "vec4 swap=lightOrientation*model*vec4(accum, 1.0);
"

//store the distance and other variables
    "teDistance=abs(swap.z);
"
    "teClip=swap.z;
"

//calculate and set X and Y coordinates
    "swap.xyz=normalize(swap.xyz);
"
    "if (swap.z<=0.0) {
"
        "swap.xy=swap.xy/(1.0-swap.z);
"
    "} else {
"
        "swap.xy=swap.xy/(1.0+swap.z);
"
    "}
"

//calculate and set Z and W coordinates
//	"swap.z=-sign(swap.z)*swap.z;
"  //Wrong way
//	"swap.z=sign(swap.z)*swap.z;
"  //Wrong way
//	"swap.z=-(teDistance-n)/(f-n);
"      //Wrong way
    "swap.z=(teDistance-n)/(f-n);
"      //Right way
    "swap.w=1.0;
"

    "gl_Position=swap;
"
"}\0";


static const char* geometry1=
"#version 400 core
"

"layout(triangles) in;
"
"layout(triangle_strip, max_vertices=3) out;
"

"in float teDistance[];
"
"in float teClip[];
"
"out float gDistance;
"

"void main() {
"
    "for (int i=0; i<3; i++) {
"
        "gDistance=teDistance[i];
"
        "if (teClip[i]<=0.0) {
"
            "gl_Layer=0;
"
        "} else {
"
            "gl_Layer=1;
"
        "}
"
        "gl_Position=gl_in[i].gl_Position;
"
        "EmitVertex();
"
    "}
"
    "EndPrimitive();
"
"}\0";


static const char* fragment1=
"#version 400 core
"

"in float gDistance;
"

"out vec2 fragmentVari;
"

"void main() {
"

    "fragmentVari=vec2(gDistance, gDistance*gDistance);
"

"}\0";


const char *vertex2=
"#version 400 core
"

"layout (location=1) in vec3 vertexPosition;
"
"layout (location=2) in vec2 vertexTexCoord;
"
"layout (location=3) in vec3 vertexNormal;
"

"const float n=0.5;
"
"const float f=20000.0;
"

"uniform vec4 colour;
"
"uniform mat4 model;
"
"uniform mat4 view;
"
"uniform mat4 normal;
"
"uniform mat4 projection;
"
"uniform mat4 lightOrientation;
"

"out vec2 texKoord;
"
"out vec3 pointNormal;
"
"out vec3 point;
"
"out vec4 color;
"
"out vec4 vOriginPoint;
"

"void main() {
"
    "texKoord=vertexTexCoord;
"
    "pointNormal=normalize(vec3(normal*vec4(vertexNormal, 1.0)));
"
    "point=vec3(model*vec4(vertexPosition, 1.0));
"
    "color=colour;
"
    "vOriginPoint=vec4(vertexPosition, 1.0);
"
    "gl_Position=projection*view*model*vec4(vertexPosition, 1.0);
"
"}\0";


const char *fragment2=
"#version 400 core
"

"uniform sampler2DArray tex1;
"

"uniform vec4 colour;
"
"uniform mat4 model;
"
"uniform mat4 view;
"
"uniform mat4 normal;
"
"uniform mat4 projection;
"
"uniform mat4 lightOrientation;
"

"in vec2 texKoord;
"
"in vec3 pointNormal;
"
"in vec3 point;
"
"in vec4 color;
"
"in vec4 vOriginPoint;
"
"out vec4 fragmentColor;
"

"const float SHADOW_EPSILON = 0.05f;
"
"const vec3 Ka=vec3(0.05, 0.05, 0.05);
"  //Ambient reflectivity
"const vec3 Kd=vec3(1.0, 1.0, 1.0);
"  //Diffuse reflectivity
"const float At=0.4;
"                 //Light attenuation

"vec3 ads(in vec3 position, in vec3 normal) {
"
    "vec3 l=vec3(lightOrientation*model*vOriginPoint);
"
    "vec3 s=normalize(l - position);
"
    "vec3 intensity=vec3(0.5, 0.5, 0.5)*10.0;
"
    "float attenuation=1.0/(1.0+At*max(length(l), 1.0));
"
    "intensity=intensity*attenuation*Kd*abs(dot(s, normal));
"
    "return intensity;
"
"}
"

"float drawShadow() {
"
    "vec3 texKoord;
"
    "vec4 textureDepth;
"

    "vec4 originPoint=vec4(lightOrientation*model*vOriginPoint);
"
    "float distance=abs(originPoint.z);
"
    "vec3 normalized=normalize(originPoint.xyz);
"

    "if (normalized.z<=0.0) {
"
        "texKoord.xy=normalized.xy/(1.0-normalized.z);
"
        "texKoord.xy=0.5*texKoord.xy+0.5;
"
        "texKoord.z=0.0;
"
        "textureDepth=texture(tex1, texKoord);
"
    "} else {
"
        "texKoord.xy=normalized.xy/(1.0+normalized.z);
"
        "texKoord.xy=0.5*texKoord.xy+0.5;
"
        "texKoord.z=1.0;
"
        "textureDepth=texture(tex1, texKoord);
"
    "}
"

    "if (textureDepth.x+SHADOW_EPSILON>=distance) {
"
        "return 1.0;
"
    "} else {
"
        "return 0.0;
"
    "}
"
"}
"

"void main() {
"
    "vec4 lightning=vec4(Ka, 1.0);
"
    "vec4 swap2=vec4(ads(point, pointNormal), 0.0);
"
    "swap2=swap2*drawShadow();
"
    "lightning=lightning+swap2;
"
    "fragmentColor=color*lightning;
"
"}\0";


const char *vertexLight=
"#version 400 core
"

"layout (location=1) in vec3 vertexPosition;
"

"uniform mat4 view;
"
"uniform mat4 projection;
"
"uniform mat4 lightOrientation;
"

"void main() {
"
    "gl_Position=projection*view*lightOrientation*vec4(vertexPosition, 1.0);
"
"}\0";


const char *fragmentLight=
"#version 400 core
"

"out vec4 fragmentColor;
"

"void main() {
"
    "fragmentColor=vec4(1.0, 1.0, 1.0, 1.0);
"
"}\0";

And here is the Display function:


void TForm1::display()
{
    GLuint loc1, loc2, loc3, loc4, loc5, loc6;
    float swap[16];
    float normal[16]={0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};

//first we render a shadow map
    {
        glUseProgram(shaderT1);
        glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer[0]);
        glClearColor(20000.0f, 0.0f, 0.0f, 0.0f);
        glDepthMask(GL_TRUE);
        glDepthRange(0, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glViewport(0, 0, textureDim.x, textureDim.y);
        glPatchParameteri(GL_PATCH_VERTICES, 3);
        loc1=glGetUniformLocation(shaderT1, "model\0");
        loc2=glGetUniformLocation(shaderT1, "lightOrientation\0");
        loc3=glGetUniformLocation(shaderT1, "view\0");
        swap[0]=1.0; swap[1]=0.0; swap[2]=0.0; swap[3]=0.0;
        swap[4]=0.0; swap[5]=1.0; swap[6]=0.0; swap[7]=0.0;
        swap[8]=0.0; swap[9]=0.0; swap[10]=1.0; swap[11]=0.0;
        swap[12]=-lightMatrix[12]; swap[13]=-lightMatrix[13]; swap[14]=-lightMatrix[14]; swap[15]=1.0;
        glUniformMatrix4fv(loc1, 1, GL_FALSE, triangleMatrix);
        glUniformMatrix4fv(loc2, 1, GL_FALSE, swap);
        glUniformMatrix4fv(loc3, 1, GL_FALSE, view);
        glBindVertexArray(VAO[1]);
        glDrawArrays(GL_PATCHES, 0, 3);
        glUniformMatrix4fv(loc1, 1, GL_FALSE, identity);
        glUniformMatrix4fv(loc2, 1, GL_FALSE, swap);
        glUniformMatrix4fv(loc3, 1, GL_FALSE, view);
        glBindVertexArray(VAO[0]);
        glDrawArrays(GL_PATCHES, 0, 6);
    }

//then we render the world and make use of that rendered shadow map
    {
        glUseProgram(shaderT2);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glDepthMask(GL_TRUE);
        glDepthRange(0, 1);
        glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glUniform1i(glGetUniformLocation(shaderT2, "tex1"), 1);
        glActiveTexture(GL_TEXTURE0+1);
        glBindTexture(GL_TEXTURE_2D_ARRAY, texID[0]);
        glViewport(0, 0, 512, 512);
        loc1=glGetUniformLocation(shaderT2, "model\0");
        loc2=glGetUniformLocation(shaderT2, "view\0");
        loc3=glGetUniformLocation(shaderT2, "normal\0");
        loc4=glGetUniformLocation(shaderT2, "colour\0");
        loc5=glGetUniformLocation(shaderT2, "projection\0");
        loc6=glGetUniformLocation(shaderT2, "lightOrientation\0");

//render a rectangle where the shadow is drawn onto
        glUniformMatrix4fv(loc1, 1, GL_FALSE, identity);
        glUniformMatrix4fv(loc2, 1, GL_FALSE, view);
        matrixMultiply4D(swap, view, identity);
        inverseMatrix4D(swap, swap);
        transpose4D(normal, swap);
        glUniformMatrix4fv(loc3, 1, GL_FALSE, normal);
        glUniform4fv(loc4, 1, red);
        glUniformMatrix4fv(loc5, 1, GL_FALSE, projection);
        swap[0]=1.0; swap[1]=0.0; swap[2]=0.0; swap[3]=0.0;
        swap[4]=0.0; swap[5]=1.0; swap[6]=0.0; swap[7]=0.0;
        swap[8]=0.0; swap[9]=0.0; swap[10]=1.0; swap[11]=0.0;
        swap[12]=-lightMatrix[12]; swap[13]=-lightMatrix[13]; swap[14]=-lightMatrix[14]; swap[15]=1.0;
        glUniformMatrix4fv(loc6, 1, GL_FALSE, swap);
        glBindVertexArray(VAO[0]);
        glDrawArrays(GL_TRIANGLES, 0, 6);

//render the triangle which makes a shadow
        glUniformMatrix4fv(loc1, 1, GL_FALSE, triangleMatrix);
        glUniformMatrix4fv(loc2, 1, GL_FALSE, view);
        matrixMultiply4D(swap, view, triangleMatrix);
        inverseMatrix4D(swap, swap);
        transpose4D(normal, swap);
        glUniformMatrix4fv(loc3, 1, GL_FALSE, normal);
        glUniform4fv(loc4, 1, yellow);
        glUniformMatrix4fv(loc5, 1, GL_FALSE, projection);
        swap[0]=1.0; swap[1]=0.0; swap[2]=0.0; swap[3]=0.0;
        swap[4]=0.0; swap[5]=1.0; swap[6]=0.0; swap[7]=0.0;
        swap[8]=0.0; swap[9]=0.0; swap[10]=1.0; swap[11]=0.0;
        swap[12]=-lightMatrix[12]; swap[13]=-lightMatrix[13]; swap[14]=-lightMatrix[14]; swap[15]=1.0;
        glUniformMatrix4fv(loc6, 1, GL_FALSE, swap);
        glBindVertexArray(VAO[1]);
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }

//finally render a white triangle which represents a location of the light
    {
        glUseProgram(shaderT3);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);
        glDepthMask(GL_TRUE);
        glDepthRange(0, 1);
        glViewport(0, 0, 512, 512);
        loc1=glGetUniformLocation(shaderT3, "view\0");
        loc2=glGetUniformLocation(shaderT3, "projection\0");
        loc3=glGetUniformLocation(shaderT3, "lightOrientation\0");
        glUniformMatrix4fv(loc1, 1, GL_FALSE, view);
        glUniformMatrix4fv(loc2, 1, GL_FALSE, projection);
        glUniformMatrix4fv(loc3, 1, GL_FALSE, lightMatrix);
        glBindVertexArray(VAO[2]);
        glDrawArrays(GL_TRIANGLES, 0, 3);
    }

    glFinish();

//rotate a light on it's orbit
    matrixMultiply4D(lightMatrix, rotationMatrix, lightMatrix);

}

I have taken four movies representing the test program and code lines 1-4 above. Below is movie 1 done with code 1:

https://vid.me/1Inq

Movie 2 with code 2:

https://vid.me/nc89

Movie 3 with code 3:

https://vid.me/MuA2

Movie 4 with code 4:

https://vid.me/AaQK

You can see that Movie 4 is the only one where shadow mapping works (there aren’t a shadow in the border zone between the two hemispheres, but I can live with that. However, if someone knows how to fix it, I’d be happy if you tell me:) ) But it’s not working in the main project! I hope you could give me advice about what could be wrong, what should I check or give me your working dual paraboloid shadow mapping code samples…

Please help me!

Thank’s for reading and Merry Christmas and Happy New Year!

Nobody helping me either…

Luckily I found error by myself :biggrin-new: It was as simple as when renderin a shadow map I also rendered the white ball representing the light. No wonder the world was dark…

There is still a dark thin stripe between the hemispheres, but at least i’m moving ahead!