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.
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.
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());
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.
Render light spheres again now with the following states.