1.创建render类
**
* 绘制三角形
* Created by hxk on 2018/10/10.
*/
public class TriangleRenderer implements GLSurfaceView.Renderer {
private final Context context;
private final FloatBuffer vertexData;
private static final int POSITION_COMPONENT_COUNT = 2;
private static final int BYTES_PER_FLOAT = 4;
private int program;//存储链接程序的ID
private static final String U_COLOR = "u_Color";
private int uColorLocation;
private static final String A_POSITION = "a_Position";
private int aPositionLocation;
public TriangleRenderer(Context context) {
this.context = context;
float[] triangleVertices = {
0f, 0.5f,
-0.5f, 0f,
0.5f, 0
};
vertexData = ByteBuffer.allocateDirect(triangleVertices.length * BYTES_PER_FLOAT)
.order(ByteOrder.nativeOrder())
.asFloatBuffer();
vertexData.put(triangleVertices);//本地内存缓冲区
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//清屏 RGBA
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
String vertexShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_vertex_shader);
String fragmentShaderSource = TextResourceReader.readTextFileFromResource(context, R.raw.simple_fragment_shader);
int vertexShader = ShaderHelper.compileVertexShader(vertexShaderSource);
int fragmentShader = ShaderHelper.compileFragmentShader(fragmentShaderSource);
//链接着色器
program = ShaderHelper.linkProgram(vertexShader, fragmentShader);
//验证程序对于opengl是否有效 debug
ShaderHelper.validateProgram(program);
//告诉opengl绘制任何东西到屏幕上需要使用这里定义的程序
glUseProgram(program);
//获取uniform的位置,把位置存入uColorLocation中
uColorLocation = glGetUniformLocation(program, U_COLOR);
//获取属性位置
aPositionLocation = glGetAttribLocation(program, A_POSITION);
//关联属性与顶点数据数组的数组
//vertextData是我们在本地内存中创建的一个缓冲区,存的是位置
//确保缓冲区从头开始读数据,置0
vertexData.position(0);
//告诉opengl从缓冲区vertextData中取数据找到属性a_Position的数据
glVertexAttribPointer(aPositionLocation, POSITION_COMPONENT_COUNT, GL_FLOAT, false, 0, vertexData);
//使能顶点数组
glEnableVertexAttribArray(aPositionLocation);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
glClear(GL_COLOR_BUFFER_BIT);
//绘制桌子
glUniform4f(uColorLocation, 1.0f, 1.0f, 1.0f, 1.0f);
//画个三角形,每个三角形有三个顶点,2个三角形6个顶点
glDrawArrays(GL_TRIANGLES, 0, 3);
}
}
2.创建activity
public class TriangleActivity extends AppCompatActivity {
private GLSurfaceView glSurfaceView;
private boolean rendererSet = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
glSurfaceView = new GLSurfaceView(this);
// Check if the system supports OpenGL ES 2.0.
ActivityManager activityManager =
(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo configurationInfo = activityManager
.getDeviceConfigurationInfo();
// Even though the latest emulator supports OpenGL ES 2.0,
// it has a bug where it doesn't set the reqGlEsVersion so
// the above check doesn't work. The below will detect if the
// app is running on an emulator, and assume that it supports
// OpenGL ES 2.0.
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")));
if (supportsEs2) {
// Request an OpenGL ES 2.0 compatible context.
glSurfaceView.setEGLContextClientVersion(2);
// Assign our renderer.
glSurfaceView.setRenderer(new TriangleRenderer(this));
rendererSet = true;
} else {
/*
* This is where you could create an OpenGL ES 1.x compatible
* renderer if you wanted to support both ES 1 and ES 2. Since
* we're not doing anything, the app will crash if the device
* doesn't support OpenGL ES 2.0. If we publish on the market, we
* should also add the following to AndroidManifest.xml:
*
* <uses-feature android:glEsVersion="0x00020000"
* android:required="true" />
*
* This hides our app from those devices which don't support OpenGL
* ES 2.0.
*/
Toast.makeText(this, "This device does not support OpenGL ES 2.0.",
Toast.LENGTH_LONG).show();
return;
}
setContentView(glSurfaceView);
}
@Override
protected void onPause() {
super.onPause();
if (rendererSet) {
glSurfaceView.onPause();
}
}
@Override
protected void onResume() {
super.onResume();
if (rendererSet) {
glSurfaceView.onResume();
}
}
}
3.显示效果