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);
    glViewport(0, 0, width, height);
    glOrtho(30, width, 0, height, 100, -100);

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

  • \$\begingroup\$ 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. \$\endgroup\$ – 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.

| improve this answer | |
  • \$\begingroup\$ 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 \$\endgroup\$ – Samuel Kodytek Nov 9 '15 at 20:29
  • \$\begingroup\$ That would not be correct. See the link "perspective projection" in my answer. \$\endgroup\$ – 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.

| improve this answer | |
  • \$\begingroup\$ 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? \$\endgroup\$ – Samuel Kodytek Nov 9 '15 at 20:24
  • \$\begingroup\$ 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. \$\endgroup\$ – Lasse Nov 9 '15 at 20:26
  • \$\begingroup\$ Ok thx for the info any good tutorials for opengl 3/4? sorry for the questions \$\endgroup\$ – Samuel Kodytek Nov 9 '15 at 20:32
  • \$\begingroup\$ 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 \$\endgroup\$ – Lasse Nov 9 '15 at 20:37
  • \$\begingroup\$ @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. \$\endgroup\$ – user253751 Nov 10 '15 at 0:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.