Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 KB | None | 0 0
  1. package nehe02.myapplication;
  2.  
  3. import android.graphics.PointF;
  4. import android.opengl.GLU;
  5.  
  6. import java.nio.ByteBuffer;
  7. import java.nio.ByteOrder;
  8. import java.nio.FloatBuffer;
  9. import java.util.ArrayList;
  10.  
  11. import static android.opengl.GLES20.GL_FLOAT;
  12. import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
  13. import static android.opengl.GLES20.GL_LINE_STRIP;
  14. import static android.opengl.GLES20.GL_POINTS;
  15. import static android.opengl.GLES20.GL_VERTEX_SHADER;
  16. import static android.opengl.GLES20.glAttachShader;
  17. import static android.opengl.GLES20.glCreateProgram;
  18. import static android.opengl.GLES20.glDisableVertexAttribArray;
  19. import static android.opengl.GLES20.glDrawArrays;
  20. import static android.opengl.GLES20.glEnableVertexAttribArray;
  21. import static android.opengl.GLES20.glGetAttribLocation;
  22. import static android.opengl.GLES20.glGetUniformLocation;
  23. import static android.opengl.GLES20.glLinkProgram;
  24. import static android.opengl.GLES20.glUniform4fv;
  25. import static android.opengl.GLES20.glUniformMatrix4fv;
  26. import static android.opengl.GLES20.glUseProgram;
  27. import static android.opengl.GLES20.glVertexAttribPointer;
  28.  
  29. /**
  30. * Created by mir on 3/10/15.
  31. */
  32. public class Point {
  33.  
  34. private final int COORDINATES_PER_VERTEX = 3;
  35. @SuppressWarnings("FieldCanBeLocal")
  36. private final String vertexShaderCode =
  37. "attribute vec4 vPosition;" +
  38. "uniform mat4 uMVPMatrix;" +
  39. "void main() {" +
  40. " gl_Position = uMVPMatrix * vPosition;" +
  41. " gl_PointSize = 10.0;" +
  42. "}";
  43. @SuppressWarnings("FieldCanBeLocal")
  44. private final String fragmentShaderCode =
  45. "precision mediump float;" +
  46. "uniform vec4 vColor;" +
  47. "void main() {" +
  48. " gl_FragColor = vColor;" +
  49. "}";
  50. float coords[] = {/*0.7f, -0.9f, 0f,
  51. 0.5f, 0.5f, 0,
  52. 0, -0.7f, 0,
  53. -0.7f, 0.7f, 0*/};
  54. float color[] = {0f, 0f, 0f, 1f};
  55. FloatBuffer floatBuffer;
  56. int program;
  57. int positionHandle, mvpMatrixHandle, colorHandle;
  58.  
  59. public Point() {
  60. init();
  61. }
  62.  
  63. @SuppressWarnings("unused")
  64. public Point(float x, float y, float z) {
  65. coords = new float[]{x, y, z};
  66. init();
  67. }
  68.  
  69. public static PointF screenToWorld(float[] viewMatrix,
  70. float[] projMatrix, float screenX, float screenY) {
  71. float[] nearPos = unProject(viewMatrix, projMatrix, screenX, screenY, 0);
  72. float[] farPos = unProject(viewMatrix, projMatrix, screenX, screenY, 1);
  73. // The click occurred in somewhere on the line between the two points
  74. // nearPos and farPos. We want to find
  75. // where that line intersects the plane at z=0
  76. float distance = nearPos[2] / (nearPos[2] - farPos[2]); // Distance between nearPos and z=0
  77. float x = nearPos[0] + (farPos[0] - nearPos[0]) * distance;
  78. float y = nearPos[1] + (farPos[1] - nearPos[0]) * distance;
  79. return new PointF(x, y);
  80. }
  81.  
  82. private static float[] unProject(float[] viewMatrix,
  83. float[] projMatrix, float screenX, float screenY, float depth) {
  84. float[] position = {0, 0, 0, 0};
  85. int[] viewPort = {0, 0, 1, 1};
  86. GLU.gluUnProject(screenX, screenY, depth, viewMatrix, 0, projMatrix, 0,
  87. viewPort, 0, position, 0);
  88. position[0] /= position[3];
  89. position[1] /= position[3];
  90. position[2] /= position[3];
  91. position[3] = 1;
  92. return position;
  93.  
  94. }
  95.  
  96. private void init() {
  97. int vertexShader = MyRenderer.loadShader(GL_VERTEX_SHADER, vertexShaderCode);
  98. int fragmentShader = MyRenderer.loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
  99.  
  100. program = glCreateProgram();
  101. glAttachShader(program, vertexShader);
  102. glAttachShader(program, fragmentShader);
  103. glLinkProgram(program);
  104. }
  105.  
  106. void draw(float[] mvpMatrix) {
  107. glUseProgram(program);
  108. positionHandle = glGetAttribLocation(program, "vPosition");
  109. glEnableVertexAttribArray(positionHandle);
  110. ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4);
  111. bb.order(ByteOrder.nativeOrder());
  112. floatBuffer = bb.asFloatBuffer();
  113. floatBuffer.put(coords);
  114. floatBuffer.position(0);
  115. glVertexAttribPointer(positionHandle, COORDINATES_PER_VERTEX, GL_FLOAT, false, COORDINATES_PER_VERTEX * 4, floatBuffer);
  116.  
  117. colorHandle = glGetUniformLocation(program, "vColor");
  118. glUniform4fv(colorHandle, 1, color, 0);
  119.  
  120. mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix");
  121. glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);
  122.  
  123. glDrawArrays(GL_POINTS, 0, coords.length / COORDINATES_PER_VERTEX);
  124.  
  125. bspline();
  126.  
  127. glDisableVertexAttribArray(positionHandle);
  128. }
  129.  
  130. void bspline() {
  131. int m = 100, n = coords.length / COORDINATES_PER_VERTEX;
  132. float xA, yA, zA, xB, yB, zB, xC, yC, zC, xD, yD, zD,
  133. a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, x = 0, y = 0, z = 0, x0, y0, z0;
  134. boolean first = true;
  135. ArrayList<Float> mCoords = new ArrayList<>();
  136. for (int i = 1; i < n - 2; i++) {
  137. xA = coords[i * 3 - 3]; // 0
  138. xB = coords[i * 3]; // 3
  139. xC = coords[i * 3 + 3]; // 6
  140. xD = coords[i * 3 + 6]; // 9
  141. yA = coords[i * 3 - 3 + 1]; // 1
  142. yB = coords[i * 3 + 1]; // 4
  143. yC = coords[i * 3 + 3 + 1]; // 7
  144. yD = coords[i * 3 + 6 + 1]; // 10
  145. zA = coords[i * 3 - 3 + 2];
  146. zB = coords[i * 3 + 2];
  147. zC = coords[i * 3 + 3 + 2];
  148. zD = coords[i * 3 + 6 + 2];
  149. a3 = (-xA + 3 * (xB - xC) + xD) / 6;
  150. b3 = (-yA + 3 * (yB - yC) + yD) / 6;
  151. c3 = (-zA + 3 * (zB - zC) + zD) / 6;
  152. a2 = (xA - 2 * xB + xC) / 2;
  153. b2 = (yA - 2 * yB + yC) / 2;
  154. c2 = (zA - 2 * zB + zC) / 2;
  155. a1 = (xC - xA) / 2;
  156. b1 = (yC - yA) / 2;
  157. c1 = (zC - zA) / 2;
  158. a0 = (xA + 4 * xB + xC) / 6;
  159. b0 = (yA + 4 * yB + yC) / 6;
  160. c0 = (zA + 4 * zB + zC) / 6;
  161. for (int j = 0; j <= m; j++) {
  162. x0 = x;
  163. y0 = y;
  164. z0 = z;
  165. float t = (float) j / (float) m;
  166. x = ((a3 * t + a2) * t + a1) * t + a0;
  167. y = ((b3 * t + b2) * t + b1) * t + b0;
  168. z = ((c3 * t + c2) * t + c1) * t + c0;
  169. mCoords.add(x);
  170. mCoords.add(y);
  171. mCoords.add(z);
  172. /*if (first) first = false;
  173. else {
  174. if (addTwo) {
  175. addTwo = false;
  176. mCoords.add(x0);
  177. mCoords.add(y0);
  178. mCoords.add(z0);
  179. }
  180. mCoords.add(x);
  181. mCoords.add(y);
  182. mCoords.add(z);
  183. g.drawLine(iX(x0), iY(y0), iX(x), iY(y));
  184. }*/
  185. }
  186. }
  187.  
  188. float[] floatArray = new float[mCoords.size()];
  189. int i = 0;
  190. for (Float f : mCoords) {
  191. floatArray[i++] = (f != null ? f : Float.NaN); // Or whatever default you want.
  192. }
  193.  
  194. ByteBuffer bb = ByteBuffer.allocateDirect(floatArray.length * 4);
  195. bb.order(ByteOrder.nativeOrder());
  196. FloatBuffer fb = bb.asFloatBuffer();
  197. fb.put(floatArray);
  198. fb.position(0);
  199.  
  200. glVertexAttribPointer(positionHandle, COORDINATES_PER_VERTEX, GL_FLOAT, false, COORDINATES_PER_VERTEX * 4, fb);
  201. glDrawArrays(GL_LINE_STRIP, 0, floatArray.length / COORDINATES_PER_VERTEX);
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement