Java Code Examples for android.opengl.GLES20#glGetAttribLocation()

The following examples show how to use android.opengl.GLES20#glGetAttribLocation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Grid.java    From ParaViewTangoRecorder with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(float[] viewMatrix, float[] projectionMatrix) {
    GLES20.glUseProgram(mProgram);
    mVertexBuffer.position(0);

    // Compose the model, view, and projection matrices into a single m-v-p
    // matrix
    updateMvpMatrix(viewMatrix, projectionMatrix);

    // Load vertex attribute data
    mPosHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    GLES20.glVertexAttribPointer(mPosHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false, 0, mVertexBuffer);
    GLES20.glEnableVertexAttribArray(mPosHandle);

    // Draw the Grid
    mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, getMvpMatrix(), 0);
    GLES20.glLineWidth(1);
    GLES20.glDrawArrays(GLES20.GL_LINES, 0, (GRID_RANGE_M * 2 + 1) * 4);
}
 
Example 2
Source File: GLDrawer2D.java    From MegviiFacepp-Android-SDK with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor
 * this should be called in GL context
 */
public GLDrawer2D() {
	pVertex = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
			.order(ByteOrder.nativeOrder()).asFloatBuffer();
	pVertex.put(VERTICES);
	pVertex.flip();
	pTexCoord = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
			.order(ByteOrder.nativeOrder()).asFloatBuffer();
	pTexCoord.put(TEXCOORD);
	pTexCoord.flip();

	hProgram = loadShader(vss, fss);
	GLES20.glUseProgram(hProgram);
       maPositionLoc = GLES20.glGetAttribLocation(hProgram, "aPosition");
       maTextureCoordLoc = GLES20.glGetAttribLocation(hProgram, "aTextureCoord");
       muMVPMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");
       muTexMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uTexMatrix");

	Matrix.setIdentityM(mMvpMatrix, 0);
       GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);
       GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, mMvpMatrix, 0);
	GLES20.glVertexAttribPointer(maPositionLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pVertex);
	GLES20.glVertexAttribPointer(maTextureCoordLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pTexCoord);
	GLES20.glEnableVertexAttribArray(maPositionLoc);
	GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
}
 
Example 3
Source File: GLDrawer2D.java    From AudioVideoPlayerSample with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor
 * this should be called in GL context
 */
public GLDrawer2D() {
	pVertex = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
			.order(ByteOrder.nativeOrder()).asFloatBuffer();
	pVertex.put(VERTICES);
	pVertex.flip();
	pTexCoord = ByteBuffer.allocateDirect(VERTEX_SZ * FLOAT_SZ)
			.order(ByteOrder.nativeOrder()).asFloatBuffer();
	pTexCoord.put(TEXCOORD);
	pTexCoord.flip();

	hProgram = loadShader(vss, fss);
	GLES20.glUseProgram(hProgram);
       maPositionLoc = GLES20.glGetAttribLocation(hProgram, "aPosition");
       maTextureCoordLoc = GLES20.glGetAttribLocation(hProgram, "aTextureCoord");
       muMVPMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uMVPMatrix");
       muTexMatrixLoc = GLES20.glGetUniformLocation(hProgram, "uTexMatrix");

	Matrix.setIdentityM(mMvpMatrix, 0);
       GLES20.glUniformMatrix4fv(muMVPMatrixLoc, 1, false, mMvpMatrix, 0);
       GLES20.glUniformMatrix4fv(muTexMatrixLoc, 1, false, mMvpMatrix, 0);
	GLES20.glVertexAttribPointer(maPositionLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pVertex);
	GLES20.glVertexAttribPointer(maTextureCoordLoc, 2, GLES20.GL_FLOAT, false, VERTEX_SZ, pTexCoord);
	GLES20.glEnableVertexAttribArray(maPositionLoc);
	GLES20.glEnableVertexAttribArray(maTextureCoordLoc);
}
 
