Example usage for android.opengl GLES20 glGetAttribLocation

List of usage examples for android.opengl GLES20 glGetAttribLocation

Introduction

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

Prototype

public static native int glGetAttribLocation(int program, String name);

Source Link

Usage

From source file:Triangle.java

public void draw() {
    GLES20.glUseProgram(mProgram);//w  ww . j  a  v  a  2s  . co m
    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);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

From source file:Triangle.java

public void draw(float[] mvpMatrix) {
    GLES20.glUseProgram(mProgram);// w w w.j  a  v a 2  s .  c  o  m
    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");
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    GLES20.glDisableVertexAttribArray(mPositionHandle);
}

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

/**
 * Encapsulates the OpenGL ES instructions for drawing this page.
 *
 * @param perspective//w  w w .jav a2s.  co m
 * @param view
 */
public void draw(float[] perspective, float[] view) {
    mPositionParam = GLES20.glGetAttribLocation(mGlProgram, "a_Position");
    mNormalParam = GLES20.glGetAttribLocation(mGlProgram, "a_Normal");
    mColorParam = GLES20.glGetAttribLocation(mGlProgram, "a_Color");
    mModelViewProjectionParam = GLES20.glGetUniformLocation(mGlProgram, "u_MVP");
    mIsFloorParam = GLES20.glGetUniformLocation(mGlProgram, "u_IsFloor");
    mModelParam = GLES20.glGetUniformLocation(mGlProgram, "u_Model");
    mModelViewParam = GLES20.glGetUniformLocation(mGlProgram, "u_MVMatrix");

    // This is not the floor!
    GLES20.glUniform1f(mIsFloorParam, 0f);

    // Set the Model in the shader, used to calculate lighting
    GLES20.glUniformMatrix4fv(mModelParam, 1, false, mModel, 0);

    // Build the ModelView and ModelViewProjection matrices
    // for calculating cube position and light.
    float[] modelView = new float[16];
    float[] modelViewProjection = new float[16];
    Matrix.multiplyMM(modelView, 0, view, 0, mModel, 0);
    Matrix.multiplyMM(modelViewProjection, 0, perspective, 0, modelView, 0);

    // Set the ModelView in the shader, used to calculate lighting
    GLES20.glUniformMatrix4fv(mModelViewParam, 1, false, modelView, 0);

    // Set the position of the cube
    GLES20.glVertexAttribPointer(mPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, 0, mPageVertices);

    // Set the ModelViewProjection matrix in the shader.
    GLES20.glUniformMatrix4fv(mModelViewProjectionParam, 1, false, modelViewProjection, 0);

    // Set the normal positions of the cube, again for shading
    GLES20.glVertexAttribPointer(mNormalParam, 3, GLES20.GL_FLOAT, false, 0, mPageNormals);

    GLES20.glVertexAttribPointer(mColorParam, 4, GLES20.GL_FLOAT, false, 0, mPageColors);

    // Animate over all the paths every 30 seconds.
    long time = SystemClock.uptimeMillis() % 30000L;
    int numberOfPathsToDraw = Math.round(mNumberOfPaths / 30000.0f * time);

    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, numberOfPathsToDraw * 6);
}

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

/**
 * Draws a frame for an eye. The transformation for that eye (from the camera) is passed in as
 * a parameter./*from  w w  w. ja  va  2  s .  co m*/
 * @param transform The transformations to apply to render this eye.
 */
@Override
public void onDrawEye(EyeTransform transform) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    GLES20.glClearColor(0f, 0f, 0f, 1.00f); // Dark background so text shows up well

    mPositionParam = GLES20.glGetAttribLocation(mGlProgram, "a_Position");
    mNormalParam = GLES20.glGetAttribLocation(mGlProgram, "a_Normal");
    mColorParam = GLES20.glGetAttribLocation(mGlProgram, "a_Color");

    GLES20.glEnableVertexAttribArray(mPositionParam);
    GLES20.glEnableVertexAttribArray(mNormalParam);
    GLES20.glEnableVertexAttribArray(mColorParam);
    checkGLError("mColorParam");

    // Apply the eye transformation to the camera.
    Matrix.multiplyMM(mView, 0, transform.getEyeView(), 0, mCamera, 0);

    // Set the position of the light
    Matrix.multiplyMV(mLightPosInEyeSpace, 0, mView, 0, mLightPosInWorldSpace, 0);
    GLES20.glUniform3f(mLightPosParam, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);

    // Draw the pages.
    for (Page page : mPages) {
        page.draw(transform.getPerspective(), mView);
        checkGLError("Drawing page");
    }

    // Set mModelView for the floor, so we draw floor in the correct location
    Matrix.multiplyMM(mModelView, 0, mView, 0, mModelFloor, 0);
    Matrix.multiplyMM(mModelViewProjection, 0, transform.getPerspective(), 0, mModelView, 0);
    drawFloor(transform.getPerspective());
}

