The edge of the screen isn't where it should be?

Started by
8 comments, last by binarybob0001 19 years, 3 months ago
First, my question is how does the x value get devided by element[1, 1] of the projection matrix. If you can answer that please just write your response now. Otherwise, please read on to figure out what I mean. The projection matrix is set up with a call to gluPerspective(fovy, aspect, zNear, zFar) f = cotangent(fovy/2) f/aspect, 0, 0, 0 0, f, 0, 0 0, 0, (zFar+zNear)/(zNear-zFar), (2*zFar*zNear)/(zNear-zFar) 0, 0, -1, 0 Sorry if I can't write the matrix that clearly but I can't scan in a picture or anything to make it look better. When you multiply this by your coordinates [x, y, z, w] transposed, you get the fallowing: [x*f/aspect, y*f, z*(zFar+zNear)/(zNear-zFar) + w*(2zFar*zNear)/(zNear-zFar), -z] transposed. The z*(zFar+zNear)/(zNear-zFar) + w*(2zFar*zNear)/(zNear-zFar) is used for clipping out objects too far away. The projection matrix is multiplied with this orthagonal matrix: 1, 0, 0, 0 0, 1, 0, 0 0, 0, 0, 0 0, 0, 1, 0 The result looks like this [x*f/aspect, y*f, 0, -z]. Finally, perspective division yields what we're used to seeing [x/z*f/aspect, y/z*f, 0, 1]. Here's what is strange set f equal to cottan(45/2) and the aspect to .75. This means that f/aspect is approximatly .55228 which is what x is multiplied by. If x is equal to 1 and z is equal to -1, our rastorized x coordinate comes out to .55228. If the right side of the screen represents 1 and the left side -1 this puts us at about 3/4 of the way across the screen, but it doesn't. Instead, x = .55228 is the very edge of the screen when z = -1. This means that instead of being multiplied by .55228 it is being devided by it. The y coordinate has a simular thing happening. I cannot figure out why. Anyone know why?
Advertisement
I think your projection matrix is incorrect, using the same variable names as the one you gave above i believe it should look like this (i am using the one below aswell)

f / aspect, 0, 0, 00, f, 0, 00, 0, -(zFar + zNear) / (zFar - zNear), -10, 0, -2 * zNear * zFar / (zFar - zNear), 0


EDIT: if you want to see how it is setup with gluPerspective you can download the Mesa 3d library source and have a look, i believe it'll be something like i posted above

HTH
I got the projection matrix information off of a man page in a quick internet search. Here's the site www.hmug.org/man/3/gluPerspective.html. Of course, the page could always be wrong. There's an easy way to find out though. I'm gonna grab the matrix and print each value. Hehe.
Your projection matrix is correct. nts's is just transposed (but if written like that as a C-style array it would be correct due to the memory layout used by OpenGL).

Enigma
Thanks guys. I still haven't figured it out yet and I'm still looking. I made a little mistake though perspective division yields [-x/z*f/aspect, -y/z*f, 0, 1], but that doesn't change anything.
Quote:Original post by binarybob0001
First, my question is how does the x value get devided by element[1, 1] of the projection matrix. If you can answer that please just write your response now. Otherwise, please read on to figure out what I mean.
The projection matrix is set up with a call to gluPerspective(fovy, aspect, zNear, zFar)
f = cotangent(fovy/2)
f/aspect, 0, 0, 0
0, f, 0, 0
0, 0, (zFar+zNear)/(zNear-zFar), (2*zFar*zNear)/(zNear-zFar)
0, 0, -1, 0
Sorry if I can't write the matrix that clearly but I can't scan in a picture or anything to make it look better.
When you multiply this by your coordinates [x, y, z, w] transposed, you get the fallowing: [x*f/aspect, y*f, z*(zFar+zNear)/(zNear-zFar) + w*(2zFar*zNear)/(zNear-zFar), -z] transposed. The z*(zFar+zNear)/(zNear-zFar) + w*(2zFar*zNear)/(zNear-zFar) is used for clipping out objects too far away. The projection matrix is multiplied with this orthagonal matrix:
1, 0, 0, 0
0, 1, 0, 0
0, 0, 0, 0
0, 0, 1, 0
The result looks like this [x*f/aspect, y*f, 0, -z]. Finally, perspective division yields what we're used to seeing [x/z*f/aspect, y/z*f, 0, 1].
Here's what is strange set f equal to cottan(45/2) and the aspect to .75. This means that f/aspect is approximatly .55228 which is what x is multiplied by. If x is equal to 1 and z is equal to -1, our rastorized x coordinate comes out to .55228. If the right side of the screen represents 1 and the left side -1 this puts us at about 3/4 of the way across the screen, but it doesn't. Instead, x = .55228 is the very edge of the screen when z = -1. This means that instead of being multiplied by .55228 it is being devided by it.
The y coordinate has a simular thing happening. I cannot figure out why. Anyone know why?


I do not know much about this stuff - but I do see a few math problems that might be messing you up.

: f = cotangent(fovy/2) ==> f = cotangent(fovy/2.0f)
: f/aspect, 0, 0, 0 ==? (float)f/(float)aspect, 0, 0, 0 // just in case!

I'd just make sure all the operations are using floats, just to be sure.
Yes, when I'm programming I should indicate that I'm dealing with floats. I'm just trying to figure out exactly what happens mathematically, and in math we usually assume an infinite precision real or complex number system. My math is probably wrong, and I really, really want to know why. So if anyone has a book that explains everything or knows the name of a book then please post or cite it here. Thanks.
Surely someone here knows. A book anything please.
I'm not sure if this answers your question, but the vector in eye coords (after the modelview transform) gets multiplied by the perspective matrix to give clip coords. This 4-element vector is then divided by its W value (the perspective divide) to get normalised device coords. See The red book, chapter 3.

You probably already know this, but remember that OpenGL matrices are column-major order, unlike most maths texts.

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

I looked at that site, but it didn't explain the numbers. It only explained what happens with the numbers. It's a start though. Any other suggestions?

This topic is closed to new replies.

Advertisement