http://blog.sina.com.cn/s/blog_6084f588010192ug.htmlhtml
在opengles1.1中设置正交矩阵只要一个函数调用就能够了:glOrthof,可是opengles2.0开始,为了增长渲染灵活性摆脱了固定管道渲染,这样就须要手动去实现glOrthof所对应的矩阵。编程
在iphone3D 编程一书中给出了这个矩阵的定义:iphone
void RenderingEngine2::ApplyOrtho(float maxX, float maxY) const函数
{3d
float a = 1.0f / maxX;orm
float b = 1.0f / maxY;htm
float ortho[16] = {blog
a, 0, 0, 0,ip
0, b, 0, 0,get
0, 0, -1, 0,
0, 0, 0, 1
};
GLintprojectionUniform = glGetUniformLocation(m_simpleProgram, "Projection");
glUniformMatrix4fv(projectionUniform, 1, 0, &ortho[0]);
}
这个函数用于将3d正交矩阵投影到xy平面,也就是转换到二维平面,也就是须要舍掉一个纬度。这里须要舍掉z轴,也就是z轴为零。
对矩阵分解成四个向量
float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, -1, 0,
0, 0, 0, 1
};
Ix(a,0,0,0), Iy(0,b,0,0) , Iz(0,0,-1,0) , Iw(0,0,0,1)
正常状况投影到xy平面z向量应该为零,这里他设置成了-1,有点不解,难道说是说z方向上的裁减范围,有待进一步研究。对于w了向量,是为了计算方便,能够不考虑。
float a = 1.0f / maxX;
float b = 1.0f / maxY;
对于a,b由于最终的向量要进行缩放以适应屏幕,因此这里是对x,y进行缩放的因子(把屏幕宽高理解为一个单位就好理解了)。
再来看一下对应的顶点着色器文件
const char* SimpleVertexShader = STRINGIFY(
attribute vec4 Position;
attribute vec4 SourceColor;
varying vec4DestinationColor;
uniform mat4 Projection;
uniform mat4 Modelview;
void main(void)
{
DestinationColor = SourceColor;
gl_Position = Projection * Modelview * Position;
}
);
这一行正是对映射到屏幕上点的最终变换
gl_Position = Projection * Modelview * Position;