PDA

View Full Version : Pixel-Texel alignment with perspective projection



Bicubic
12-26-2009, 07:37 PM
Hi all! I'm new here and new to OpenGL, hope this is the right place to post.

I am drawing 2d sprites by using an orthographic projection but I also want to be able to rotate them with perspective-correct texture behavior.

How do I go about positioning and scaling my sprites in 3d space to get them pixel perfect with a perspective projection?

Edit: I seem to have hacked together something that works but I don't understand how it works:


//Projection
float aspect = ClientRectangle.Width / (float)ClientRectangle.Height;
Matrix4 p = Matrix4.CreatePerspectiveFieldOfView(MathHelper.Pi Over4, aspect, 0.01f, 10000f);
GL.MatrixMode(MatrixMode.Projection);
GL.LoadMatrix(ref p);

//WorldView
float z = ClientRectangle.Height * 1.208f;
p = Matrix4.Identity;
p *= Matrix4.CreateTranslation(-ClientRectangle.Width / 2f, -ClientRectangle.Height / 2f, 0);
p *= Matrix4.LookAt(new Vector3(0, 0, z), new Vector3(0, 0, -1), Vector3.UnitY);
p *= Matrix4.Scale(1f,-1f,1f);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref p);

Can someone explain what the magic number 1.208 means and where it comes from? I calculated it from literaly sticking in random z values until it 'looked about right'. After two sets of screen height and z values I worked out that number and it's somehow giving me pixel perfect results.

Ilian Dinev
12-27-2009, 12:55 AM
Multiply the two matrices (that you computed in 'p'), it could show.

Bicubic
12-27-2009, 01:31 AM
Well, I had a look at the resulting matrix and its all gibberish to me!

ColacX
12-28-2009, 05:14 AM
If it looks right, it is right, welcome to the land of 3D-programming