Example usage for android.opengl GLES20 GL_TEXTURE0

List of usage examples for android.opengl GLES20 GL_TEXTURE0

Introduction

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

Prototype

int GL_TEXTURE0

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

Click Source Link

Usage

From source file:Main.java

/**
 * Looks up the appropriate texture unit handle for a given index.  Example: 2 - GLES20.GL_TEXTURE2
 *
 * @param textureUnitIndex The index of the texture unit
 * @return// w ww . j  a  va 2 s  .com
 */
public static int getGLTextureUnitHandle(int textureUnitIndex) {
    return GLES20.GL_TEXTURE0 + textureUnitIndex;
}

From source file:Main.java

public static int setTexture(Bitmap bitmap) {
    GLES20.glGenTextures(1, texturenames, 0);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturenames[0]);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    bitmap.recycle();//from   w ww.j a  v  a2 s.c om

    return texturenames[0];
}

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

/**
 * Draw the rect. We've set all of our transformation matrices. Now we simply pass them into
 * the shader.//from www  .  j a  v a 2 s. com
 */
public void drawRect(int texIndex) {
    if (mRectTextureIds[texIndex] < INVALID_TEXTURE) {
        // can't draw this rectangle
        return;
    }

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

    // Set the active texture unit
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + texIndex);

    // Bind the texture to this unit.
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mRectTextureIds[texIndex]);

    // Tell the texture uniform sampler to use this texture in the shader by binding to texture unit 0.
    GLES20.glUniform1i(mRectTextureUniformParam, texIndex);

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

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

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

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

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

    // Connect texBuffer to "aTextureCoord".
    GLES20.glVertexAttribPointer(mRectTextureCoordinateParam, 2, GLES20.GL_FLOAT, false, 0, mRectTexCoords);

    // Enable the "aTextureCoord" vertex attribute.
    GLES20.glEnableVertexAttribArray(mRectTextureCoordinateParam);

    if (texIndex == mSelectedTexIndex || isLookingAtObject(texIndex)) {
        GLES20.glVertexAttribPointer(mColorParam, 4, GLES20.GL_FLOAT, false, 0, mRectFoundColors);
    } else {
        GLES20.glVertexAttribPointer(mColorParam, 4, GLES20.GL_FLOAT, false, 0, mRectColors);
    }
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, WorldLayoutData.RECT_COORDS.length / 3); // 3 b/c triangles
    checkGLError("Drawing rect");
}

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

/**
 * Draws a frame for an eye./*  w w w .  j  a  va 2 s  .c  o m*/
 *
 * @param eye The eye to render. Includes all required transformations.
 */
@Override
public void onDrawEye(Eye eye) {
    if (mAssetInd == -1) {
        // we are still showing instruction, return without doing anything
        return;
    }

    if (!mAssetChangedLeft && !mAssetChangedRight) {
        // nothing changed, do nothing and return
        return;
    }

    if (eye.getType() == Eye.Type.LEFT)
        mAssetChangedLeft = false;
    else if (eye.getType() == Eye.Type.RIGHT)
        mAssetChangedRight = false;

    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    checkGLError("mColorParam");

    GLES20.glUseProgram(mPicProgram);

    GLES20.glUniform1f(mDimRatioParam, mDimRatio);

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    if (eye.getType() == Eye.Type.LEFT) {
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureCurr[0]);
    } else {
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureCurr[1]);
    }

    // set the zoom level
    GLES20.glUniform1f(mZoomParam, sZoom[mImgZoomInd]);

    /*
     * if user prefers negative parallax, shift window on left frame leftward and right frame
     * rightward. if user prefers positive parallax, do the opposite
     */
    if (eye.getType() == Eye.Type.LEFT) {
        GLES20.glUniform1f(mParallaxParam, mImgParallaxAdj / 2.0f);
    } else {
        GLES20.glUniform1f(mParallaxParam, -mImgParallaxAdj / 2.0f);
    }

    // Set the position of the picture
    //float zoomCoords[] = new float[picCoords.length];
    //for(int i=0; i<picCoords.length; i++)
    //zoomCoords[i] = picCoords[i] * zoom[zoomInd];

    //ByteBuffer bblVertices = ByteBuffer.allocateDirect(zoomCoords.length * 4);
    ByteBuffer bblVertices = ByteBuffer.allocateDirect(picCoords.length * 4);
    bblVertices.order(ByteOrder.nativeOrder());
    mPicVertices = bblVertices.asFloatBuffer();
    //mPicVertices.put(zoomCoords);
    mPicVertices.put(picCoords);
    mPicVertices.position(0);

    GLES20.glVertexAttribPointer(mPicPositionParam, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride,
            mPicVertices);

    GLES20.glDrawElements(GLES20.GL_TRIANGLES, /* mode */
            6, /* count */
            GLES20.GL_UNSIGNED_SHORT, /* type */
            mPicElements /* element array buffer offset */
    );
}

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

/**
 * Loads a bitmap into OpenGL./*w ww. j a va2 s  . c  om*/
 *
 * @param texIndex the desired texture index
 * @param bitmap   the bitmap to put into OpenGL
 */
private void loadTextureInternal(int texIndex, Bitmap bitmap, boolean recycle) {

    GLES20.glGenTextures(1, mTextureIds, texIndex);

    Log.d(TAG, "loading texture: " + texIndex + " -> " + mTextureIds[texIndex]);

    if (mTextureIds[texIndex] != INVALID_TEXTURE && bitmap != null && !bitmap.isRecycled()) {

        // Set the active texture unit
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + texIndex);

        Matrix.setIdentityM(mImageRect[texIndex], 0);
        Matrix.scaleM(mImageRect[texIndex], 0, 1f, (float) bitmap.getHeight() / bitmap.getWidth(), 1f);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIds[texIndex]);

        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

        // Set filtering
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        mRectTextureIds[texIndex] = mTextureIds[texIndex];
    } else {
        Log.w(TAG, "Failed to load: " + texIndex);
    }

    if (mTextureIds[texIndex] == INVALID_TEXTURE) {
        Log.e(TAG, "Error loading texture.");
    }
}

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

private void updateTexture(int texIndex, Bitmap bitmap) {
    if (mTextureIds[texIndex] != INVALID_TEXTURE && bitmap != null && !bitmap.isRecycled()) {

        // Set the active texture unit
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + texIndex);

        Matrix.setIdentityM(mImageRect[texIndex], 0);
        Matrix.scaleM(mImageRect[texIndex], 0, 1f, (float) bitmap.getHeight() / bitmap.getWidth(), 1f);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureIds[texIndex]);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
    } else {//from w  w w .  j a  v  a  2 s.co  m
        Log.w(TAG, "Failed to update: " + texIndex + " val: " + mTextureIds[texIndex]);
    }
}