openGL es2.0 创建可移动的纹理平面

深藏阁楼爱情的钟 2022-08-06 11:20 244阅读 0赞

一、Java部分代码

  1. package com.gzdxid.utils;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import android.opengl.GLES20;
  6. public class DrawRectMove {
  7. int mProgram;
  8. int muMVPMatrixHandle;
  9. int maPositionHandle;
  10. int maTexCoorHandle;
  11. int muSpanHandle;
  12. FloatBuffer mVertexBuffer;
  13. FloatBuffer mTexCoorBuffer;
  14. int vCount=0;
  15. public DrawRectMove(float width,float height,int mProgram) {
  16. // TODO Auto-generated constructor stub
  17. initVertex(width,height);
  18. initShader(mProgram);
  19. }
  20. private void initVertex(float width, float height) {
  21. // TODO Auto-generated method stub
  22. vCount = 6;
  23. float w = width / 2;
  24. float h = height / 2;
  25. float vertices[] = new float[] {
  26. -w, h, 0,
  27. -w, -h, 0,
  28. w, -h, 0,
  29. w, -h, 0,
  30. w, h, 0,
  31. -w, h, 0,
  32. };
  33. ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  34. vbb.order(ByteOrder.nativeOrder());
  35. mVertexBuffer = vbb.asFloatBuffer();
  36. mVertexBuffer.put(vertices);
  37. mVertexBuffer.position(0);
  38. float texCoor[] = new float[] { 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0 };
  39. ByteBuffer cbb = ByteBuffer.allocateDirect(texCoor.length * 4);
  40. cbb.order(ByteOrder.nativeOrder());
  41. mTexCoorBuffer = cbb.asFloatBuffer();
  42. mTexCoorBuffer.put(texCoor);
  43. mTexCoorBuffer.position(0);
  44. }
  45. private void initShader(int mProgram) {
  46. // TODO Auto-generated method stub
  47. this.mProgram = mProgram;
  48. muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
  49. maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
  50. maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
  51. muSpanHandle=GLES20.glGetUniformLocation(mProgram, "uSpan");
  52. }
  53. public void drawSelf(int texId,float currStart){
  54. GLES20.glUseProgram(mProgram);
  55. GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
  56. GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3*4, mVertexBuffer);
  57. GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2*4, mTexCoorBuffer);
  58. GLES20.glEnableVertexAttribArray(maPositionHandle);
  59. GLES20.glEnableVertexAttribArray(maTexCoorHandle);
  60. GLES20.glUniform1f(muSpanHandle, currStart);
  61. GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  62. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
  63. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
  64. }
  65. }

二、定点着色器:

  1. uniform mat4 uMVPMatrix;
  2. attribute vec3 aPosition;
  3. attribute vec2 aTexCoor;
  4. varying vec2 vTextureCoord;
  5. void main()
  6. {
  7. gl_Position=uMVPMatrix*vec4(aPosition,1);
  8. vTextureCoord=aTexCoor;
  9. }

三、片源着色器:
precision mediump float;
varying vec2 vTextureCoord;
uniform sampler2D sTexture;
uniform float uSpan;
void main()
{
vec2 st_Result=vec2(0,0);
st_Result.x=vTextureCoord.x;
st_Result.y=vTextureCoord.y+uSpan;
gl_FragColor=texture2D(sTexture,st_Result);
}

这里面是移动y轴参数,当然也可以移动x轴的。在Java代码中的drawSelf()中,currStart参数改变影响图片在指定大小平面上移动。

发表评论

表情:
评论列表 (有 0 条评论,244人围观)

还没有评论,来说两句吧...

相关阅读