安卓的OpenGL ES" glDrawTexfOES"吸引倒挂ES、OpenGL、glDrawTexfOES、QUOT

2023-09-07 14:57:11 作者:知南茶温暖

我使用OpenGL为Android吸引我的二维图像。

每当我绘制用code的内容:

  gl.glViewport(aspectRatioOffset,0,屏幕宽度,screenHeight);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    GLU.gluOrtho2D(GL,aspectRatioOffset,屏幕宽度+ aspectRatioOffset,screenHeight,0);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBindTexture(GL10.GL_TEXTURE_2D,myScene.neededGraphics.get(ID)获得(animationID)获得(animationIndex));

    作物[0] = 0;
    作物[1] = 0;
    作物[2] =宽度;
    作物[3] =高度;
    ((GL11Ext)GL).glDrawTexfOES(X,Y,Z,宽,高)
 

我得到一个倒置的结果。我心中已经看到人们通过这样做解决这个问题:

 作物[0] = 0;
    作物[1] =高度;
    作物[2] =宽度;
    作物[3] = -height;
 

这也不过伤逻辑在我的应用程序,所以我想结果不被颠倒翻转。没有人知道它为什么会发生,以及任何方式避免或解决呢?

编辑:我发现了一个解决方案,但我不知道这是否是一个很好的:

  INT [] =作物新INT [4];
    作物[0] = 0;
    作物[1] = imageDimension [ID] [1];
    作物[2] = imageDimension [ID] [0];
    作物[3] = -imageDimension [ID] [1];

    ((GL11)GL).glTexParameteriv(GL10.GL_TEXTURE_2D,GL11Ext.GL_TEXTURE_CROP_RECT_OES,作物,0);
    ((GL11Ext)GL).glDrawTexfOES(X,ScreenHeight  -  Y  - 高度,0,宽度,高度);
 
唯一完美支持OpenGL ES 3.0的安卓模拟器 BlueStacks蓝叠

解决方案

倒挂定义。 OpenGL的定义(0,0)是在显示其中y向上走的左下方。 glDrawTexOES 被明确定义窗口的工作坐标,以便有在ES 1.0之间,即使没有矩阵堆栈。如果您已设置了突起或模型视图矩阵翻转到任何的OpenGL认为倒挂那么这不会有一个呼叫 glDrawTexOES 中的任何影响坐标。

什么普遍的情况是,人们加载它们的时候(因为它们忽略了原产地的OpenGL的安置)隐含翻转他们的图形,从而导致在使用它们倒挂出现 glDrawTexOES 。正确的解决方法是,如果你不希望有翻转坐标手动稍后再加载图像和/​​或设置你的矩阵堆叠时,请勿隐式翻转他们。

I'm using OpenGL for Android to draw my 2D images.

Whenever I draw something using the code:

    gl.glViewport(aspectRatioOffset, 0, screenWidth, screenHeight);
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();

    GLU.gluOrtho2D(gl, aspectRatioOffset, screenWidth + aspectRatioOffset,screenHeight, 0);

    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBindTexture(GL10.GL_TEXTURE_2D, myScene.neededGraphics.get(ID).get(animationID).get(animationIndex));

    crop[0] = 0;
    crop[1] = 0;
    crop[2] = width;
    crop[3] = height;
    ((GL11Ext)gl).glDrawTexfOES(x, y, z, width, height)

I get an upside down result. I'v seen people solve this through doing:

    crop[0] = 0;
    crop[1] = height;
    crop[2] = width;
    crop[3] = -height;

This does however hurt the logic in my application, so I would like the result to not be flipped upside down. Does anyone know why it happen, and any way of avoiding or solving it?

Edit: I found a solutions, though I don't know if it is a good one:

    int[] crop = new int[4];
    crop[0] = 0;
    crop[1] = imageDimension[ID][1];
    crop[2] = imageDimension[ID][0];
    crop[3] = -imageDimension[ID][1];

    ((GL11)gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, crop, 0);
    ((GL11Ext)gl).glDrawTexfOES(x, ScreenHeight - y - height, 0, width, height);

解决方案

Define upside down. OpenGL defines (0, 0) to be in the lower left of the display with y going upward. glDrawTexOES is explicitly defined to work in window coordinates so there's no matrix stack in between even in ES 1.0. If you've set up your projection or modelview matrices to flip coordinates to whatever OpenGL considers upside down then that's not going to have any effect during a call to glDrawTexOES.

What generally happens is that people implicitly flip their graphics when loading them (because they ignore OpenGL's placement of the origin), causing them to appear upside down when using glDrawTexOES. The correct solution is that if you don't want to have to flip coordinates manually later on then don't implicitly flip them when loading your images and/or when setting up your matrix stacks.