PDA

View Full Version : Light Volume Stenciling not clearing



Hackerjoe
08-17-2015, 10:47 PM
Hey, I am new to these forums so I don't know if this is the correct place to post this. I kind of got my light volume stenciling for my deferred renderer working but when i move the camera the stencil doesnt seem to update when the camera moves.

Here is the first frame.
2048



Then the camera moves and the stencil does not seem to update.
2049

Here is my gBuffer.


glGenFramebuffers(1, &gBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, gBuffer);

// - Position color buffer
glGenTextures(1, &gPosition);
glBindTexture(GL_TEXTURE_2D, gPosition);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, ScreenWidth, ScreenHeight, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gPosition, 0);
// - Normal color buffer
glGenTextures(1, &gNormal);
glBindTexture(GL_TEXTURE_2D, gNormal);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, ScreenWidth, ScreenHeight, 0, GL_RGB, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, gNormal, 0);
// - Color + Specular color buffer
glGenTextures(1, &gAlbedoSpec);
glBindTexture(GL_TEXTURE_2D, gAlbedoSpec);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ScreenWidth, ScreenHeight, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, gAlbedoSpec, 0);

glGenTextures(1, &FinalTexture);
glBindTexture(GL_TEXTURE_2D, FinalTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ScreenWidth, ScreenHeight, 0, GL_RGBA, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, FinalTexture, 0);

// - Create and attach depth buffer (renderbuffer)
GLuint rboDepth;
glGenRenderbuffers(1, &rboDepth);
glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH32F_STENCIL8, ScreenWidth, ScreenHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
// - Finally check if framebuffer is complete
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "Framebuffer not complete!" << std::endl;
glBindFramebuffer(GL_FRAMEBUFFER, 0);


Here is my render loop.


testx += 0.001;
view = glm::lookAt(glm::vec3(0, testx, 4), glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 1.0f, 0.0f));

glEnable(GL_STENCIL_TEST);

GeoPass();

StencilPass();

LightPass();

glDisable(GL_STENCIL_TEST);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, gBuffer);
glReadBuffer(GL_COLOR_ATTACHMENT3);

glBlitFramebuffer(0, 0, ScreenWidth, ScreenHeight,
0, 0, ScreenWidth, ScreenHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR);

glfwSwapInterval(0);
glfwSwapBuffers(window);



Here is my geometry pass.


//Bind gBuffer
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, gBuffer);
glDrawBuffer(GL_COLOR_ATTACHMENT3);
glClear(GL_COLOR_BUFFER_BIT);

GLuint attachments[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, attachments);

glDepthMask(GL_TRUE);
// Clear Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);


//Use ShaderGeo shader to draw into gBuffer
shaderGeometryPass->UseShader();

// Create Model 4x4Matix for nanosuit model
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, -1.75, 0.0f)); // Translate it down a bit so it's at the center of the scene
model = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f)); // It's a bit too big for our scene, so scale it down

// Insert values into shader progam
glUniformMatrix4fv(glGetUniformLocation(shaderGeom etryPass->GetShader(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderGeom etryPass->GetShader(), "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shaderGeom etryPass->GetShader(), "model"), 1, GL_FALSE, glm::value_ptr(model));


//Draw into model into gBuffer
Nanosuit->Draw(*shaderGeometryPass);



glDepthMask(GL_FALSE);



Here is where the stenciling happens.



glDrawBuffer(GL_NONE);

glEnable(GL_DEPTH_TEST);

glDisable(GL_CULL_FACE);

glClear(GL_STENCIL_BUFFER_BIT);

// We need the stencil test to be enabled but we want it
// to succeed always. Only the depth test matters.
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);


glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, -1.75, 0.0f)); // Translate it down a bit so it's at the center of the scene
model = glm::scale(model, glm::vec3(4.0f, 4.0f, 4.0f)); // It's a bit too big for our scene, so scale it down

shader->UseShader();

// Insert values into shader progam
glUniformMatrix4fv(glGetUniformLocation(shader->GetShader(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shader->GetShader(), "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader->GetShader(), "model"), 1, GL_FALSE, glm::value_ptr(model));

SphereModel->Draw(*shader);


Then my light pass.


glDrawBuffer(GL_COLOR_ATTACHMENT3);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gPosition);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, gNormal);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, gAlbedoSpec);

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);

glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f, -1.75, 0.0f)); // Translate it down a bit so it's at the center of the scene
model = glm::scale(model, glm::vec3(4.0f, 4.0f, 4.0f)); // It's a bit too big for our scene, so scale it down

shaderLightingPass->UseShader();

// Insert values into shader progam
glUniformMatrix4fv(glGetUniformLocation(shaderLigh tingPass->GetShader(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderLigh tingPass->GetShader(), "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shaderLigh tingPass->GetShader(), "model"), 1, GL_FALSE, glm::value_ptr(model));

SphereModel->Draw(*shaderLightingPass);

glCullFace(GL_BACK);

glDisable(GL_BLEND);



Sorry that it is so long. Thank you so much for looking. If I am in the wrong place please tell me.

Hackerjoe
08-17-2015, 11:35 PM
If I'm in the wrong place tell me.

Hackerjoe
08-19-2015, 12:15 PM
If someone has the same problem I fixed it. Make sure in your geometry pass you are also always allowing everything to be written.

With this.

glStencilFunc(GL_ALWAYS, 0, 0x00);