在OpenGL ES 2.0中渲染同一对象的多个实例?

时间:2017-12-11 16:51:48

标签: java android opengl-es opengl-es-2.0

我更改了this OpenGL ES sample code,以便将单个Hexagon渲染到屏幕上,点击时会从黄色变为绿色。

我的目标是制作一个六边形网格,每个六边形单独点击时会改变颜色。

这是我试图复制的网格:

grid

有关如何最好地完成此任务的任何建议?

public class MainActivity extends Activity {

private GLSurfaceView mGLView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mGLView = new MyGLSurfaceView(this);
    setContentView(mGLView);
}

@Override
protected void onPause() {
    super.onPause();
    mGLView.onPause();
}

@Override
protected void onResume() {
    super.onResume();
    mGLView.onResume();
}
}



public class MyGLSurfaceView extends GLSurfaceView {

private final MyGLRenderer mRenderer;

public MyGLSurfaceView(Context context) {
    super(context);
    setEGLContextClientVersion(2);

    mRenderer = new MyGLRenderer();
    setRenderer(mRenderer);
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}

@Override
public boolean onTouchEvent(MotionEvent e) {
    // hexagon = the hexagon that is clicked
    switch (e.getAction()) {
        case MotionEvent.ACTION_UP:
            if (mRenderer.hexagon.color == mRenderer.hexagon.yellow) {
                if (mRenderer.hexagon.onPath == true) {
                    mRenderer.hexagon.color = mRenderer.hexagon.green;
                }
            }
            else if (mRenderer.hexagon.color == mRenderer.hexagon.green) {
                mRenderer.hexagon.color = mRenderer.hexagon.yellow;
                }
                requestRender();
            }
    return true;
}
}



public class MyGLRenderer implements GLSurfaceView.Renderer {

private static final String TAG = "MyGLRenderer";
public Hexagon h1, h2, h3, h4, h5, h6,
h7, h8, h9, h10, h11, h12,
h13, h14, h15, h16, h17, h18,
h19, h20, h21, h22, h23, h24,
h25, h26, h27, h28, h29, h30,
h31, h32, h33, h34, h35, h36;

private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];


@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    h1 = new Hexagon(true, 1);
    h2 = new Hexagon(true, 2);
    h3 = new Hexagon(true, 3);
    h4 = new Hexagon(true, 4);
    h5 = new Hexagon(true, 5);
    h6 = new Hexagon(true, 6);
    h7 = new Hexagon(true, 7);
    h8 = new Hexagon(true, 8);
    h9 = new Hexagon(true, 9);
    h10 = new Hexagon(true, 10);
    h11 = new Hexagon(true, 11);
    h12 = new Hexagon(true, 12);
    h13 = new Hexagon(true, 13);
    h14 = new Hexagon(true, 14);
    h15 = new Hexagon(true, 15);
    h16 = new Hexagon(true, 16);
    h17 = new Hexagon(true, 17);
    h18 = new Hexagon(true, 18);
    h19 = new Hexagon(true, 19);
    h20 = new Hexagon(true, 20);
    h21 = new Hexagon(true, 21);
    h22 = new Hexagon(true, 22);
    h23 = new Hexagon(true, 23);
    h24 = new Hexagon(true, 24);
    h25 = new Hexagon(true, 25);
    h26 = new Hexagon(true, 26);
    h27 = new Hexagon(true, 27);
    h28 = new Hexagon(true, 28);
    h29 = new Hexagon(true, 29);
    h30 = new Hexagon(true, 30);
    h31 = new Hexagon(true, 31);
    h32 = new Hexagon(true, 32);
    h33 = new Hexagon(true, 33);
    h34 = new Hexagon(true, 34);
    h35 = new Hexagon(true, 35);
    h36 = new Hexagon(true, 36);

}


@Override
public void onDrawFrame(GL10 unused) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);

    Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);

    // Draw the grid
    h1.draw(mMVPMatrix);
    h2.draw(mMVPMatrix);
    h3.draw(mMVPMatrix);
    h4.draw(mMVPMatrix);
    h5.draw(mMVPMatrix);
    h6.draw(mMVPMatrix);
    h7.draw(mMVPMatrix);
    h8.draw(mMVPMatrix);
    h9.draw(mMVPMatrix);
    h10.draw(mMVPMatrix);
    h11.draw(mMVPMatrix);
    h12.draw(mMVPMatrix);
    h13.draw(mMVPMatrix);
    h14.draw(mMVPMatrix);
    h15.draw(mMVPMatrix);
    h16.draw(mMVPMatrix);
    h17.draw(mMVPMatrix);
    h18.draw(mMVPMatrix);
    h19.draw(mMVPMatrix);
    h20.draw(mMVPMatrix);
    h21.draw(mMVPMatrix);
    h22.draw(mMVPMatrix);
    h23.draw(mMVPMatrix);
    h24.draw(mMVPMatrix);
    h25.draw(mMVPMatrix);
    h26.draw(mMVPMatrix);
    h27.draw(mMVPMatrix);
    h28.draw(mMVPMatrix);
    h29.draw(mMVPMatrix);
    h30.draw(mMVPMatrix);
    h31.draw(mMVPMatrix);
    h32.draw(mMVPMatrix);
    h33.draw(mMVPMatrix);
    h34.draw(mMVPMatrix);
    h35.draw(mMVPMatrix);
    h36.draw(mMVPMatrix);
}

@Override
public void onSurfaceChanged(GL10 unused, int width, int height) {
    GLES20.glViewport(0, 0, width, height);

    float ratio = (float) width / height;

    Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}

public static int loadShader(int type, String shaderCode){
    int shader = GLES20.glCreateShader(type);

    GLES20.glShaderSource(shader, shaderCode);
    GLES20.glCompileShader(shader);

    return shader;
}

public static void checkGlError(String glOperation) {
    int error;
    while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
        Log.e(TAG, glOperation + ": glError " + error);
        throw new RuntimeException(glOperation + ": glError " + error);
    }
}
}

