PDA

View Full Version : Perspective Projection


mjonsson
12-15-2008, 01:35 PM
I'm trying to get my head around some 3D graphics, but ran into trouble.

I have a number of points that make up a cube. Very simple.

I use an orthographic projection matrix to display it.

But when I try to apply a perspective projection matrix it looks like anything but a cube. I've tried multiple ways to get it to work but it still doesn't.

You can download the source code here: http://www.bastardindustries.com/temp/example.zip

It is written in ActionScript 3.0. But the code is very basic and straight forward so I don't think it's will be a problem even if you are not familiar with ActionScript.

By commenting an uncommenting the following lines (in 3DGraphics.as) you can switch between the orthographic projection and perspective projection matrix. As you can see when just using the orthographic projection matrix it works fine. But with the perspective it doesn't.

// Ortographic and perspective
// mP = mPers.multiplyMatrix(mOrto);

// Just ortographic
mP = mOrto;

Any help on why my perspective matrix isn't working is appreciated.

The perspective matrix looks like this. In a column major order:

n is the near plane and f the far plane.

mA[0] = n;
mA[5] = n;
mA[10] = n+f;
mA[11] = 1;
mA[14] = -(f*n);
mA[15] = 0;

Thanks!

/ Magnus

Reedbeta
12-15-2008, 02:30 PM
Your matrix looks like it's missing a bunch of stuff.

Look at the projection matrix here: http://msdn.microsoft.com/en-us/library/ms537094(VS.85).aspx

That is a standard OpenGL projection matrix...yours may be transposed and possibly there will be some sign changes depending on how ActionScript sets up its coordinate system.

The main thing is the [10] and [14] entries are missing the (f - n) in the denominator. Also, I think the [0] and [5] entries should probably be constants, not n (unless you want the field of view to change as you move the near plane in and out).

mjonsson
12-16-2008, 11:44 AM
Thank you for you reply, unfortunately I can't get it to work...

I've tried using that matrix before as well...

You can see the result here...
Using the orthographic for reference:
http://www.bastardindustries.com/temp/projection/ortho.swf

and using the new perspective:
http://www.bastardindustries.com/temp/projection/persp.swf

Here is the source code as well:
http://www.bastardindustries.com/temp/example2.zip

It feels like I'm missing something, I've tried applying the perspective matrix in many different ways and many different versions of it and nothing works.

ActionScripts coordinates system starts from the top left corner.

The perspective matrix now looks like this:

mA[0] = (2*n)/(r-l);
mA[1] = 0;
mA[2] = 0;
mA[3] = 0;
mA[4] = 0;
mA[5] = (2*n)/(t-b);
mA[6] = 0;
mA[7] = 0;
mA[8] = (r+l)/(r-l);
mA[9] = (t+b)/(t-b);;
mA[10] = (f+n)/(f-n);;
mA[11] = -1;
mA[12] = 0;
mA[13] = 0;
mA[14] = (2*f*n)/(f-n);
mA[15] = 0;

I also tried using constants at [0] and [5]

Any ideas?

Thanks!

Reedbeta
12-16-2008, 01:23 PM
Okay, I looked at your code and you seem to be putting in bad numbers for the near and far values. For perspective projection both values should be positive, and near should be something small like 0.1, while far is something large like 100 or 1000 (depending on the size of your scene).

Also, the perspective matrix outputs coordinates that are in the [-1, 1] range after dividing by W (which you seem to call H for some reason...). You presumably need to remap them to the appropriate range of screen coordinates, which might be [0, 1] for instance.

mjonsson
12-17-2008, 10:26 AM
First of all, thank you for your help I really appreciate it.

I did the changes you suggested and have been playing around with it some more after that, the thing that started to make things look a bit better was the fact that I stopped using negative values for coordinates (although I don't see why I shouldn't be able to use negative values), then things started to look a bit better, however it's still not 100% it only works when I rotate the cube in certain ways.

Here are some examples:

If I don't rotate the cube at all it doesn't look like an ok perspective, not to me at least:
http://www.bastardindustries.com/temp/projection/none.swf

If I rotate it 45 degrees on z and x it actually looks ok:
http://www.bastardindustries.com/temp/projection/xz.swf

I I rotate it 45 degrees just on x it looks hmm, a bit weird:
http://www.bastardindustries.com/temp/projection/x.swf

And if i rotate 45 degrees on x and y it looks really messed up:
http://www.bastardindustries.com/temp/projection/xy.swf

My latest source code is here:
http://www.bastardindustries.com/temp/example3.zip

karligula
12-17-2008, 10:49 AM
Have you tried using the standard openGL perspective commands first, just to make sure it is the perspective that's going wrong and not something else? That last screenshot looks like more than a wonky matrix to me.

mjonsson
12-17-2008, 11:52 AM
I'm not sure if I understand what you mean? But there are no support for the OpenGL API in Flash / ActionScript, so I can't use any OpenGL functions out of the box so to speak.

In my earlier examples I used a orthographic projection matrix and then everything works fine, it's when I try to replace it with a perspective matrix things go bad.