Search code examples
openglglfw

How to increase depth of window in OpenGL?


In the assignment, I'm working on, I have to create a huge box in an OpenGL window and be able to rotate it.

A window is being created by this code:

  window = glfwCreateWindow(1000, 1000, "Window for Box", NULL, NULL);

This first 1000 in the above line corresponds to the width of the window where the second 1000 window corresponds to the height of the window. Now, when I open the lid of the box I've created and rotated it, it looks like this:

box with lid open in OpenGL

The yellow coloured square is the lid and it is opened. You can see that it doesn't look like a box as the depth of the window is less and the box is cut with a white surface. How can I increase the window size to display box perfectly?


Solution

  • The window is 2D, it has no depth. What you're looking for is a projection matrix. If you have used glOrtho(), the last parameter defines how far from the camera a fragment can be, to still be visible. If you have defined your own projection matrix set the zFar value to be greater than the z coordinates of all your vertices.

    Alternatively you can use depth clamping: https://www.khronos.org/opengl/wiki/Vertex_Post-Processing#Depth_clamping