【发布时间】:2015-10-07 02:42:48
【问题描述】:
我正在尝试为我的 Android 应用程序制作 2D 图形,该应用程序由六个细长的矩形组成,每个矩形的宽度约为屏幕的 1/6,并且等于屏幕的高度。我不确定确定屏幕上 x 和 y OpenGL 坐标平面边界的正确方法。最终我需要编写逻辑来测试触摸事件发生在 6 个矩形中的哪一个,所以我一直试图通过将 OpenGL 的坐标平面重新映射到设备的屏幕坐标平面(原点 (0,0) 是在屏幕的左上角而不是中间。
我这样声明我的六个矩形之一:
private float vertices1[] = {
2.0f, 10.0f, 0.0f, // 0, Top Left
2.0f, -1.0f, 0.0f, // 1, Bottom Left
4.0f, -1.0f, 0.0f, // 2, Bottom Right
4.0f, 10.0f, 0.0f, // 3, Top Right
};
但由于我不确定 x 和 y 平面(在 OpenGL 坐标系中)上的可见限制是什么,我没有具体的方法来知道我的矩形需要实例化哪些顶点才能占据 1/6显示器。这样做的理想方法是什么?
我尝试了诸如using glOrthoof() 之类的方法来将 OpenGL 的坐标重新映射为易于使用的设备屏幕坐标:
gl.glViewport(0, 0, width, height);
// Select the projection matrix
gl.glMatrixMode(GL10.GL_PROJECTION);
// Reset the projection matrix
gl.glLoadIdentity();
// Calculate the aspect ratio of the window
GLU.gluPerspective(gl, 45.0f,(float) width / (float) height,0.1f, 100.0f);
gl.glOrthof(0.0f,width,height, 0.0f, -1.0f, 5.0f);
// Select the modelview matrix
gl.glMatrixMode(GL10.GL_MODELVIEW);
// Reset the modelview matrix
gl.glLoadIdentity();
但是当我这样做时,我的矩形完全消失了。
【问题讨论】: