2013-08-02 55 views
2

假設我有一個2D OpenGL應用程序,它使用一個簡單的正交投影,其中(0,0,0)對應屏幕的左下角,(480,640,0)對應於右上角。OpenGL:如何做透視投影,其中z = 0視覺等價。正交

現在說我想讓我的2D應用程序有點3D,即引入小的3D效果,如從屏幕上方落下的東西。要做到這一點,我需要一個透視投影,其中通常的渲染平面(z = 0)上的所有內容看起來與使用正交投影時相同。

任何人都知道如何創建這樣的投影?數學似乎稍微超出了我。據推測,我希望我的平截頭體的遠平面爲z = 0,其中(0,0,0)對應於左下像素,但glFrustum()等人只允許我指定近平面的座標。我如何選擇明智的近遠值?我試過使用glFrustum()和gluLookAt()從屏幕上方的某個點觀察起點,但我該如何選擇眼點?

回答

0

我想我已經解決了。下面的作品適合我。

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