正常程序都是在CPU上执行,而gles是在GPU上运行的。
先简单实现一个直角三角形
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.opengl.GLSurfaceView
android:id="@+id/gl_surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.lzc.gles01;
import androidx.appcompat.app.AppCompatActivity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private GLSurfaceView mGlSurfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGlSurfaceView = findViewById(R.id.gl_surface);
mGlSurfaceView.setRenderer(new MyRender());
}
@Override
protected void onPause() {
super.onPause();
mGlSurfaceView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGlSurfaceView.onResume();
}
package com.lzc.gles01;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
public class MyRender implements GLSurfaceView.Renderer {
//是一个直角三角形的坐标系
private final float mVertexes[] = {
0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
};
//将坐标转换为OpenGl理解的形式
private FloatBuffer mVertexBuffer;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//初始化清理颜色
GLES30.glClearColor(0f, 0f, 0f, 0f);
init();
}
private void init() {
//这里是将坐标转换为OpenGl可以理解的坐标
ByteBuffer vbb = ByteBuffer.allocateDirect(mVertexes.length * Float.BYTES);
vbb.order(ByteOrder.nativeOrder());
mVertexBuffer = vbb.asFloatBuffer();
mVertexBuffer.put(mVertexes);
mVertexBuffer.position(0);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
//宽和高可以不是手机的宽和高,可以自己更改
GLES30.glViewport(0, 0, width, height);
}
@Override
public void onDrawFrame(GL10 gl) {
//初始化清理GLES30.GL_COLOR_BUFFER_BIT
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
//启用顶点数组
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
//点是由3个浮点数组成,类型为float
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, mVertexBuffer);
//这三个点要绘制一个三角形,first代表从第一个点开始绘制
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
}
}
运行截图如下: