Java Code Examples for android.opengl.GLES11Ext#GL_TEXTURE_EXTERNAL_OES

The following examples show how to use android.opengl.GLES11Ext#GL_TEXTURE_EXTERNAL_OES . 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: BackgroundRenderer.java    From poly-sample-android with Apache License 2.0 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) throws IOException {
  // Generate the background texture.
  int[] textures = new int[1];
  GLES20.glGenTextures(1, textures, 0);
  textureId = textures[0];
  int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
  GLES20.glBindTexture(textureTarget, textureId);
  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());
  quadVertices = bbVertices.asFloatBuffer();
  quadVertices.put(QUAD_COORDS);
  quadVertices.position(0);

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

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

  int vertexShader =
      ShaderUtil.loadGLShader(TAG, context, GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_NAME);
  int fragmentShader =
      ShaderUtil.loadGLShader(TAG, context, GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_NAME);

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

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

  quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position");
  quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord");

  ShaderUtil.checkGLError(TAG, "Program parameters");
}
 
Example 2
Source File: Texture2dProgram.java    From cineio-broadcast-android with MIT License 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);
    }
}
 
Example 3
Source File: Texture2dProgram.java    From AndroidPlayground with MIT License 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);
    }
}
 
Example 4
Source File: CameraFilter.java    From PLDroidMediaStreaming with Apache License 2.0 4 votes vote down vote up
@Override
public int getTextureTarget() {
    return GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
}
 
Example 5
Source File: Texture2dProgram.java    From pause-resume-video-recording with Apache License 2.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);
    }
}
 
Example 6
Source File: BackgroundRenderer.java    From unity-ads-android with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
void createOnGlThread() {
	int[] textures = new int[1];
	GLES20.glGenTextures(1, textures, 0);
	textureId = textures[0];
	int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
	GLES20.glBindTexture(textureTarget, textureId);
	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());
	quadVertices = bbVertices.asFloatBuffer();
	quadVertices.put(QUAD_COORDS);
	quadVertices.position(0);

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

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

	int vertexShader = ShaderLoader.load(VERTEX_SHADER, GLES20.GL_VERTEX_SHADER);
	int fragmentShader = ShaderLoader.load(FRAGMENT_SHADER, GLES20.GL_FRAGMENT_SHADER);

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

	ShaderLoader.checkGLError("Program creation");

	quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position");
	quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord");

	ShaderLoader.checkGLError("Program parameters");
}
 
Example 7
Source File: ShaderRenderer.java    From ShaderEditor with MIT License 4 votes vote down vote up
private String indexTextureNames(String source) {
	if (source == null) {
		return null;
	}

	textureNames.clear();
	textureParameters.clear();
	numberOfTextures = 0;
	backBufferTextureParams.reset();

	final int maxTextures = textureIds.length;
	for (Matcher m = PATTERN_SAMPLER.matcher(source);
			m.find() && numberOfTextures < maxTextures; ) {
		String type = m.group(1);
		String name = m.group(2);
		String params = m.group(3);

		if (type == null || name == null) {
			continue;
		}

		if (UNIFORM_BACKBUFFER.equals(name)) {
			backBufferTextureParams.parse(params);
			continue;
		}

		int target;
		switch (type) {
			case SAMPLER_2D:
				target = GLES20.GL_TEXTURE_2D;
				break;
			case SAMPLER_CUBE:
				target = GLES20.GL_TEXTURE_CUBE_MAP;
				break;
			case SAMPLER_EXTERNAL_OES:
				if (Build.VERSION.SDK_INT >
						Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
					// needs to be done here or lint won't recognize
					// we're checking SDK version
					target = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
				} else {
					// ignore that uniform on lower SDKs
					continue;
				}
				if (!source.contains(OES_EXTERNAL)) {
					source = addPreprocessorDirective(source,
							OES_EXTERNAL);
				}
				break;
			default:
				continue;
		}

		textureTargets[numberOfTextures++] = target;
		textureNames.add(name);
		textureParameters.add(new TextureParameters(params));
	}

	return source;
}
 
Example 8
Source File: Texture2dProgram.java    From IjkVRPlayer with Apache License 2.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);
    }
}
 
Example 9
Source File: Texture2dProgram.java    From LiveVideoBroadcaster with Apache License 2.0 4 votes vote down vote up
/**
 * Prepares the program in the current EGL activity.
 */
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_GRAYSCALE:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_GRAY_SCALE);
            break;
        case TEXTURE_EXT_SEPIA:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_SEPIA);
            break;
        case TEXTURE_EXT_POSTERIZE:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_POSTERIZE);
            break;
        case TEXTURE_EXT_CROSSPROCESS:
            mTextureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
            mProgramHandle = GlUtil.createProgram(VERTEX_SHADER, FRAGMENT_SHADER_EXT_CROSSPROCESS);
            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);
    }
}
 
Example 10
Source File: Texture2dProgram.java    From FuAgoraDemoDroid with MIT License 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);
    }
}
 
