Example usage for android.opengl GLES20 GL_VERTEX_SHADER

List of usage examples for android.opengl GLES20 GL_VERTEX_SHADER

Introduction

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

Prototype

int GL_VERTEX_SHADER

To view the source code for android.opengl GLES20 GL_VERTEX_SHADER.

Click Source Link

Usage

From source file:Main.java

public static int createShader(int type, String source) {
    String typeString;/*from   w  w  w . j  a  v  a  2 s . c o  m*/
    if (type == GLES20.GL_VERTEX_SHADER)
        typeString = "vertex";
    else if (type == GLES20.GL_FRAGMENT_SHADER)
        typeString = "fragment";
    else
        throw new RuntimeException("Unknown shader type");

    int sh = GLES20.glCreateShader(type);
    if (sh <= 0) {
        throw new RuntimeException("Could not create " + typeString + " shader");
    }
    GLES20.glShaderSource(sh, source);
    GLES20.glCompileShader(sh);
    int[] status = new int[1];
    GLES20.glGetShaderiv(sh, GLES20.GL_COMPILE_STATUS, status, 0);
    if (status[0] <= 0) {
        String message = GLES20.glGetShaderInfoLog(sh);
        GLES20.glDeleteShader(sh);
        throw new RuntimeException("Could not compile " + typeString + " shader: " + message);
    }

    return sh;
}

From source file:Main.java

public static int loadShader(String vss, String fss) {
    int vshader = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
    GLES20.glShaderSource(vshader, vss);
    GLES20.glCompileShader(vshader);/*  www .ja  v a  2  s  .c o  m*/
    int[] compiled = new int[1];
    GLES20.glGetShaderiv(vshader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e("Shader", "Could not compile vshader");
        Log.v("Shader", "Could not compile vshader:" + GLES20.glGetShaderInfoLog(vshader));
        GLES20.glDeleteShader(vshader);
        vshader = 0;
    }

    int fshader = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
    GLES20.glShaderSource(fshader, fss);
    GLES20.glCompileShader(fshader);
    GLES20.glGetShaderiv(fshader, GLES20.GL_COMPILE_STATUS, compiled, 0);
    if (compiled[0] == 0) {
        Log.e("Shader", "Could not compile fshader");
        Log.v("Shader", "Could not compile fshader:" + GLES20.glGetShaderInfoLog(fshader));
        GLES20.glDeleteShader(fshader);
        fshader = 0;
    }

    int program = GLES20.glCreateProgram();
    GLES20.glAttachShader(program, vshader);
    GLES20.glAttachShader(program, fshader);
    GLES20.glLinkProgram(program);

    return program;
}

From source file:Main.java

public static int setupProgram(String vxShaderCode, String frShaderCode) {
    int mProgram;
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vxShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, frShaderCode);
    if (vertexShader == 0 || fragmentShader == 0) {
        throw new RuntimeException("shader not loaded");
    }/*from w w  w .j av a 2s .c  o m*/
    mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
    GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader
    // to program
    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment
    // shader to program
    GLES20.glLinkProgram(mProgram);

    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(mProgram, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] != GLES20.GL_TRUE) {
        Log.e("jay", "Could not link program: ");
        Log.e("jay", GLES20.glGetProgramInfoLog(mProgram));
        GLES20.glDeleteProgram(mProgram);
        mProgram = 0;
    }

    return mProgram;
}

From source file:Main.java

public static int createProgramFromShaderSrc(String vertexShaderSrc, String fragmentShaderSrc) {
    int vertShader = initShader(GLES20.GL_VERTEX_SHADER, vertexShaderSrc);
    int fragShader = initShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderSrc);

    if (vertShader == 0 || fragShader == 0)
        return 0;

    int program = GLES20.glCreateProgram();
    if (program != 0) {
        GLES20.glAttachShader(program, vertShader);
        checkGLError("glAttchShader(vert)");

        GLES20.glAttachShader(program, fragShader);
        checkGLError("glAttchShader(frag)");

        GLES20.glLinkProgram(program);/*from w ww . j  a  va  2  s  .  c o m*/
        int[] glStatusVar = { GLES20.GL_FALSE };
        GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, glStatusVar, 0);
        if (glStatusVar[0] == GLES20.GL_FALSE) {
            Log.e(LOGTAG, "Could NOT link program : " + GLES20.glGetProgramInfoLog(program));
            GLES20.glDeleteProgram(program);
            program = 0;
        }
    }

    return program;
}

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]);
    }/*from w w  w. j a v  a 2 s .c om*/
    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

public static int loadProgram(final String strVSource, final String strFSource) {
    int iVShader;
    int iFShader;
    int iProgId;/*  w ww. ja va  2s  .c  o m*/
    int[] link = new int[1];
    iVShader = loadShader(strVSource, GLES20.GL_VERTEX_SHADER);
    if (iVShader == 0) {
        Log.d("Load Program", "Vertex Shader Failed");
        return 0;
    }
    iFShader = loadShader(strFSource, GLES20.GL_FRAGMENT_SHADER);
    if (iFShader == 0) {
        Log.d("Load Program", "Fragment Shader Failed");
        return 0;
    }

    iProgId = GLES20.glCreateProgram();

    GLES20.glAttachShader(iProgId, iVShader);
    GLES20.glAttachShader(iProgId, iFShader);

    GLES20.glLinkProgram(iProgId);

    GLES20.glGetProgramiv(iProgId, GLES20.GL_LINK_STATUS, link, 0);
    if (link[0] <= 0) {
        Log.d("Load Program", "Linking Failed");
        return 0;
    }
    GLES20.glDeleteShader(iVShader);
    GLES20.glDeleteShader(iFShader);
    return iProgId;
}