Example 4
Source File: ProgramTexture2d.java    From PLDroidShortVideo with Apache License 2.0 5 votes vote down vote up
@Override
protected void getLocations() {
    maPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "aPosition");
    GlUtil.checkLocation(maPositionLoc, "aPosition");
    maTextureCoordLoc = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord");
    GlUtil.checkLocation(maTextureCoordLoc, "aTextureCoord");
    muMVPMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix");
    GlUtil.checkLocation(muMVPMatrixLoc, "uMVPMatrix");
    muTexMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix");
    GlUtil.checkLocation(muTexMatrixLoc, "uTexMatrix");
}
 
Example 5
Source File: VideoStreamsView.java    From licodeAndroidClient with MIT License 5 votes vote down vote up
@Override
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
	int program = GLES20.glCreateProgram();
	addShaderTo(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_STRING, program);
	addShaderTo(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_STRING, program);

	GLES20.glLinkProgram(program);
	int[] result = new int[] { GLES20.GL_FALSE };
	result[0] = GLES20.GL_FALSE;
	GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, result, 0);
	abortUnless(result[0] == GLES20.GL_TRUE,
			GLES20.glGetProgramInfoLog(program));
	GLES20.glUseProgram(program);

	GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "y_tex"), 0);
	GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "u_tex"), 1);
	GLES20.glUniform1i(GLES20.glGetUniformLocation(program, "v_tex"), 2);

	// Actually set in drawRectangle(), but queried only once here.
	posLocation = GLES20.glGetAttribLocation(program, "in_pos");

	int tcLocation = GLES20.glGetAttribLocation(program, "in_tc");
	GLES20.glEnableVertexAttribArray(tcLocation);
	GLES20.glVertexAttribPointer(tcLocation, 2, GLES20.GL_FLOAT, false, 0,
			textureCoords);

	GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	checkNoGLES2Error();
}
 
Example 6
Source File: GLRectangle.java    From Android-AudioRecorder-App with Apache License 2.0 5 votes vote down vote up
/**
 * Draw rectangle.
 */
public void draw() {
  GLES20.glUseProgram(getProgram());
  int positionHandle = GLES20.glGetAttribLocation(getProgram(), VERTEX_POSITION);
  GLES20.glEnableVertexAttribArray(positionHandle);
  GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,
      COORDS_PER_VERTEX * SIZE_OF_FLOAT, vertexBuffer);
  int colorHandle = GLES20.glGetUniformLocation(getProgram(), VERTEX_COLOR);
  GLES20.glUniform4fv(colorHandle, 1, getColor(), 0);
  GLES20.glDrawElements(GLES20.GL_TRIANGLE_FAN, shortBuffer.capacity(), GLES20.GL_UNSIGNED_SHORT,
      shortBuffer);
  GLES20.glDisableVertexAttribArray(positionHandle);
}
 
Example 7
Source File: Lines.java    From ShapesInOpenGLES2.0 with MIT License 5 votes vote down vote up
/**
 * draws the Lines shape object
 * @param aMVPMatrix
 */
public void render(float[] aMVPMatrix) {

    // Set our per-vertex lighting program.
    GLES20.glUseProgram(aLineProgramHandle);

    int aLineMVPMatrixHandle = GLES20.glGetUniformLocation(aLineProgramHandle, "u_MVPMatrix");
    int aLinePositionHandle = GLES20.glGetAttribLocation(aLineProgramHandle, "a_Position");
    int aLineColorHandle = GLES20.glGetAttribLocation(aLineProgramHandle, "a_Color");

    // Pass in the combined matrix.
    GLES20.glUniformMatrix4fv(aLineMVPMatrixHandle, 1, false, aMVPMatrix, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, aLinePositionsBufferIdx);
    GLES20.glEnableVertexAttribArray(aLinePositionHandle);
    GLES20.glVertexAttribPointer(aLinePositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, 0);

    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, aLineColorsBufferIdx);
    GLES20.glEnableVertexAttribArray(aLineColorHandle);
    GLES20.glVertexAttribPointer(aLineColorHandle, 4, GLES20.GL_FLOAT, false, 0, 0);

    // Clear the currently bound buffer (so future OpenGL calls do not use this buffer).
    GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);

    // Draw the line.
    GLES20.glDrawArrays(GLES20.GL_LINES, 0, vertexCount);

}
 