From source file:com.google.vrtoolkit.cardboard.samples.treasurehunt.MainActivity.java

/**
 * Creates the buffers we use to store information about the 3D world.
 *
 * <p>OpenGL doesn't use Java arrays, but rather needs data in a format it can understand.
 * Hence we use ByteBuffers./*w w  w .ja  va2  s  . com*/
 *
 * @param config The EGL configuration used when creating the surface.
 */
@Override
public void onSurfaceCreated(EGLConfig config) {
    Log.i(TAG, "onSurfaceCreated");
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f); // Dark background so text shows up well.

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COORDS.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    cubeVertices = bbVertices.asFloatBuffer();
    cubeVertices.put(WorldLayoutData.CUBE_COORDS);
    cubeVertices.position(0);

    ByteBuffer bbColors = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_COLORS.length * 4);
    bbColors.order(ByteOrder.nativeOrder());
    cubeColors = bbColors.asFloatBuffer();
    cubeColors.put(WorldLayoutData.CUBE_COLORS);
    cubeColors.position(0);

    ByteBuffer bbFoundColors = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_FOUND_COLORS.length * 4);
    bbFoundColors.order(ByteOrder.nativeOrder());
    cubeFoundColors = bbFoundColors.asFloatBuffer();
    cubeFoundColors.put(WorldLayoutData.CUBE_FOUND_COLORS);
    cubeFoundColors.position(0);

    ByteBuffer bbNormals = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_NORMALS.length * 4);
    bbNormals.order(ByteOrder.nativeOrder());
    cubeNormals = bbNormals.asFloatBuffer();
    cubeNormals.put(WorldLayoutData.CUBE_NORMALS);
    cubeNormals.position(0);

    ByteBuffer mcbbVertices = ByteBuffer.allocateDirect(WorldLayoutData.MINI_CUBE_COORDS.length * 4);
    mcbbVertices.order(ByteOrder.nativeOrder());
    miniCubeVertices = mcbbVertices.asFloatBuffer();
    miniCubeVertices.put(WorldLayoutData.MINI_CUBE_COORDS);
    miniCubeVertices.position(0);

    ByteBuffer mcbbColors = ByteBuffer.allocateDirect(WorldLayoutData.MINI_CUBE_COLORS.length * 4);
    mcbbColors.order(ByteOrder.nativeOrder());
    miniCubeColors = mcbbColors.asFloatBuffer();
    miniCubeColors.put(WorldLayoutData.MINI_CUBE_COLORS);
    miniCubeColors.position(0);

    ByteBuffer mcbbNormals = ByteBuffer.allocateDirect(WorldLayoutData.CUBE_NORMALS.length * 4);
    mcbbNormals.order(ByteOrder.nativeOrder());
    miniCubeNormals = mcbbNormals.asFloatBuffer();
    miniCubeNormals.put(WorldLayoutData.CUBE_NORMALS);
    miniCubeNormals.position(0);

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

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

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

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

    cubeProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(cubeProgram, vertexShader);
    GLES20.glAttachShader(cubeProgram, passthroughShader);
    GLES20.glLinkProgram(cubeProgram);
    GLES20.glUseProgram(cubeProgram);

    checkGLError("Cube program");

    cubePositionParam = GLES20.glGetAttribLocation(cubeProgram, "a_Position");
    cubeNormalParam = GLES20.glGetAttribLocation(cubeProgram, "a_Normal");
    cubeColorParam = GLES20.glGetAttribLocation(cubeProgram, "a_Color");

    cubeModelParam = GLES20.glGetUniformLocation(cubeProgram, "u_Model");
    cubeModelViewParam = GLES20.glGetUniformLocation(cubeProgram, "u_MVMatrix");
    cubeModelViewProjectionParam = GLES20.glGetUniformLocation(cubeProgram, "u_MVP");
    cubeLightPosParam = GLES20.glGetUniformLocation(cubeProgram, "u_LightPos");

    GLES20.glEnableVertexAttribArray(cubePositionParam);
    GLES20.glEnableVertexAttribArray(cubeNormalParam);
    GLES20.glEnableVertexAttribArray(cubeColorParam);

    checkGLError("Cube program params");

    //Minicube
    miniCubeProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(miniCubeProgram, vertexShader);
    GLES20.glAttachShader(miniCubeProgram, passthroughShader);
    GLES20.glLinkProgram(miniCubeProgram);
    GLES20.glUseProgram(miniCubeProgram);

    checkGLError("Cube program");

    miniCubePositionParam = GLES20.glGetAttribLocation(miniCubeProgram, "a_Position");
    miniCubeNormalParam = GLES20.glGetAttribLocation(miniCubeProgram, "a_Normal");
    miniCubeColorParam = GLES20.glGetAttribLocation(miniCubeProgram, "a_Color");

    miniCubeModelParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_Model");
    miniCubeModelViewParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_MVMatrix");
    miniCubeModelViewProjectionParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_MVP");
    miniCubeLightPosParam = GLES20.glGetUniformLocation(miniCubeProgram, "u_LightPos");

    GLES20.glEnableVertexAttribArray(miniCubePositionParam);
    GLES20.glEnableVertexAttribArray(miniCubeNormalParam);
    GLES20.glEnableVertexAttribArray(miniCubeColorParam);

    checkGLError("Cube program params");

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

    checkGLError("Floor program");

    floorModelParam = GLES20.glGetUniformLocation(floorProgram, "u_Model");
    floorModelViewParam = GLES20.glGetUniformLocation(floorProgram, "u_MVMatrix");
    floorModelViewProjectionParam = GLES20.glGetUniformLocation(floorProgram, "u_MVP");
    floorLightPosParam = GLES20.glGetUniformLocation(floorProgram, "u_LightPos");

    floorPositionParam = GLES20.glGetAttribLocation(floorProgram, "a_Position");
    floorNormalParam = GLES20.glGetAttribLocation(floorProgram, "a_Normal");
    floorColorParam = GLES20.glGetAttribLocation(floorProgram, "a_Color");

    GLES20.glEnableVertexAttribArray(floorPositionParam);
    GLES20.glEnableVertexAttribArray(floorNormalParam);
    GLES20.glEnableVertexAttribArray(floorColorParam);

    checkGLError("Floor program params");

    Matrix.setIdentityM(modelFloor, 0);
    Matrix.translateM(modelFloor, 0, 0, -floorDepth, 0); // Floor appears below user.

    // Avoid any delays during start-up due to decoding of sound files.
    new Thread(new Runnable() {
        public void run() {
            // Start spatial audio playback of SOUND_FILE at the model postion. The returned
            //soundId handle is stored and allows for repositioning the sound object whenever
            // the cube position changes.
            cardboardAudioEngine.preloadSoundFile(SOUND_FILE);
            soundId = cardboardAudioEngine.createSoundObject(SOUND_FILE);
            cardboardAudioEngine.setSoundObjectPosition(soundId, modelPosition[0], modelPosition[1],
                    modelPosition[2]);
            cardboardAudioEngine.playSound(soundId, true /* looped playback */);
        }
    }).start();

    updateModelPosition();

    checkGLError("onSurfaceCreated");
}

