Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package android.opengles20.views;
- import android.opengl.GLES20;
- import android.opengl.Matrix;
- import utils.Utils;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- import java.nio.FloatBuffer;
- import java.nio.ShortBuffer;
- public class Line {
- int iProgId;
- int iPosition;
- int iColor;
- int iVPMatrix;
- public float xAngle = 0;
- public float yAngle = 0;
- //to store projection matrix
- float[] mProjectionMatrix = new float[16];
- //to store view matrix
- float[] mViewMatrix = new float[16];
- //to store projection*view matrix
- float[] mViewProjectionMatrix = new float[16];
- //to translate or rotate model
- float[] mModelMatrix = new float[16];
- //final result p*v*m
- float[] mMVPMatrix = new float[16];
- float[] mVertices = {
- -2, -2, -3,
- 3, -3, 3
- };
- float[] mColors = {
- 1, 1, 1,
- 0, 1, 1,
- 0, 1, 1
- };
- short[] mIndices = {
- 0, 1, 1,
- 2, 2, 3
- };
- FloatBuffer lineBuffer = null;
- FloatBuffer colorBuffer = null;
- ShortBuffer indexBuffer = null;
- public Line() {
- lineBuffer = ByteBuffer.allocateDirect(mVertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- lineBuffer.put(mVertices).position(0);
- colorBuffer = ByteBuffer.allocateDirect(mColors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- colorBuffer.put(mColors).position(0);
- indexBuffer = ByteBuffer.allocateDirect(mIndices.length * 4).order(ByteOrder.nativeOrder()).asShortBuffer();
- indexBuffer.put(mIndices).position(0);
- prepare();
- }
- public Line(float[] points) {
- mVertices = points;
- lineBuffer = ByteBuffer.allocateDirect(mVertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- lineBuffer.put(mVertices).position(0);
- colorBuffer = ByteBuffer.allocateDirect(mColors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
- colorBuffer.put(mColors).position(0);
- indexBuffer = ByteBuffer.allocateDirect(mIndices.length * 4).order(ByteOrder.nativeOrder()).asShortBuffer();
- indexBuffer.put(mIndices).position(0);
- prepare();
- }
- public void prepare() {
- String strVShader = "attribute vec4 a_position;" +
- "attribute vec4 a_color;" +
- "uniform mat4 u_VPMatrix;" +
- "varying vec4 v_color;" +
- "void main()" +
- "{" +
- "v_color = a_color;" +
- "gl_Position = u_VPMatrix * a_position;" +
- "}";
- String strFShader = "precision mediump float;" +
- "varying vec4 v_color;" +
- "void main()" +
- "{" +
- "gl_FragColor = v_color;" +
- "}";
- iProgId = Utils.LoadProgram(strVShader, strFShader);
- iColor = GLES20.glGetAttribLocation(iProgId, "a_color");
- iPosition = GLES20.glGetAttribLocation(iProgId, "a_position");
- iVPMatrix = GLES20.glGetUniformLocation(iProgId, "u_VPMatrix");
- }
- private float rotX = 0f;
- private float rotY = 0f;
- public void setRotation(float x, float y) {
- this.rotX = x;
- this.rotY = y;
- }
- private float[] mTranslation = new float[]{0, 0, 0};
- public void setTranslation(float[] t) {
- mTranslation = t;
- }
- private float[] mScale = new float[]{1, 1, 1};
- public void setScale(float[] s) {
- this.mScale = s;
- }
- public void draw(float[] mViewMatrix, float[] mProjectionMatrix) {
- Matrix.multiplyMM(mViewProjectionMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
- GLES20.glUseProgram(iProgId);
- lineBuffer.position(0);
- GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 0, lineBuffer);
- GLES20.glEnableVertexAttribArray(iPosition);
- colorBuffer.position(0);
- GLES20.glVertexAttribPointer(iColor, 3, GLES20.GL_FLOAT, false, 0, colorBuffer);
- GLES20.glEnableVertexAttribArray(iColor);
- // (translate * scale) * rotation -> rot -> scale -> translate
- // reset transformations in each step.
- Matrix.setIdentityM(mModelMatrix, 0);
- // Matrix.scaleM(mModelMatrix, 0, mScale[0], mScale[1], mScale[1]);
- Matrix.translateM(mModelMatrix, 0, mTranslation[0], mTranslation[1], mTranslation[2]);
- Matrix.rotateM(mModelMatrix, 0, rotX, 1, 0, 0);
- Matrix.rotateM(mModelMatrix, 0, -rotY, 0, 1, 0);
- Matrix.setIdentityM(mViewProjectionMatrix, 0);
- Matrix.multiplyMM(mViewProjectionMatrix, 0, mViewMatrix, 0, mModelMatrix, 0);
- //GLES20.glUniformMatrix4fv(iVPMatrix, 1, false, mMVPMatrix, 0);
- Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewProjectionMatrix, 0);
- GLES20.glUniformMatrix4fv(iVPMatrix, 1, false, mMVPMatrix, 0);
- //GLES20.glDrawElements(GLES20.GL_LINES, mVertices.length/2, GLES20.GL_UNSIGNED_SHORT, indexBuffer);
- GLES20.glDrawArrays(GLES20.GL_LINES, 0, mVertices.length / 3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement