我最近开始使用openGL,因为普通视图中的画布不再足以满足我的需求。但我似乎无法理解opengl中的坐标系统。
当我制作一个底部坐标为y = 0的矩形时,我会在屏幕的上半部分得到一个矩形的结果。这是可以预料的。
但是,当我设置y = 0.25f时,我预计矩形将是屏幕的四分之一顶部。但实际上它更少,比如1/6或1/7。
或者当我尝试y = 0.375f(应该是屏幕的1/8)时,结果类似于1/11或1/12。 some1可以解释为什么会这样吗?我错过了什么。那我怎么能做一个填满屏幕前1/8的矩形呢?
(我知道屏幕的顶部和底部有 - 缩放时的+ 0.5f坐标)
答案 0 :(得分:3)
这可能是由View矩阵中相机的位置引起的。 回想一下OpenGL中坐标系的方向:
设置相机:
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 2, 0f, 0f, 0f, 0f, 1.0f, 0f);
// My camera is located at (0,0,2),
// it's looking toward (0,0,0)
// its top is pointing along (0,1,0) aka. Y-axis.
如果我的矩形位于XY平面上的某个位置,意味着Z坐标为0,那么我的相机在正Z轴上距离它2个单位。从该位置查看矩形使其看起来比预期的小。要使矩形看起来更大,请尝试通过更改其在视图矩阵中的位置来将相机移近它。我将Z坐标从2更改为1:
Matrix.setLookAtM(mVMatrix, 0, 0, 0, 1, 0f, 0f, 0f, 0f, 1.0f, 0f);
// My camera is located at (0,0,1),
// it's looking toward (0,0,0)
// its top is pointing along (0,1,0) aka. Y-axis.
答案 1 :(得分:0)
示例中的代码示例:
Android Training > Applying Projection and Camera Views
定义相机视图
// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
在我的情况下产生一个坐标系,y轴范围从-1到+1。
定义投影
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
更正窗口比例