Lesson 168. OpenGL. Introduction.
Android Lessons

Lesson 168. OpenGL. Introduction.


In this lesson:

– we create the simplest example from OpenGL

We continue the topic of graphics, and move on to the next level, called OpenGL ES. It stands for OpenGL for Embedded Systems, ie OpenGL for Embedded Systems (android devices in our case).

A couple of years ago, I read a book on the subject, made examples from it, and in general, without much difficulty, understood everything there was written. But the book was on OpenGL ES version 1.0. Now this version is already outdated and versions 2.0, 3.0 and 3.1 are used. These API versions are substantially different from and incompatible with 1.0. Therefore, I myself will have to study the subject almost again.

The first lesson will be similar to Lesson 141. We’ll do a minimal set of actions to fill the screen with any color, but this time we’ll do it with OpenGL. By the way, I just want to warn you that OpenGL is not necessarily 3D. First we draw a little 2D, and then add the volume.

Well, as always, at first, most likely, little will be understood, but as you dive into the subject the overall picture will become clearer.

Let’s start creating our first minimal example. Let’s discuss its key elements.

1) The image has something to show. For this we will use the GLSurfaceView component (hereinafter referred to as surface).

2) Someone has to create an image, that is, to receive from us instructions on how and how to draw. This will be done by Renderer (hereinafter referred to as “Renderer”).

3) Well, you will need to check that the device supports OpenGL 2.0, otherwise nothing will work.

Let’s start by creating a rendering class. We will then pass the object of this rendering class to the surface, which in the course of our work will call rendering methods.

Render has three methods:

onSurfaceCreated – invoked when creating / re-creating a surface. That is, the method will be called when you start the program or, for example, in an already running application when the device goes out of sleep. This will install the OpenGL parameters and initialize the graphical objects.

onSurfaceChanged – Called when resizing surface. The most common example is changing the screen orientation.

onDrawFrame – Called when the surface is ready to display the next frame. In this method we will create images.

we create a class OpenGLRendererImplementing the Renderer interface:

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT;
import static android.opengl.GLES20.glClear;
import static android.opengl.GLES20.glClearColor;
import static android.opengl.GLES20.glViewport;

import android.opengl.GLSurfaceView.Renderer;

public class OpenGLRenderer implements Renderer {

  @Override
 
public void onDrawFrame(GL10 arg0) {
   
glClear(GL_COLOR_BUFFER_BIT);
   
 
}

  @Override
 
public void onSurfaceChanged(GL10 arg0, int width, int height) {
   
glViewport(0, 0, width, height);

  }

  @Override
 
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
   
glClearColor(0f, 1f, 0f, 1f);
 
}

}

IN onSurfaceCreated we call the glClearColor method and pass it the RGBA components in the range from 0 to 1. This sets the default color to be displayed after full surface cleaning.

And in the method onDrawFrame we are just doing this cleaning. The glClear method with parameter GL_COLOR_BUFFER_BIT clears all the colors on the screen and sets the color specified by the glClearColor method.

In the method onSurfaceChanged we use the glViewPort method to specify the surface area that will be available to display the image. We indicate the left lower point – (0,0) and the size of the area – (width, height), that is, the image will be displayed on all surface.

Render ready. Now we need to hang the surface in Activity and adjust it.


import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 
private GLSurfaceView glSurfaceView;

  @Override
 
protected void onCreate(Bundle savedInstanceState) {
   
super.onCreate(savedInstanceState);
   
if (!supportES2()) {
     
Toast.makeText(this, "OpenGl ES 2.0 is not supported", Toast.LENGTH_LONG).show();
      finish
();
     
return;
   
}
   
glSurfaceView = new GLSurfaceView(this);
    glSurfaceView.setEGLContextClientVersion
(2);
    glSurfaceView.setRenderer
(new OpenGLRenderer());
    setContentView
(glSurfaceView);
 
}
 
 
@Override
 
protected void onPause() {
   
super.onPause();
    glSurfaceView.onPause
();
 
}
 
 
@Override
 
protected void onResume() {
   
super.onResume();
    glSurfaceView.onResume
();
 
}
 
 
private boolean supportES2() {
       
ActivityManager activityManager =
               
(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo
();
           
return (configurationInfo.reqGlEsVersion >= 0x20000);
 
}

}

IN onCreate first, with our supportES2 method, we determine that the device supports OpenGL ES 2.0 and higher. If not, then we close.

If everything is ok then
– create GLSurfaceView,
– we tell him by the setEGLContextClientVersion method that we will use OpenGL ES version 2
– we pass an instance of our OpenGLRenderer class using the setRenderer method. This renderer will now be responsible for drawing on the surface
– setContentView method sets surface as the primary View for Activity

In addition, you need to bind the surface to lifecycle Activity methods: onPause and onResume, Calling them surface methods.

You are done. run

The screen is green. The first simplest OpenGL application is ready. Not Need For Speed, of course, but where to start)

Three points I would like to dwell on

1) For some reason, the alpha component in the glClearColor method does not work. That is, pass the last parameters at least 0 though 1, transparency is not added. I do not have an answer to this question yet.

2) The viewport coordinates we set with the glViewport method do not affect the result in any way, and even if you set the viewport area to only half the surface, all the surface will be painted green. In this regard, I read that this is the norm. The glClear method works on all surfaces, regardless of the size of the viewport.

3) About launching applications. Usually it is written that OpenGL ES does not break into emulators. I did not check on a standard emulator, but on Genymotion it starts without problems. Extreme is always a real device, you can test it.




Discuss in the forum [5 replies]

Leave a Reply

Your email address will not be published. Required fields are marked *