Home > other >  Center rectangle-object in the screen by using glOrthof in Android with opengles 2, with java
Center rectangle-object in the screen by using glOrthof in Android with opengles 2, with java

Time:01-02

I'm working on the examples of the book OpenGlEs 2 for Android. I did the first example, for drawing a rectangle of base 9, and height 14, by using the below array for defining the coordinates

private float[] tableVerticesWithTriangles = {
        //Triangle
        0f, 0f,
        9f, 14f,
        0f, 14f,
        //Triangle 2
        0f, 0f,
        9f, 0f,
        9f, 14f
};

The rectangle is appearing as in the example, the white rectangle in the top right corner:

rectangle drawn as shown in the book

The code I'm working on is in the repository enter image description here

The key is to know the width and the height of the collision box of the object I want to draw, then it is just a matter of scaling the left, right or bottom/top variables based on the orientation of the screen, with the aspectRatio variable. I placed the code in the repository:

https://github.com/quimperval/opengl-es-android-draw-object

  • Related