OpenGLES入门教程1-Tutorial01-GLKit
OpenGLES入门教程2-Tutorial02-shader入门
OpenGLES入门教程4-Tutorial04-GLKit进阶
这一次的是性能优化。
渲染的优化不是仅仅提高渲染的速度,超过60Hz的渲染速度没有任何意义,用户永远看不到这些信息。同时在考虑用电消耗的情况下,30Hz的刷新率能延长电池的使用时间。
以下的渲染优化策略总是管用的:
通过减少渲染的几何对象,在不影响显示效果的前提下,尽可能减少需要绘制的图元。
在一个场景中,很多物体是处于平截体外部,这些物体是用户永远看不到的对象。
计算眼睛到当前测试点的向量,提取这个向量关于平截体X、Y、Z轴的分量,分别进行判断。
// eye到point的向量
const GLKVector3 eyeToPoint = GLKVector3Subtract(frustumPtr->eyePosition, point);
// z轴分量
const GLfloat pointZComponent = GLKVector3DotProduct(eyeToPoint, frustumPtr->zUnitVector);
if(pointZComponent > frustumPtr->farDistance || pointZComponent < frustumPtr->nearDistance)
{
result = AGLKFrustumOut;
}
// y轴分量
const GLfloat pointYComponent =
GLKVector3DotProduct(eyeToPoint,
frustumPtr->yUnitVector);
const GLfloat frustumHeightAtZ = pointZComponent * frustumPtr->tangentOfHalfFieldOfView;
if(pointYComponent > frustumHeightAtZ || pointYComponent < -frustumHeightAtZ)
{
result = AGLKFrustumOut;
}
//X轴分量
const GLfloat pointXComponent =
GLKVector3DotProduct(eyeToPoint,
frustumPtr->xUnitVector);
const GLfloat frustumWidthAtZ = frustumHeightAtZ *
frustumPtr->aspectRatio;
if(pointXComponent > frustumWidthAtZ ||
pointXComponent < -frustumWidthAtZ)
{
result = AGLKFrustumOut;
}
测试球体会测试点更复杂,同样是对比X/Y/Z轴分量,在判断的范围加上半径的距离。
但是,考虑下面的情况
按照上面的判断,球体是在平截体之外,但是实际上是相交的。
解决方案
把半径乘以特定的因子。
如下图,考虑球体被外切情况,得出相应的放大因子。
sphereFactorY = 1.0f/cosf(halfFieldOfViewRad);
const GLfloat angleX = atanf(frustumPtr->tangentOfHalfFieldOfView * aspectRatio);
frustumPtr->sphereFactorX = 1.0f/cosf(angleX);
为什么FPS会在20FPS和30FPS之间摆动?
绘制 和 显示 并不一样。
通过CADisplayLink(hardware generated),绘制的速率可能是60FPS。
如果绘制的时间超过1/60s,理论上帧率最多为30FPS。
想象一条1s的线段,分隔成60小段,每个小段的起点都可以作为绘制的起点。
如果绘制的时间超过1/60s,那么绘制的终点会延伸到第二个小段。
这样,一条1s的线段,最多有30个绘制的时间段。
Since CADisplayLink is hardware generated the only thing you can do with it is divide the time, that's what the frameInterval is there to do. frameInterval = 1 gets you 60 fps frameInterval = 2 gets you 30 fps frameInterval = 3 gets you 20 fps I use a lot frameInterval = 5 for menus for example, it still gives me 12fps (about the minimum for reasonable simple animation) and the battery consumption reduces drastically.
你能得到FPS,但是它不代表真正的性能,每帧持续时间是一个更佳选择。FPS不能线性评判性能表现。
For example, if your FPS goes from 30 to 20, then that's a pretty significant decrease in frame performance time (33 ms to 50 ms) However if your FPS goes from 2000 to 400, that's a miniscule difference in real time (only 2ms difference).
最后,即使你自己通过自定义线程(不采用CADisplayLink),把绘制时间的空缺填补,实际上绘制的速率并不会变快。
主要讲解的是数学部分的知识,OpenGL ES的部分没有引入新的技术点。
工作原因,以后更新会慢一些。能看到这里,你也是喜欢技术的,谢谢支持。来一波关注和喜欢如何 -> 我会加油更新。
附上源码