Overview
This is a new series. Learning OpengGl Es is actually the study notes of the "OpenGl Es Application Development Practice Guide Android Volume". If you are interested, you can directly read this book. Of course, this will record your own understanding. The following are only notes. In case you forget later
Later, the first nine chapters of the book will be analyzed and recorded in turn
Android OpenGl Es learning (1): Create an OpenGl es program
Android OpenGl Es learning (two): define vertices and shaders
Android OpenGl Es learning (3): Compile shaders
Android OpenGl Es learning (four): add color
Android OpenGl Es learning (5): adjust the aspect ratio
Android OpenGl Es learning (6): enter the three-dimensional
Android OpenGl Es learning (7): use texture
Android OpenGl Es learning (eight): build simple objects
Android OpenGl Es learning (9): add touch feedback
The final goal is to realize a simple game of hockey, like this
Create a new project
First create a new project with Android Studio
Initialize OpenGl
We use GLSurfaceView
to initialize OpenGl
, which GLSurfaceView
will handle the OpenGl
basic operations in the initialization process, such as configuring the display device (display) and rendering in the background thread, which GLSurfaceView
can process the life cycle of the Activity faster, GLSurfaceView
and provides many auxiliary methods for this
Write GLSurfaceView in xml
<android.opengl.GLSurfaceView
android:id="@+id/glsurface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Initialize GLSurfaceView in Activity
GLSurfaceView glSurfaceView = findViewById(R.id.glsurface);
Determine to support OpenGl version
We are using version 2.0, so we judge whether the phone supports version 2.0
private boolean supportsEs2() {
ActivityManager activityManager =
(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo configurationInfo = activityManager
.getDeviceConfigurationInfo();
final boolean supportsEs2 =
configurationInfo.reqGlEsVersion >= 0x20000
|| (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
&& (Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")));
return supportsEs2;
}
Configure the rendering surface for OpenGl2.0
private void initData() {
if (supportsEs2()) {
AirHockKeyRender myGlRender = new AirHockKeyRender(this);
//设置opengl版本
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(myGlRender);
//RenderMode 有两种,RENDERMODE_WHEN_DIRTY 和 RENDERMODE_CONTINUOUSLY,前者是懒惰渲染,需要手动调用
// glSurfaceView.requestRender() 才会进行更新,而后者则是不停渲染。
glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
} else {
Log.d("mmm", "不支持2.0版本");
}
}
If you support version 2.0, set the version number and set the renderer
Adapt to the life cycle of Activity
@Override
protected void onResume() {
super.onResume();
glSurfaceView.onResume();
}
@Override
protected void onPause() {
super.onPause();
glSurfaceView.onPause();
}
Create the Renderer class
class AirHockKeyRender implements GLSurfaceView.Renderer {
public AirHockKeyRender(Context context) {
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//当surface被创建时,GlsurfaceView会调用这个方法,这个发生在应用程序
// 第一次运行的时候或者从其他Activity回来的时候也会调用
GLES20.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
//在Surface创建以后,每次surface尺寸大小发生变化,这个方法会被调用到,比如横竖屏切换
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
//当绘制每一帧数据的时候,会调用这个放方法,这个方法一定要绘制一些东西,即使只是清空屏幕
//因为这个方法返回后,渲染区的数据会被交换并显示在屏幕上,如果什么都没有话,会看到闪烁效果
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
}
GlSurafceView
In the method will be a separate thread calls rendered by default will continue to render the device's screen refresh rate, can be rendered at the request of the election, RenderMode
there are two, RENDERMODE_WHEN_DIRTY
and RENDERMODE_CONTINUOUSLY
the former is lazy rendering, you need to manually call glSurfaceView.requestRender () It will be updated, and the latter will continue to render.
method | description |
---|---|
GLES20.glClearColor | Set the color used to clear the screen. The first three parameters are red, green, and blue. The last parameter becomes alpha (alpha) representing transparency. Above we set the first one to 1, and the rest to 0, which means red. |
GLES20.glViewport | Set the viewport size, tell opengl to usher in the size of the rendered surface |
GLES20.glClear | Clear the screen and fill it with the color defined by glClearColor before |
At last
Now our first program has been written, it looks like this after running