public class Hexagon {

private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of the objects that use this vertex shader
        "uniform mat4 uMVPMatrix;" +
                "attribute vec4 vPosition;" +
                "void main() {" +
                // The matrix must be included as a modifier of gl_Position.
                // Note that the uMVPMatrix factor *must be first* in order
                // for the matrix multiplication product to be correct.
                "  gl_Position = uMVPMatrix * vPosition;" +
                "}";

private final String fragmentShaderCode =
        "precision mediump float;" +
                "uniform vec4 vColor;" +
                "void main() {" +
                "  gl_FragColor = vColor;" +
                "}";

private final FloatBuffer vertexBuffer;
private final ShortBuffer drawListBuffer;
public final int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;

// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
public static float coords[];


// order to draw vertices
private final short drawOrder[] = {0, 1, 2, 0, 2, 3, 0, 3, 4, 0, 4, 5, 0, 5, 6, 0, 6, 1};

private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex

public float yellow[] = { 1f, 1f, 0f, 1f };
public float green[] = { 0f, 1f, 0f, 1f };
public float[] color = yellow;

public boolean onPath;

public Hexagon(boolean path, int coordId) {
    switch(coordId){
        //Top to Bottom, Left to Right
        // 0/7 = 0f
        // 1/7 = 0.14285714286f
        // 2/7 = 0.28571428572f
        // 3/7 = 0.42857142858f
        // 4/7 = 0.57142857143f
        // 5/7 = 0.71428571429f
        // 6/7 = 0.85714285714f
        // 7/7 = 1f

        case (1):
            coords = new float[] {
                    -0.8f, 0.57142857143f, 0f,     //center
                    -1f, 0.57142857143f, 0f,      //left
                    -0.9f, 0.71428571429f, 0f,     //top left
                    -0.7f, 0.71428571429f, 0f,    //top right
                    -0.6f, 0.57142857143f, 0f,    //right
                    -0.7f, 0.42857142858f, 0f,    //bottom right
                    -0.9f, 0.42857142858f, 0f};   //bottom left
        case (2):
            coords = new float[] {
                    -0.8f, 0.28571428572f, 0f,         //center
                    -1f, 0.28571428572f, 0f,          //left
                    -0.9f,0.42857142858f, 0f,         //top left
                    -0.7f, 0.42857142858f, 0f,        //top right
                    -0.6f, 0.28571428572f, 0f,        //right
                    -0.7f, 0.14285714286f, 0f,        //bottom right
                    -0.9f, 0.14285714286f, 0f};       //bottom left
        case (3):
            coords = new float[] {
                    -0.8f, 0f, 0f,               //center
                    -1f, 0f, 0f,                //left
                    -0.9f,0.14285714286f, 0f,     //top left
                    -0.7f, 0.14285714286f, 0f,    //top right
                    -0.6f, 0f, 0f,              //right
                    -0.7f, -0.14285714286f, 0f,   //bottom right
                    -0.9f, -0.14285714286f, 0f};  //bottom left
        case (4):
            coords = new float[] {
                    -0.8f, -0.28571428572f, 0f,     //center
                    -1f, -0.28571428572f, 0f,      //left
                    -0.9f, -0.14285714286f, 0f,    //top left
                    -0.7f, -0.14285714286f, 0f,    //top right
                    -0.6f, -0.28571428572f, 0f     //right
                    -0.7f, -0.42857142858f, 0f,    //bottom right
                    -0.9f, -0.42857142858f, 0f};   //bottom left
        case (5):
            coords = new float[] {
                    -0.8f, -0.57142857143f, 0f,     //center
                    -1f, -0.57142857143f, 0f,      //left
                    -0.9f, -0.42857142858f, 0f,    //top left
                    -0.7f, -0.42857142858f, 0f,    //top right
                    -0.6f, -0.57142857143f, 0f     //right
                    -0.7f, -0.71428571429f, 0f,    //bottom right
                    -0.9f, -0.71428571429f, 0f};   //bottom left
        case (6):
            coords = new float[] {
                    -0.8f, -0.85714285714f, 0f,     //center
                    -1f, -0.85714285714f, 0f,      //left
                    -0.9f, -0.71428571429f, 0f,    //top left
                    -0.7f, -0.71428571429f, 0f,    //top right
                    -0.6f, -0.85714285714f, 0f     //right
                    -0.7f, -1f, 0f,              //bottom right
                    -0.9f, -1f, 0f};             //bottom left
        case (7):
            coords = new float[] {
                    -0.5f, 0.71428571429f, 0f,     //center
                    -0.7f, 0.71428571429f, 0f,    //left
                    -0.6f, 0.85714285714f, 0f,    //top left
                    -0.4f, 0.85714285714f, 0f,    //top right
                    -0.3f, 0.71428571429f, 0f,    //right
                    -0.4f, 0.57142857143f, 0f,    //bottom right
                    -0.6f, 0.57142857143f, 0f};   //bottom left
        case (8):
            coords = new float[] {
                    -0.5f, 0.42857142858f, 0f,     //center
                    -0.7f, 0.42857142858f, 0f,    //left
                    -0.6f, 0.57142857143f, 0f,    //top left
                    -0.4f, 0.57142857143f, 0f,    //top right
                    -0.3f, 0.42857142858f, 0f,    //right
                    -0.4f, 0.28571428572f, 0f,    //bottom right
                    -0.6f, 0.28571428572f, 0f};   //bottom left
        case (9):
            coords = new float[] {
                    -0.5f, 0.14285714286f, 0f,     //center
                    -0.7f, 0.14285714286f, 0f,    //left
                    -0.6f, 0.28571428572f, 0f,    //top left
                    -0.4f, 0.28571428572f, 0f,    //top right
                    -0.3f, 0.14285714286f, 0f,    //right
                    -0.4f, 0f, 0f,              //bottom right
                    -0.6f, 0f, 0f};             //bottom left
        case (10):
            coords = new float[] {
                    -0.5f, -0.14285714286f, 0f,     //center
                    -0.7f, -0.14285714286f, 0f,    //left
                    -0.6f, 0f, 0f,               //top left
                    -0.4f, 0f, 0f,               //top right
                    -0.3f, -0.14285714286f, 0f,    //right
                    -0.4f, -0.28571428572f, 0f,    //bottom right
                    -0.6f, -0.28571428572f, 0f};   //bottom left
        case (11):
            coords = new float[] {
                    -0.5f, -0.42857142858f, 0f,     //center
                    -0.7f, -0.42857142858f, 0f,    //left
                    -0.6f, -0.28571428572f, 0f,     //top left
                    -0.4f, -0.28571428572f, 0f,    //top right
                    -0.3f, -0.42857142858f, 0f,    //right
                    -0.4f, -0.57142857143f, 0f,    //bottom right
                    -0.6f, -0.57142857143f, 0f};   //bottom left
        case (12):
            coords = new float[] {
                    -0.5f, -0.71428571429f, 0f,     //center
                    -0.7f, -0.71428571429f, 0f,    //left
                    -0.6f, -0.57142857143f, 0f,     //top left
                    -0.4f, -0.57142857143f, 0f,    //top right
                    -0.3f, -0.71428571429f, 0f,    //right
                    -0.4f, -0.85714285714f, 0f,    //bottom right
                    -0.6f, -0.85714285714f, 0f};   //bottom left
        case (13):
            coords = new float[] {
                    -0.2f, 0.57142857143f, 0f,     //center
                    -0.4f, 0.57142857143f, 0f,      //left
                    -0.3f,0.71428571429f, 0f,     //top left
                    -0.1f, 0.71428571429f, 0f,    //top right
                    0f, 0.57142857143f, 0f,    //right
                    -0.1f, 0.42857142858f, 0f,    //bottom right
                    -0.3f, 0.42857142858f, 0f};   //bottom left
        case (14):
            coords = new float[] {
                    -0.2f, 0.28571428572f, 0f,     //center
                    -0.4f, 0.28571428572f, 0f,          //left
                    -0.3f,0.42857142858f, 0f,         //top left
                    -0.1f, 0.42857142858f, 0f,        //top right
                    0f, 0.28571428572f, 0f,        //right
                    -0.1f, 0.14285714286f, 0f,        //bottom right
                    -0.3f, 0.14285714286f, 0f};       //bottom left
        case (15):
            coords = new float[] {
                    -0.2f, 0f, 0f,                //center
                    -0.4f, 0f, 0f,                //left
                    -0.3f,0.14285714286f, 0f,     //top left
                    -0.1f, 0.14285714286f, 0f,    //top right
                    0f, 0f, 0f,                   //right
                    -0.1f, -0.14285714286f, 0f,   //bottom right
                    -0.3f, -0.14285714286f, 0f};  //bottom left
        case (16):
            coords = new float[] {
                    -0.2f, -0.28571428572f, 0f,     //center
                    -0.4f, -0.28571428572f, 0f,      //left
                    -0.3f, -0.14285714286f, 0f,    //top left
                    -0.1f, -0.14285714286f, 0f,    //top right
                    0f, -0.28571428572f, 0f     //right
                    -0.1f, -0.42857142858f, 0f,    //bottom right
                    -0.3f, -0.42857142858f, 0f};   //bottom left
        case (17):
            coords = new float[] {
                    -0.2f, -0.57142857143f, 0f,     //center
                    -0.4f, -0.57142857143f, 0f,      //left
                    -0.3f, -0.42857142858f, 0f,    //top left
                    -0.1f, -0.42857142858f, 0f,    //top right
                    0f, -0.57142857143f, 0f     //right
                    -0.1f, -0.71428571429f, 0f,    //bottom right
                    -0.3f, -0.71428571429f, 0f};   //bottom left
        case (18):
            coords = new float[] {
                    -0.2f, -0.85714285714f, 0f,     //center
                    -0.4f, -0.85714285714f, 0f,      //left
                    -0.3f, -0.71428571429f, 0f,    //top left
                    -0.1f, -0.71428571429f, 0f,    //top right
                    0f, -0.85714285714f, 0f     //right
                    -0.1f, -1f, 0f,              //bottom right
                    -0.3f, -1f, 0f};             //bottom left
        case (19):
            coords = new float[] {
                    0.1f, 0.71428571429f, 0f,     //center
                    -0.1f, 0.71428571429f, 0f,    //left
                    0f,0.85714285714f, 0f,     //top left
                    0.2f, 0.85714285714f, 0f,    //top right
                    0.3f, 0.71428571429f, 0f,    //right
                    0.2f, 0.57142857143f, 0f,    //bottom right
                    0f, 0.57142857143f, 0f};   //bottom left
        case (20):
            coords = new float[] {
                    0.1f, 0.42857142858f, 0f     //center
                    -0.1f, 0.42857142858f, 0f,    //left
                    0f,0.57142857143f, 0f,     //top left
                    0.2f, 0.57142857143f, 0f,    //top right
                    0.3f, 0.42857142858f, 0f,    //right
                    0.2f, 0.28571428572f, 0f,    //bottom right
                    0f, 0.28571428572f, 0f};   //bottom left
        case (21):
            coords = new float[] {
                    0.1f, 0.14285714286f, 0f     //center
                    -0.1f, 0.14285714286f, 0f,    //left
                    0f, 0.28571428572f, 0f,    //top left
                    0.2f, 0.28571428572f, 0f,    //top right
                    0.3f, 0.14285714286f, 0f,    //right
                    0.2f, 0f, 0f,              //bottom right
                    0f, 0f, 0f};             //bottom left
        case (22):
            coords = new float[] {
                    0.1f, -0.14285714286f, 0f     //center
                    -0.1f, -0.14285714286f, 0f,    //left
                    0f, 0f, 0f,               //top left
                    0.2f, 0f, 0f,               //top right
                    0.3f, -0.14285714286f, 0f,    //right
                    0.2f, -0.28571428572f, 0f,    //bottom right
                    0f, -0.28571428572f, 0f};   //bottom left
        case (23):
            coords = new float[] {
                    0.1f, -0.42857142858f, 0f     //center
                    -0.1f, -0.42857142858f, 0f,    //left
                    0f, -0.28571428572f, 0f,     //top left
                    0.2f, -0.28571428572f, 0f,    //top right
                    0.3f, -0.42857142858f, 0f,    //right
                    0.2f, -0.57142857143f, 0f,    //bottom right
                    0f, -0.57142857143f, 0f};   //bottom left
        case (24):
            coords = new float[] {
                    0.1f, -0.71428571429f, 0f     //center
                    -0.1f, -0.71428571429f, 0f,    //left
                    0f, -0.57142857143f, 0f,     //top left
                    0.2f, -0.57142857143f, 0f,    //top right
                    0.3f, -0.71428571429f, 0f,    //right
                    0.2f, -0.85714285714f, 0f,    //bottom right
                    0f, -0.85714285714f, 0f};   //bottom left
        case (25):
            coords = new float[] {
                    0.4f, 0.57142857143f, 0f,     //center
                    0.2f, 0.57142857143f, 0f,      //left
                    0.3f,0.71428571429f, 0f,     //top left
                    0.5f, 0.71428571429f, 0f,    //top right
                    0.6f, 0.57142857143f, 0f,    //right
                    0.5f, 0.42857142858f, 0f,    //bottom right
                    0.3f, 0.42857142858f, 0f};   //bottom left
        case (26):
            coords = new float[] {
                    0.4f, 0.28571428572f, 0f,     //center
                    0.2f, 0.28571428572f, 0f,          //left
                    0.3f,0.42857142858f, 0f,         //top left
                    0.5f, 0.42857142858f, 0f,        //top right
                    0.6f, 0.28571428572f, 0f,        //right
                    0.5f, 0.14285714286f, 0f,        //bottom right
                    0.3f, 0.14285714286f, 0f};       //bottom left
        case (27):
            coords = new float[] {
                    0.4f, 0f, 0f,              //center
                    0.2f, 0f, 0f,              //left
                    0.3f,0.14285714286f, 0f,     //top left
                    0.5f, 0.14285714286f, 0f,    //top right
                    0.6f, 0f, 0f,              //right
                    0.5f, -0.14285714286f, 0f,   //bottom right
                    0.3f, -0.14285714286f, 0f};  //bottom left
        case (28):
            coords = new float[] {
                    0.4f, -0.28571428572f, 0f,    //center
                    0.2f, -0.28571428572f, 0f,    //left
                    0.3f, -0.14285714286f, 0f,    //top left
                    0.5f, -0.14285714286f, 0f,    //top right
                    0.6f, -0.28571428572f, 0f,    //right
                    0.5f, -0.42857142858f, 0f,    //bottom right
                    0.3f, -0.42857142858f, 0f};   //bottom left
        case (29):
            coords = new float[] {
                    0.4f, -0.57142857143f, 0f,    //center
                    0.2f, -0.57142857143f, 0f,    //left
                    0.3f, -0.42857142858f, 0f,    //top left
                    0.5f, -0.42857142858f, 0f,    //top right
                    0.6f, -0.57142857143f, 0f,    //right
                    0.5f, -0.71428571429f, 0f,    //bottom right
                    0.3f, -0.71428571429f, 0f};   //bottom left
        case (30):
            coords = new float[] {
                    0.4f, -0.85714285714f, 0f,    //centre
                    0.2f, -0.85714285714f, 0f,    //left
                    0.3f, -0.71428571429f, 0f,    //top left
                    0.5f, -0.71428571429f, 0f,    //top right
                    0.6f, -0.85714285714f, 0f,    //right
                    0.5f, -1f, 0f,              //bottom right
                    0.3f, -1f, 0f};             //bottom left
        case (31):
            coords = new float[] {
                    0.4f, 0.71428571429f, 0f,    //centre
                    0.5f, 0.71428571429f, 0f,    //left
                    0.6f, 0.85714285714f, 0f,     //top left
                    0.8f, 0.85714285714f, 0f,    //top right
                    0.9f, 0.71428571429f, 0f,    //right
                    0.8f, 0.57142857143f, 0f,    //bottom right
                    0.6f, 0.57142857143f, 0f};   //bottom left
        case (32):
            coords = new float[] {
                    0.4f, 0.42857142858f, 0f,    //centre
                    0.5f, 0.42857142858f, 0f,    //left
                    0.6f,0.57142857143f, 0f,     //top left
                    0.8f, 0.57142857143f, 0f,    //top right
                    0.9f, 0.42857142858f, 0f,    //right
                    0.8f, 0.28571428572f, 0f,    //bottom right
                    0.6f, 0.28571428572f, 0f};   //bottom left
        case (33):
            coords = new float[] {
                    0.4f, 0.14285714286f, 0f,    //centre
                    0.5f, 0.14285714286f, 0f,    //left
                    0.6f, 0.28571428572f, 0f,    //top left
                    0.8f, 0.28571428572f, 0f,    //top right
                    0.9f, 0.14285714286f, 0f,    //right
                    0.8f, 0f, 0f,              //bottom right
                    0.6f, 0f, 0f};             //bottom left
        case (34):
            coords = new float[] {
                    0.4f, -0.14285714286f, 0f,    //centre
                    0.5f, -0.14285714286f, 0f,    //left
                    0.6f, 0f, 0f,               //top left
                    0.8f, 0f, 0f,               //top right
                    0.9f, -0.14285714286f, 0f,    //right
                    0.8f, -0.28571428572f, 0f,    //bottom right
                    0.6f, -0.28571428572f, 0f};   //bottom left
        case (35):
            coords = new float[] {
                    0.4f, -0.42857142858f, 0f,    //centre
                    0.5f, -0.42857142858f, 0f,    //left
                    0.6f, -0.28571428572f, 0f,    //top left
                    0.8f, -0.28571428572f, 0f,    //top right
                    0.9f, -0.42857142858f, 0f,    //right
                    0.8f, -0.57142857143f, 0f,    //bottom right
                    0.6f, -0.57142857143f, 0f};   //bottom left
        case (36):
            coords = new float[] {
                    0.4f, -0.71428571429f, 0f,    //centre
                    0.5f, -0.71428571429f, 0f,    //left
                    0.6f, -0.57142857143f, 0f,    //top left
                    0.8f, -0.57142857143f, 0f,    //top right
                    0.9f, -0.71428571429f, 0f,    //right
                    0.8f, -0.85714285714f, 0f,    //bottom right
                    0.6f, -0.85714285714f, 0f};   //bottom left
    }

    onPath = path;

    ByteBuffer bb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 4 bytes per float)
            coords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();