Example 8
Source File: ShaderPolyLine.java    From Tanks with MIT License 5 votes vote down vote up
@Override
protected void getLocations()
{
  attributePositionHandle =       GLES20.glGetAttribLocation(programHandle, "a_position");
  uniformMatrixProjectionHandle = GLES20.glGetUniformLocation(programHandle, "u_modelViewProjectionMatrix");
  uniformColorHandle =            GLES20.glGetUniformLocation(programHandle, "u_color");
}
 
Example 9
Source File: CartoonFilterRender.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void initGlFilter(Context context) {
  String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex);
  String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.cartoon_fragment);

  program = GlUtil.createProgram(vertexShader, fragmentShader);
  aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
  aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
  uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
  uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");
  uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
  uCartoonHandle = GLES20.glGetUniformLocation(program, "uCartoon");
}
 
Example 10
Source File: RainbowFilterRender.java    From rtmp-rtsp-stream-client-java with Apache License 2.0 5 votes vote down vote up
@Override
protected void initGlFilter(Context context) {
  String vertexShader = GlUtil.getStringFromRaw(context, R.raw.simple_vertex);
  String fragmentShader = GlUtil.getStringFromRaw(context, R.raw.rainbow_fragment);

  program = GlUtil.createProgram(vertexShader, fragmentShader);
  aPositionHandle = GLES20.glGetAttribLocation(program, "aPosition");
  aTextureHandle = GLES20.glGetAttribLocation(program, "aTextureCoord");
  uMVPMatrixHandle = GLES20.glGetUniformLocation(program, "uMVPMatrix");
  uSTMatrixHandle = GLES20.glGetUniformLocation(program, "uSTMatrix");
  uSamplerHandle = GLES20.glGetUniformLocation(program, "uSampler");
  uTimeHandle = GLES20.glGetUniformLocation(program, "uTime");
}
 
Example 11
Source File: MyGLRenderer.java    From myMediaCodecPlayer-for-FPV with MIT License 4 votes vote down vote up
@Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
{
    System.out.println("onSurfaceCreated");
    GLES20.glClearColor(0.0f, 0.0f, 0.2f, 0.0f);
    //
    mProgram = OpenGLHelper.createProgram(OpenGLHelper.getVertexShader(), OpenGLHelper.getFragmentShader());
    if (mProgram == 0) {
        return;
    }
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    OpenGLHelper.checkGlError("glGetAttribLocation aPosition");
    if (maPositionHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aPosition");
    }
    maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
    OpenGLHelper.checkGlError("glGetAttribLocation aTextureCoord");
    if (maTextureHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aTextureCoord");
    }
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    OpenGLHelper.checkGlError("glGetUniformLocation uMVPMatrix");
    if (muMVPMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uMVPMatrix");
    }
    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    OpenGLHelper.checkGlError("glGetUniformLocation uSTMatrix");
    if (muSTMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uSTMatrix");
    }
    //
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    mTextureID = textures[0];
    GLES20.glBindTexture(GL_TEXTURE_EXTERNAL_OES, mTextureID);
    OpenGLHelper.checkGlError("glBindTexture mTextureID");
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    //mSurfaceTexture = new SurfaceTexture(mTextureID);
    //Don't enable double buffering
    mSurfaceTexture = new SurfaceTexture(mTextureID,false);
    //MediaCodec overrides this size anyway
    //mSurfaceTexture.setDefaultBufferSize(960, 810);
    //It seems like we don't need any synchronization for updateTexImage; it immediately returns,when no data has changed;
    //by the way: onFrameAvailable seems to have issues on my Mali 450mp gpu,so it's better for me to avoid.
    /*mSurfaceTexture.setOnFrameAvailableListener(new SurfaceTexture.OnFrameAvailableListener() {
        @Override
        public void onFrameAvailable(SurfaceTexture surfaceTexture) {
            synchronized (this){
                updateSurface=true;
            }
        }
    });*/
    mDecoderSurface=new Surface(mSurfaceTexture);
    mDecoder=new UdpReceiverDecoderThread(mDecoderSurface,5000,mcontext);
    mDecoder.start();

}
 
Example 12
Source File: TextureManager.java    From spydroid-ipcamera with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Initializes GL state.  Call this after the EGL surface has been created and made current.
 */
