I am trying to make a planar reflection in OpenGL.
I have set the stencil test and projecting the reflection on a mirror object, but the reflection is rendered above all other objects and floor in the scene (like on images). And the second problem is that the background color of the reflection is not rendered at all.
Most likely I have some problem with the depth testing, but I am not sure where is it and how to fix it.
So the questions are:
1) Where is the reflection depth testing problem?
2) Why is the black background not reflected at all?
Thank you!
Here is the part of the code where I render both the original scene and the reflected one and use the stencil test.
void render_example_scene() // Renders the example scene.
{
// Render the final scene into the main window
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, win_width, win_height);
// Clear the main window
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Enable the depth test for scene rendering
glEnable(GL_DEPTH_TEST);
render_reflection(); //Rendering reflection
// Set the data of the camera and the lights
CameraData_ubo.BindBuffer(DEFAULT_CAMERA_BINDING);
PhongLights_ubo.BindBuffer(DEFAULT_LIGHTS_BINDING);
// Renders the randomly generated objects and floor.
render_random_objects_and_floor();
// Renders the golem.
render_scene_object(golem_scene_object);
// Renders the mirror.
render_mirror();
}
void render_mirrored_example_scene()
{
//// Render the final scene into the main window
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, win_width, win_height);
//// Clear the main window
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//// Enable the depth test for scene rendering
glEnable(GL_DEPTH_TEST);
// Set the data of the camera and the lights
MirroredCameraData_ubo.BindBuffer(DEFAULT_CAMERA_BINDING);
PhongLights_ubo.BindBuffer(DEFAULT_LIGHTS_BINDING);
// Renders the randomly generated objects and floor.
render_random_objects_and_floor();
// Renders the golem.
render_scene_object(golem_scene_object);
}
/// Renders the mirror.
void render_mirror()
{
render_scene_object(mirror_frame_scene_object);
render_scene_object(mirror_scene_object);
}
void render_reflection()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glDisable(GL_DEPTH_TEST);
render_scene_object(mirror_scene_object); //filling stencil with mirror object
glEnable(GL_DEPTH_TEST);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDepthMask(GL_TRUE);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilFunc(GL_EQUAL, 1, 1);
render_mirrored_example_scene(); //rendering reflected scene
glDisable(GL_STENCIL_TEST);
}
Aucun commentaire:
Enregistrer un commentaire