Intersection detection with stencil buffer

356 views Asked by At

In order to implement deferred lightning, I render light sources as a sphere with a radius of light.

I rendered only actual positions of light to screen. The result is below. enter image description here

so I thought maybe if I can render the intersection of this spheres then I can get rid of for loop for each light in the deferred shader.

by using the following document in the link https://kayru.org/articles/deferred-stencil/ I implemented the intersection of light spheres with my actual scene, and I save it to the texture below.

enter image description here

The problem is the result is a bit different than what I expect. Intersection also includes other spheres. Light volumes should not intersect with each other. How can I implement a proper intersection method?

    int k = 0;
    for(GLXlight & glxlightdata : entitySystem->glxlights){
        lBufferShader->uniform4f(lBufferShader->lightPos[k], &glxlightdata.lightPos[0]);
        k++;
    }
    lBufferShader->uniformMatrix4(lBufferShader->ViewMatrix, &entitySystem->view[0][0]); //ViewMatrix
    lBufferShader->uniform3f(lBufferShader->viewPos, &(*entitySystem->viewpos)[0]); //viewPos
    lBufferShader->uniformMatrix4(lBufferShader->ProjectionMatrix, &entitySystem->projection[0][0]); //ProjectionMatrix
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, lightObject->glxobject.elementBufferID);
        glCullFace(GL_BACK);                                    //Front (near) faces only
        glColorMask(GL_FALSE , GL_FALSE , GL_FALSE , GL_FALSE); //Colour write is disabled
        glDepthMask(GL_FALSE);                                  //Z-write is disabled
        glDepthFunc(GL_LEQUAL);                                 //Z function is 'Less/Equal'//
        glStencilFunc(GL_ALWAYS, 0, 0x00);                      //Stencil test result does not modify Stencil buffer
        glStencilMask(0xFF);
        glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);                 //Z-Fail writes non-zero value to Stencil buffer 
    glDrawElementsInstanced(GL_TRIANGLES, lightObject->glxobject.size, GL_UNSIGNED_SHORT, (void *) 0, lightObject->glxinstances.size());
        glCullFace(GL_FRONT);                                   //Back (far) faces only
        glColorMask(GL_TRUE , GL_TRUE , GL_TRUE , GL_TRUE);     //Colour write is enabled
        glDepthMask(GL_FALSE);                                  //Z-write is disabled
        glDepthFunc(GL_GEQUAL);                                 //Z function is 'Greater/Equal'
        glStencilFunc(GL_EQUAL, 0, 0xFF);                       //Stencil test result does not modify Stencil buffer
        glStencilMask(0xFF);
        glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP);                 //Z-Fail writes non-zero value to Stencil buffer 
    glDrawElementsInstanced(GL_TRIANGLES, lightObject->glxobject.size, GL_UNSIGNED_SHORT, (void *) 0, lightObject->glxinstances.size());
1

There are 1 answers

0
aramok On BEST ANSWER

I solved this problem. After rendering geometry to buffer, instead of drawing quad to screen, I rendered spheres in the following.

First, render light spheres in the null pass with the following states.

    glEnable(GL_STENCIL_TEST);
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_FALSE);
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    glClear(GL_STENCIL_BUFFER_BIT);
    glStencilFunc(GL_ALWAYS, 0, 0);
    glStencilOpSeparate(GL_BACK, GL_KEEP, GL_INCR_WRAP, GL_KEEP);
    glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR_WRAP, GL_KEEP);
    glDepthMask(GL_FALSE);//disable writing to depth buffer
    glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);//!! disable writing to color buffer

Render light spheres again now with the following states.

    glStencilFunc(GL_NOTEQUAL, 0, 0xFF);
    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);
    glBlendEquation(GL_FUNC_ADD);
    glBlendFunc(GL_ONE, GL_ONE);
    glEnable(GL_CULL_FACE);
    glCullFace(GL_FRONT);
    glDepthMask(GL_TRUE);
    glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);