上一篇文章具体参考上文:
Android上Java程序和Opengl通信方式和opengl es着色器
着色器原理:
我们之前多次介绍过OpenGL里面图形都是通过顶点着色器和片段着色器共同完成的,顶点着色器计算每个顶点在屏幕上的最终位置,OpenGL把这些顶点组装成点,直线,三角形并且分解成片段,会询问片段着色器每个片段的最终颜色,如果没有顶点着色器OpenGL就不知道在哪绘制图形,如果没有片段着色器就不知道要怎么绘制组成图形的点,直线,三角形的片段,所以他们总是一起工作的,最终一起合成屏幕上的一幅图像。
上文中我们已经编写了两个着色器,simple_vertex_shader.glsl和simple_fragment_shader.glsl,本文将具体讲解如何编译和使用。
加载着色器:
打开TextResourceReader类代码如下:
/***
* Excerpted from "OpenGL ES for Android",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/kbogla for more book information.
***/
package opengl.timothy.net.openglesproject_lesson2.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.content.Context;
import android.content.res.Resources;
public class TextResourceReader {
/**
* Reads in text from a resource file and returns a String containing the
* text.
*/
public static String readTextFileFromResource(Context context,
int resourceId) {
StringBuilder body = new StringBuilder();
try {
InputStream inputStream =
context.getResources().openRawResource(resourceId);
InputStreamReader inputStreamReader =
new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String nextLine;
while ((nextLine = bufferedReader.readLine()) != null) {
body.append(nextLine);
body.append('\n');
}
} catch (IOException e) {
throw