0
votes

I am trying to display two cubes using modern opengl (https://preview.ibb.co/hif8t6/Screenshot_2017_10_31_09_59_27.png). The first cube is displayed by orthographic projection (left) and the second by perspective projection (right). That is working fine but i am unable to get the left cube to go behind the right cube. Here is the relavant snippet of code

    ourShader_ortho.Use();
    ourShader_persp.Use();

    glm::mat4 model_ortho, model1, model2, model;
    glm::mat4 view_ortho, view_persp;
    glm::mat4 orthographic;
    glm::mat4 perspective;

    model1 = glm::rotate(model, (GLfloat)glfwGetTime()*1.0f, glm::vec3(0.5f, 1.0f, 0.0f));
    model2 = glm::rotate(model, (GLfloat)glfwGetTime()*1.0f, glm::vec3(0.0f, 1.0f, 0.5f));
    model = model1 * model2;

    view_ortho = glm::translate(view_ortho, glm::vec3(200.0f, 200.0f, -150.0f));
    orthographic = glm::ortho(0.0f, (GLfloat)width, 0.0f, (GLfloat)height, 0.1f, 200.0f);

    view_persp = glm::translate(view_persp, glm::vec3(1.0f, 0.0f, -3.0f));
    perspective = glm::perspective(45.0f, (GLfloat)width/(GLfloat)height, 0.1f, 200.0f);

    GLint modelLoc_ortho = glGetUniformLocation(ourShader_ortho.Program, "model");
    GLint viewLoc_ortho = glGetUniformLocation(ourShader_ortho.Program, "view");
    GLint projLoc_ortho = glGetUniformLocation(ourShader_ortho.Program, "orthographic");

    glUniformMatrix4fv(modelLoc_ortho, 1, GL_FALSE, glm::value_ptr(model_ortho));
    glUniformMatrix4fv(viewLoc_ortho, 1, GL_FALSE, glm::value_ptr(view_ortho));
    glUniformMatrix4fv(projLoc_ortho, 1, GL_FALSE, glm::value_ptr(orthographic));   

    glBindVertexArray(VAO_O);
    glDrawArrays(GL_TRIANGLES, 0, 6);
    glBindVertexArray(0);

    GLint modelLoc_persp = glGetUniformLocation(ourShader_persp.Program, "model");
    GLint viewLoc_persp = glGetUniformLocation(ourShader_persp.Program, "view");
    GLint projLoc_persp = glGetUniformLocation(ourShader_persp.Program, "perspective");

    glUniformMatrix4fv(modelLoc_persp, 1, GL_FALSE, glm::value_ptr(model));
    glUniformMatrix4fv(viewLoc_persp, 1, GL_FALSE, glm::value_ptr(view_persp));
    glUniformMatrix4fv(projLoc_persp, 1, GL_FALSE, glm::value_ptr(perspective));    

    glBindVertexArray(VAO_P);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);

What do i do to get the cube with orthographic projection to go behind the cube with perspective projection?

1
To get the cube with orthographic projection "behind" the cube with perspective projection means, that the dapth value calculated by the orthographic projection is grater than the depth value calcualted by the perspective projection. While for orthographic projection the depth is is linear, it is not linear for perspective projection. - see How to render depth linearly in modern OpenGL with gl_FragCoord.z in fragment shader?Rabbid76
Yes i did enable the depth test. And you are right, the "behind" in orthographic is linear while for perspective it not. Thanks that worked!ram

1 Answers

-2
votes

I'm not a OpenGL programmer but this is what I think.

You are drawing the object twice using the glDrawArrays lines. For the projection matrices they are using your orthographic and perspective matrices.

However these matrices are created using different parameters:

view_ortho = glm::translate(view_ortho, glm::vec3(200.0f, 200.0f, -150.0f));
view_persp = glm::translate(view_persp, glm::vec3(1.0f, 0.0f, -3.0f));

My guess is that you want to unify the translation parameters so that the cubes lie exactly on top of each other.

E.g.

view_ortho = glm::translate(view_ortho, glm::vec3(200.0f, 200.0f, -150.0f));
view_persp = glm::translate(view_persp, glm::vec3(200.0f, 200.0f, -150.0f));