【发布时间】:2015-04-19 13:37:21
【问题描述】:
我正在尝试采用 opengl es 坐标系并将其转换为屏幕坐标系。这将使我能够在屏幕上找到顶点位置,并使碰撞检测更容易。到目前为止,我已经找到了顶点并将它们乘以模型视图投影矩阵,然后使用 android 屏幕的高度和宽度(以像素为单位),我已经将顶点转换为正确的范围。我必须进行转换,因为opengl es坐标系的原点位于屏幕的中心并且从-1变为1。而屏幕的原点来自屏幕的左下角,并且当设备处于横向模式时我'我们发现尺寸为 800x480 像素。
这就是问题所在,当我查看一个顶点在屏幕上可以具有的值范围时,它的范围约为 147 - 640 宽度和 185 - 480 高度。它的范围应为 0-800 宽度和 0-480 高度。
我的代码有什么问题?是模型视图投影矩阵,还是我使用了错误的屏幕测量值,或者是我从 opengl es 范围转换为屏幕像素范围的方式。
这会找到形状顶点,将其乘以 mvp 矩阵并转换值的范围,以便它们具有像素屏幕尺寸。我只检查一个形状顶点。
dimension[0] = MyGLSurfaceView.width;
dimension[1] = MyGLSurfaceView.height;
float starW;
float starH;
for (int i = 0; i < star.vertices.length; i += star.vertices.length) {//only checking one vertex
Matrix.multiplyMM(starVerts, 0, mMVPMatrix, 0, star.vertices, 0);//vertices multiplied by model view projection matrix
//starVerts[i] is in the range .433 to -.466 should be 1 to -1
//starVert[i+1] is in the range .973 to -.246 should be 1 to -1
starW = (starVerts[i] * (dimension[0] / 2)) + (dimension[0] / 2);//should be range 0-800 // instead 147 - 640
starH = (starVerts[i + 1] * (dimension[1] / 2)) + (dimension[1] / 2);//should be range 0-480 // instead 185 - 480
这就是我找到 mvp 矩阵的方法
@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);// Sets the current view port to the new size.
float RATIO = (float) width / height;
Matrix.frustumM(mProjectionMatrix, 0, -RATIO, RATIO, -1, 1, 3, 7);// this projection matrix is applied to object coordinates in the onDrawFrame() method
}
@Override
public void onDrawFrame(GL10 unused) {
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);// Set the camera position (View matrix)
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);// Calculate the projection and view transformation
这就是我找到安卓屏幕尺寸(以像素为单位)的方法
Display display = ((WindowManager)
context.getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y;
width = size.x;
自derhass回答后更新
starVerts[i] = starVerts[i]/starVerts[i+3]; //clip.x divided by clip.w
starVerts[i+1] = starVerts[i+1]/starVerts[i+3];//clip.y divided by clip.w
【问题讨论】:
标签: android opengl-es coordinates