public SurfaceTexture createTexture() {
	mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
	if (mProgram == 0) {
		throw new RuntimeException("failed creating program");
	}
	maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
	checkGlError("glGetAttribLocation aPosition");
	if (maPositionHandle == -1) {
		throw new RuntimeException("Could not get attrib location for aPosition");
	}
	maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
	checkGlError("glGetAttribLocation aTextureCoord");
	if (maTextureHandle == -1) {
		throw new RuntimeException("Could not get attrib location for aTextureCoord");
	}

	muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
	checkGlError("glGetUniformLocation uMVPMatrix");
	if (muMVPMatrixHandle == -1) {
		throw new RuntimeException("Could not get attrib location for uMVPMatrix");
	}

	muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
	checkGlError("glGetUniformLocation uSTMatrix");
	if (muSTMatrixHandle == -1) {
		throw new RuntimeException("Could not get attrib location for uSTMatrix");
	}

	int[] textures = new int[1];
	GLES20.glGenTextures(1, textures, 0);

	mTextureID = textures[0];
	GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
	checkGlError("glBindTexture mTextureID");

	GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
			GLES20.GL_NEAREST);
	GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
			GLES20.GL_LINEAR);
	GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S,
			GLES20.GL_CLAMP_TO_EDGE);
	GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
			GLES20.GL_CLAMP_TO_EDGE);
	checkGlError("glTexParameter");
	
	mSurfaceTexture = new SurfaceTexture(mTextureID);
	return mSurfaceTexture;
}
 
Example 13
Source File: BackgroundRenderer.java    From react-native-arcore with MIT License 4 votes vote down vote up
/**
 * Allocates and initializes OpenGL resources needed by the background renderer.  Must be
 * called on the OpenGL thread, typically in
 * {@link GLSurfaceView.Renderer#onSurfaceCreated(GL10, EGLConfig)}.
 *
 * @param context Needed to access shader source.
 */
public void createOnGlThread(Context context) {
    // Generate the background texture.
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    mTextureId = textures[0];
    int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
    GLES20.glBindTexture(textureTarget, mTextureId);
    GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameteri(textureTarget, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

    int numVertices = 4;
    if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) {
      throw new RuntimeException("Unexpected number of vertices in BackgroundRenderer.");
    }

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(QUAD_COORDS.length * FLOAT_SIZE);
    bbVertices.order(ByteOrder.nativeOrder());
    mQuadVertices = bbVertices.asFloatBuffer();
    mQuadVertices.put(QUAD_COORDS);
    mQuadVertices.position(0);

    ByteBuffer bbTexCoords = ByteBuffer.allocateDirect(
            numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE);
    bbTexCoords.order(ByteOrder.nativeOrder());
    mQuadTexCoord = bbTexCoords.asFloatBuffer();
    mQuadTexCoord.put(QUAD_TEXCOORDS);
    mQuadTexCoord.position(0);

    ByteBuffer bbTexCoordsTransformed = ByteBuffer.allocateDirect(
        numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE);
    bbTexCoordsTransformed.order(ByteOrder.nativeOrder());
    mQuadTexCoordTransformed = bbTexCoordsTransformed.asFloatBuffer();

    int vertexShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_VERTEX_SHADER, R.raw.screenquad_vertex);
    int fragmentShader = ShaderUtil.loadGLShader(TAG, context,
            GLES20.GL_FRAGMENT_SHADER, R.raw.screenquad_fragment_oes);

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

    ShaderUtil.checkGLError(TAG, "Program creation");

    mQuadPositionParam = GLES20.glGetAttribLocation(mQuadProgram, "a_Position");
    mQuadTexCoordParam = GLES20.glGetAttribLocation(mQuadProgram, "a_TexCoord");

    ShaderUtil.checkGLError(TAG, "Program parameters");
}
 
Example 14
Source File: TextureRender.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes GL state.  Call this after the EGL surface has been created and made current.
 */