From source file:Main.java

/**
 * Creates a new program from the supplied vertex and fragment shaders.
 *
 * @return A handle to the program, or 0 on failure.
 *///from   ww w.  j av  a  2  s. c  o  m
public static int createProgram(String vertexSource, String fragmentSource) {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
        return 0;
    }
    int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (pixelShader == 0) {
        return 0;
    }

    int program = GLES20.glCreateProgram();
    checkGlError("glCreateProgram");
    if (program == 0) {
        Log.e(TAG, "Could not create program");
    }
    GLES20.glAttachShader(program, vertexShader);
    checkGlError("glAttachShader");
    GLES20.glAttachShader(program, pixelShader);
    checkGlError("glAttachShader");
    GLES20.glLinkProgram(program);
    int[] linkStatus = new int[1];
    GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, linkStatus, 0);
    if (linkStatus[0] != GLES20.GL_TRUE) {
        Log.e(TAG, "Could not link program: ");
        Log.e(TAG, GLES20.glGetProgramInfoLog(program));
        GLES20.glDeleteProgram(program);
        program = 0;
    }
    return program;
}

From source file:Main.java

public static int buildProgram(String vertexSource, String fragmentSource) {
    final int vertexShader = buildShader(GLES20.GL_VERTEX_SHADER, vertexSource);
    if (vertexShader == 0) {
        return 0;
    }//w  w w.ja  va 2s . c  o m

    final int fragmentShader = buildShader(GLES20.GL_FRAGMENT_SHADER, fragmentSource);
    if (fragmentShader == 0) {
        return 0;
    }

    final int program = GLES20.glCreateProgram();
    if (program == 0) {
        return 0;
    }

    GLES20.glAttachShader(program, vertexShader);
    GLES20.glAttachShader(program, fragmentShader);
    GLES20.glLinkProgram(program);

    return program;
}

From source file:Triangle.java

public Triangle() {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);/*from w  ww  .  j  a  v a2s .c  om*/

    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());

    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
}

From source file:com.kentdisplays.synccardboarddemo.MainActivity.java

/**
 * Creates the buffers we use to store information about the 3D world. OpenGL doesn't use Java
 * arrays, but rather needs data in a format it can understand. Hence we use ByteBuffers.
 * @param config The EGL configuration used when creating the surface.
 *//*w w w. j  a v  a2 s  .  c o m*/
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");

    // make a floor
    ByteBuffer bbFloorVertices = ByteBuffer.allocateDirect(DATA.FLOOR_COORDS.length * 4);
    bbFloorVertices.order(ByteOrder.nativeOrder());
    mFloorVertices = bbFloorVertices.asFloatBuffer();
    mFloorVertices.put(DATA.FLOOR_COORDS);
    mFloorVertices.position(0);

    ByteBuffer bbFloorNormals = ByteBuffer.allocateDirect(DATA.FLOOR_NORMALS.length * 4);
    bbFloorNormals.order(ByteOrder.nativeOrder());
    mFloorNormals = bbFloorNormals.asFloatBuffer();
    mFloorNormals.put(DATA.FLOOR_NORMALS);
    mFloorNormals.position(0);

    ByteBuffer bbFloorColors = ByteBuffer.allocateDirect(DATA.FLOOR_COLORS.length * 4);
    bbFloorColors.order(ByteOrder.nativeOrder());
    mFloorColors = bbFloorColors.asFloatBuffer();
    mFloorColors.put(DATA.FLOOR_COLORS);
    mFloorColors.position(0);

    int vertexShader = loadGLShader(GLES20.GL_VERTEX_SHADER, R.raw.light_vertex);
    int gridShader = loadGLShader(GLES20.GL_FRAGMENT_SHADER, R.raw.grid_fragment);

    mGlProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mGlProgram, vertexShader);
    GLES20.glAttachShader(mGlProgram, gridShader);
    GLES20.glLinkProgram(mGlProgram);

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);

    Matrix.setIdentityM(mModelFloor, 0);
    Matrix.translateM(mModelFloor, 0, 0, -mFloorDepth, 0); // Floor appears below user

    // Create the placeholder pages.
    mPages = new Page[4];
    mPages[0] = new Page(getResources().openRawResource(R.raw.boogie_board), mGlProgram, 0);
    mPages[1] = new Page(getResources().openRawResource(R.raw.house), mGlProgram, 1);
    mPages[2] = new Page(getResources().openRawResource(R.raw.placeholder), mGlProgram, 2);
    mPages[3] = new Page(getResources().openRawResource(R.raw.cylinder), mGlProgram, 3);

    checkGLError("onSurfaceCreated");
}