搬运以前在csdn的博文
之前大概接触过OpenGL ES 1.0,最近买了本书打算学习一下2.0的相关知识。本来以为顶多是多了一些API而已,结果真正学起来才发现用法完全不一样。最大的不同还是多了shader这么一个概念,目前我也没法完全理解shader是什么,只能在学习的过程中慢慢理解。不过基本的东西还都没变,点,线,三角等等
首先要使用es2.0的话需要先在manifest文件里声明
以下为Activity的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 隐藏标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mPoint = new CZPoint();
// 初始化GLSurfaceView
GLSurfaceView glSurfaceView = new GLSurfaceView(this);
glSurfaceView.setEGLContextClientVersion(2);
glSurfaceView.setRenderer(new CZRenderer(mPoint));
setContentView(glSurfaceView);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// 获取touch的事件
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
mPoint.setPosition(x * 2 / CZRenderer.width - 1.0f, 1.0f - y * 2 / CZRenderer.height);
mPoint.setColor(0.0f, 1.0f, 0.0f, 1.0f);
break;
case MotionEvent.ACTION_UP:
mPoint.setPosition(x * 2 / CZRenderer.width - 1.0f, 1.0f - y * 2 / CZRenderer.height);
mPoint.setColor(0.0f, 0.0f, 1.0f, 1.0f);
break;
}
return super.onTouchEvent(event);
}
为了以后的扩展性定义了一个接口:
public interface ICZgles {
void init(int program, int vertexShader, int fragmentShader);
String getVertexShader();
String getFragmentShader();
void draw();
}
以下为顶点相关的类,其中涉及到glsl语言,以后还得慢慢学
public class CZPoint implements ICZgles {
private static final String TAG = "CZPoint";
private static final String VERTEX_SHADER
= "attribute vec4 a_Position;"
+ "void main() {"
+ " gl_Position = a_Position;"
+ " gl_PointSize = 50.0;"
+ "}";
private static final String FRAGMENT_SHADER
= "precision mediump float;"
+ "uniform vec4 u_FragmentColor;"
+ "void main() {"
+ " gl_FragColor = u_FragmentColor;"
+ "}";
private int mAPosition;
private int mUFragmentColor;
private float[] mPosition = {0.0f, 0.0f};
private float[] mColor = {1.0f, 0.0f, 0.0f, 1.0f};
CZPoint() {
Log.d(TAG, "chenzhen");
}
@Override
public void init(int program, int vertexShader, int fragmentShader) {
mAPosition = GLES20.glGetAttribLocation(program, "a_Position");
mUFragmentColor = GLES20.glGetUniformLocation(program, "u_FragmentColor");
}
@Override
public String getVertexShader() {
return VERTEX_SHADER;
}
@Override
public String getFragmentShader() {
return FRAGMENT_SHADER;
}
@Override
public void draw() {
GLES20.glVertexAttrib3f(mAPosition, mPosition[0], mPosition[1], 0.0f);
GLES20.glUniform4f(mUFragmentColor, mColor[0], mColor[1], mColor[2], mColor[3]);
GLES20.glDrawArrays(GLES20.GL_POINTS, 0, 1);
}
public void setPosition(float x, float y) {
mPosition[0] = x;
mPosition[1] = y;
}
public void setColor(float r, float g, float b, float a) {
mColor[0] = r;
mColor[1] = g;
mColor[2] = b;
mColor[3] = a;
}
}
最后是Renderer的代码:
public class CZRenderer implements GLSurfaceView.Renderer {
public static float width = 0;
public static float height = 0;
private int mProgram;
private String mVertexShaderSource;
private String mFragmentShaderSource;
private ICZgles iCZ;
public CZRenderer(ICZgles icZgles) {
iCZ = icZgles;
mVertexShaderSource = iCZ.getVertexShader();
mFragmentShaderSource = iCZ.getFragmentShader();
}
@Override
public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
mProgram = GLES20.glCreateProgram();
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, mVertexShaderSource);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, mFragmentShaderSource);
GLES20.glAttachShader(mProgram, vertexShader);
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glLinkProgram(mProgram);
GLES20.glUseProgram(mProgram);
iCZ.init(mProgram, vertexShader, fragmentShader);
}
@Override
public void onSurfaceChanged(GL10 gl10, int i, int i1) {
GLES20.glViewport(0, 0, i, i1);
width = i;
height = i1;
}
@Override
public void onDrawFrame(GL10 gl10) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
iCZ.draw();
}
private int loadShader(int type, String source) {
int shader = GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
return shader;
}
}
针对shader多了很多api,也需要慢慢理解