From source file:com.tumblr.cardboard.Tumblr3DActivity.java

/**
 * Draws a frame for an eye. The transformation for that eye (from the camera) is passed in as
 * a parameter.//from   w ww  .j  a  v a2 s  .c  o  m
 *
 * @param eye The transformations to apply to render this eye.
 */
@Override
public void onDrawEye(Eye eye) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    mPositionParam = GLES20.glGetAttribLocation(mGlProgram, "a_Position");
    mNormalParam = GLES20.glGetAttribLocation(mGlProgram, "a_Normal");
    mColorParam = GLES20.glGetAttribLocation(mGlProgram, "a_Color");
    mRectTextureCoordinateParam = GLES20.glGetAttribLocation(mGlProgram, "a_TexCoordinate");

    GLES20.glEnableVertexAttribArray(mPositionParam);
    GLES20.glEnableVertexAttribArray(mNormalParam);
    GLES20.glEnableVertexAttribArray(mColorParam);
    checkGLError("mColorParam");

    // Apply the eye transformation to the camera.
    Matrix.multiplyMM(mView, 0, eye.getEyeView(), 0, mCamera, 0);

    // Set the position of the light
    Matrix.multiplyMV(mLightPosInEyeSpace, 0, mView, 0, mLightPosInWorldSpace, 0);
    GLES20.glUniform3f(mLightPosParam, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1], mLightPosInEyeSpace[2]);

    // Set mModelView for the floor, so we draw floor in the correct location
    Matrix.multiplyMM(mModelView, 0, mView, 0, mModelFloor, 0);
    Matrix.multiplyMM(mModelViewProjection, 0, eye.getPerspective(Z_NEAR, Z_FAR), 0, mModelView, 0);
    drawFloor(eye.getPerspective(Z_NEAR, Z_FAR));

    // Build the ModelView and ModelViewProjection matrices
    // for calculating rect position and light.
    for (int i = 0; i < mModelRect.length; i++) {
        Matrix.multiplyMM(mModelView, 0, mView, 0, mModelRect[i], 0);
        Matrix.multiplyMM(mModelViewProjection, 0, eye.getPerspective(Z_NEAR, Z_FAR), 0, mModelView, 0);
        drawRect(i);
    }
}