public void surfaceCreated() {
    mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
    if (mProgram == 0) {
        throw new RuntimeException("failed creating program");
    }
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    checkGlError("glGetAttribLocation aPosition");
    if (maPositionHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aPosition");
    }
    maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
    checkGlError("glGetAttribLocation aTextureCoord");
    if (maTextureHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aTextureCoord");
    }
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    checkGlError("glGetUniformLocation uMVPMatrix");
    if (muMVPMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uMVPMatrix");
    }
    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    checkGlError("glGetUniformLocation uSTMatrix");
    if (muSTMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uSTMatrix");
    }
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    mTextureID = textures[0];
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
    checkGlError("glBindTexture mTextureID");
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    checkGlError("glTexParameter");
}
 
Example 15
Source File: ImageRenderer.java    From ARCore-Location with MIT License 4 votes vote down vote up
@Override
public void createOnGlThread(Context context, int distance) {
    String pngName = imagePngName;

    // Read the texture.
    Bitmap textureBitmap = null;
    try {
        textureBitmap = BitmapFactory.decodeStream(context.getAssets().open(pngName));

        // Adjusts size of 3D shape to keep image aspect correct
        float adjustedWidth = ((float)textureBitmap.getWidth() / 250) / 2;
        float adjustedHeight = ((float)textureBitmap.getHeight() / 250) / 2;
        QUAD_COORDS = new float[] {
                // x, y, z
                -adjustedWidth, -adjustedHeight, 0.0f,
                -adjustedWidth, +adjustedHeight, 0.0f,
                +adjustedWidth, -adjustedHeight, 0.0f,
                +adjustedWidth, +adjustedHeight, 0.0f,
        };
    } catch (IOException e) {
        Log.e(TAG, "Exception reading texture", e);
        return;
    }

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    GLES20.glGenTextures(mTextures.length, mTextures, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextures[0]);

    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_NEAREST);
    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, textureBitmap, 0);
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);

    textureBitmap.recycle();

    ShaderUtil.checkGLError(TAG, "Texture loading");

    // Build the geometry of a simple imageRenderer.

    int numVertices = 4;
    if (numVertices != QUAD_COORDS.length / COORDS_PER_VERTEX) {
        throw new RuntimeException("Unexpected number of vertices in BackgroundRenderer.");
    }

    ByteBuffer bbVertices = ByteBuffer.allocateDirect(QUAD_COORDS.length * Float.BYTES);
    bbVertices.order(ByteOrder.nativeOrder());
    mQuadVertices = bbVertices.asFloatBuffer();
    mQuadVertices.put(QUAD_COORDS);
    mQuadVertices.position(0);

    ByteBuffer bbTexCoords =
            ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * Float.BYTES);
    bbTexCoords.order(ByteOrder.nativeOrder());
    mQuadTexCoord = bbTexCoords.asFloatBuffer();
    mQuadTexCoord.put(QUAD_TEXCOORDS);
    mQuadTexCoord.position(0);

    ByteBuffer bbTexCoordsTransformed =
            ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * Float.BYTES);
    bbTexCoordsTransformed.order(ByteOrder.nativeOrder());

    int vertexShader = loadGLShader(TAG, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER);
    int fragmentShader = loadGLShader(TAG, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER);

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

    ShaderUtil.checkGLError(TAG, "Program creation");

    mQuadPositionParam = GLES20.glGetAttribLocation(mQuadProgram, "a_Position");
    mQuadTexCoordParam = GLES20.glGetAttribLocation(mQuadProgram, "a_TexCoord");
    mTextureUniform = GLES20.glGetUniformLocation(mQuadProgram, "u_Texture");
    mModelViewProjectionUniform =
            GLES20.glGetUniformLocation(mQuadProgram, "u_ModelViewProjection");

    ShaderUtil.checkGLError(TAG, "Program parameters");

    Matrix.setIdentityM(mModelMatrix, 0);
}
 
Example 16
Source File: TextureRenderer.java    From VideoCompressor with Apache License 2.0 4 votes vote down vote up
public void surfaceCreated() {
    mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
    if (mProgram == 0) {
        throw new RuntimeException("failed creating program");
    }
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    checkGlError("glGetAttribLocation aPosition");
    if (maPositionHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aPosition");
    }
    maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
    checkGlError("glGetAttribLocation aTextureCoord");
    if (maTextureHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aTextureCoord");
    }
    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    checkGlError("glGetUniformLocation uMVPMatrix");
    if (muMVPMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uMVPMatrix");
    }
    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    checkGlError("glGetUniformLocation uSTMatrix");
    if (muSTMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uSTMatrix");
    }
    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);
    mTextureID = textures[0];
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
    checkGlError("glBindTexture mTextureID");
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    checkGlError("glTexParameter");

    Matrix.setIdentityM(mMVPMatrix, 0);
    if (rotationAngle != 0) {
        Matrix.rotateM(mMVPMatrix, 0, rotationAngle, 0, 0, 1);
    }
}
 