Example 11
Source File: Texture2dProgram.java    From VIA-AI with MIT License 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);
    }
}
 
Example 12
Source File: Texture2dProgram.java    From PLDroidShortVideo with Apache License 2.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);
    }
}
 
Example 13
Source File: BackgroundRenderer.java    From augmentedreality with Apache License 2.0 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);
  textureId = textures[0];
  int textureTarget = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
  GLES20.glBindTexture(textureTarget, textureId);
  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());
  quadVertices = bbVertices.asFloatBuffer();
  quadVertices.put(QUAD_COORDS);
  quadVertices.position(0);

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

  ByteBuffer bbTexCoordsTransformed =
      ByteBuffer.allocateDirect(numVertices * TEXCOORDS_PER_VERTEX * FLOAT_SIZE);
  bbTexCoordsTransformed.order(ByteOrder.nativeOrder());
  quadTexCoordTransformed = 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);

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

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

  quadPositionParam = GLES20.glGetAttribLocation(quadProgram, "a_Position");
  quadTexCoordParam = GLES20.glGetAttribLocation(quadProgram, "a_TexCoord");

  ShaderUtil.checkGLError(TAG, "Program parameters");
}
 
Example 14
Source File: Texture2dProgram.java    From PhotoMovie with Apache License 2.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);
    }
}
 
Example 15
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 16
Source File: Texture2dProgram.java    From RtmpPublisher with Apache License 2.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);
    }
}
 
Example 17
Source File: BackgroundRenderer.java    From amazon-sumerian-arcore-starter-app with Apache License 2.0 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 18
Source File: EglUtil.java    From media-for-mobile with Apache License 2.0 4 votes vote down vote up
@Override
public void drawFrameStart(
        Program program,
        FloatBuffer triangleVertices,
        float[] mvpMatrix,
        float[] stMatrix,
        float angle,
        TextureType textureType,
        int textureId,
        Resolution inputResolution,
        TextureRenderer.FillMode fillMode
) {
    checkEglError("onDrawFrame start");

    Resolution out;
    switch (fillMode) {
        case PreserveSize:
            out = inputResolution;
            break;
        case PreserveAspectFit:
        case PreserveAspectCrop:
            out = getCurrentSurfaceResolution();
            break;
        default:
            out = new Resolution(0, 0);
            break;
    }

    GLES20.glViewport(0, 0, out.width(), out.height());

    GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);

    GLES20.glUseProgram(program.programHandle);
    checkEglError("glUseProgram");

    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    checkEglError("glActiveTexture");

    int textureTypeId;
    if (textureType == TextureType.GL_TEXTURE_2D) {
        textureTypeId = GLES20.GL_TEXTURE_2D;
    } else {
        textureTypeId = GLES11Ext.GL_TEXTURE_EXTERNAL_OES;
    }

    GLES20.glBindTexture(textureTypeId, textureId);
    checkEglError("glBindTexture");

    triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET);
    GLES20.glVertexAttribPointer(program.positionHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
    checkEglError("glVertexAttribPointer maPosition");
    GLES20.glEnableVertexAttribArray(program.positionHandle);
    checkEglError("glEnableVertexAttribArray maPositionHandle");

    triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET);
    GLES20.glVertexAttribPointer(program.textureHandle, 3, GLES20.GL_FLOAT, false, TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices);
    checkEglError("glVertexAttribPointer maTextureHandle");
    GLES20.glEnableVertexAttribArray(program.textureHandle);
    checkEglError("glEnableVertexAttribArray maTextureHandle");

    Matrix.setIdentityM(mvpMatrix, 0);

    float scale[];
    switch (fillMode) {
        case PreserveAspectFit:
            scale = scaleCalculator.getScale_PreserveAspectFit((int) angle, inputResolution.width(), inputResolution.height(), out.width(), out.height());
            Matrix.scaleM(mvpMatrix, 0, scale[0], scale[1], 1);
            Matrix.rotateM(mvpMatrix, 0, -angle, 0.f, 0.f, 1.f);
            break;
        case PreserveAspectCrop:
            scale = scaleCalculator.getScale_PreserveAspectCrop((int) angle, inputResolution.width(), inputResolution.height(), out.width(), out.height());
            Matrix.scaleM(mvpMatrix, 0, scale[0], scale[1], 1);
            Matrix.rotateM(mvpMatrix, 0, -angle, 0.f, 0.f, 1.f);
            break;
        case PreserveSize:
            if (angle == 90 || angle == 270) {
                Matrix.rotateM(mvpMatrix, 0, 180, 0.f, 0.f, 1.f);
            }
            break;
        default:
            break;
    }

    GLES20.glUniformMatrix4fv(program.mvpMatrixHandle, 1, false, mvpMatrix, 0);
    GLES20.glUniformMatrix4fv(program.stMatrixHandle, 1, false, stMatrix, 0);
}
 
Example 19
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);
    }
}
 
Example 20
Source File: Texture2dProgram.java    From grafika with Apache License 2.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);
    }
}