# Handling light bounding volume

So, i managed to do deferred shading like yesterday or 2 days ago. I got pretty big impact which causes my fps to drop from 1000fps to around 300~fps for 25 lights (after some optimization, i don't know if this is even normal lol).

Then i read about this bounding volume, so then i implemented this with stencil cubes and it is capable of bumping my fps to 400~fps. The problem is whenever i enter those bounding boxes, lighting for fragments inside the box is now gone. Like so:

I resize the bounding box to half of the size for testing. And i already disable GL_CULL_FACE when rendering those bounding volume cubes. Here is how i handle the rendering:

glEnable(GL_STENCIL_TEST);
glDisable(GL_CULL_FACE);
for (int i = 0; i < sizeof(lights) / sizeof(Light); i++) {
glStencilFunc(GL_ALWAYS, 1, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glClear(GL_STENCIL_BUFFER_BIT);

// Bind and unbind vao & shaders here for stencil

// Setting up matrices and uniforms for bounding volume cube
glDrawArrays(GL_TRIANGLES, 0, 36);

// Bind and unbind vao & shaders here for lighting

glStencilFunc(GL_EQUAL, 1, 0xFF);

// Setting up uniforms for light's properties
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
glEnable(GL_CULL_FACE);
glDisable(GL_STENCIL_TEST);


Though i don't think the code i supply above is where the problem lies. I think it is because the stencil only draws where the cubes is (the cubes walls) and not the volume inside of it.

Is there a way so that the stencil draws the volume too? Or perhaps a trick so that the lighting is still occur when i'm inside the bounding volumes?

Edit: I thought of checking cam position if it is inside those cubes, if yes then don't use bounding volume for that particular light. But i don't think that's the best solution here..

Okay, after some hours of research, i finally ran into this site which work exactly how i wanted.

The techique works as follows:

1. Render the objects as usual into the G buffer so that the depth buffer will be properly populated.
2. Disable writing into the depth buffer. From now on we want it to be read-only
3. Disable back face culling. We want the rasterizer to process all polygons of the sphere.
4. Set the stencil test to always succeed. What we really care about is the stencil operation.
5. Configure the stencil operation for the back facing polygons to increment the value in the stencil buffer when the depth test fails but to keep it unchanged when either depth test or stencil test succeed.
6. Configure the stencil operation for the front facing polygons to decrement the value in the stencil buffer when the depth test fails but to keep it unchanged when either depth test or stencil test succeed.
7. Render the light sphere.

Though, this actually lower my fps a little somehow.. I got 100-200~fps for 125 lights now and 300~fps for 25 lights..