From source file:com.aimfire.gallery.cardboard.PhotoActivity.java

@Override
public void onSurfaceCreated(EGLConfig config) {
    if (BuildConfig.DEBUG)
        Log.i(TAG, "onSurfaceCreated");

    /*// w  ww  . ja v  a2s  .  co  m
     *  Dark background so text shows up well.
     */
    GLES20.glClearColor(0.1f, 0.1f, 0.1f, 0.5f);

    ByteBuffer bbElements = ByteBuffer.allocateDirect(drawOrder.length * 2);
    bbElements.order(ByteOrder.nativeOrder());
    mPicElements = bbElements.asShortBuffer();
    mPicElements.put(drawOrder);
    mPicElements.position(0);

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

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

    checkGLError("Pic program");

    mDimRatioParam = GLES20.glGetUniformLocation(mPicProgram, "u_dimRatio");
    mZoomParam = GLES20.glGetUniformLocation(mPicProgram, "u_zoom");
    mParallaxParam = GLES20.glGetUniformLocation(mPicProgram, "u_parallax");

    mPicPositionParam = GLES20.glGetAttribLocation(mPicProgram, "a_position");

    GLES20.glEnableVertexAttribArray(mPicPositionParam);
    checkGLError("Pic program params");

    GLES20.glEnable(GLES20.GL_DEPTH_TEST);
    checkGLError("onSurfaceCreated");

    /*
     * initializes a few textures (current, previous and next). we have to do this
     * here (as opposed to onCreate) as gl context is only available here
     */
    initTextures();

    /*
     * so onDrawEye will know to draw
     */
    mAssetChangedLeft = mAssetChangedRight = true;
}

From source file:com.sveder.cardboardpassthrough.MainActivity.java

/**
 * Draws a frame for an eye. The transformation for that eye (from the camera) is passed in as
 * a parameter./*from  w w  w.  j  a v a 2 s  .  co  m*/
 * @param transform The transformations to apply to render this eye.
 */
@Override
public void onDrawEye(EyeTransform transform) {
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

    GLES20.glUseProgram(mProgram);

    GLES20.glActiveTexture(GL_TEXTURE_EXTERNAL_OES);
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);

    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "position");
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride,
            vertexBuffer);

    mTextureCoordHandle = GLES20.glGetAttribLocation(mProgram, "inputTextureCoordinate");
    GLES20.glEnableVertexAttribArray(mTextureCoordHandle);
    GLES20.glVertexAttribPointer(mTextureCoordHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride,
            textureVerticesBuffer);

    mColorHandle = GLES20.glGetAttribLocation(mProgram, "s_texture");

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

    // Disable vertex array
    GLES20.glDisableVertexAttribArray(mPositionHandle);
    GLES20.glDisableVertexAttribArray(mTextureCoordHandle);

    Matrix.multiplyMM(mView, 0, transform.getEyeView(), 0, mCamera, 0);

    //        mPositionParam = GLES20.glGetAttribLocation(mGlProgram, "a_Position");
    //        mNormalParam = GLES20.glGetAttribLocation(mGlProgram, "a_Normal");
    //        mColorParam = GLES20.glGetAttribLocation(mGlProgram, "a_Color");
    //
    //        GLES20.glEnableVertexAttribArray(mPositionParam);
    //        GLES20.glEnableVertexAttribArray(mNormalParam);
    //        GLES20.glEnableVertexAttribArray(mColorParam);
    //        checkGLError("mColorParam");
    //
    //        // Apply the eye transformation to the camera.
    //        Matrix.multiplyMM(mView, 0, transform.getEyeView(), 0, mCamera, 0);
    //
    //        // Set the position of the light
    //        Matrix.multiplyMV(mLightPosInEyeSpace, 0, mView, 0, mLightPosInWorldSpace, 0);
    //        GLES20.glUniform3f(mLightPosParam, mLightPosInEyeSpace[0], mLightPosInEyeSpace[1],
    //                mLightPosInEyeSpace[2]);
    //
    //        // Build the ModelView and ModelViewProjection matrices
    //        // for calculating cube position and light.
    //        Matrix.multiplyMM(mModelView, 0, mView, 0, mModelCube, 0);
    //        Matrix.multiplyMM(mModelViewProjection, 0, transform.getPerspective(), 0, mModelView, 0);
    //        drawCube();
    //
    //        // Set mModelView for the floor, so we draw floor in the correct location
    //        Matrix.multiplyMM(mModelView, 0, mView, 0, mModelFloor, 0);
    //        Matrix.multiplyMM(mModelViewProjection, 0, transform.getPerspective(), 0,
    //            mModelView, 0);
    //        drawFloor(transform.getPerspective());
}