PDA

View Full Version : How to view the bounding box of the view volume

Lee_Jennifer_82
10-21-2015, 12:17 PM
Hello, I'd like to draw the bounding box after applying the following projection matrix:

glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, 10.0f);

How can I find the coordinates of the bounding volume?

Dark Photon
10-21-2015, 06:03 PM
The arguments are the bounds of the volume in eye-space, except that the near/far values are negated. Use those.

GClements
10-21-2015, 10:32 PM
Hello, I'd like to draw the bounding box
The 2D projection of the bounding box is the viewport rectangle. That's where the concept of "view volume" comes from. Unless you're drawing the view volume for one projection using a different projection, you aren't going to be able to see it.

Lee_Jennifer_82
10-22-2015, 01:02 AM
The arguments are the bounds of the volume in eye-space, except that the near/far values are negated. Use those.

Ok, thanks. Now I am again asking the following question.

I've drawn a cube at the origin. Now I've called the ortho function from glm library as follows:

glm::mat4 projection_matrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, 50.0f, 100.0f);

I understand it makes the near clipping plane at -50.0f and far clipping plane at -100.0f from eye position (used default position at origin)

But the cube is at origin and bounding planes are far from the cube (near at -50 and far at -100 from eye).

The cube shouldn't be visible because it doesn't lie within the view volume.

But why the cube is still visible.

Thanks again.

Carmine
10-22-2015, 07:30 PM
I've drawn a cube at the origin. Now I've called the ortho function from glm library as follows:
glm::mat4 projection_matrix = glm::ortho(-2.0f, 2.0f, -2.0f, 2.0f, 50.0f, 100.0f);
The cube shouldn't be visible because it doesn't lie within the view volume.
Perhaps the projection matrix hasn't been applied properly?
Perhaps the cube isn't where you think it should be?
Sounds like a basic issue like that.
Post your code, or a simplified version of it.

Lee_Jennifer_82
10-22-2015, 11:49 PM
I figured it out. there was something wrong in the shader. I used some scale factor which was producing wrong result. But I swapped near and far plane and that didnot have effect on the output. Could you let me know why? Does it work the same way for perspective projection.

GClements
10-23-2015, 02:40 AM
But I swapped near and far plane and that didnot have effect on the output. Could you let me know why? Does it work the same way for perspective projection.
Swapping the near and far plane has the same effect as negating the Z coordinate after the projection transformation. If you aren't using depth testing, or if there's no overdraw, the clip-space Z coordinate has no effect on the resulting image. The effect would be the same for a perspective projection.

Lee_Jennifer_82
10-24-2015, 01:29 AM
Swapping the near and far plane has the same effect as negating the Z coordinate after the projection transformation. If you aren't using depth testing, or if there's no overdraw, the clip-space Z coordinate has no effect on the resulting image. The effect would be the same for a perspective projection.
But why it should have the same effect.Isn't the near plane at the back of camera in this case? What did you mean by overdraw, drawing two objects one behind another? Will then this change be reflected? I used glEnable(GL_DEPTH_TEST).

GClements
10-24-2015, 04:42 PM
But why it should have the same effect.Isn't the near plane at the back of camera in this case? What did you mean by overdraw, drawing two objects one behind another? Will then this change be reflected? I used glEnable(GL_DEPTH_TEST).
In an orthographic projection, "behind the camera" isn't a meaningful concept; it's as if the camera is infinitely far from the scene.

With a perspective projection, the projection lines converge at the eye-space origin, and the near and far planes must be in front of that point (i.e. neither zNear nor zFar can be negative).

With an orthographic projection, the projection lines are parallel, and the near and far planes can be at any distance (either or both of zNear and zFar can be negative). If you're using a floating-point depth buffer (with the appropriate extensions), you don't even need to have near or far planes. For a normalised (fixed-point) depth buffer, the near and far planes simply define the eye-space Z coordinates corresponding to the minimum and maximum depth values.

Overdraw refers to having primitives which overlap in 2D. If there is no overdraw, then depth tests don't matter (the test should never fail). And for an orthographic projection, eye-space Z has no effect upon anything other than depth (unlike a perspective projection, where objects appear smaller as they get farther away).

So for an orthographic projection, the only effect of swapping the near and far planes is that the depth will be inverted, causing far objects will obscure near objects (when it should be the other way around). But if depth testing isn't used, or there's no overdraw, or overdraw isn't visible (e.g. because all primitives have the same colour), then there is no effect.

