查找/重映射的OpenGL ES边界坐标平面 [英] Finding/Remapping bounds of OpenGL ES coordinate plane

查看:230
本文介绍了查找/重映射的OpenGL ES边界坐标平面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让2D图形为我的Andr​​oid应用程序,包括六个薄的矩形,每个占用宽屏幕的约1/6日和平等屏幕的高度。我不知道,以确定x的范围以正确的方式和y OpenGL的屏幕坐标平面。最后,我将需要编写这6个矩形触摸事件发生,所以我一直在努力测试,通过重新映射的OpenGL的坐标平面到设备的屏幕坐标平面(其中原点(0,0)是解决这一问题的逻辑在屏幕代替中间的左上

I'm trying to make 2D graphics for my Android app that consists of six thin rectangles that each take up about 1/6th of the screen in width and equal the screen's height. I'm not sure the right way to determine the bounds of the x and y OpenGL coordinate plane on screen. Eventually I will need to write logic that tests which of the 6 rectangles a touch event occurs in, so I have been trying to solve this problem by remapping OpenGL's coordinate plane into the device's screen coordinate plane (where the origin (0,0) is at the top left of the screen instead of the middle.

我宣布参加六矩形之一,像这样:

I declare one of my six rectangles like so:

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的显示。请告诉我理想的方式做到这一点?

but since i'm not sure what the visible limits are on the x and y planes (in the OpenGL coordinate system) I have no concrete way of knowing what vertices my rectangle needs to be instantiated with to occupy 1/6th of the display. Whats the ideal way to do this?

我试过的方法,如 重新映射OpenGL的坐标为方便与设备屏幕的工作坐标:

I've tried approaches such as using glOrthoof() to remap OpenGL's coordinates into easy to work with device screen coordinates:

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();

但是当我完全做我的矩形自败。

but when I do my rectangle dissapears completely.

推荐答案

您肯定不希望使用透视投影2D图形。这只是没有多大意义。透视投影是......嗯,营造出透视投影,如果你的对象实际上是放置在三维空间中这是唯一有用的。

You certainly don't want to use a perspective projection for 2D graphics. That just doesn't make much sense. A perspective projection is for... well, creating a perspective projection, which is only useful if your objects are actually placed in 3D space.

更糟的是,你有两个电话设置透视矩阵:

Even worse, you have two calls to set up a perspective matrix:

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);

虽然这是合法的,它很少是有道理的。如果你这样做实际上发生的事情是,这两个预测是连续应用。所以,我们要做的第一件事就是甩掉 gluPerspective()通话。

要放在你的6矩形,你有几种选择。几乎一个最简单的是在所有不适用任何转换。这意味着,要指定在标准化设备坐标(又名NDC),这是在x和y方向的两个的范围内的[-1.0,1.0]的输入坐标。因此,对于6矩形并排呈现的一面,你会用[-1.0,1.0]的y范围的所有矩形,和X-范围[-1.0,-2.0 / 3.0]为先,[-2.0 /3.0,-1.0 / 3.0]为第二等

To place your 6 rectangles, you have a few options. Almost the easiest one is to not apply any transformations at all. This means that you will specify your input coordinates in normalized device coordinates (aka NDC), which is a range of [-1.0, 1.0] in both the x- and y-direction. So for 6 rectangles rendered side by side, you would use a y-range of [-1.0, 1.0] for all the rectangles, and an x-range of [-1.0, -2.0/3.0] for the first, [-2.0/3.0, -1.0/3.0] for the second, etc.

另一种选择是,你使用正投影,使指定的矩形就更方便了。例如,一个范围[0.0,6.0]为x和[0.0,1.0]的y的将使它特别容易

Another option is that you use an orthographic projection that makes specifying the rectangles even more convenient. For example, a range of [0.0, 6.0] for x and [0.0, 1.0] for y would make it particularly easy:

gl.glOrthof(0.0f, 6.0f, 0.0f, 1.0f, -1.0f, 1.0f);

然后所有的矩形有[0.0,1.0]的y范围内,第一个矩形为[0.0,1.0]一个x范围,第二个矩形[1.0,2.0],等等。

Then all rectangles have a y-range of [0.0, 1.0], the first rectangle has a x-range of [0.0, 1.0], the second rectangle [1.0, 2.0], etc.

顺便说一句,如果你只是用OpenGL开始,我会通过对ES 1.x中,并直接学习ES 2.0。 ES 1.x的是在这一点上遗留API,我不会将其用于任何新的发展。

BTW, if you're just starting with OpenGL, I would pass on ES 1.x, and directly learn ES 2.0. ES 1.x is a legacy API at this point, and I wouldn't use it for any new development.

这篇关于查找/重映射的OpenGL ES边界坐标平面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