Jump to content


Perspective Projection


6 replies to this topic

#1 mjonsson

    New Member

  • Members
  • Pip
  • 4 posts

Posted 15 December 2008 - 09: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.bastardin...emp/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

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 15 December 2008 - 10:30 PM

Your matrix looks like it's missing a bunch of stuff.

Look at the projection matrix here: http://msdn.microsof...094(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).
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 mjonsson

    New Member

  • Members
  • Pip
  • 4 posts

Posted 16 December 2008 - 07:44 PM

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:


and using the new perspective:


Here is the source code as well:
http://www.bastardin...mp/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!

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4782 posts
  • LocationBellevue, WA

Posted 16 December 2008 - 09: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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 mjonsson

    New Member

  • Members
  • Pip
  • 4 posts

Posted 17 December 2008 - 06:26 PM

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:


If I rotate it 45 degrees on z and x it actually looks ok:


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.bastardin...mp/example3.zip

#6 karligula

    Valued Member

  • Members
  • PipPipPip
  • 180 posts

Posted 17 December 2008 - 06:49 PM

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.

#7 mjonsson

    New Member

  • Members
  • Pip
  • 4 posts

Posted 17 December 2008 - 07:52 PM

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users