    vertexBuffer.put(coords);

    vertexBuffer.position(0);

    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            drawOrder.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(drawOrder);
    drawListBuffer.position(0);


    int vertexShader = MyGLRenderer.loadShader(
            GLES20.GL_VERTEX_SHADER,
            vertexShaderCode);
    int fragmentShader = MyGLRenderer.loadShader(
            GLES20.GL_FRAGMENT_SHADER,
            fragmentShaderCode);

    mProgram = GLES20.glCreateProgram();             
    GLES20.glAttachShader(mProgram, vertexShader);   
    GLES20.glAttachShader(mProgram, fragmentShader); 
    GLES20.glLinkProgram(mProgram);                  

}

public void draw(float[] mvpMatrix) {
    GLES20.glUseProgram(mProgram);

    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");

    GLES20.glEnableVertexAttribArray(mPositionHandle);

    GLES20.glVertexAttribPointer(
            mPositionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false,
            vertexStride, vertexBuffer);

    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");

    GLES20.glUniform4fv(mColorHandle, 1, color, 0);

    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    MyGLRenderer.checkGlError("glGetUniformLocation");

    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
    MyGLRenderer.checkGlError("glUniformMatrix4fv");

    GLES20.glDrawElements(
            GLES20.GL_TRIANGLES, drawOrder.length,
            GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

}

onPath布尔值将在下一步中有用。最终我将把它变成一个游戏,其中一些瓷砖在正确的路径上,而其他瓷砖不在

0 个答案:

没有答案