I want to make a scene with portals and my apprach is the following:
(my problem involves only the view from the entry portal)
1) i am drawing the scene but not the portal. (without writing into the stencil buffer at all)
2) then draw the portal while also writing to the stencil buffer a value of 2.
3) drawing the scene again on the portal where the stencil has a value of 2. (like step 1 but with the view matrix set to point to another place e.g. in front of the exit portal
The portal also updates on every movement of the light that happens at the original scene, since i have it rendered again isnide the portal
**The problem that i cant seem to solve is how can i have the contents of the portal (the view) , be in a fixed position regardless of where i point my camera and when i move left and right. **
I want to get the same view through the portal regardless if
i point my camera to the left or right (mouse movement) .
And also regardless of pressing the A or D button (the rendered scene inside the portal translates along with the camera, im assuming its because the portal is rendered with the dynamic camera
viewMatrix)
The gif below shows everything i dont want to happen. I want the exact oposite. I want regardless of the camera movement, the view from the portal to be fixed .
My code looks like this : ( viewMatrix and projectionMatrix belong to the camera)
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, W_WIDTH, W_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT| GL_STENCIL_BUFFER_BIT );
**glUseProgram(shaderProgram);**
//shadows
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &**viewMatrix**[0][0]);
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &**projectionMatrix**[0][0]);
uploadLight(*light);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glUniform1i(depthMapSampler, 0);
mat4 light_vp = light->lightVP();
glUniformMatrix4fv(lightVPLocation, 1, GL_FALSE, &light_vp[0][0]);
//DRAWING SCENE WITH DYNAMIC CAMERA VIEW
glStencilMask(0x00);
mat4 scale = glm::scale(mat4(), vec3(0.2f, 0.2f, 0.2f));
mat4 model_matrix3 = translate(mat4(), light->lightPosition_worldspace) * scale;
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &model_matrix3[0][0]);
uploadMaterial(polishedSilver);
glUniform1i(useTextureLocation, 0);
model3->bind();
model3->draw();
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
uploadMaterial(turquoise);
glUniform1i(useTextureLocation, 0);
float size = 15.0f;
mat4 planemodel = glm::translate(mat4(), vec3(size / 2, 0, size / 2)) *
glm::scale(mat4(), vec3(size, 1, size));
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &planemodel[0][0]);
plane1->bind();
plane1->draw();
glStencilMask(0x00);// disable writing to the stencil buffer
uploadMaterial(turquoise);
glEnable(GL_DEPTH_TEST);
glUniform1i(useTextureLocation, 0);
planemodel = glm::translate(mat4(), vec3(size, size / 3.4f, size / 2)) *
glm::rotate(mat4(), 3.14f / 2.0f, vec3(0, 0, 1)) * glm::scale(mat4(), vec3(size / 1.7f, 1, size));
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &planemodel[0][0]);
plane2->bind();
plane2->draw();
glStencilMask(0x00);
uploadMaterial(polishedSilver);
glUniform1i(useTextureLocation, 0);
mat4 spheremodel = glm::translate(mat4(), vec3(size / 1.5f, size / 10, size / 3))*
glm::scale(mat4(), vec3(0.2f, 0.2f, 0.2f));;
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &spheremodel[0][0]);
model2->bind();
model2->draw();
//DRAWING THE PORTAL AND SETTING THE STENCIL BUFFER
mat4 planemodel2 = glm::translate(mat4(), vec3(size / 2.6f, size / 6, size / 1.2)) *
glm::rotate(mat4(), 3.14f / 2.0f, vec3(0, 0, 1)) *
glm::scale(mat4(), vec3(size / 5, 1, size / 4));
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glStencilFunc(GL_ALWAYS, 2, 0xFF);
glStencilMask(0xFF);
glDisable(GL_DEPTH_TEST);
**glUseProgram(stencp);**
glUniformMatrix4fv(viewMatrixLocation2, 1, GL_FALSE, &**viewMatrix**[0][0]);
glUniformMatrix4fv(projectionMatrixLocation2, 1, GL_FALSE, &**projectionMatrix**[0][0]);
uploadMaterial(turquoise);
glUniform1i(useTextureLocation, 0);
planemodel2 *= glm::scale(mat4(), vec3(0.9f, 0.9f, 0.9f));
glUniformMatrix4fv(modelMatrixLocation2, 1, GL_FALSE, &planemodel2[0][0]);
plane4->bind();
plane4->draw();
//REDRAW THE SCENE WHERE STENCIL=2, WITH FIXED CAMERA VIEW
mat4 VM2 = lookAt(
vec3(5, 5, 5),
vec3(6, 3, 5) + vec3(1, 0, 0),
vec3(0, 1, 0)
);
glStencilFunc(GL_EQUAL, 2, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
**glUseProgram(shaderProgram);**
glUniformMatrix4fv(viewMatrixLocation, 1, GL_FALSE, &**VM2**[0][0]);
glUniformMatrix4fv(projectionMatrixLocation, 1, GL_FALSE, &**projectionMatrix**[0][0]);
uploadLight(*light);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glUniform1i(depthMapSampler, 0);
light_vp = light->lightVP();
glUniformMatrix4fv(lightVPLocation, 1, GL_FALSE, &light_vp[0][0]);
glStencilFunc(GL_EQUAL, 2, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
model_matrix2 = translate(mat4(), vec3(-3.0, 1.0, -3.0));
scale = glm::scale(mat4(), vec3(0.2f, 0.2f, 0.2f));
model_matrix3 = translate(mat4(), light->lightPosition_worldspace) * scale;
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &model_matrix3[0][0]);
uploadMaterial(polishedSilver);
glUniform1i(useTextureLocation, 0);
model3->bind();
model3->draw();
glStencilFunc(GL_EQUAL, 2, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
uploadMaterial(turquoise);
glUniform1i(useTextureLocation, 0);
size = 15.0f;
planemodel = glm::translate(mat4(), vec3(size / 2, 0, size / 2)) *
glm::scale(mat4(), vec3(size, 1, size));
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &planemodel[0][0]);
plane1->bind();
plane1->draw();
glStencilFunc(GL_EQUAL, 2, 0xFF);
glStencilMask(0x00);
glDisable(GL_DEPTH_TEST);
uploadMaterial(turquoise);
glUniform1i(useTextureLocation, 0);
planemodel = glm::translate(mat4(), vec3(size, size / 3.4f, size / 2)) *
glm::rotate(mat4(), 3.14f / 2.0f, vec3(0, 0, 1)) * glm::scale(mat4(), vec3(size / 1.7f, 1, size));
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &planemodel[0][0]);
plane2->bind();
plane2->draw();
/*vec3 direction(
cos(camera->verticalAngle)* sin(camera->horizontalAngle),
sin(camera->verticalAngle),
cos(camera->verticalAngle)* cos(camera->horizontalAngle)
);*/
//vec3 new_z = normalize(cross(direction, vec3(0, 1, 0)));
glStencilFunc(GL_EQUAL, 2, 0xFF);
glStencilMask(0x00);
glEnable(GL_DEPTH_TEST);
uploadMaterial(polishedSilver);
glUniform1i(useTextureLocation, 0);
spheremodel =
glm::translate(mat4(), vec3(size / 1.5f, size / 10, size / 3)) *
glm::scale(mat4(), vec3(0.2f, 0.2f, 0.2f));
glUniformMatrix4fv(modelMatrixLocation, 1, GL_FALSE, &spheremodel[0][0]);
model2->bind();
model2->draw();
glStencilMask(0xFF);
glStencilFunc(GL_ALWAYS, 0, 0xFF);
glEnable(GL_DEPTH_TEST);