Example usage for android.opengl GLES20 glBindAttribLocation

List of usage examples for android.opengl GLES20 glBindAttribLocation

Introduction

In this page you can find the example usage for android.opengl GLES20 glBindAttribLocation.

Prototype

public static native void glBindAttribLocation(int program, int index, String name);

Source Link

Usage

From source file:Main.java

public static int createProgram(int vsh, int fsh, String... attributes) {
    int[] status = new int[1];
    int ph = GLES20.glCreateProgram();
    if (ph <= 0) {
        throw new RuntimeException("Could not create program");
    }//from   w  w w  .  j  a  v  a  2s.c o  m
    GLES20.glAttachShader(ph, vsh);
    GLES20.glAttachShader(ph, fsh);
    for (int i = 0; i < attributes.length; ++i) {
        GLES20.glBindAttribLocation(ph, i, attributes[i]);
    }
    GLES20.glLinkProgram(ph);
    GLES20.glGetProgramiv(ph, GLES20.GL_LINK_STATUS, status, 0);
    if (status[0] == 0) {
        GLES20.glDeleteProgram(ph);
        throw new RuntimeException("Could not link program");
    }

    return ph;
}

From source file:Main.java

public static int createProgram(String vertSrc, String fragSrc, String[] attributeNames, int[] attributeBinding,
        String[] uniformNames, int[] uniformBinding) {

    int program = GLES20.glCreateProgram();

    int status = 1;
    int[] vertSh = new int[1];
    int[] fragSh = new int[1];
    status *= compileShader(GLES20.GL_VERTEX_SHADER, vertSrc, vertSh);
    status *= compileShader(GLES20.GL_FRAGMENT_SHADER, fragSrc, fragSh);
    checkGLError("Compiling shaders");

    GLES20.glAttachShader(program, vertSh[0]);
    checkGLError("Attach shader");
    GLES20.glAttachShader(program, fragSh[0]);
    checkGLError("Attach shader fragment");

    //Bind attributes
    for (int i = 0; i < attributeNames.length; i++) {
        GLES20.glBindAttribLocation(program, attributeBinding[i], attributeNames[i]);
        checkGLError("Bind attribute: " + attributeNames[i]);
    }/*  w w w  .  j a v a2s .co m*/
    status *= linkProgram(program);

    status *= validateProgram(program);

    //location of uniforms
    if (status > 0) {
        for (int i = 0; i < uniformNames.length; i++) {
            //         if (uniformsLocations.at(i).first.length()) {
            int loc = GLES20.glGetUniformLocation(program, uniformNames[i]);
            checkGLError("glGetUniformLocation - " + uniformNames[i]);
            if (loc < 0)
                Log.e(TAG, "Bad uniform " + uniformNames[i]);
            uniformBinding[i] = loc;
        }
    } else {
        GLES20.glDeleteProgram(program);
        program = 0;
    }

    if (vertSh[0] > 0) {
        GLES20.glDeleteShader(vertSh[0]);
        GLES20.glDetachShader(program, vertSh[0]);
    }
    if (fragSh[0] > 0) {
        GLES20.glDeleteShader(fragSh[0]);
        GLES20.glDetachShader(program, fragSh[0]);
    }
    checkGLError("Shaders deleted");
    return program;
}

From source file:Main.java

/**
 * Helper function to compile and link a program.
 *
 * @param vertexShaderHandle   An OpenGL handle to an already-compiled vertex shader.
 * @param fragmentShaderHandle An OpenGL handle to an already-compiled fragment shader.
 * @param attributes           Attributes that need to be bound to the program.
 * @return An OpenGL handle to the program.
 *///  ww  w .j a  va  2 s  .  co m
public static int createAndLinkProgram(final int vertexShaderHandle, final int fragmentShaderHandle,
        final String[] attributes) {
    int programHandle = GLES20.glCreateProgram();

    if (programHandle != 0) {
        // Bind the vertex shader to the program.
        GLES20.glAttachShader(programHandle, vertexShaderHandle);

        // Bind the fragment shader to the program.
        GLES20.glAttachShader(programHandle, fragmentShaderHandle);

        // Bind attributes
        if (attributes != null) {
            final int size = attributes.length;
            for (int i = 0; i < size; i++) {
                GLES20.glBindAttribLocation(programHandle, i, attributes[i]);
            }
        }

        // Link the two shaders together into a program.
        GLES20.glLinkProgram(programHandle);

        // Get the link status.
        final int[] linkStatus = new int[1];
        GLES20.glGetProgramiv(programHandle, GLES20.GL_LINK_STATUS, linkStatus, 0);

        // If the link failed, delete the program.
        if (linkStatus[0] == 0) {
            Log.e(TAG, "Error compiling program: " + GLES20.glGetProgramInfoLog(programHandle));
            GLES20.glDeleteProgram(programHandle);
            programHandle = 0;
        }
    }

    if (programHandle == 0) {
        throw new RuntimeException("Error creating program.");
    }

    return programHandle;
}