在opengl android中显示图像的纹理坐标反转

kml*_*ckr 4 android opengl-es

我写了一个opengl android代码来显示一个正方形的位图.但是位图是相反的.当我将纹理数组组合更改为注释代码时,它被正确绘制.但我坚持我的纹理数组必须如下所示.我错了吗?

  /** The initial vertex definition */
  private float vertices[] = { 
                      -1.0f, 1.0f, 0.0f,      //Top Left
                      -1.0f, -1.0f, 0.0f,     //Bottom Left
                      1.0f, -1.0f, 0.0f,      //Bottom Right
                      1.0f, 1.0f, 0.0f        //Top Right
                                      };
  /** Our texture pointer */
  private int[] textures = new int[1];

  /** The initial texture coordinates (u, v) */
  private float texture[] = {         
          //Mapping coordinates for the vertices
//            1.0f, 0.0f,
//            1.0f, 1.0f,
//            0.0f, 1.0f,
//            0.0f, 0.0f,
          0.0f, 1.0f,
          0.0f, 0.0f,
          1.0f, 0.0f,
          1.0f, 1.0f,
                              };

    /** The initial indices definition */ 
    private byte indices[] = {
                      //2 triangles
          0,1,2, 2,3,0,           
                                          };
Run Code Online (Sandbox Code Playgroud)

Wil*_*Kru 5

Android使用左上角作为坐标系的0,0,而OpenGL使用左下角为0,0这就是你的纹理被翻转的原因.

一个常见的解决方案是在加载时翻转纹理,

Matrix flip = new Matrix();
flip.postScale(1f, -1f);
Bitmap bmp = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), flip, true);
Run Code Online (Sandbox Code Playgroud)

  • 您也可以只翻转纹理坐标,而不是翻转位图. (2认同)