Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Texture coordinates in opengl android showing image reversed

I wrote opengl android code to show a bitmap on a square. But bitmap was drawn in reverse. When i change texture array combination to the commented code it is drawn correctly. But i insist my texture array must be as below . Am i thinking wrong ?

  /** 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,           
                                          };
like image 343
kml_ckr Avatar asked Dec 23 '10 12:12

kml_ckr


1 Answers

Whereas Android uses the top-left corner as being 0,0 of the coordinate system, OpenGL uses the bottom-left corner being 0,0 which is why your texture gets flipped.

A common solution to this is to flip your texture at load time,

Matrix flip = new Matrix();
flip.postScale(1f, -1f);
Bitmap bmp = Bitmap.createBitmap(resource, 0, 0, resource.getWidth(), resource.getHeight(), flip, true);
like image 137
Will Kru Avatar answered Oct 26 '22 08:10

Will Kru