# opengl z index changing does not zoom in

I am making a simple opengl application where I have a cube and I want to move forward to it with the glTranslate3d function, where I change the z index. One would expect the object to get bigger because of the view but it does not

My code:

    glClearColor(0, 0, 0, 1);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glOrtho(30, width, 0, height, 100, -100);
glMatrixMode(GL_MODELVIEW);


I have tried many things also I have been told that gluPerspective is out dated and should not be used....

EDIT: it works when I rotate the object you can see how 3d it is :p

• gluPerspective is outdated with the rest of the fixed-function pipeline. You're either using the fixed-function pipeline, or the shader pipeline. If you've decided to use the fixed-function pipeline (which you apparently have) then there's no reason not to use gluPerspective. – user253751 Nov 10 '15 at 0:02

Orthographic projection is not perspective. This means it won't zoom in if you move the object closer to the camera. If you want to zoom in that way, you need to use perspective projection by using glFrustum.

Other way is to change the relative size of the orthographic projection. Something like this:

glOrtho(0, width * zoomlevel, 0, height * zoomlevel, 100, -100);


But this messes up your coordinate system if you are trying to achieve pixel-perfect screen-space coordinates. Also the above code will look weird if you expect your objects to be relative to the center of the screen. For that to work better you can use something like this:

glOrtho( -width * 0.5f * zoomlevel, width * 0.5f * zoomlevel, -height  * 0.5f * zoomlevel, height  * 0.5f * zoomlevel, 100, -100 );


Now you can control the zoom level by setting the zoomlevel variable, for example to 0.1f to make it zoom in or 5f to make it zoom out.

• So if I choose to use the perspective view? glFrustum(0, width, height, 0, -100, 100); would this be correct ? I am trying to still learn openGL sorry for the trouble :p – Samuel Kodytek Nov 9 '15 at 20:29
• That would not be correct. See the link "perspective projection" in my answer. – Lasse Nov 9 '15 at 20:38

You issue is that you use

glOrtho(30, width, 0, height, 100, -100);


Which makes an orthographic view of the scene. With this, the distance of the objects to the camera is not visible. It is the equivalent of a parallel projection.

You have to make a perspective projection for the objects to be displayed bigger as the get closer, and smaller as they get further away.

You could look into glFrustum to make a perspective matrix.

• The method glFrustum is pretty archaic if I am not mistaken right? atleast I read it somewhere :p and glOrtho should be used with glFrustum right? or do I have it really mixed up? – Samuel Kodytek Nov 9 '15 at 20:24
• Your code is old deprecated OpenGL, which is itself very archaic. You probably want to look into OpenGL 3 or OpenGL 4 standards if you want to use the newer API. – Lasse Nov 9 '15 at 20:26
• Ok thx for the info any good tutorials for opengl 3/4? sorry for the questions – Samuel Kodytek Nov 9 '15 at 20:32
• This is something that I have used for learning on C#, so it should translate to Java / LWJGL fairly well too. The tutorial itself is C/C++. The pipeline is always the same regardless to the language used. opengl-tutorial.org – Lasse Nov 9 '15 at 20:37
• @Lasse I don't recommend diving straight into OpenGL 3 (or 4); it's not very intuitive compared to OpenGL 1. Ofc it's ultimately your choice. – user253751 Nov 10 '15 at 0:03