5

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,           
                                          };
1
  • did you load the image with GLUtils.texImage2D? Commented Dec 17, 2019 at 14:11

3 Answers 3

5

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);
Sign up to request clarification or add additional context in comments.

3 Comments

I know how opengl coordinate system is but there a confuse where is the 0,0 of the image. I want to understand how opengl reads the image for texture mapping. In my texture array noncommented texture elements must be correct combination according to opengl coordinate system but the image is drawn reversed vertically. It is correct in combination of commented texture array.
Thanks, worked like a charm. However still I don't get why the image is drawn reversed, tried everything, changed vertex order to anything possible, but still the issue remains!!
Instead of flipping the bitmap you can also just flip the texture coordinate.
0

Actually, I think Will Kru's solution should have flipped the background around both axes

flip.postScale(-1f, -1f);

That solution worked for me!

Comments

0

This worked for me. (no need to create another bitmap and scale)

 private float vertices[] = { 
                   1.0f, -1.0f, 0.0f,     //Bottom Right
                   1.0f,  1.0f, 0.0f,     //Top Right
                  -1.0f,  1.0f, 0.0f,     //Top Left
                  -1.0f, -1.0f, 0.0f,     //Bottom Left
              };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.