How to ensure that a plane perfectly fills the viewport in OpenGL ES

ぐ巨炮叔叔 提交于 2019-12-21 19:45:33

问题


I'm trying to create a flat plane such that it perfectly fills the viewport in an OpenGL ES application. I can achieve this effect by creating the plane and then experimentally moving it forwards and backwards until I get the right effect, but I'm certain that it must be possible to work out exactly how far away from the camera it should be. I would welcome any pointers!

I need to be accurate because the plane has a texture applied to it, which I want to fill the window and not be clipped at all.

Thanks!


回答1:


Use glFrustum instead of gluPerspective to set the perspective projection. With glFrustum you define the projection in terms of a plane you want to display viewport-filling. Pseudocode:

aspect = win.width/win.height
bt = tan( fov/2 ) 
lr = bt * aspect

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glFrustum( -lr * zclip.near,
            lr * zclip.near,
           -bt * zclip.near,
            bt * zclip.near,
            zclip.near, zclip.far)

glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

vertices = [
    (-lr * planedist, -bt * planedist, -planedist),
    ( lr * planedist, -bt * planedist, -planedist),
    ( lr * planedist,  bt * planedist, -planedist),
    (-lr * planedist,  bt * planedist, -planedist)
]
draw_vertices(vertices)



回答2:


Projecting rays through the screen/viewport corners onto your plane should give your the correct vertices for the corners.



来源:https://stackoverflow.com/questions/4792952/how-to-ensure-that-a-plane-perfectly-fills-the-viewport-in-opengl-es

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!