SHOW:
|
|
- or go back to the newest paste.
| 1 | - | package net.randomcompany.android.opengles20.views; |
| 1 | + | package android.opengles20.views; |
| 2 | ||
| 3 | import android.opengl.GLES20; | |
| 4 | import android.opengl.Matrix; | |
| 5 | ||
| 6 | - | import net.randomcompany.android.opengles20.utils.Utils; |
| 6 | + | import utils.Utils; |
| 7 | ||
| 8 | import java.nio.ByteBuffer; | |
| 9 | import java.nio.ByteOrder; | |
| 10 | import java.nio.FloatBuffer; | |
| 11 | import java.nio.ShortBuffer; | |
| 12 | ||
| 13 | - | /** |
| 13 | + | |
| 14 | - | * Created by Damian Burke |
| 14 | + | |
| 15 | - | */ |
| 15 | + | |
| 16 | int iProgId; | |
| 17 | int iPosition; | |
| 18 | int iColor; | |
| 19 | int iVPMatrix; | |
| 20 | ||
| 21 | public float xAngle = 0; | |
| 22 | public float yAngle = 0; | |
| 23 | ||
| 24 | //to store projection matrix | |
| 25 | float[] mProjectionMatrix = new float[16]; | |
| 26 | //to store view matrix | |
| 27 | float[] mViewMatrix = new float[16]; | |
| 28 | //to store projection*view matrix | |
| 29 | float[] mViewProjectionMatrix = new float[16]; | |
| 30 | //to translate or rotate model | |
| 31 | float[] mModelMatrix = new float[16]; | |
| 32 | //final result p*v*m | |
| 33 | float[] mMVPMatrix = new float[16]; | |
| 34 | ||
| 35 | float[] mVertices = {
| |
| 36 | -2, -2, -3, | |
| 37 | 3, -3, 3 | |
| 38 | }; | |
| 39 | ||
| 40 | float[] mColors = {
| |
| 41 | 1, 1, 1, | |
| 42 | 0, 1, 1, | |
| 43 | 0, 1, 1 | |
| 44 | }; | |
| 45 | ||
| 46 | short[] mIndices = {
| |
| 47 | 0, 1, 1, | |
| 48 | 2, 2, 3 | |
| 49 | }; | |
| 50 | ||
| 51 | FloatBuffer lineBuffer = null; | |
| 52 | FloatBuffer colorBuffer = null; | |
| 53 | ShortBuffer indexBuffer = null; | |
| 54 | ||
| 55 | ||
| 56 | public Line() {
| |
| 57 | lineBuffer = ByteBuffer.allocateDirect(mVertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); | |
| 58 | lineBuffer.put(mVertices).position(0); | |
| 59 | ||
| 60 | colorBuffer = ByteBuffer.allocateDirect(mColors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); | |
| 61 | colorBuffer.put(mColors).position(0); | |
| 62 | ||
| 63 | indexBuffer = ByteBuffer.allocateDirect(mIndices.length * 4).order(ByteOrder.nativeOrder()).asShortBuffer(); | |
| 64 | indexBuffer.put(mIndices).position(0); | |
| 65 | ||
| 66 | prepare(); | |
| 67 | } | |
| 68 | ||
| 69 | public Line(float[] points) {
| |
| 70 | mVertices = points; | |
| 71 | ||
| 72 | lineBuffer = ByteBuffer.allocateDirect(mVertices.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); | |
| 73 | lineBuffer.put(mVertices).position(0); | |
| 74 | ||
| 75 | colorBuffer = ByteBuffer.allocateDirect(mColors.length * 4).order(ByteOrder.nativeOrder()).asFloatBuffer(); | |
| 76 | colorBuffer.put(mColors).position(0); | |
| 77 | ||
| 78 | indexBuffer = ByteBuffer.allocateDirect(mIndices.length * 4).order(ByteOrder.nativeOrder()).asShortBuffer(); | |
| 79 | indexBuffer.put(mIndices).position(0); | |
| 80 | ||
| 81 | prepare(); | |
| 82 | } | |
| 83 | ||
| 84 | public void prepare() {
| |
| 85 | String strVShader = "attribute vec4 a_position;" + | |
| 86 | "attribute vec4 a_color;" + | |
| 87 | "uniform mat4 u_VPMatrix;" + | |
| 88 | "varying vec4 v_color;" + | |
| 89 | "void main()" + | |
| 90 | "{" +
| |
| 91 | "v_color = a_color;" + | |
| 92 | "gl_Position = u_VPMatrix * a_position;" + | |
| 93 | "}"; | |
| 94 | ||
| 95 | String strFShader = "precision mediump float;" + | |
| 96 | "varying vec4 v_color;" + | |
| 97 | "void main()" + | |
| 98 | "{" +
| |
| 99 | "gl_FragColor = v_color;" + | |
| 100 | "}"; | |
| 101 | iProgId = Utils.LoadProgram(strVShader, strFShader); | |
| 102 | iColor = GLES20.glGetAttribLocation(iProgId, "a_color"); | |
| 103 | iPosition = GLES20.glGetAttribLocation(iProgId, "a_position"); | |
| 104 | iVPMatrix = GLES20.glGetUniformLocation(iProgId, "u_VPMatrix"); | |
| 105 | } | |
| 106 | ||
| 107 | ||
| 108 | private float rotX = 0f; | |
| 109 | private float rotY = 0f; | |
| 110 | ||
| 111 | public void setRotation(float x, float y) {
| |
| 112 | this.rotX = x; | |
| 113 | this.rotY = y; | |
| 114 | } | |
| 115 | ||
| 116 | private float[] mTranslation = new float[]{0, 0, 0};
| |
| 117 | ||
| 118 | public void setTranslation(float[] t) {
| |
| 119 | mTranslation = t; | |
| 120 | } | |
| 121 | ||
| 122 | private float[] mScale = new float[]{1, 1, 1};
| |
| 123 | ||
| 124 | public void setScale(float[] s) {
| |
| 125 | this.mScale = s; | |
| 126 | } | |
| 127 | ||
| 128 | public void draw(float[] mViewMatrix, float[] mProjectionMatrix) {
| |
| 129 | Matrix.multiplyMM(mViewProjectionMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); | |
| 130 | GLES20.glUseProgram(iProgId); | |
| 131 | lineBuffer.position(0); | |
| 132 | GLES20.glVertexAttribPointer(iPosition, 3, GLES20.GL_FLOAT, false, 0, lineBuffer); | |
| 133 | GLES20.glEnableVertexAttribArray(iPosition); | |
| 134 | ||
| 135 | colorBuffer.position(0); | |
| 136 | GLES20.glVertexAttribPointer(iColor, 3, GLES20.GL_FLOAT, false, 0, colorBuffer); | |
| 137 | GLES20.glEnableVertexAttribArray(iColor); | |
| 138 | ||
| 139 | // (translate * scale) * rotation -> rot -> scale -> translate | |
| 140 | ||
| 141 | ||
| 142 | // reset transformations in each step. | |
| 143 | Matrix.setIdentityM(mModelMatrix, 0); | |
| 144 | ||
| 145 | // Matrix.scaleM(mModelMatrix, 0, mScale[0], mScale[1], mScale[1]); | |
| 146 | ||
| 147 | Matrix.translateM(mModelMatrix, 0, mTranslation[0], mTranslation[1], mTranslation[2]); | |
| 148 | ||
| 149 | Matrix.rotateM(mModelMatrix, 0, rotX, 1, 0, 0); | |
| 150 | Matrix.rotateM(mModelMatrix, 0, -rotY, 0, 1, 0); | |
| 151 | ||
| 152 | Matrix.setIdentityM(mViewProjectionMatrix, 0); | |
| 153 | Matrix.multiplyMM(mViewProjectionMatrix, 0, mViewMatrix, 0, mModelMatrix, 0); | |
| 154 | //GLES20.glUniformMatrix4fv(iVPMatrix, 1, false, mMVPMatrix, 0); | |
| 155 | ||
| 156 | Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewProjectionMatrix, 0); | |
| 157 | GLES20.glUniformMatrix4fv(iVPMatrix, 1, false, mMVPMatrix, 0); | |
| 158 | ||
| 159 | ||
| 160 | //GLES20.glDrawElements(GLES20.GL_LINES, mVertices.length/2, GLES20.GL_UNSIGNED_SHORT, indexBuffer); | |
| 161 | GLES20.glDrawArrays(GLES20.GL_LINES, 0, mVertices.length / 3); | |
| 162 | } | |
| 163 | } |