Graphics Programs Reference
Note We used the glClear method in almost all applications so
far, but the argument we usually passed (like, in GL SURFACE
application) was different from what we discussed in this topic.
This was done intentionally, so you get into the habit of using
the code snippet shown in Listing 3-7 ; there would be no change
GLES20.GL_DEPTH_BUFFER_BIT ) from the argument.
Listing 3-7. GL SURFACE CHANGED/src/com/apress/android/glsur-
Setting the Viewport
When the display device accesses “front” color buffer to display a final image of the
graphics rendered using OpenGL ES, it needs to know about the viewport - that is,
the area on the display screen on which this image is to be mapped.
We set the viewport using the glViewport(int x, int y, int width,
int height) method, where (x, y) is a position on the display screen, meas-
ured from its bottom-left corner (in pixels). The rest of the arguments are for setting
the size of viewport in pixels. To ensure the viewport is visible, (x, y) should lie
within the bottom-left (x=0, y=0) and top-right (x=width, y=height) corners of the
So, while setting the size of the viewport, we make use of the int width and int
height arguments of the onSurfaceChanged method. These arguments store
the width and height of the display screen in any orientation. For this reason, we
need to call the glViewport method inside the onSurfaceChanged method.
Whenever the orientation of the device changes, the onSurfaceChanged method
helps keep track of the new width and height of the display screen in landscape or