Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nehe02.myapplication;
- import android.graphics.PointF;
- import android.opengl.GLU;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.util.ArrayList;
- import static android.opengl.GLES20.GL_FLOAT;
- import static android.opengl.GLES20.GL_FRAGMENT_SHADER;
- import static android.opengl.GLES20.GL_LINE_STRIP;
- import static android.opengl.GLES20.GL_POINTS;
- import static android.opengl.GLES20.GL_VERTEX_SHADER;
- import static android.opengl.GLES20.glAttachShader;
- import static android.opengl.GLES20.glCreateProgram;
- import static android.opengl.GLES20.glDisableVertexAttribArray;
- import static android.opengl.GLES20.glDrawArrays;
- import static android.opengl.GLES20.glEnableVertexAttribArray;
- import static android.opengl.GLES20.glGetAttribLocation;
- import static android.opengl.GLES20.glGetUniformLocation;
- import static android.opengl.GLES20.glLinkProgram;
- import static android.opengl.GLES20.glUniform4fv;
- import static android.opengl.GLES20.glUniformMatrix4fv;
- import static android.opengl.GLES20.glUseProgram;
- import static android.opengl.GLES20.glVertexAttribPointer;
- /**
- * Created by mir on 3/10/15.
- */
- public class Point {
- private final int COORDINATES_PER_VERTEX = 3;
- @SuppressWarnings("FieldCanBeLocal")
- private final String vertexShaderCode =
- "attribute vec4 vPosition;" +
- "uniform mat4 uMVPMatrix;" +
- "void main() {" +
- " gl_Position = uMVPMatrix * vPosition;" +
- " gl_PointSize = 10.0;" +
- "}";
- @SuppressWarnings("FieldCanBeLocal")
- private final String fragmentShaderCode =
- "precision mediump float;" +
- "uniform vec4 vColor;" +
- "void main() {" +
- " gl_FragColor = vColor;" +
- "}";
- float coords[] = {/*0.7f, -0.9f, 0f,
- 0.5f, 0.5f, 0,
- 0, -0.7f, 0,
- -0.7f, 0.7f, 0*/};
- float color[] = {0f, 0f, 0f, 1f};
- FloatBuffer floatBuffer;
- int program;
- int positionHandle, mvpMatrixHandle, colorHandle;
- public Point() {
- init();
- }
- @SuppressWarnings("unused")
- public Point(float x, float y, float z) {
- coords = new float[]{x, y, z};
- init();
- }
- public static PointF screenToWorld(float[] viewMatrix,
- float[] projMatrix, float screenX, float screenY) {
- float[] nearPos = unProject(viewMatrix, projMatrix, screenX, screenY, 0);
- float[] farPos = unProject(viewMatrix, projMatrix, screenX, screenY, 1);
- // The click occurred in somewhere on the line between the two points
- // nearPos and farPos. We want to find
- // where that line intersects the plane at z=0
- float distance = nearPos[2] / (nearPos[2] - farPos[2]); // Distance between nearPos and z=0
- float x = nearPos[0] + (farPos[0] - nearPos[0]) * distance;
- float y = nearPos[1] + (farPos[1] - nearPos[0]) * distance;
- return new PointF(x, y);
- }
- private static float[] unProject(float[] viewMatrix,
- float[] projMatrix, float screenX, float screenY, float depth) {
- float[] position = {0, 0, 0, 0};
- int[] viewPort = {0, 0, 1, 1};
- GLU.gluUnProject(screenX, screenY, depth, viewMatrix, 0, projMatrix, 0,
- viewPort, 0, position, 0);
- position[0] /= position[3];
- position[1] /= position[3];
- position[2] /= position[3];
- position[3] = 1;
- return position;
- }
- private void init() {
- int vertexShader = MyRenderer.loadShader(GL_VERTEX_SHADER, vertexShaderCode);
- int fragmentShader = MyRenderer.loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode);
- program = glCreateProgram();
- glAttachShader(program, vertexShader);
- glAttachShader(program, fragmentShader);
- glLinkProgram(program);
- }
- void draw(float[] mvpMatrix) {
- glUseProgram(program);
- positionHandle = glGetAttribLocation(program, "vPosition");
- glEnableVertexAttribArray(positionHandle);
- ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4);
- bb.order(ByteOrder.nativeOrder());
- floatBuffer = bb.asFloatBuffer();
- floatBuffer.put(coords);
- floatBuffer.position(0);
- glVertexAttribPointer(positionHandle, COORDINATES_PER_VERTEX, GL_FLOAT, false, COORDINATES_PER_VERTEX * 4, floatBuffer);
- colorHandle = glGetUniformLocation(program, "vColor");
- glUniform4fv(colorHandle, 1, color, 0);
- mvpMatrixHandle = glGetUniformLocation(program, "uMVPMatrix");
- glUniformMatrix4fv(mvpMatrixHandle, 1, false, mvpMatrix, 0);
- glDrawArrays(GL_POINTS, 0, coords.length / COORDINATES_PER_VERTEX);
- bspline();
- glDisableVertexAttribArray(positionHandle);
- }
- void bspline() {
- int m = 100, n = coords.length / COORDINATES_PER_VERTEX;
- float xA, yA, zA, xB, yB, zB, xC, yC, zC, xD, yD, zD,
- a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3, x = 0, y = 0, z = 0, x0, y0, z0;
- boolean first = true;
- ArrayList<Float> mCoords = new ArrayList<>();
- for (int i = 1; i < n - 2; i++) {
- xA = coords[i * 3 - 3]; // 0
- xB = coords[i * 3]; // 3
- xC = coords[i * 3 + 3]; // 6
- xD = coords[i * 3 + 6]; // 9
- yA = coords[i * 3 - 3 + 1]; // 1
- yB = coords[i * 3 + 1]; // 4
- yC = coords[i * 3 + 3 + 1]; // 7
- yD = coords[i * 3 + 6 + 1]; // 10
- zA = coords[i * 3 - 3 + 2];
- zB = coords[i * 3 + 2];
- zC = coords[i * 3 + 3 + 2];
- zD = coords[i * 3 + 6 + 2];
- a3 = (-xA + 3 * (xB - xC) + xD) / 6;
- b3 = (-yA + 3 * (yB - yC) + yD) / 6;
- c3 = (-zA + 3 * (zB - zC) + zD) / 6;
- a2 = (xA - 2 * xB + xC) / 2;
- b2 = (yA - 2 * yB + yC) / 2;
- c2 = (zA - 2 * zB + zC) / 2;
- a1 = (xC - xA) / 2;
- b1 = (yC - yA) / 2;
- c1 = (zC - zA) / 2;
- a0 = (xA + 4 * xB + xC) / 6;
- b0 = (yA + 4 * yB + yC) / 6;
- c0 = (zA + 4 * zB + zC) / 6;
- for (int j = 0; j <= m; j++) {
- x0 = x;
- y0 = y;
- z0 = z;
- float t = (float) j / (float) m;
- x = ((a3 * t + a2) * t + a1) * t + a0;
- y = ((b3 * t + b2) * t + b1) * t + b0;
- z = ((c3 * t + c2) * t + c1) * t + c0;
- mCoords.add(x);
- mCoords.add(y);
- mCoords.add(z);
- /*if (first) first = false;
- else {
- if (addTwo) {
- addTwo = false;
- mCoords.add(x0);
- mCoords.add(y0);
- mCoords.add(z0);
- }
- mCoords.add(x);
- mCoords.add(y);
- mCoords.add(z);
- g.drawLine(iX(x0), iY(y0), iX(x), iY(y));
- }*/
- }
- }
- float[] floatArray = new float[mCoords.size()];
- int i = 0;
- for (Float f : mCoords) {
- floatArray[i++] = (f != null ? f : Float.NaN); // Or whatever default you want.
- }
- ByteBuffer bb = ByteBuffer.allocateDirect(floatArray.length * 4);
- bb.order(ByteOrder.nativeOrder());
- FloatBuffer fb = bb.asFloatBuffer();
- fb.put(floatArray);
- fb.position(0);
- glVertexAttribPointer(positionHandle, COORDINATES_PER_VERTEX, GL_FLOAT, false, COORDINATES_PER_VERTEX * 4, fb);
- glDrawArrays(GL_LINE_STRIP, 0, floatArray.length / COORDINATES_PER_VERTEX);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement