2
votes

Let's say I have a 2D OpenGL app which uses a simple orthogonal projection where (0,0,0) corresponds to the bottom left corner of the screen, and (480,640,0) corresponds to the top right.

Now say I want to make my 2D app a bit 3D, i.e. introduce little 3D effects such as things falling into place from above the screen. To do this I need a perspective projection where everything on the usual plane of rendering (z=0) looks identical to when the orthogonal projection was being used.

Anyone know how to create such a projection? The math appears to be slightly beyond me. Presumably I want my frustum's far plane to be z=0, where (0,0,0) corresponds to the bottom left pixel, but glFrustum() et al only let me specify coordinates for the near plane. How do I choose sensible near and far values? I've tried using glFrustum() and gluLookAt() to look at the origin from some point above the screen, but how do I choose the eye point?

1

1 Answers

0
votes

I think I worked it out. The below works for me.

esMatrixLoadIdentity(projMatrix);
esMatrixLoadIdentity(modelViewMatrix);
float w = screen.width;  // 480
float h = screen.height; // 640
int height = 20;
float nw = w/height;  // width of near plane
float nh = h/height;  // height of near plane
esFrustum(projMatrix, -nw/2, nw/2, -nh/2, nh/2, 1.0f, 1.0f + height);
esMatrixLookAt(modelViewMatrix, 0,0, height, // eye = above the origin 
               0,0, 0,                    // looking at the midpoint on the plane Z=0
               0,1, 0);
esTranslate(modelViewMatrix, -w/2, -h/2, 0); // adjust so (0,0,0) is now bottom left pixel and on the far plane z=0