android opengles 绑定顶点数据的几种方式

1.边申请GPU内存边绑定数据

原始数据

//一组顶点数据
 newArray[pointIndex * 3- pointArrayIndex * pointMaxColumn] = p.getX();
 newArray[pointIndex * 3+ 1 - pointArrayIndex * pointMaxColumn] = p.getY();
 newArray[pointIndex * 3+ 2 - pointArrayIndex * pointMaxColumn] = p.getPressure();
 //就表示这段数据是内存中一个一个连续的顶点数组块
 FloatBuffer vertexBuffer; //填充到FloatBuffer略过
a.申请内存的同时绑定数据(不推荐,测试发现在有些设备上一次传输点太多的时候会导致内存泄漏
GLES30.glVertexAttribPointer(p_Position, 3, GLES30.GL_FLOAT, false, 3 * 4, vertexBuffer);
GLES30.glDrawArrays(GLES30.GL_POINTS, 0, vertexBuffer.capacity()/3);
shader输入
"attribute vec3 position; \n" +		//这里用了opengles2.0的方式 attribute 

2.创建VAO VBO同时填充数据

原始数据

//一组顶点数据
 newArray[pointIndex * 6- pointArrayIndex * pointMaxColumn] = p.getX();
 newArray[pointIndex * 6+ 1 - pointArrayIndex * pointMaxColumn] = p.getY();
 newArray[pointIndex * 6+ 2 - pointArrayIndex * pointMaxColumn] = p.getPressure();
 newArray[pointIndex * 6+ 3 - pointArrayIndex * pointMaxColumn] = p.getV();
 newArray[pointIndex * 6+ 4 - pointArrayIndex * pointMaxColumn] = p.getOrientation();
 newArray[pointIndex * 6+ 5 - pointArrayIndex * pointMaxColumn] = p.getTilt();
 //就表示这段数据是内存中一个一个连续的顶点数组块
  FloatBuffer vertexBuffer; //填充到FloatBuffer略过
传输数据
int[] VAO=new int[1];
int[] VBO=new int[1];
GLES30.glGenVertexArrays(1, VAO,0);
GLES30.glGenBuffers(1, VBO,0);
GLES30.glBindVertexArray(VAO[0]);
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, VBO[0]);
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, vertexBuffer.capacity()*4, vertexBuffer, GLES30.GL_STATIC_DRAW);

GLES30.glVertexAttribPointer(0, 2, GLES30.GL_FLOAT, false, 6 * 4, 0);
GLES30.glEnableVertexAttribArray(0);
GLES30.glVertexAttribPointer(1, 4, GLES30.GL_FLOAT, false, 6 * 4, 2*4);
GLES30.glEnableVertexAttribArray(1);

GLES30.glDrawArrays(GLES30.GL_POINTS, 0, vertexBuffer.capacity() / 6);

GLES30.glDisable(GLES30.GL_BLEND);

GLES30.glDeleteBuffers(1, VBO,0);
GLES30.glDeleteVertexArrays(1, VAO,0);
shader输入
"#version 300 es\n" +
"precision highp float;\n" +
"layout(std140, column_major) uniform;" +
"layout (location = 0) in vec2 position; \n" +
"layout (location = 1) in vec4 frag; \n" +

3.先申请GPU内存再填充数据,glBufferSubData分批填充顶点属性

分别有三段数据,分三批填充到GPU缓存

//第一批数据
newArray[pointIndex * 4 - pointArrayIndex * pointTexMaxColumn] = X;
newArray[pointIndex * 4 + 1 - pointArrayIndex * pointTexMaxColumn] = Y;
newArray[pointIndex * 4 + 2 - pointArrayIndex * pointTexMaxColumn] = press;
newArray[pointIndex * 4 + 3 - pointArrayIndex * pointTexMaxColumn] = calAlpha;
//填充到FloatBuffer vertexBuffer略过

//第二批数据
newTextArray[pointIndex * 4 - pointArrayIndex * pointTexMaxColumn] = (float) p.getV();
newTextArray[pointIndex * 4 + 1 - pointArrayIndex * pointTexMaxColumn] = (float) p.getTilt();
newTextArray[pointIndex * 4 + 2 - pointArrayIndex * pointTexMaxColumn] = (float) ((float) 
newTextArray[pointIndex * 4 + 3 - pointArrayIndex * pointTexMaxColumn] = (float) p.getTime();
//填充到FloatBuffer texBuffer略过

//第三批数据
newRealPosArray[pointIndex * 4 - pointArrayIndex * pointTexMaxColumn] =posX ;
newRealPosArray[pointIndex * 4 + 1 - pointArrayIndex * pointTexMaxColumn] = posY;
newRealPosArray[pointIndex * 4 + 2 - pointArrayIndex * pointTexMaxColumn] = (float) 1.0;
newRealPosArray[pointIndex * 4 + 3 - pointArrayIndex * pointTexMaxColumn] = (float) 1.0;
//填充到FloatBuffer realPosBuffer略过

 //每个FloatBuffer表示这段数据是内存中一个一个连续的顶点数组块

绑定数据
int[] VAO=new int[1];
int[] VBO_POS=new int[1];

GLES30.glGenVertexArrays(1, VAO,0);
GLES30.glGenBuffers(1, VBO_POS,0);
GLES30.glBindVertexArray(VAO[0]);
GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, VBO_POS[0]);
//申请GPU内存但是不填充数据
GLES30.glBufferData(GLES30.GL_ARRAY_BUFFER, vertexBuffer.capacity()*4+texBuffer.capacity()*4+realPosBuffer.capacity()*4, null, GLES30.GL_STATIC_DRAW);

//将vertexBuffer填充进去并指定顶点读取方式
GLES30.glBufferSubData(GLES30.GL_ARRAY_BUFFER,0,vertexBuffer.capacity()*4, vertexBuffer);
GLES30.glVertexAttribPointer(waterColorPosition, 4, GLES30.GL_FLOAT, false, 4 * 4, 0);
GLES30.glEnableVertexAttribArray(waterColorPosition);

//将texBuffer填充进去并指定顶点读取方式
GLES30.glBufferSubData(GLES30.GL_ARRAY_BUFFER,vertexBuffer.capacity()*4,texBuffer.capacity()*4, texBuffer);
GLES30.glVertexAttribPointer(pointSpeed, 4, GLES30.GL_FLOAT, false, 4 * 4, vertexBuffer.capacity()*4);
GLES30.glEnableVertexAttribArray(pointSpeed);

//将realPosBuffer填充进去并指定顶点读取方式
GLES30.glBufferSubData(GLES30.GL_ARRAY_BUFFER,vertexBuffer.capacity()*4+texBuffer.capacity()*4,realPosBuffer.capacity()*4, realPosBuffer);
GLES30.glVertexAttribPointer(realPos, 4, GLES30.GL_FLOAT, false, 4 * 4, vertexBuffer.capacity()*4+texBuffer.capacity()*4);
GLES30.glEnableVertexAttribArray(realPos);

GLES20.glDrawArrays(GLES20.GL_POINTS, 0, vertexBuffer.capacity() / 4);

GLES20.glDisable(GLES20.GL_BLEND);
GLES20.glDisable(GLES20.GL_DEPTH_TEST);

GLES30.glDeleteBuffers(1, VBO_POS,0);
GLES30.glDeleteVertexArrays(1, VAO,0);
shader输入
"#version 300 es\n" +
"precision highp float;\n" +
"in vec4 position; \n" +
"in vec4 frag; \n" +
"in vec4 realPos;\n" +
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值