1

In previous projects, I enabled depth testing used gluPerspective called once on startup to set up a 3D space. Currently, I am rendering a square between -0.5 and 0.5 with 0.0 as its origin after the 3D world has initialised with code below will cause a square to cover the entire screen:

glBegin(GL_QUADS);
{
  glVertex3f(-0.5, -0.5, 0);
  glVertex3f(-0.5, 0.5, 0);
  glVertex3f(0.5, 0.5, 0);
  glVertex3f(0.5, -0.5, 0);
}
glEnd();

What I am looking is a way to set the perspective so that shapes are rendered in world space. For example, the snippet below should cause a square of 200x200 to be rendered:

glBegin(GL_QUADS);
{
  glVertex3f(-100, -100, 0);
  glVertex3f(-100, 100, 0);
  glVertex3f(100, 100, 0);
  glVertex3f(100, -100, 0);
}
glEnd();

The code below is what I am currently using to initialise a 3D world.

// WINDOW_WIDTH = 1600, WINDOW_HEIGHT = 900
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(47, WINDOW_WIDTH / WINDOW_HEIGHT, 0.01, 1000);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_DEPTH_TEST);

Have I missed any steps in setting up a 3D space and if gluPerspective is used to do this any suggestions why it is not working?

I am able to achieve this in 2D using ortho, it is important that the world is 3D. Everything is being written in C using OpenGL and GLU up to 1.3 with my GLFW set up identical to this. Due to technical restraints, I am unable to use the modern pipeline.

Spektre
  • 49,595
  • 11
  • 110
  • 380
Blunderchips
  • 534
  • 4
  • 22
  • What are you expecting to see? Note that your near clip plane is at z 0.01 and your quad is at z 0 so it's closer than the near clip plane, so it's not rendered. But when you fix that, it will still be a square. – user253751 Jul 07 '20 at 19:20

1 Answers1

2

First of all, the result of WINDOW_WIDTH / WINDOW_HEIGHT is 1, because WINDOW_WIDTH and WINDOW_HEIGHT are integral values. You have to perform a floating point division ((float)WINDOW_WIDTH / WINDOW_HEIGHT) to compute the correct aspect ratio.


At Perspective Projection the projection matrix describes the mapping from 3D points in the world as they are seen from of a pinhole camera, to 2D points of the viewport.
The projected size of an object on the viewport depends on its distance to the camera. The different size at different distances (depths) causes the perspective effect. The perspective projection matrix defines a Viewing frustum

The ratio of projected size and the distance to the camera depends on the field of view angle:

maxDim / cameraZ = tan(FOV / 2) * 2

So there is exactly 1 distance where an object with a length of 200 covers 200 pixel. For instance, If you have a filed of view angle of 90° then an object with a z distance of half the window height (height /2) and a vertical size of 200 covers 200 pixel (vertical) because tan(90° / 2) * 2 = 2.

When you use gluPerspective, then you define the field of view angle along the y axis. The field of view along the x axis depends on the aspect ratio. If the aspect ratio is set correctly, then the projection of a square which is parallel to the xy plane of the view is still a square.

Note, if you would use orthographic projection, then the size of the object is independent on the distance.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Yes, that is what I expect to happen yet -0.5 and 0.5 fix the screen perfectly. It maintains the initial ratio as I zoom out. So when viewing the shape of 200x200 at distance instead of being a square it is a rectagle. – Blunderchips Jul 07 '20 at 17:35
  • @Blunderchips No that's not true. You have to set the aspect ratio correctly (2nd parameter of `gluPerspective`). – Rabbid76 Jul 07 '20 at 17:47
  • 1
    @Blunderchips the result of `WINDOW_WIDTH / WINDOW_HEIGHT` is 1, because `WINDOW_WIDTH` and `WINDOW_HEIGHT` are integral values. You have to perform a floating point division (`(float)WINDOW_WIDTH / WINDOW_HEIGHT`). – Rabbid76 Jul 07 '20 at 17:54