之前有过几个案例,使用的着色器都写在游戏中,一般的我们会写在一个文件中,通过文件来读取shader。然后会说明一个特殊的区域ByteBuffer,介绍为什么需要使用他们,然后介绍着色器的加载编译,程序的创建,附着,链接以及使用。形成一个完整的复用代码、
文件加载
public String uRes(String path){
if (mRes == null) System.out.println("八嘎!");
StringBuilder result=new StringBuilder();
try{
InputStream is=mRes.getAssets().open(path);
int ch;
byte[] buffer=new byte[1024];
while (-1!=(ch=is.read(buffer))){
result.append(new String(buffer,0,ch));
}
}catch (Exception e){
return null;
}
return result.toString().replaceAll("\\r\\n","\n");
}
将读入的结果变为一个字符串。这个通过上下文读取asset文件夹下的文件,让按照字节读取,然后变为一个字符串输出;
ByteBuffer
它是一个字节缓冲区,它的属性和方法。
- mark:记录了当前所标记的索引下标
- position:表示下次读取下标或者是写入下标
- limit:结束位置
在open gl es使用的过程中,需要数组排序用nativeOrder,根据本地的排列顺序,
操作过程:
ByteBuffer bb = ByteBuffer.allocateDirect( triangleCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(triangleCoords);
vertexBuffer.position(0);
创建着色器
创建着色器在步骤:
- 创建着色器
- 添加代码
- 编译
- 检查错误
public int loadShader(int type, String shaderCode){
//根据type创建顶点着色器或者片元着色器
int shader = GLES20.glCreateShader(type);
//将资源加入到着色器中,并编译
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
int []arr = new int[1];
GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,arr,0);
int i = arr[0];
if (i == 0){
//失败了
int [] length = new int[1];
GLES20.glGetShaderiv(shader,GLES20.GL_INFO_LOG_LENGTH,length,0);
if (length[0]>0){
String s = GLES20.glGetShaderInfoLog(shader);
System.out.println(s);
}
}
return shader;
}
创建程序
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram,vertexShader);
GLES20.glAttachShader(mProgram,fragmentShader);
GLES20.glLinkProgram(mProgram);
int lin[] = new int[1];
GLES20.glGetProgramiv(mProgram,GLES20.GL_LINK_STATUS,lin,0);
if (lin[0] == 0){
String s = GLES20.glGetProgramInfoLog(mProgram);
System.out.println(s);
}
GLES20.glDeleteShader(vertexShader);
GLES20.glDeleteShader(fragmentShader);
在链接之后,着色器就可以被删除了。
BaseScrren基本的代码就折磨多了。一个完整的代码:
package com.example.myapplication.learn.shape.base;
import android.content.res.Resources;
import android.opengl.GLES20;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
public abstract class BaseGameScreen {
protected float color[];
protected float triangleCoords[];
protected String fragmentShaderCode;
protected String vertexShaderCode;
protected FloatBuffer vertexBuffer;
protected FloatBuffer colorBuffer;
protected int mProgram ;
private Resources mRes;
public BaseGameScreen(){}
public BaseGameScreen(Resources resources){
this.mRes = resources;
}
public int loadShader(int type, String shaderCode){
//根据type创建顶点着色器或者片元着色器
int shader = GLES20.glCreateShader(type);
//将资源加入到着色器中,并编译
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
int []arr = new int[1];
GLES20.glGetShaderiv(shader,GLES20.GL_COMPILE_STATUS,arr,0);
int i = arr[0];
if (i == 0){
//失败了
int [] length = new int[1];
GLES20.glGetShaderiv(shader,GLES20.GL_INFO_LOG_LENGTH,length,0);
if (length[0]>0){
String s = GLES20.glGetShaderInfoLog(shader);
System.out.println(s);
}
}
return shader;
}
public abstract void render();
public void create(){
ByteBuffer bb = ByteBuffer.allocateDirect(
triangleCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(triangleCoords);
vertexBuffer.position(0);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(color.length*4);
byteBuffer.order(ByteOrder.nativeOrder());
colorBuffer = byteBuffer.asFloatBuffer();
colorBuffer.put(color);
colorBuffer.position(0);
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER,vertexShaderCode);
int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode);
mProgram = GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram,vertexShader);
GLES20.glAttachShader(mProgram,fragmentShader);
GLES20.glLinkProgram(mProgram);
int lin[] = new int[1];
GLES20.glGetProgramiv(mProgram,GLES20.GL_LINK_STATUS,lin,0);
if (lin[0] == 0){
String s = GLES20.glGetProgramInfoLog(mProgram);
System.out.println(s);
}
GLES20.glDeleteShader(vertexShader);
GLES20.glDeleteShader(fragmentShader);
}
public abstract void surfaceChange(int width,int height);
public abstract void dispose();
public void resume() {
}
public String uRes(String path){
if (mRes == null) System.out.println("八嘎!");
StringBuilder result=new StringBuilder();
try{
InputStream is=mRes.getAssets().open(path);
int ch;
byte[] buffer=new byte[1024];
while (-1!=(ch=is.read(buffer))){
result.append(new String(buffer,0,ch));
}
}catch (Exception e){
return null;
}
return result.toString().replaceAll("\\r\\n","\n");
}
}
补充个东西
有时候我们在点击一个按钮之后执绘制线程中的操作就崩了,这个时候一般的操作是:点击之后将事件操作保存起来,然后在帧刷新的时候取出来执行。
网友评论