# OpenGL Problem with perspective

This topic is 2777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm learning OpenGL and there's something really simple I'm missing about setting the perspective. (Yes, I've googled, read articles, many books, faqs, and tried lots of stuff.)

My understanding is that projection matrix transforms the eye space into clipping space. It seems like eye space can be whatever coordinates I want (meaning they can be outside the range of -1 to 1) and that the perspective matrix should handle transforming to clipping and NDC coordinates.

So, I would like to draw a 64x64 grid of points in the xz plane where the values go from 0 to 63. (Y is a positive constant.) I'm considering these to represent meters and want the grid to be shown in perspective and fill up most of the viewport. Sort of like looking at the ground or something. I can draw the points, but I'm having trouble figuring out how to set my projection matrix so that it draws in perspective and isn't really, really tiny. I've looked at diagrams and it seems like the left, right, top, bottom, near, and far should just be simple numbers indicating where in eye space you want the clipping edges to be.

So, for example, if I want points (0,10,0) through (63,10,63) to show up on the screen, I would set my left = 0, right = 63, top = 20 (or something), bottom = 0, near = 1 (I know it has to be positive), far = 64. But that just gives me a series of dots in in a very small part of the space. Therefore, the left, right, top, etc. must mean something else.

Can anyone explain?

##### Share on other sites
near = 10
or points
0,1,0 to 63,1,63.

It's perspective, so things further away are smaller. And 10 is further than 1. Much further....

##### Share on other sites

So, for example, if I want points (0,10,0) through (63,10,63) to show up on the screen, I would set my left = 0, right = 63, top = 20 (or something), bottom = 0, near = 1 (I know it has to be positive), far = 64. But that just gives me a series of dots in in a very small part of the space. Therefore, the left, right, top, etc. must mean something else.

Afaik those values are the left/right/top/bottom at the near plane, so your frustum is way bigger than you intend. Never really used glFrustum, since I pretty much moved from gluPerspective to building the matrix myself (and thinking in "normalized" values for z = 1 instead of z = near).

##### Share on other sites
Well, if I change the values to near = 1, far =2, left = 0, right =1, top = 1, bottom = 0 to make a smaller frustrum, it did make the area of points larger. Now, it's just a small area (about 1/5th of the viewport) rather than being a tiny area.

Yes, I understand they're the values of the near plane. (Found that in one of the tutorials I read.)

The statements I've seen are that projection maps the eye space to the clipping/NDC space. If that's true, then I would expect that by specifying my near plane (at 1) to have corners at left = 0, right = 63, top = 63, bottom = 0, that it would then map say coordinate (0,0,1) in eye space to coordinate (-1,0,0) in clipping space. And (63,0,1) to (1,0,0). And the rest of the coordinates between them on the x axes, and up to get the perspective.

In other words, I'm expecting the values for left, top, right, bottom to be the coordinate values of my eye space. So, based on my experiments, I would say that I don't understand eye space or the projection transformation (or both).

I'd love to just use my own matrices. (I prefer working with them actually.) But doing my own matrices won't help if I don't understand the basic concepts.

If anyone could point me to a perspective projection example that is not using NDC coordinates for the eye space so I can study how the modelview and projection matrices work that would be helpful.

@szecs

I didn't have my near set to 10. But I've just tried variations of setting my near and far with near being 10. But none of it helped. And how would that let me see objects close to the "camera" anyway?

##### Share on other sites

The statements I've seen are that projection maps the eye space to the clipping/NDC space. If that's true, then I would expect that by specifying my near plane (at 1) to have corners at left = 0, right = 63, top = 63, bottom = 0, that it would then map say coordinate (0,0,1) in eye space to coordinate (-1,0,0) in clipping space. And (63,0,1) to (1,0,0). And the rest of the coordinates between them on the x axes, and up to get the perspective.
[/quote]
This is correct if you do perspective (w) divide after the projection matrix. You don't need to worry about this if you're letting the API handle your projection through.

I don't quite understand all the terms you are using ('coordinate values of eye space'?), but it almost seems like you expect your view frustum to define a box, where the eye space coordinates go from 0 to 64 no matter how far away they are.

When you're using a perspective frustum, its like looking through the top of a pyramid. The near plane may be 0 to 63, but the far plane is much much larger than the near plane. Therefore if you have an object that is 64x64 that is located close the far plane, it appears much smaller than a 64x64 object near the near plane:

[attachment=2179:perspective.jpg]

If you know that you want to draw a 64x64 grid that fills the screen, why dont you just draw it orthographically? This shrinking is what perspective is, so if you don't want it to be shrunk than you shouldn't be using perspective projection.

##### Share on other sites
Hmm.... Well, most, if not all, of the instructional material I've read talk about OpenGL having eye space - which is often referred to as camera space, but since other people like to point out that there is no camera in OpenGL, I thought it would be better to use the term eye space since that's what the OpenGL website uses. And by coordinates, I mean the vertices in the eye space using whatever coordinate system the eye space is using (which might be different than object space or world space).

When I mean "fills the screen," I mean it fills it horizontally. For example, I'm trying to draw a flat piece of ground. I want the ground to look like ground that gets smaller in the distance. I realize that it won't fill the entire xy screen space. (It will be shorter.)

Orthographically won't work since I'm trying to look at scenery (eventually). The grid I'm trying to draw is 64x64 in the xz plane, not the xy plane.

The shrinking that I'm referring to is the entire plane, not the plane narrowing as it gets farther away. The way it looks now, the player is hovering way out in space looking at a distant piece of land. I want the player on the land, seeing it shrink toward the horizon.

Well, actually I expect the frustum to be defined by 6 planes - which are not a box. However, the functions ask for the numbers as left, right, top, bottom, near, far. These are scalar numbers, not 3d numbers. So, I don't know how to pick the scalar numbers to get the planes I want.

Do I need to transform my coordinates for my xz plane from what I have to something else - so that the perspective projection works and gives me a plane that fills the window horizontally (at the lower part) and narrows as it gets higher to show an horizon?

##### Share on other sites

The shrinking that I'm referring to is the entire plane, not the plane narrowing as it gets farther away. The way it looks now, the player is hovering way out in space looking at a distant piece of land. I want the player on the land, seeing it shrink toward the horizon.
[/quote]

I think you're confusing the point of the perspective matrix. If your eye is floating out in space and you want it parallel to the ground, this is a problem with your view matrix, not the perspective matrix.

The perspective matrix only handles the squashing of the eye space into a flat image, it doesn't determine from where you are viewing the scene. In fact you probably don't really need to calculate what near plane you want, usually an algorithm like that in gluPerspective (link) is sufficient, all you need to know is your near and far plane distance, and the FOV that you want.

Can you explain how you have formed your view/eye/camera matrix?

##### Share on other sites
Yes, that's it.

I read everything the responders have said - and decided that it must be a problem with my eye space (modelview). So, I started over with that. Several people here (and some of my google searching) gave me ideas about how to construct my modelview matrix. So, now I have something that looks reasonable. And I switched to a perspectivefieldofview function (which I think is similar to the gluPerspective function). I will look at your link.

It looks like the FOV is more important for what I'm doing (at this point) than the near/far values which I have at 1, 64. My FOV is PI/4.

I switched my coordinates back to +x, +y, -z (which I was using a few days ago).

And my new modelview matrix is.
1 0 0 0
0 1 0 0
0 0 1 0
-32 -36 0 10

where my xz plane is at y = 30 and x = [0..63] and z = [0..-63]. The number 10 is just a temporary value while I figure things out. (I need to do some calculations to see what value it should be to get the scale I want.) And the -32 is so that I'm looking at the centre of the plane. The -36 is so that the plane looks more like the ground. (I also need to do some calculations to see exactly what number this should be).

But now, I can proceed.

Thanks to everyone who responded.

I'll check back just in case anyone has any more ideas or pointers that I should think about.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013429
×