Example 17
Source File: TextureRender.java    From LiveMultimedia with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes GL state.  Call this after the EGL surface has been created and made current.
 */
public void surfaceCreated() {
    mProgram = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
    if (mProgram == 0) {
        throw new RuntimeException("failed creating program");
    }
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
    checkGlError("glGetAttribLocation aPosition");
    if (maPositionHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aPosition");
    }
    maTextureHandle = GLES20.glGetAttribLocation(mProgram, "aTextureCoord");
    checkGlError("glGetAttribLocation aTextureCoord");
    if (maTextureHandle == -1) {
        throw new RuntimeException("Could not get attrib location for aTextureCoord");
    }

    muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    checkGlError("glGetUniformLocation uMVPMatrix");
    if (muMVPMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uMVPMatrix");
    }

    muSTMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uSTMatrix");
    checkGlError("glGetUniformLocation uSTMatrix");
    if (muSTMatrixHandle == -1) {
        throw new RuntimeException("Could not get attrib location for uSTMatrix");
    }


    int[] textures = new int[1];
    GLES20.glGenTextures(1, textures, 0);

    mTextureID = textures[0];
    GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureID);
    checkGlError("glBindTexture mTextureID");

    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER,
            GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER,
            GLES20.GL_LINEAR);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S,
            GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameteri(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T,
            GLES20.GL_CLAMP_TO_EDGE);
    checkGlError("glTexParameter");
}
 
Example 18
Source File: GLScene.java    From MusicVisualization with Apache License 2.0 4 votes vote down vote up
protected void runShandertoyProgram(
        int program, FloatBuffer vertex, FloatBuffer textureCoord,
        int[] iResolution, int[] iChannels, int[][] iChannelResolutions) {

    GLES20.glUseProgram(program);

    int iResolutionLocation = GLES20.glGetUniformLocation(program, "iResolution");
    GLES20.glUniform3fv(iResolutionLocation, 1,
            FloatBuffer.wrap(new float[]{(float) iResolution[0], (float) iResolution[1], 1.0f}));

    float time = ((float) (System.currentTimeMillis() - mStartTime)) / 1000.0f;
    int iGlobalTimeLocation = GLES20.glGetUniformLocation(program, "iTime");
    GLES20.glUniform1f(iGlobalTimeLocation, time);

    int iFrameLocation = GLES20.glGetUniformLocation(program, "iFrame");
    GLES20.glUniform1i(iFrameLocation, iFrame);

    int vPositionLocation = GLES20.glGetAttribLocation(program, "vPosition");
    GLES20.glEnableVertexAttribArray(vPositionLocation);
    GLES20.glVertexAttribPointer(vPositionLocation, 2, GLES20.GL_FLOAT, false, 4 * 2, vertex);

    int vTexCoordLocation = GLES20.glGetAttribLocation(program, "vTexCoord");
    GLES20.glEnableVertexAttribArray(vTexCoordLocation);
    GLES20.glVertexAttribPointer(vTexCoordLocation, 2, GLES20.GL_FLOAT, false, 4 * 2, textureCoord);

    for (int i = 0; i < iChannels.length; i++) {
        int sTextureLocation = GLES20.glGetUniformLocation(program, "iChannel" + i);
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0 + i);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, iChannels[i]);
        GLES20.glUniform1i(sTextureLocation, i);
    }

    float _iChannelResolutions[] = new float[iChannelResolutions.length * 3];
    for (int i = 0; i < iChannelResolutions.length; i++) {
        _iChannelResolutions[i * 3] = iChannelResolutions[i][0];
        _iChannelResolutions[i * 3 + 1] = iChannelResolutions[i][1];
        _iChannelResolutions[i * 3 + 2] = 1.0f;
    }

    int iChannelResolutionLocation = GLES20.glGetUniformLocation(program, "iChannelResolution");
    GLES20.glUniform3fv(iChannelResolutionLocation,
            _iChannelResolutions.length, FloatBuffer.wrap(_iChannelResolutions));

    // Draw
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
}
 
Example 19
Source File: InvertedColorsVideoRenderer.java    From opentok-android-sdk-samples with MIT License 4 votes vote down vote up
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0, 0, 0, 1);
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

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

    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 positionHandle = GLES20.glGetAttribLocation(mProgram,
            "aPosition");
    int textureHandle = GLES20.glGetAttribLocation(mProgram,
            "aTextureCoord");

    GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
            GLES20.GL_FLOAT, false, COORDS_PER_VERTEX * 4,
            mVertexBuffer);

    GLES20.glEnableVertexAttribArray(positionHandle);

    GLES20.glVertexAttribPointer(textureHandle,
            TEXTURECOORDS_PER_VERTEX, GLES20.GL_FLOAT, false,
            TEXTURECOORDS_PER_VERTEX * 4, mTextureBuffer);

    GLES20.glEnableVertexAttribArray(textureHandle);

    GLES20.glUseProgram(mProgram);
    int i = GLES20.glGetUniformLocation(mProgram, "Ytex");
    GLES20.glUniform1i(i, 0); /* Bind Ytex to texture unit 0 */

    i = GLES20.glGetUniformLocation(mProgram, "Utex");
    GLES20.glUniform1i(i, 1); /* Bind Utex to texture unit 1 */

    i = GLES20.glGetUniformLocation(mProgram, "Vtex");
    GLES20.glUniform1i(i, 2); /* Bind Vtex to texture unit 2 */

    mTextureWidth = 0;
    mTextureHeight = 0;
}
 
Example 20
Source File: Texture2dProgram.java    From mobile-ar-sensor-logger with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Prepares the program in the current EGL context.
 */
public Texture2dProgram(ProgramType programType) {
    mProgramType = programType;

    switch (programType) {
        case TEXTURE_2D:
            mTextureTarget = GLES20.GL_TEXTURE_2D;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_2D);
            break;
        case TEXTURE_EXT:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT);
            break;
        case TEXTURE_EXT_BW:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_BW);
            break;
        case TEXTURE_EXT_FILT:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_FILT);
            break;
        default:
            throw new RuntimeException("Unhandled type " + programType);
    }
    if (mProgramHandle == 0) {
        throw new RuntimeException("Unable to create program");
    }
    Log.d(TAG, "Created program " + mProgramHandle + " (" + programType + ")");

    // get locations of attributes and uniforms

    maPositionLoc = GLES20.glGetAttribLocation(mProgramHandle, "aPosition");
    GlUtil.checkLocation(maPositionLoc, "aPosition");
    maTextureCoordLoc = GLES20.glGetAttribLocation(mProgramHandle, "aTextureCoord");
    GlUtil.checkLocation(maTextureCoordLoc, "aTextureCoord");
    muMVPMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uMVPMatrix");
    GlUtil.checkLocation(muMVPMatrixLoc, "uMVPMatrix");
    muTexMatrixLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexMatrix");
    GlUtil.checkLocation(muTexMatrixLoc, "uTexMatrix");
    muKernelLoc = GLES20.glGetUniformLocation(mProgramHandle, "uKernel");
    if (muKernelLoc < 0) {
        // no kernel in this one
        muKernelLoc = -1;
        muTexOffsetLoc = -1;
        muColorAdjustLoc = -1;
    } else {
        // has kernel, must also have tex offset and color adj
        muTexOffsetLoc = GLES20.glGetUniformLocation(mProgramHandle, "uTexOffset");
        GlUtil.checkLocation(muTexOffsetLoc, "uTexOffset");
        muColorAdjustLoc = GLES20.glGetUniformLocation(mProgramHandle, "uColorAdjust");
        GlUtil.checkLocation(muColorAdjustLoc, "uColorAdjust");

        // initialize default values
        setKernel(new float[] {0f, 0f, 0f,  0f, 1f, 0f,  0f, 0f, 0f}, 0f);
        setTexSize(256, 256);
    }
}