openGL es2.0 创建纹理球

野性酷女 2022-08-06 13:21 269阅读 0赞

一、Java代码:

  1. package com.gzdxid.utils;
  2. import java.nio.ByteBuffer;
  3. import java.nio.ByteOrder;
  4. import java.nio.FloatBuffer;
  5. import java.util.ArrayList;
  6. import android.opengl.GLES20;
  7. public class DrawBallTexture {
  8. int mProgram;
  9. int muMVPMatrixHandle;
  10. int maPositionHandle;
  11. int maTexCoorHandle;
  12. FloatBuffer mVertexBuffer;
  13. FloatBuffer mTexCoorBuffer;
  14. int vCount = 0;
  15. final float UNIT_SIZE = 1f;
  16. final float angleSpan = 10f;
  17. float R = 0;
  18. public float roateX;
  19. public float roateY;
  20. public DrawBallTexture(float r, int mProgram) {
  21. // TODO Auto-generated constructor stub
  22. initVertex(r);
  23. initShader(mProgram);
  24. }
  25. private void initVertex(float r) {
  26. // TODO Auto-generated method stub
  27. R = r;
  28. ArrayList<Float> alVertix = new ArrayList<Float>();
  29. for (float vAngle = 90; vAngle > -90; vAngle -= angleSpan) {
  30. for (float hAngle = 360; hAngle > 0; hAngle -= angleSpan) {
  31. float x1 = getCoor(0, vAngle, hAngle);
  32. float y1 = getCoor(1, vAngle, hAngle);
  33. float z1 = getCoor(2, vAngle, hAngle);
  34. float x2 = getCoor(0, vAngle - angleSpan, hAngle);
  35. float y2 = getCoor(1, vAngle - angleSpan, hAngle);
  36. float z2 = getCoor(2, vAngle - angleSpan, hAngle);
  37. float x3 = getCoor(0, vAngle - angleSpan, hAngle - angleSpan);
  38. float y3 = getCoor(1, vAngle - angleSpan, hAngle - angleSpan);
  39. float z3 = getCoor(2, vAngle - angleSpan, hAngle - angleSpan);
  40. float x4 = getCoor(0, vAngle, hAngle - angleSpan);
  41. float y4 = getCoor(1, vAngle, hAngle - angleSpan);
  42. float z4 = getCoor(2, vAngle, hAngle - angleSpan);
  43. alVertix.add(x1);
  44. alVertix.add(y1);
  45. alVertix.add(z1);
  46. alVertix.add(x2);
  47. alVertix.add(y2);
  48. alVertix.add(z2);
  49. alVertix.add(x4);
  50. alVertix.add(y4);
  51. alVertix.add(z4);
  52. // 构建第二三角形
  53. alVertix.add(x4);
  54. alVertix.add(y4);
  55. alVertix.add(z4);
  56. alVertix.add(x2);
  57. alVertix.add(y2);
  58. alVertix.add(z2);
  59. alVertix.add(x3);
  60. alVertix.add(y3);
  61. alVertix.add(z3);
  62. }
  63. }
  64. vCount = alVertix.size() / 3;
  65. float vertices[] = new float[vCount * 3];
  66. for (int i = 0; i < alVertix.size(); i++) {
  67. vertices[i] = alVertix.get(i);
  68. }
  69. ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
  70. vbb.order(ByteOrder.nativeOrder());
  71. mVertexBuffer = vbb.asFloatBuffer();
  72. mVertexBuffer.put(vertices);
  73. mVertexBuffer.position(0);
  74. float[] texCoor = generateTexCoor(// 获取切分整图的纹理数组
  75. (int) (360 / angleSpan), // 纹理图切分的列数
  76. (int) (180 / angleSpan) // 纹理图切分的行数
  77. );
  78. ByteBuffer llbb = ByteBuffer.allocateDirect(texCoor.length * 4);
  79. llbb.order(ByteOrder.nativeOrder());// 设置字节顺序
  80. mTexCoorBuffer = llbb.asFloatBuffer();
  81. mTexCoorBuffer.put(texCoor);
  82. mTexCoorBuffer.position(0);
  83. }
  84. private void initShader(int mProgram) {
  85. // TODO Auto-generated method stub
  86. this.mProgram = mProgram;
  87. muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
  88. maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
  89. maTexCoorHandle = GLES20.glGetAttribLocation(mProgram, "aTexCoor");
  90. }
  91. public void drawSelf(int texId) {
  92. MatrixState.rotate(roateX, 1, 0, 0);
  93. MatrixState.rotate(roateY, 0, 1, 0);
  94. GLES20.glUseProgram(mProgram);
  95. GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, MatrixState.getFinalMatrix(), 0);
  96. GLES20.glVertexAttribPointer(maPositionHandle, 3, GLES20.GL_FLOAT, false, 3 * 4, mVertexBuffer);
  97. GLES20.glVertexAttribPointer(maTexCoorHandle, 2, GLES20.GL_FLOAT, false, 2 * 4, mTexCoorBuffer);
  98. GLES20.glEnableVertexAttribArray(maPositionHandle);
  99. GLES20.glEnableVertexAttribArray(maTexCoorHandle);
  100. GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
  101. GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texId);
  102. GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vCount);
  103. }
  104. private float getCoor(int which, float vAngle, float hAngle) {
  105. switch (which) {
  106. case 0:// x
  107. return (float) (R * UNIT_SIZE * Math.cos(Math.toRadians(vAngle)) * Math.cos(Math.toRadians(hAngle)));
  108. case 1:// y
  109. return (float) (R * UNIT_SIZE * Math.sin(Math.toRadians(vAngle)));
  110. case 2:// z
  111. return (float) (R * UNIT_SIZE * Math.cos(Math.toRadians(vAngle)) * Math.sin(Math.toRadians(hAngle)));
  112. }
  113. return 0;
  114. }
  115. // 自动切分纹理产生纹理数组的方法
  116. public float[] generateTexCoor(int bw, int bh) {
  117. float[] result = new float[bw * bh * 6 * 2];
  118. float sizew = 1.0f / bw;// 列数
  119. float sizeh = 1.0f / bh;// 行数
  120. int c = 0;
  121. for (int i = 0; i < bh; i++) {
  122. for (int j = 0; j < bw; j++) {
  123. // 每行列一个矩形,由两个三角形构成,共六个点,12个纹理坐标
  124. float s = j * sizew;
  125. float t = i * sizeh;
  126. result[c++] = s;
  127. result[c++] = t;
  128. result[c++] = s;
  129. result[c++] = t + sizeh;
  130. result[c++] = s + sizew;
  131. result[c++] = t;
  132. result[c++] = s + sizew;
  133. result[c++] = t;
  134. result[c++] = s;
  135. result[c++] = t + sizeh;
  136. result[c++] = s + sizew;
  137. result[c++] = t + sizeh;
  138. }
  139. }
  140. return result;
  141. }
  142. }

二、顶点着色器:

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

三、片源着色器:

  1. precision mediump float;
  2. varying vec2 vTextureCoord;
  3. uniform sampler2D sTexture;
  4. void main(){
  5. gl_FragColor=texture2D(sTexture,vTextureCoord);
  6. }

发表评论

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

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

相关阅读

    相关 openGL纹理

    纹理压缩技术已经广泛应用在各种3D游戏之中,它们包括:DXTC(Direct X Texture Compress,DirectX纹理压缩,以S3TC为基础)、S3TC(S3