Lee_Jennifer_82
10-25-2015, 05:54 PM
Hello, GClements, thank you so much for the explanation.

With a perspective projection, the projection lines converge at the eye-space origin, and the near and far planes must be in front of that point (i.e. neither zNear nor zFar can be negative).

Does it always hold for perspective projection that near and far planes both should be in front of the eye? Or there is some interpretation when the sign of near and far planes opposite?

GClements
10-25-2015, 07:24 PM
Does it always hold for perspective projection that near and far planes both should be in front of the eye? Or there is some interpretation when the sign of near and far planes opposite?
In simple terms, both planes need to be on the same side.

It's not meaningful to project geometry which is "level" with the viewpoint, as it results in division by zero.

Even if you don't have a near plane, the planes for the top, bottom, left and right edges will exclude any geometry that's level with or behind the viewpoint.

Remember that a perspective projection sets clip-space W to eye-space -Z (in theory, it can actually set it to k*Z for any non-zero k, but that's equivalent to simply scaling eye-space Z prior to projection).

The four sides of the clip volume correspond to the constraints -w<x, x<w, -w<y and y<w. If w=-z, then these become z<x, x<-z, z<y and y<-z. If Z is positive, then z<x and x<-z are mutually exclusive, similarly for y. I.e. any point for which z>0 will fail either the z<x or x<-z constraint, and either the z<y or y<-z constraint. Thus, even without a near plane,

Aside from that, the relationship between eye-space Z and NDC Z is Zndc=A-B/Zeye (where A and B are determined by the near and far planes). If you're familiar with the shape of the curve y=1/x, you'll know that it has a pole (vertical asymptote) at x=0. The curve w=A-B/z is similar; in particular, it also has a pole at z=0. Thus if the range of allowable eye-space Z values were to include zero, the mapping between Z and depth would be discontinuous and the range would include infinity.

Lee_Jennifer_82
10-25-2015, 09:43 PM
Thanks for the explanation. I have another simple question:suppose I'd like to see the same object using orthographic as well as perspective projection. My eye is at the same distance suppose (0, 0, 2) from the cube with dimension 1 centered at origin. Now if switch between orthographic and perspective projection using the following commands:

projection_matrix = frustum(-2.0f, 2.0f, -2.0f, 2.0f, 0.5f, 50.0f);
projection_matrix = ortho(-2.0f, 2.0f, -2.0f, 2.0f, 0.5f, 50.0f);

I found when switched to perspective projection, the object look smaller. My question is why the front face looks smaller when switched to perspective projection. The eye distance is the same. Am I doing something wrong.

GClements
10-25-2015, 10:42 PM
I found when switched to perspective projection, the object look smaller. My question is why the front face looks smaller when switched to perspective projection. The eye distance is the same. Am I doing something wrong.
"Eye distance" doesn't mean anything for an orthographic projection.

For glFrustum(), the left, right, top and bottom parameters define a rectangle on the near plane. A primitive that's exactly on the near plane (or a negligible distance behind it) should appear the same size in both orthographic and perspective projections. As the object gets farther away, it will appear smaller with a perspective projection but the size will remain constant with an orthographic projection.

Lee_Jennifer_82
10-25-2015, 11:23 PM
"Eye distance" doesn't mean anything for an orthographic projection.

For glFrustum(), the left, right, top and bottom parameters define a rectangle on the near plane. A primitive that's exactly on the near plane (or a negligible distance behind it) should appear the same size in both orthographic and perspective projections. As the object gets farther away, it will appear smaller with a perspective projection but the size will remain constant with an orthographic projection.
Please ignore my poor knowledge. Is it that viewplane is the near plane in perspective projection?

GClements
10-26-2015, 08:42 AM
Is it that viewplane is the near plane in perspective projection?
The view plane can be any plane perpendicular to the projected Z axis.

Projecting a point from 3D to 2D first constructs a "projector" or projection line through that point (for a parallel projection, the line has a fixed direction, for a perspective projection it passes through a fixed point, i.e. the viewpoint), then finds the intersection between the projection line and the projection plane (view plane).

For a parallel projection, if the projection plane is perpendicular to the projectors, the projection is an orthographic projection, otherwise it is an oblique projection.

OpenGL handles projections by first applying a 3D transformation to 3D space (transforming eye space to NDC), then discarding the projected Z coordinate. For a parallel projection, the transformation is affine; for a perspective projection, it is projective.