Android OpenGL-简单的2D转换遇到的困难

Android OpenGL-简单的2D转换遇到的困难,第1张

概述由于性能原因,我试图将游戏中的图形迁移到OpenGL.>我需要使用精确的屏幕坐标绘制对象.在240×320屏幕的中央说一个100×100像素的框.>我需要围绕Z轴旋转它,以保持其大小.>我需要绕X轴旋转并具有透视效果,以保持(或接近)其大小.>我需要绕Y轴旋转并具有透视效果,以保持(或接近)其大

由于性能原因,我试图将游戏中的图形迁移到OpenGL.

>我需要使用精确的屏幕坐标绘制对象.在240×320屏幕的中央说一个100×100像素的框.
>我需要围绕Z轴旋转它,以保持其大小.
>我需要绕X轴旋转并具有透视效果,以保持(或接近)其大小.
>我需要绕Y轴旋转并具有透视效果,以保持(或接近)其大小.

Here’s a picture.

到目前为止,我设法完成了前两个任务:

public voID onDrawFrame(GL10 gl) {    gl.glClear(GL10.GL_color_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    gl.glLoadIDentity();    gl.glTranslatef(120, 160, 0); // move rotation point     gl.glrotatef(angle, 0.0f, 0.0f, 1.0f); // rotate     gl.glTranslatef(-120, -160, 0); // restore rotation point    mesh.draw(gl); // draws 100x100 px rectangle with the following coordinates: (70, 110, 170, 210)}public voID onSurfaceChanged(GL10 gl, int wIDth, int height) {    gl.glVIEwport(0, 0, wIDth, height);    gl.glMatrixMode(GL10.GL_PROJECTION);    gl.glLoadIDentity();    gl.glOrthof(0f, (float)wIDth, (float)height, 0f, -1f, 1f);    gl.glMatrixMode(GL10.GL_MODELVIEW);    gl.glLoadIDentity();}

但是,当我尝试将框围绕x或y旋转时,我的框正在发生讨厌的事情,并且没有透视效果.我尝试使用其他函数代替glrotate(glFrustum,glPerspective,gluLookAt,应用“倾斜”矩阵),但无法使其正常工作.

解决方法:

I’m trying to migrate graphics in my game to OpenGL for performance reasons.

I need to draw an object using exact screen coordinates. Say a Box 100×100 pixels in the center of 240×320 screen.

对于透视图,您还需要一定长度的镜头,这决定了FOV. FOV是视平面距离与可见范围的比率.因此,在近平面的情况下,它变为{左,右,上,下} /近.为了简单起见,我们假设水平FOV和对称投影,即

FOV = 2*|left|/near = 2*|right|/near = extent/distance

或者如果您更喜欢角度

FOV = 2*tan(angular FOV / 2)

对于90°FOV,镜头的长度是焦平面宽度的一半.您的焦平面为240×320像素,因此左右分别为120,顶部和底部为160. OpenGL并没有真正的重点,但是可以说,近与远之间的中间平面是“焦点”.

因此,假设对象的平均范围约为可见平面限制的数量级,即对于240×360的可见平面,对象的平均大小约为200px.因此,有意义的是接近远剪切的距离为200,因此-围绕焦平面为100.因此,对于90°的FOV,焦平面具有一定距离

2*tan(90°/2) = extent/distance2*tan(45°) = 2 = 240/distance             2*distance = 240             distance = 120

120,因此近和远的裁剪距离为20和220.

最后但并非最不重要的一点是,必须通过near_distance / focal_distance = 20/120缩放近剪辑平面限制

所以

left   = -120 * 20/120 = -20right  =  120 * 20/120 =  20bottom = -180 * 20/120 = -30top    =  180 * 20/120 =  30

因此,这为我们提供了glFrustum参数:

glMatrixMode(GL_PROJECTION);glLoadIDentity();glFrustum(-20, 20, -30, 30, 20, 220);

最后但并非最不重要的一点是,我们必须将世界原点转移到“焦点”平面中

glMatrixMode(GL_MODELVIEW);glLoadIDentity();glTranslatef(0, 0, -120);

I need to rotate it around Z axis, preserving its size.

完成.

I need to rotate it around X axis, with perspective effect, preserving (or close to) its size.
I need to rotate it around Y axis, with perspective effect, preserving (or close to) its size.

透视图不会保留大小.这就是使它成为一种观点的原因.您可以使用很长的镜头,即小的FOV.

代码更新

作为一般提示:在图形处理程序中执行所有OpenGL *** 作.不要在整形处理程序中设置投影.这很丑陋,一旦您想要使用HUD或其他类型的覆盖层,就必须将其丢弃.因此,这是更改方法:

public voID onDrawFrame(GL10 gl) {    // fov, extents are parameters set somewhere else    // 2*tan(fov/2.) = wIDth/distance =>    float distance = wIDth/(2.*tan(fov));    float near = distance - extent/2;    float far  = distance + extent/2;    if(near < 1.) {        near = 1.;    }    float  left  =  (-wIDth/2) * near/distance;    float  right =  ( wIDth/2) * near/distance;    float bottom = (-height/2) * near/distance;    float    top = ( height/2) * near/distance;    gl.glClear(GL10.GL_color_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    gl.glVIEwport(0, 0, wIDth, height);    gl.glMatrixMode(GL10.GL_PROJECTION);    gl.glLoadIDentity();    gl.glFrustum(left, right, bottom, top, near, far);    gl.glMatrixMode(GL10.GL_MODELVIEW);    gl.glLoadIDentity();    gl.glTranslatef(0, 0, -focal);    gl.glTranslatef(120, 160, 0); // move rotation point     gl.glrotatef(angle, 0.0f, 0.0f, 1.0f); // rotate     gl.glTranslatef(-120, -160, 0); // restore rotation point    mesh.draw(gl); // draws 100x100 px rectangle with the following coordinates: (70, 110, 170, 210)}public voID onSurfaceChanged(GL10 gl, int new_wIDth, int new_height) {    wIDth = new_wIDth;    height = new_height;}

总结

以上是内存溢出为你收集整理的Android OpenGL-简单的2D转换遇到的困难全部内容,希望文章能够帮你解决Android OpenGL-简单的2D转换遇到的困难所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1084212.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存