OpenGL绘制纹理,缩放相机导致纹理闪烁的解决方法gluPerspective ()

来源:互联网 发布:杭州汉聚网络骚扰电话 编辑:程序博客网 时间:2024/03/29 16:04

做项目的时候,对三维场景中的物体进行了纹理贴图。但是奇怪的是,缩放相机或者是左右移动视角,变换视图矩阵时,纹理贴图会出现大范围的闪烁现象。查了好久,最后发现问题出在投影视景体的参数设置上。我们的投影视景体用的方法是:gluPerspective (GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar);

glPerspective:用于透视投影,功能和glFrustum非常相似(这个函数的具体用法可以自己去看一下),参数不一样。相对于glFrustum来说不常用。百度百科的解释如下:

void gluPerspective(
GLdouble fovy, //角度
GLdouble aspect,//视景体的宽高比
GLdouble zNear,//沿z轴方向的两裁面之间的距离的近处
GLdouble zFar //沿z轴方向的两裁面之间的距离的远处
)
PARAMETERS(参数含义)
fovy
Specifies the field of view angle, in degrees, in the y direction.
指定视景体的视野的角度,以度数为单位,y轴的上下方向
aspect
Specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
指定你的视景体的宽高比(x 平面上)
zNear
Specifies the distance from the viewer to the near clipping plane (always positive).
指定观察者到视景体的最近的裁剪面的距离(必须为正数)
zFar
Specifies the distance from the viewer to the far clipping plane (always positive).
与上面的参数相反,这个指定观察者到视景体的最远的裁剪面的距离(必须为正数)
问题关键在于:透视投影可能会导致深度精度的损失。在显示设备中该问题很明显。问题出在深度缓存中深度的限制以及由透视投影引起的非线性比例变换。当近裁剪面非常靠近投影中心的时候,误差就非常大。就是说,当zNears设置的过于接近0时(投影中心),OpenGL就不知道如何去处理这种情况了,然后就会导致深度精度的损失。而刚开始我们项目中zNear设置成了0.1,调整为1以后,纹理闪烁的问题就消失了。
以下两张图片,左边是zNear为0.1时的效果图,右边是zNear为1时的效果图。                   
以上是对gluPerspective()的一些理解,其中有一些参考,连接如下:
http://bbs.gameres.com/thread_106693_1_1.html
http://blog.csdn.net/tan_handsome/article/details/47321385
http://bbs.gameres.com/forum.php?mod=viewthread&tid=23660&extra=&page=2
http://www.cppblog.com/COOOOOOOOL/archive/2009/12/28/104255.html
欢迎指正,共同进步。


0 0
原创粉丝点击